Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Energy ADE
ADE Xjc
Commits
9416ebfb
Commit
9416ebfb
authored
Jan 21, 2018
by
Claus Nagel
Browse files
switched to Gradle
parent
e248ab95
Changes
27
Hide whitespace changes
Inline
Side-by-side
schemas/CityGML/ade_hooks/ade_hooks.xsd
View file @
9416ebfb
...
...
@@ -5,7 +5,7 @@
*
* ade-xjc is part of the citygml4j project
*
* Copyright 2013-201
7
Claus Nagel <claus.nagel@gmail.com>
* Copyright 2013-201
8
Claus Nagel <claus.nagel@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
...
...
@@ -19,8 +19,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
-->
<xs:schema
xmlns=
"http://www.
igg.tu-berlin.de/
citygml4j/ade_hooks"
xmlns:xs=
"http://www.w3.org/2001/XMLSchema"
targetNamespace=
"http://www.
igg.tu-berlin.de/
citygml4j/ade_hooks"
<xs:schema
xmlns=
"http://www.citygml4j
.org
/ade_hooks"
xmlns:xs=
"http://www.w3.org/2001/XMLSchema"
targetNamespace=
"http://www.citygml4j
.org
/ade_hooks"
xmlns:core1=
"http://www.opengis.net/citygml/1.0"
xmlns:app1=
"http://www.opengis.net/citygml/appearance/1.0"
xmlns:bldg1=
"http://www.opengis.net/citygml/building/1.0"
xmlns:frn1=
"http://www.opengis.net/citygml/cityfurniture/1.0"
xmlns:grp1=
"http://www.opengis.net/citygml/cityobjectgroup/1.0"
xmlns:luse1=
"http://www.opengis.net/citygml/landuse/1.0"
...
...
schemas/CityGML/citygml4j_profile.xsd
View file @
9416ebfb
...
...
@@ -46,5 +46,5 @@
<xs:import
namespace=
"http://www.opengis.net/citygml/waterbody/2.0"
schemaLocation=
"2.0.0/waterBody.xsd"
/>
<xs:import
namespace=
"http://www.opengis.net/citygml/texturedsurface/2.0"
schemaLocation=
"2.0.0/texturedSurface.xsd"
/>
<!-- ade hooks -->
<xs:import
namespace=
"http://www.
igg.tu-berlin.de/
citygml4j/ade_hooks"
schemaLocation=
"ade_hooks/ade_hooks.xsd"
/>
<xs:import
namespace=
"http://www.citygml4j
.org
/ade_hooks"
schemaLocation=
"ade_hooks/ade_hooks.xsd"
/>
</xs:schema>
settings.gradle
0 → 100644
View file @
9416ebfb
rootProject
.
name
=
'ade-xjc'
src/org/citygml4j/ade_xjc/ADE
_XJC
.java
→
src/
main/java/
org/citygml4j/ade_xjc/ADE
SchemaCompiler
.java
View file @
9416ebfb
/*
* ade-xjc - XML Schema binding compiler for CityGML ADEs
* https://github.com/citygml4j/ade-xjc
*
* ade-xjc is part of the citygml4j project
*
* Copyright 2013-2017 Claus Nagel <claus.nagel@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.citygml4j.ade_xjc
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.io.PrintStream
;
import
java.math.BigInteger
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.security.NoSuchAlgorithmException
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.ServiceLoader
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
import
org.citygml4j.ade_xjc.Util.URLClassLoader
;
import
org.kohsuke.args4j.Argument
;
import
org.kohsuke.args4j.CmdLineException
;
import
org.kohsuke.args4j.CmdLineParser
;
import
org.kohsuke.args4j.Option
;
import
org.kohsuke.args4j.ParserProperties
;
import
org.xml.sax.InputSource
;
import
com.sun.codemodel.JCodeModel
;
import
com.sun.codemodel.writer.FileCodeWriter
;
import
com.sun.codemodel.writer.PrologCodeWriter
;
import
com.sun.tools.xjc.Options
;
import
com.sun.tools.xjc.Plugin
;
import
com.sun.tools.xjc.api.S2JJAXBModel
;
import
com.sun.tools.xjc.api.SchemaCompiler
;
import
com.sun.tools.xjc.api.XJC
;
import
com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl
;
public
class
ADE_XJC
{
private
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"HH:mm:ss"
);
// some dirs we will need
private
Path
schema_dir
=
Paths
.
get
(
"schemas"
);
private
Path
plugins_dir
=
Paths
.
get
(
"jaxb-plugins"
);
// some files we will need
private
Path
baseProfileFile
=
schema_dir
.
resolve
(
"CityGML/citygml4j_profile.xsd"
);
private
Path
bindingFile
=
schema_dir
.
resolve
(
"CityGML/binding.xjb"
);
private
Path
adeSchemaFile
=
null
;
// args
@Argument
private
List
<
String
>
adeSchemaFileList
=
new
ArrayList
<
String
>();
@Option
(
name
=
"-package"
,
usage
=
"package for the ADE JAXB classes"
,
metaVar
=
"<packageName>"
)
private
String
packageName
=
"ade"
;
@Option
(
name
=
"-binding"
,
usage
=
"optional JAXB binding file for ADE XML Schema"
,
metaVar
=
"<fileName>"
)
private
File
adeBindingFile
=
null
;
@Option
(
name
=
"-output"
,
usage
=
"output folder"
,
metaVar
=
"<folderName>"
)
private
File
outputFolder
=
new
File
(
"src-gen"
);
@Option
(
name
=
"-non-strict"
,
usage
=
"allow changes to contents of subfolder 'schemas'"
)
private
boolean
nonStrict
=
false
;
@Option
(
name
=
"-clean"
,
usage
=
"clean output folder"
)
private
boolean
clean
;
@Option
(
name
=
"-h"
,
aliases
={
"-help"
},
usage
=
"print this help message and exit"
)
private
boolean
help
;
@Option
(
name
=
"-version"
,
usage
=
"print product version and exit"
)
private
boolean
version
;
@Option
(
name
=
"-X{arg}"
,
usage
=
"one or more arguments for JAXB plugins"
)
private
Boolean
dummy
=
null
;
public
static
void
main
(
String
[]
args
)
{
new
ADE_XJC
().
doMain
(
args
);
}
private
void
doMain
(
String
[]
_args
)
{
List
<
String
>
tmp
=
new
ArrayList
<
String
>();
for
(
String
arg
:
_args
)
tmp
.
addAll
(
Arrays
.
asList
(
arg
.
split
(
" +"
)));
Map
<
Boolean
,
List
<
String
>>
args
=
tmp
.
stream
().
collect
(
Collectors
.
partitioningBy
(
arg
->
!
arg
.
startsWith
(
"-X"
)));
CmdLineParser
parser
=
new
CmdLineParser
(
this
,
ParserProperties
.
defaults
().
withUsageWidth
(
80
));
try
{
parser
.
parseArgument
(
args
.
get
(
Boolean
.
TRUE
));
}
catch
(
CmdLineException
e
)
{
System
.
err
.
println
(
e
.
getMessage
());
printUsage
(
parser
,
System
.
err
);
System
.
exit
(
1
);
}
if
(
help
)
{
printUsage
(
parser
,
System
.
out
);
System
.
exit
(
0
);
}
if
(
version
)
{
System
.
out
.
println
(
"ade-xjc version "
+
this
.
getClass
().
getPackage
().
getImplementationVersion
());
System
.
out
.
println
(
this
.
getClass
().
getPackage
().
getImplementationTitle
());
System
.
exit
(
0
);
}
if
(
adeSchemaFileList
.
isEmpty
())
{
System
.
out
.
println
(
"Provide the ADE XML Schema definition file to be compiled as argument"
);
printUsage
(
parser
,
System
.
out
);
System
.
exit
(
1
);
}
if
(
adeSchemaFileList
.
size
()
>
1
)
{
System
.
out
.
println
(
"Just provide one ADE XML Schema definition file at a time"
);
printUsage
(
parser
,
System
.
out
);
System
.
exit
(
1
);
}
log
(
LogLevel
.
INFO
,
"Starting ade-xjc compiler"
);
adeSchemaFile
=
Paths
.
get
(
adeSchemaFileList
.
get
(
0
));
int
status
=
0
;
try
{
log
(
LogLevel
.
INFO
,
"Setting up build environment"
);
checkBuildEnvironment
();
if
(
clean
&&
Files
.
exists
(
outputFolder
.
toPath
()))
{
log
(
LogLevel
.
INFO
,
"Cleaning output folder"
);
Files
.
walkFileTree
(
outputFolder
.
toPath
(),
new
FileDeleter
(
true
));
}
createBuildEnvironment
();
if
(
packageName
.
startsWith
(
"org.citygml4j"
))
{
log
(
LogLevel
.
ERROR
,
"The package "
+
packageName
+
" is not allowed for the ADE classes"
);
log
(
LogLevel
.
ERROR
,
"Choose a package which is not a subpackage of org.citygml4j. Aborting."
);
System
.
exit
(
1
);
}
log
(
LogLevel
.
INFO
,
"Using ADE schema "
+
adeSchemaFile
.
toAbsolutePath
().
toString
());
if
(
adeBindingFile
!=
null
)
log
(
LogLevel
.
INFO
,
"Using JAXB binding "
+
adeBindingFile
.
getCanonicalFile
());
log
(
LogLevel
.
INFO
,
"Using Java package "
+
packageName
+
" for JAXB classes"
);
SchemaCompiler
sc
=
XJC
.
createSchemaCompiler
();
sc
.
setDefaultPackageName
(
packageName
);
if
(!
args
.
get
(
Boolean
.
FALSE
).
isEmpty
()
&&
Files
.
exists
(
plugins_dir
))
{
log
(
LogLevel
.
INFO
,
"Loading and configuring JAXB plugins"
);
String
[]
pluginArgs
=
args
.
get
(
Boolean
.
FALSE
).
stream
().
toArray
(
String
[]::
new
);
Options
options
=
((
SchemaCompilerImpl
)
sc
).
getOptions
();
URLClassLoader
loader
=
new
URLClassLoader
();
try
(
Stream
<
Path
>
stream
=
Files
.
walk
(
plugins_dir
).
filter
(
path
->
path
.
getFileName
().
toString
().
toLowerCase
().
endsWith
(
".jar"
)))
{
stream
.
forEach
(
path
->
loader
.
addPath
(
path
));
}
ServiceLoader
<
Plugin
>
plugins
=
ServiceLoader
.
load
(
Plugin
.
class
,
loader
);
for
(
Plugin
plugin
:
plugins
)
{
for
(
int
i
=
0
;
i
<
pluginArgs
.
length
;
i
++)
{
if
((
'-'
+
plugin
.
getOptionName
()).
equals
(
pluginArgs
[
i
]))
{
log
(
LogLevel
.
INFO
,
"Found JAXB plugin for option '-"
+
plugin
.
getOptionName
()
+
"'"
);
options
.
activePlugins
.
add
(
plugin
);
plugin
.
onActivated
(
options
);
}
plugin
.
parseArgument
(
options
,
pluginArgs
,
i
);
}
}
}
XJCErrorListener
listener
=
new
XJCErrorListener
();
sc
.
setErrorListener
(
listener
);
log
(
LogLevel
.
INFO
,
"Generating JAXB classes. This may take some time..."
);
sc
.
parseSchema
(
new
InputSource
(
baseProfileFile
.
toUri
().
toString
()));
sc
.
parseSchema
(
new
InputSource
(
bindingFile
.
toUri
().
toString
()));
sc
.
parseSchema
(
new
InputSource
(
adeSchemaFile
.
toUri
().
toString
()));
if
(
adeBindingFile
!=
null
)
sc
.
parseSchema
(
new
InputSource
(
adeBindingFile
.
toURI
().
toString
()));
S2JJAXBModel
model
=
sc
.
bind
();
JCodeModel
code
=
model
.
generateCode
(
null
,
listener
);
// write classes using a header comment
PrologCodeWriter
writer
=
new
PrologCodeWriter
(
new
FileCodeWriter
(
outputFolder
,
"UTF-8"
),
"Generated with ade-xjc - XML Schema binding compiler for CityGML ADEs, version "
+
this
.
getClass
().
getPackage
().
getImplementationVersion
()
+
"\n"
+
"ade-xjc is part of the citygml4j project, see https://github.com/citygml4j\n"
+
"Any modifications to this file will be lost upon recompilation of the source\n"
+
"Generated: "
+
new
Date
().
toString
()
+
"\n"
);
code
.
build
(
writer
);
File
packageDir
=
new
File
(
outputFolder
.
getAbsolutePath
());
log
(
LogLevel
.
INFO
,
"JAXB classes successfully written to "
+
packageDir
.
getCanonicalPath
());
}
catch
(
Exception
e
)
{
if
(
e
.
getMessage
()
!=
null
)
log
(
LogLevel
.
ERROR
,
e
.
getMessage
());
log
(
LogLevel
.
ERROR
,
"Unable to recover from previous error(s). Aborting."
);
status
=
1
;
}
finally
{
try
{
cleanBuildEnvironment
(
status
==
1
);
}
catch
(
IOException
e
)
{
log
(
LogLevel
.
ERROR
,
"Failed to clean build environment."
);
status
=
1
;
}
}
System
.
exit
(
status
);
}
private
void
createBuildEnvironment
()
throws
Exception
{
boolean
success
=
true
;
if
(
success
&&
!
outputFolder
.
exists
())
{
success
=
outputFolder
.
mkdirs
();
if
(!
success
)
throw
new
Exception
(
"Could not create directory "
+
outputFolder
.
getAbsolutePath
());
}
else
if
(!
outputFolder
.
isDirectory
())
{
success
=
outputFolder
.
delete
();
if
(
success
)
success
=
outputFolder
.
mkdirs
();
else
throw
new
Exception
(
"Could not create directory "
+
outputFolder
.
getAbsolutePath
());
}
}
private
void
checkBuildEnvironment
()
throws
Exception
,
FileNotFoundException
,
NoSuchAlgorithmException
{
if
(!
Files
.
exists
(
schema_dir
))
throw
new
FileNotFoundException
(
"Failed to open folder "
+
schema_dir
.
toAbsolutePath
().
toString
());
if
(!
nonStrict
)
{
log
(
LogLevel
.
INFO
,
"Running in strict mode. Checking sanity of subfolder 'schemas'"
);
BigInteger
md5
=
new
BigInteger
(
"0"
);
md5
=
Util
.
dir2md5
(
schema_dir
.
toFile
(),
md5
);
if
(!
md5
.
toString
(
16
).
equals
(
"79facf9d68ad19afacf8d51a58c5f8b6"
))
throw
new
Exception
(
"Contents of subfolder 'schemas' have been altered. Please restore its original state."
);
}
if
(!
Files
.
exists
(
adeSchemaFile
)
||
!
Files
.
isRegularFile
(
adeSchemaFile
)
||
!
Files
.
isReadable
(
adeSchemaFile
))
throw
new
Exception
(
"Could not open ADE schema file "
+
adeSchemaFile
.
toAbsolutePath
().
toString
());
if
(
adeBindingFile
!=
null
&&
(!
adeBindingFile
.
exists
()
||
!
adeBindingFile
.
isFile
()
||
!
adeBindingFile
.
canRead
()))
throw
new
Exception
(
"Could not open ADE binding file "
+
adeBindingFile
.
getAbsolutePath
());
}
private
void
cleanBuildEnvironment
(
boolean
cleanAll
)
throws
IOException
{
if
(!
cleanAll
)
{
Files
.
walkFileTree
(
Paths
.
get
(
outputFolder
.
getAbsolutePath
(),
"net/opengis/citygml"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
Paths
.
get
(
outputFolder
.
getAbsolutePath
(),
"net/opengis/gml"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
Paths
.
get
(
outputFolder
.
getAbsolutePath
(),
"oasis/names/tc/ciq/xsdschema/xal/_2"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
Paths
.
get
(
outputFolder
.
getAbsolutePath
(),
"org/citygml4j"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
Paths
.
get
(
outputFolder
.
getAbsolutePath
(),
"org/w3/_1999/xlink"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
Paths
.
get
(
outputFolder
.
getAbsolutePath
(),
"org/w3/_2001/smil20"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
Paths
.
get
(
outputFolder
.
getAbsolutePath
(),
"net"
),
new
FileDeleter
(
true
,
true
));
Files
.
walkFileTree
(
Paths
.
get
(
outputFolder
.
getAbsolutePath
(),
"org"
),
new
FileDeleter
(
true
,
true
));
Files
.
walkFileTree
(
Paths
.
get
(
outputFolder
.
getAbsolutePath
(),
"oasis"
),
new
FileDeleter
(
true
,
true
));
}
else
Files
.
walkFileTree
(
outputFolder
.
toPath
(),
new
FileDeleter
(
true
));
}
private
void
printUsage
(
CmdLineParser
parser
,
PrintStream
out
)
{
out
.
println
(
"Usage: java -jar ade-xjc.jar [-options] ade.xsd"
);
out
.
println
();
out
.
println
(
"where options include:"
);
parser
.
printUsage
(
System
.
out
);
out
.
println
();
}
private
void
log
(
LogLevel
level
,
String
msg
)
{
StringBuilder
builder
=
new
StringBuilder
();
builder
.
append
(
"["
)
.
append
(
df
.
format
(
new
Date
()))
.
append
(
" "
)
.
append
(
level
.
toString
())
.
append
(
"] "
)
.
append
(
msg
);
System
.
out
.
println
(
builder
.
toString
());
}
private
enum
LogLevel
{
INFO
(
"info"
),
WARN
(
"warn"
),
ERROR
(
"error"
);
private
String
value
;
LogLevel
(
String
value
)
{
this
.
value
=
value
;
}
public
String
toString
()
{
return
value
;
}
}
}
/*
* ade-xjc - XML Schema binding compiler for CityGML ADEs
* https://github.com/citygml4j/ade-xjc
*
* ade-xjc is part of the citygml4j project
*
* Copyright 2013-2017 Claus Nagel <claus.nagel@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.citygml4j.ade_xjc
;
import
com.sun.codemodel.JCodeModel
;
import
com.sun.codemodel.writer.FileCodeWriter
;
import
com.sun.codemodel.writer.PrologCodeWriter
;
import
com.sun.tools.xjc.Options
;
import
com.sun.tools.xjc.Plugin
;
import
com.sun.tools.xjc.api.S2JJAXBModel
;
import
com.sun.tools.xjc.api.SchemaCompiler
;
import
com.sun.tools.xjc.api.XJC
;
import
com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl
;
import
org.citygml4j.ade_xjc.Util.URLClassLoader
;
import
org.kohsuke.args4j.Argument
;
import
org.kohsuke.args4j.CmdLineException
;
import
org.kohsuke.args4j.CmdLineParser
;
import
org.kohsuke.args4j.Option
;
import
org.kohsuke.args4j.ParserProperties
;
import
org.xml.sax.InputSource
;
import
java.io.File
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.io.PrintStream
;
import
java.math.BigInteger
;
import
java.nio.file.Files
;
import
java.nio.file.Path
;
import
java.nio.file.Paths
;
import
java.text.SimpleDateFormat
;
import
java.util.ArrayList
;
import
java.util.Arrays
;
import
java.util.Date
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.ServiceLoader
;
import
java.util.stream.Collectors
;
import
java.util.stream.Stream
;
public
class
ADESchemaCompiler
{
private
SimpleDateFormat
df
=
new
SimpleDateFormat
(
"HH:mm:ss"
);
private
Path
appHome
;
private
Path
workDir
;
@Argument
private
List
<
String
>
adeSchemaFileList
=
new
ArrayList
<>();
@Option
(
name
=
"-package"
,
usage
=
"package for the ADE JAXB classes"
,
metaVar
=
"<packageName>"
)
private
String
packageName
=
"ade"
;
@Option
(
name
=
"-binding"
,
usage
=
"optional JAXB binding file for ADE XML Schema"
,
metaVar
=
"<fileName>"
)
private
Path
adeBindingFile
;
@Option
(
name
=
"-output"
,
usage
=
"output folder"
,
metaVar
=
"<folderName>"
)
private
Path
outputFolder
=
Paths
.
get
(
"src-gen"
);
@Option
(
name
=
"-non-strict"
,
usage
=
"allow changes to contents of subfolder 'schemas'"
)
private
boolean
nonStrict
=
false
;
@Option
(
name
=
"-clean"
,
usage
=
"clean output folder"
)
private
boolean
clean
;
@Option
(
name
=
"-help"
,
aliases
={
"-h"
},
usage
=
"print this help message and exit"
)
private
boolean
help
;
@Option
(
name
=
"-version"
,
aliases
={
"-v"
},
usage
=
"print product version and exit"
)
private
boolean
version
;
@Option
(
name
=
"-X{arg}"
,
usage
=
"one or more arguments for JAXB plugins"
)
private
Boolean
dummy
;
public
static
void
main
(
String
[]
args
)
{
new
ADESchemaCompiler
().
doMain
(
args
);
}
private
void
doMain
(
String
[]
_args
)
{
CmdLineParser
parser
=
new
CmdLineParser
(
this
,
ParserProperties
.
defaults
().
withUsageWidth
(
80
));
Map
<
Boolean
,
List
<
String
>>
args
=
Arrays
.
stream
(
_args
)
.
flatMap
(
arg
->
Arrays
.
stream
(
arg
.
split
(
"\\s+"
)))
.
collect
(
Collectors
.
partitioningBy
(
arg
->
!
arg
.
startsWith
(
"-X"
)));
try
{
parser
.
parseArgument
(
args
.
get
(
Boolean
.
TRUE
));
}
catch
(
CmdLineException
e
)
{
System
.
err
.
println
(
e
.
getMessage
());
printUsage
(
parser
,
System
.
err
);
System
.
exit
(
1
);
}
if
(
help
)
{
printUsage
(
parser
,
System
.
out
);
System
.
exit
(
0
);
}
if
(
version
)
{
System
.
out
.
println
(
getClass
().
getPackage
().
getImplementationTitle
()
+
" version "
+
getClass
().
getPackage
().
getImplementationVersion
());
System
.
out
.
println
(
"XML Schema binding compiler for CityGML ADEs; use with citygml4j version "
+
getClass
().
getPackage
().
getImplementationVersion
());
System
.
exit
(
0
);
}
if
(
adeSchemaFileList
.
isEmpty
())
{
System
.
out
.
println
(
"Provide the ADE XML Schema definition file to be compiled as argument"
);
printUsage
(
parser
,
System
.
out
);
System
.
exit
(
1
);
}
if
(
adeSchemaFileList
.
size
()
>
1
)
{
System
.
out
.
println
(
"Just provide one ADE XML Schema definition file at a time"
);
printUsage
(
parser
,
System
.
out
);
System
.
exit
(
1
);
}
log
(
LogLevel
.
INFO
,
"Starting ade-xjc compiler"
);
int
status
=
0
;
initialize
();
try
{
log
(
LogLevel
.
INFO
,
"Setting up build environment"
);
Path
schemaDir
=
appHome
.
resolve
(
"schemas"
);
Path
pluginsDir
=
appHome
.
resolve
(
"jaxb-plugins"
);
Path
adeSchemaFile
=
Paths
.
get
(
adeSchemaFileList
.
get
(
0
));
if
(!
adeSchemaFile
.
isAbsolute
())
adeSchemaFile
=
workDir
.
resolve
(
adeSchemaFile
);
if
(!
adeBindingFile
.
isAbsolute
())
adeBindingFile
=
workDir
.
resolve
(
adeBindingFile
);
if
(!
outputFolder
.
isAbsolute
())
outputFolder
=
workDir
.
resolve
(
outputFolder
);
checkBuildEnvironment
(
schemaDir
,
adeSchemaFile
);
if
(
clean
&&
Files
.
exists
(
outputFolder
))
{
log
(
LogLevel
.
INFO
,
"Cleaning output folder"
);
Files
.
walkFileTree
(
outputFolder
,
new
FileDeleter
(
true
));
}
createBuildEnvironment
();
if
(
packageName
.
startsWith
(
"org.citygml4j"
))
{
log
(
LogLevel
.
ERROR
,
"The package "
+
packageName
+
" is not allowed for the ADE classes"
);
log
(
LogLevel
.
ERROR
,
"Choose a package which is not a subpackage of org.citygml4j. Aborting."
);
System
.
exit
(
1
);
}
log
(
LogLevel
.
INFO
,
"Using ADE schema "
+
adeSchemaFile
.
toAbsolutePath
().
toString
());
if
(
adeBindingFile
!=
null
)
log
(
LogLevel
.
INFO
,
"Using JAXB binding "
+
adeBindingFile
.
toAbsolutePath
());
log
(
LogLevel
.
INFO
,
"Using Java package "
+
packageName
+
" for JAXB classes"
);
SchemaCompiler
sc
=
XJC
.
createSchemaCompiler
();
sc
.
setDefaultPackageName
(
packageName
);
if
(!
args
.
get
(
Boolean
.
FALSE
).
isEmpty
()
&&
Files
.
exists
(
pluginsDir
))
{
log
(
LogLevel
.
INFO
,
"Loading and configuring JAXB plugins"
);
String
[]
pluginArgs
=
args
.
get
(
Boolean
.
FALSE
).
toArray
(
new
String
[
0
]);
Options
options
=
((
SchemaCompilerImpl
)
sc
).
getOptions
();
URLClassLoader
loader
=
new
URLClassLoader
();
try
(
Stream
<
Path
>
stream
=
Files
.
walk
(
pluginsDir
).
filter
(
path
->
path
.
getFileName
().
toString
().
toLowerCase
().
endsWith
(
".jar"
)))
{
stream
.
forEach
(
loader:
:
addPath
);
}
ServiceLoader
<
Plugin
>
plugins
=
ServiceLoader
.
load
(
Plugin
.
class
,
loader
);
for
(
Plugin
plugin
:
plugins
)
{
for
(
int
i
=
0
;
i
<
pluginArgs
.
length
;
i
++)
{
if
((
'-'
+
plugin
.
getOptionName
()).
equals
(
pluginArgs
[
i
]))
{
log
(
LogLevel
.
INFO
,
"Found JAXB plugin for option '-"
+
plugin
.
getOptionName
()
+
"'"
);
options
.
activePlugins
.
add
(
plugin
);
plugin
.
onActivated
(
options
);
}
plugin
.
parseArgument
(
options
,
pluginArgs
,
i
);
}
}
}
XJCErrorListener
listener
=
new
XJCErrorListener
();
sc
.
setErrorListener
(
listener
);
log
(
LogLevel
.
INFO
,
"Generating JAXB classes. This may take some time..."
);
sc
.
parseSchema
(
new
InputSource
(
schemaDir
.
resolve
(
"CityGML"
).
resolve
(
"citygml4j_profile.xsd"
).
toUri
().
toString
()));
sc
.
parseSchema
(
new
InputSource
(
schemaDir
.
resolve
(
"CityGML"
).
resolve
(
"binding.xjb"
).
toUri
().
toString
()));
sc
.
parseSchema
(
new
InputSource
(
adeSchemaFile
.
toUri
().
toString
()));
if
(
adeBindingFile
!=
null
)
sc
.
parseSchema
(
new
InputSource
(
adeBindingFile
.
toUri
().
toString
()));
S2JJAXBModel
model
=
sc
.
bind
();
JCodeModel
code
=
model
.
generateCode
(
null
,
listener
);
// write classes using a header comment
PrologCodeWriter
writer
=
new
PrologCodeWriter
(
new
FileCodeWriter
(
outputFolder
.
toFile
(),
"UTF-8"
),
"Generated with ade-xjc - XML Schema binding compiler for CityGML ADEs, version "
+
this
.
getClass
().
getPackage
().
getImplementationVersion
()
+
"\n"
+
"ade-xjc is part of the citygml4j project, see https://github.com/citygml4j\n"
+
"Any modifications to this file will be lost upon recompilation of the source\n"
+
"Generated: "
+
new
Date
().
toString
()
+
"\n"
);
code
.
build
(
writer
);
log
(
LogLevel
.
INFO
,
"JAXB classes successfully written to "
+
outputFolder
.
toAbsolutePath
());
}
catch
(
Exception
e
)
{
if
(
e
.
getMessage
()
!=
null
)
log
(
LogLevel
.
ERROR
,
e
.
getMessage
());
else
log
(
LogLevel
.
ERROR
,
"An unexpected "
+
e
+
" occurred."
);
log
(
LogLevel
.
ERROR
,
"Unable to recover from previous error(s). Aborting."
);
status
=
1
;
}
finally
{
try
{
cleanBuildEnvironment
(
status
==
1
);
}
catch
(
IOException
e
)
{
log
(
LogLevel
.
ERROR
,
"Failed to clean build environment."
);
status
=
1
;
}
}
System
.
exit
(
status
);
}
private
void
initialize
()
{
String
appHomeName
=
System
.
getenv
(
"APP_HOME"
);
String
workDirName
=
System
.
getenv
(
"WORKING_DIR"
);
if
(
appHomeName
==
null
)
{
log
(
LogLevel
.
WARN
,
"Environment variable APP_HOME not set. Using current working directory."
);
appHomeName
=
"."
;
}
if
(
workDirName
==
null
)
{
log
(
LogLevel
.
WARN
,
"Environment variable WORKING_DIR not set. Using current working directory."
);
workDirName
=
"."
;
}
appHome
=
Paths
.
get
(
appHomeName
).
normalize
().
toAbsolutePath
();
if
(!
Files
.
exists
(
appHome
))
{
log
(
LogLevel
.
ERROR
,
"Invalid working directory '"
+
appHomeName
+
"'."
);
System
.
exit
(
1
);
}
workDir
=
Paths
.
get
(
workDirName
).
normalize
().
toAbsolutePath
();
if
(!
Files
.
exists
(
workDir
))
{
log
(
LogLevel
.
ERROR
,
"Invalid working directory '"
+
workDirName
+
"'."
);
System
.
exit
(
1
);
}
}
private
void
createBuildEnvironment
()
throws
Exception
{
if
(!
Files
.
exists
(
outputFolder
))
Files
.
createDirectories
(
outputFolder
);
else
if
(!
Files
.
isDirectory
(
outputFolder
))
{
Files
.
delete
(
outputFolder
);
Files
.
createDirectories
(
outputFolder
);
}
}
private
void
checkBuildEnvironment
(
Path
schemaDir
,
Path
adeSchemaFile
)
throws
Exception
{
if
(!
Files
.
exists
(
schemaDir
))
throw
new
FileNotFoundException
(
"Failed to open folder "
+
schemaDir
.
toAbsolutePath
().
toString
());
if
(!
nonStrict
)
{
log
(
LogLevel
.
INFO
,
"Running in strict mode. Checking sanity of subfolder 'schemas'"
);
BigInteger
md5
=
new
BigInteger
(
"0"
);
md5
=
Util
.
dir2md5
(
schemaDir
.
toFile
(),
md5
);
if
(!
md5
.
toString
(
16
).
equals
(
"64fffffdb208ef0382a769611d2ab54"
))
throw
new
Exception
(
"Contents of subfolder 'schemas' have been altered. Please restore its original state."
);
}
if
(!
Files
.
exists
(
adeSchemaFile
)
||
!
Files
.
isRegularFile
(
adeSchemaFile
)
||
!
Files
.
isReadable
(
adeSchemaFile
))
throw
new
Exception
(
"Could not open ADE schema file "
+
adeSchemaFile
.
toAbsolutePath
().
toString
());
if
(
adeBindingFile
!=
null
&&
(!
Files
.
exists
(
adeBindingFile
)
||
!
Files
.
isRegularFile
(
adeBindingFile
)
||
!
Files
.
isReadable
(
adeBindingFile
)))
throw
new
Exception
(
"Could not open ADE binding file "
+
adeBindingFile
.
toAbsolutePath
());
}
private
void
cleanBuildEnvironment
(
boolean
cleanAll
)
throws
IOException
{
if
(!
cleanAll
)
{
Files
.
walkFileTree
(
outputFolder
.
resolve
(
"net/opengis/citygml"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
outputFolder
.
resolve
(
"net/opengis/gml"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
outputFolder
.
resolve
(
"oasis/names/tc/ciq/xsdschema/xal/_2"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
outputFolder
.
resolve
(
"org/citygml4j"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
outputFolder
.
resolve
(
"org/w3/_1999/xlink"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
outputFolder
.
resolve
(
"org/w3/_2001/smil20"
),
new
FileDeleter
(
true
));
Files
.
walkFileTree
(
outputFolder
.
resolve
(
"net"
),
new
FileDeleter
(
true
,
true
));
Files
.
walkFileTree
(
outputFolder
.
resolve
(
"org"
),
new
FileDeleter
(
true
,
true
));
Files
.
walkFileTree
(
outputFolder
.
resolve
(
"oasis"
),
new
FileDeleter
(
true
,
true
));
}
else
Files
.
walkFileTree
(
outputFolder
,
new
FileDeleter
(
true
));
}
private
void
printUsage
(
CmdLineParser
parser
,
PrintStream
out
)
{
out
.
println
(
"Usage: "
+
getClass
().
getPackage
().
getImplementationTitle
()
+
" [-options] ade.xsd"
);
out
.
println
();
out
.
println
(
"where options include:"
);
parser
.
printUsage
(
System
.
out
);
out
.
println
();
}
private
void
log
(
LogLevel
level
,
String
msg
)
{
System
.
out
.
println
(
"["
+
df
.
format
(
new
Date
())
+
" "
+
level
.
toString
()
+
"] "
+
msg
);
}
private
enum
LogLevel
{
INFO
(
"info"
),
WARN
(
"warn"
),
ERROR
(
"error"
);
private
String
value
;
LogLevel
(
String
value
)
{
this
.
value
=
value
;
}
public
String
toString
()
{
return
value
;
}
}
}
src/org/citygml4j/ade_xjc/FileDeleter.java
→
src/
main/java/
org/citygml4j/ade_xjc/FileDeleter.java
View file @
9416ebfb
...
...
@@ -32,17 +32,17 @@ public class FileDeleter extends SimpleFileVisitor<Path> {
private
boolean
recursive
;
private
boolean
onlyEmptyDirs
;
public
FileDeleter
(
boolean
recursive
)
{
FileDeleter
(
boolean
recursive
)
{
this
(
recursive
,
false
);
}
public
FileDeleter
(
boolean
recursive
,
boolean
onlyEmptyDirs
)
{
FileDeleter
(
boolean
recursive
,
boolean
onlyEmptyDirs
)
{
this
.
recursive
=
recursive
;
this
.
onlyEmptyDirs
=
onlyEmptyDirs
;
}
boolean
deleteFile
(
Path
file
)
throws
IOException
{
return
Files
.
deleteIfExists
(
file
);
private
void
deleteFile
(
Path
file
)
throws
IOException
{
Files
.
deleteIfExists
(
file
);
}
@Override
...
...
@@ -54,7 +54,7 @@ public class FileDeleter extends SimpleFileVisitor<Path> {
}
@Override
public
FileVisitResult
preVisitDirectory
(
Path
dir
,
BasicFileAttributes
attrs
)
throws
IOException
{
public
FileVisitResult
preVisitDirectory
(
Path
dir
,
BasicFileAttributes
attrs
)
{
return
FileVisitResult
.
CONTINUE
;
}
...
...
@@ -77,7 +77,7 @@ public class FileDeleter extends SimpleFileVisitor<Path> {
}
@Override
public
FileVisitResult
visitFileFailed
(
Path
file
,
IOException
exc
)
throws
IOException
{
public
FileVisitResult
visitFileFailed
(
Path
file
,
IOException
exc
)
{
return
FileVisitResult
.
CONTINUE
;
}
...
...
src/org/citygml4j/ade_xjc/Util.java
→
src/
main/java/
org/citygml4j/ade_xjc/Util.java
View file @
9416ebfb
/*
* ade-xjc - XML Schema binding compiler for CityGML ADEs
* https://github.com/citygml4j/ade-xjc
*
* ade-xjc is part of the citygml4j project
*
* Copyright 2013-2017 Claus Nagel <claus.nagel@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.citygml4j.ade_xjc
;
import
java.io.File
;
import
java.io.FileFilter
;
import
java.io.FileInputStream
;
import
java.io.FileNotFoundException
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigInteger
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.nio.file.Path
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
public
class
Util
{
public
static
BigInteger
dir2md5
(
File
file
,
BigInteger
md5
)
throws
NoSuchAlgorithmException
,
FileNotFoundException
{
md5
=
string2md5
(
file
.
getName
(),
md5
);
if
(
file
.
isDirectory
())
{
File
[]
children
=
file
.
listFiles
(
new
FileFilter
()
{
public
boolean
accept
(
File
pathname
)
{
if
(
pathname
.
isDirectory
())
return
true
;
if
(
pathname
.
getName
().
endsWith
(
"xsd"
)
||
pathname
.
getName
().
endsWith
(
"xjb"
))
return
true
;
return
false
;
}
});
for
(
File
next
:
children
)
md5
=
dir2md5
(
next
,
md5
);
}
else
md5
=
file2md5
(
file
.
getAbsolutePath
(),
md5
);
return
md5
;
}
public
static
BigInteger
file2md5
(
String
fileName
,
BigInteger
md5
)
throws
NoSuchAlgorithmException
,
FileNotFoundException
{
MessageDigest
digest
=
MessageDigest
.
getInstance
(
"MD5"
);
InputStream
is
=
new
FileInputStream
(
new
File
(
fileName
));
byte
[]
buffer
=
new
byte
[
8192
];
int
read
=
0
;
try
{
while
(
(
read
=
is
.
read
(
buffer
))
>
0
)
digest
.
update
(
buffer
,
0
,
read
);
byte
[]
md5sum
=
digest
.
digest
();
return
md5
.
xor
(
new
BigInteger
(
1
,
md5sum
));
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"Unable to process file for MD5"
,
e
);
}
finally
{
try
{
is
.
close
();
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"Unable to close input stream for MD5 calculation"
,
e
);
}
}
}
private
static
BigInteger
string2md5
(
String
input
,
BigInteger
md5
)
throws
NoSuchAlgorithmException
{
MessageDigest
digest
=
MessageDigest
.
getInstance
(
"MD5"
);
byte
[]
md5sum
=
digest
.
digest
(
input
.
getBytes
());
return
md5
.
xor
(
new
BigInteger
(
1
,
md5sum
));
}
public
static
class
URLClassLoader
extends
java
.
net
.
URLClassLoader
{
protected
URLClassLoader
()
{
super
(
new
URL
[]{},
Util
.
class
.
getClassLoader
());
}
@Override
protected
void
addURL
(
URL
url
)
{
super
.
addURL
(
url
);
}
protected
void
addPath
(
Path
path
)
{
try
{
super
.
addURL
(
path
.
toUri
().
toURL
());
}
catch
(
MalformedURLException
e
)
{
//
}
}
}
}
/*
* ade-xjc - XML Schema binding compiler for CityGML ADEs
* https://github.com/citygml4j/ade-xjc
*
* ade-xjc is part of the citygml4j project
*
* Copyright 2013-2017 Claus Nagel <claus.nagel@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.citygml4j.ade_xjc
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.InputStream
;
import
java.math.BigInteger
;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
java.nio.file.Path
;
import
java.security.MessageDigest
;
import
java.security.NoSuchAlgorithmException
;
public
class
Util
{
public
static
BigInteger
dir2md5
(
File
file
,
BigInteger
md5
)
throws
NoSuchAlgorithmException
{
md5
=
string2md5
(
file
.
getName
(),
md5
);
if
(
file
.
isDirectory
())
{
File
[]
children
=
file
.
listFiles
(
pathname
->
pathname
.
isDirectory
()
||
pathname
.
getName
().
endsWith
(
"xsd"
)
||
pathname
.
getName
().
endsWith
(
"xjb"
));
if
(
children
!=
null
)
{
for
(
File
next
:
children
)
md5
=
dir2md5
(
next
,
md5
);
}
}
else
md5
=
file2md5
(
file
.
getAbsolutePath
(),
md5
);
return
md5
;
}
private
static
BigInteger
file2md5
(
String
fileName
,
BigInteger
md5
)
throws
NoSuchAlgorithmException
{
MessageDigest
digest
=
MessageDigest
.
getInstance
(
"MD5"
);
byte
[]
buffer
=
new
byte
[
8192
];
int
read
;
try
(
InputStream
is
=
new
FileInputStream
(
new
File
(
fileName
)))
{
while
(
(
read
=
is
.
read
(
buffer
))
>
0
)
digest
.
update
(
buffer
,
0
,
read
);
byte
[]
md5sum
=
digest
.
digest
();
return
md5
.
xor
(
new
BigInteger
(
1
,
md5sum
));
}
catch
(
IOException
e
)
{
throw
new
RuntimeException
(
"Unable to process file for MD5"
,
e
);
}
}
private
static
BigInteger
string2md5
(
String
input
,
BigInteger
md5
)
throws
NoSuchAlgorithmException
{
MessageDigest
digest
=
MessageDigest
.
getInstance
(
"MD5"
);
byte
[]
md5sum
=
digest
.
digest
(
input
.
getBytes
());
return
md5
.
xor
(
new
BigInteger
(
1
,
md5sum
));
}
public
static
class
URLClassLoader
extends
java
.
net
.
URLClassLoader
{
URLClassLoader
()
{
super
(
new
URL
[]{},
ADESchemaCompiler
.
class
.
getClassLoader
());
}
@Override
protected
void
addURL
(
URL
url
)
{
super
.
addURL
(
url
);
}
void
addPath
(
Path
path
)
{
try
{
super
.
addURL
(
path
.
toUri
().
toURL
());
}
catch
(
MalformedURLException
e
)
{
//
}
}
}
}
src/org/citygml4j/ade_xjc/XJCErrorListener.java
→
src/
main/java/
org/citygml4j/ade_xjc/XJCErrorListener.java
View file @
9416ebfb
/*
* ade-xjc - XML Schema binding compiler for CityGML ADEs
* https://github.com/citygml4j/ade-xjc
*
* ade-xjc is part of the citygml4j project
*
* Copyright 2013-2017 Claus Nagel <claus.nagel@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.citygml4j.ade_xjc
;
import
org.xml.sax.SAXParseException
;
import
com.sun.tools.xjc.api.ErrorListener
;
public
class
XJCErrorListener
implements
ErrorListener
{
public
void
error
(
SAXParseException
ex
)
{
ex
.
printStackTrace
();
}
public
void
fatalError
(
SAXParseException
ex
)
{
ex
.
printStackTrace
();
}
public
void
info
(
SAXParseException
ex
)
{
ex
.
printStackTrace
();
}
public
void
warning
(
SAXParseException
ex
)
{
ex
.
printStackTrace
();
}
}
/*
* ade-xjc - XML Schema binding compiler for CityGML ADEs
* https://github.com/citygml4j/ade-xjc
*
* ade-xjc is part of the citygml4j project
*
* Copyright 2013-2017 Claus Nagel <claus.nagel@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package
org.citygml4j.ade_xjc
;
import
org.xml.sax.SAXParseException
;
import
com.sun.tools.xjc.api.ErrorListener
;
public
class
XJCErrorListener
implements
ErrorListener
{
public
void
error
(
SAXParseException
ex
)
{
ex
.
printStackTrace
();
}
public
void
fatalError
(
SAXParseException
ex
)
{
ex
.
printStackTrace
();
}
public
void
info
(
SAXParseException
ex
)
{
ex
.
printStackTrace
();
}
public
void
warning
(
SAXParseException
ex
)
{
ex
.
printStackTrace
();
}
}
Prev
1
2
Next
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment