Commit 8fff6436 authored by Claus Nagel's avatar Claus Nagel
Browse files

added possibility to use jaxb-basics in schema compilation

parent 9561fd15
......@@ -2,11 +2,22 @@
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/xercesImpl.jar"/>
<classpathentry kind="lib" path="lib/xml-apis.jar"/>
<classpathentry kind="lib" path="lib/jaxb-core.jar"/>
<classpathentry kind="lib" path="lib/jaxb-impl.jar"/>
<classpathentry kind="lib" path="lib/jaxb-xjc.jar"/>
<classpathentry kind="lib" path="lib/args4j-2.33.jar"/>
<classpathentry kind="lib" path="lib/args4j/args4j-2.33.jar"/>
<classpathentry kind="lib" path="lib/jaxb/jaxb-core.jar"/>
<classpathentry kind="lib" path="lib/jaxb/jaxb-impl.jar"/>
<classpathentry kind="lib" path="lib/jaxb/jaxb-xjc.jar"/>
<classpathentry kind="lib" path="lib/xerces/xercesImpl.jar"/>
<classpathentry kind="lib" path="lib/xerces/xml-apis.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-basics/commons-beanutils-1.9.2.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-basics/commons-collections-3.2.1.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-basics/commons-lang3-3.2.1.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-basics/javaparser-1.0.11.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-basics/jaxb2-basics-0.11.1.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-basics/jaxb2-basics-runtime-0.11.1.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-basics/jaxb2-basics-tools-0.11.1.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-basics/jcl-over-slf4j-1.7.7.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-basics/slf4j-api-1.7.7.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-basics/slf4j-simple-1.7.7.jar"/>
<classpathentry kind="lib" path="lib/jaxb2-namespace-prefix/jaxb2-namespace-prefix-1.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
......@@ -57,7 +57,7 @@
<!-- classpath -->
<path id="classpath">
<fileset dir="lib" includes="*.jar" />
<fileset dir="lib" includes="**/*.jar" />
</path>
<tstamp>
......@@ -79,7 +79,7 @@
<mkdir dir="${build.classes}" />
<!-- compile the java code from ${src} and ${src-gen} into ${build} -->
<javac classpathref="classpath" srcdir="${src}" destdir="${build.classes}" />
<javac includeantruntime="false" classpathref="classpath" srcdir="${src}" destdir="${build.classes}" />
</target>
<target name="dist" depends="clean, compile" description="generate the distribution">
......@@ -87,6 +87,11 @@
<delete dir="${dist}" />
<mkdir dir="${dist}" />
<!-- copy mandatory dependencies -->
<copy toDir="${dist.lib}" flatten="true">
<fileset dir="${lib}" includes="**/*.jar" />
</copy>
<!-- copy LICENSE file -->
<copy todir="${dist.license}">
<fileset dir="${resources.license}" includes="**/*" />
......@@ -123,10 +128,12 @@
<buildnumber file="build.num" />
<!-- set package variables -->
<pathconvert property="manifest.classpath" dirsep="/" pathsep=" " refid="classpath">
<map from="${basedir}${file.separator}lib${file.separator}" to="lib${file.separator}" />
<fileset dir="${dist.lib}" id="lib.files">
<include name="*.jar" />
</fileset>
<pathconvert property="manifest.classpath" dirsep="/" pathsep=" " refid="lib.files">
<map from="${dist}${file.separator}lib${file.separator}" to="lib${file.separator}" />
</pathconvert>
<buildnumber file="build.num" />
<!-- put everything in ${build} into .jar file -->
<jar jarfile="${dist}/${app.jar}" basedir="${build.classes}" filesetmanifest="mergewithoutmain">
......@@ -142,11 +149,6 @@
<fileset dir="${dist.license}" includes="**/*" />
</jar>
<!-- copy mandatory dependencies -->
<copy toDir="${dist.lib}">
<fileset dir="${lib}" includes="*.jar" />
</copy>
<!-- clean up template dir -->
<delete dir="${build}" />
</target>
......
......@@ -22,14 +22,20 @@ 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.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 org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.CmdLineException;
......@@ -39,9 +45,12 @@ import org.kohsuke.args4j.ParserProperties;
import org.xml.sax.InputSource;
import com.sun.codemodel.JCodeModel;
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");
......@@ -58,13 +67,13 @@ public class ADE_XJC {
@Argument
private List<String> adeSchemaFileList = new ArrayList<String>();
@Option(name="-package", usage="package for the ADE JAXB classes (default: 'ade')", metaVar="<packageName>")
@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 (default: 'src-gen')", metaVar="<folderName>")
@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'")
......@@ -79,23 +88,19 @@ public class ADE_XJC {
@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) {
private void doMain(String[] tmp) {
CmdLineParser parser = new CmdLineParser(this, ParserProperties.defaults().withUsageWidth(80));
List<String> tmp = new ArrayList<String>();
for (String arg : args)
tmp.addAll(Arrays.asList(arg.split(" +")));
String[] newArgs = new String[tmp.size()];
for (int i = 0; i < tmp.size(); i++)
newArgs[i] = tmp.get(i);
Map<Boolean, List<String>> args = Arrays.stream(tmp).collect(Collectors.partitioningBy(arg -> !arg.startsWith("-X")));
try {
parser.parseArgument(newArgs);
parser.parseArgument(args.get(Boolean.TRUE));
} catch (CmdLineException e) {
System.err.println(e.getMessage());
printUsage(parser, System.err);
......@@ -133,9 +138,9 @@ public class ADE_XJC {
log(LogLevel.INFO, "Setting up build environment");
checkBuildEnvironment();
if (clean) {
if (clean && Files.exists(outputFolder.toPath())) {
log(LogLevel.INFO, "Cleaning output folder");
Util.rmdir(outputFolder, true);
Files.walkFileTree(outputFolder.toPath(), new FileDeleter(true));
}
createBuildEnvironment();
......@@ -151,14 +156,32 @@ public class ADE_XJC {
log(LogLevel.INFO, "Using JAXB binding " + adeBindingFile.getCanonicalFile());
log(LogLevel.INFO, "Using Java package " + packageName + " for JAXB classes");
log(LogLevel.INFO, "Generating JAXB classes. This may take some time...");
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.setDefaultPackageName(packageName);
if (!args.get(Boolean.FALSE).isEmpty()) {
log(LogLevel.INFO, "Loading and configuring JAXB plugins");
String[] pluginArgs = args.get(Boolean.FALSE).stream().toArray(String[]::new);
Options options = ((SchemaCompilerImpl)sc).getOptions();
ServiceLoader<Plugin> plugins = ServiceLoader.load(Plugin.class);
for (Plugin plugin : plugins) {
for (int i = 0; i < pluginArgs.length; i++) {
if (('-' + plugin.getOptionName()).equals(pluginArgs[i])) {
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()));
......@@ -178,11 +201,13 @@ public class ADE_XJC {
log(LogLevel.ERROR, "Unable to recover from previous error(s). Aborting.");
status = 1;
if (clean)
Util.rmdir(outputFolder, true);
} finally {
cleanBuildEnvironment();
try {
cleanBuildEnvironment(status == 1);
} catch (IOException e) {
log(LogLevel.ERROR, "Failed to clean build environment.");
status = 1;
}
}
System.exit(status);
......@@ -206,7 +231,7 @@ public class ADE_XJC {
private void checkBuildEnvironment() throws Exception, FileNotFoundException, NoSuchAlgorithmException {
if (!schema_dir.exists())
throw new FileNotFoundException("Could not open folder " + schema_dir.getAbsolutePath());
throw new FileNotFoundException("Failed to open folder " + schema_dir.getAbsolutePath());
if (!nonStrict) {
log(LogLevel.INFO, "Running in strict mode. Checking sanity of subfolder 'schemas'");
......@@ -225,14 +250,19 @@ public class ADE_XJC {
throw new Exception("Could not open ADE binding file " + adeBindingFile.getAbsolutePath());
}
private void cleanBuildEnvironment() {
Util.rmdir(new File(outputFolder.getAbsolutePath() + "/net/opengis/citygml"), true);
Util.rmdir(new File(outputFolder.getAbsolutePath() + "/net/opengis/gml"), true);
Util.rmdir(new File(outputFolder.getAbsolutePath() + "/net"), false);
Util.rmdir(new File(outputFolder.getAbsolutePath() + "/oasis"), true);
if (!packageName.startsWith("org"))
Util.rmdir(new File(outputFolder.getAbsolutePath() + "/org"), true);
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) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment