001: package org.objectweb.celtix.maven_plugin;
002:
003: import java.io.File;
004: import java.util.ArrayList;
005: import java.util.List;
006:
007: import org.apache.maven.plugin.AbstractMojo;
008: import org.apache.maven.plugin.MojoExecutionException;
009: import org.apache.maven.project.MavenProject;
010: import org.apache.tools.ant.ExitException;
011: import org.apache.tools.ant.util.optional.NoExitSecurityManager;
012:
013: /**
014: * @goal xsdtojava
015: * @description Celtix XSD To Java Tool
016: */
017: public class XSDToJavaMojo extends AbstractMojo {
018: /**
019: * @parameter
020: */
021: String testSourceRoot;
022:
023: /**
024: * @parameter expression="${project.build.directory}/generated/src/main/java"
025: * @required
026: */
027: String sourceRoot;
028:
029: /**
030: * @parameter expression="${project}"
031: * @required
032: */
033: MavenProject project;
034:
035: /**
036: * @parameter
037: */
038: XSDOption xsdOptions[];
039:
040: public void execute() throws MojoExecutionException {
041: String outputDir = testSourceRoot == null ? sourceRoot
042: : testSourceRoot;
043: File outputDirFile = new File(outputDir);
044: outputDirFile.mkdirs();
045:
046: long timestamp = CodegenUtils.getCodegenTimestamp();
047: boolean result = true;
048:
049: for (int x = 0; x < xsdOptions.length; x++) {
050: List list = new ArrayList();
051: if (xsdOptions[x].getPackagename() != null) {
052: list.add("-p");
053: list.add(xsdOptions[x].getPackagename());
054: }
055: if (xsdOptions[x].getBindingFile() != null) {
056: list.add("-b");
057: list.add(xsdOptions[x].getBindingFile());
058: }
059: list.add("-quiet");
060: list.add("-d");
061: list.add(outputDir);
062: list.add(xsdOptions[x].getXsd());
063:
064: File file = new File(xsdOptions[x].getXsd());
065: File doneFile = new File(outputDirFile, "."
066: + file.getName() + ".DONE");
067: boolean doWork = timestamp > doneFile.lastModified();
068: if (!doneFile.exists()) {
069: doWork = true;
070: } else if (file.lastModified() > doneFile.lastModified()) {
071: doWork = true;
072: } else {
073: File files[] = xsdOptions[x].getDependencies();
074: if (files != null) {
075: for (int z = 0; z < files.length; ++z) {
076: if (files[z].lastModified() > doneFile
077: .lastModified()) {
078: doWork = true;
079: }
080: }
081: }
082: }
083:
084: if (doWork) {
085: SecurityManager oldSm = System.getSecurityManager();
086: try {
087: try {
088: System
089: .setSecurityManager(new NoExitSecurityManager());
090:
091: com.sun.tools.xjc.Driver.main((String[]) list
092: .toArray(new String[list.size()]));
093:
094: } catch (ExitException e) {
095: if (e.getStatus() == 0) {
096: doneFile.delete();
097: doneFile.createNewFile();
098: } else {
099: throw e;
100: }
101: } finally {
102: System.setSecurityManager(oldSm);
103: File dirs[] = xsdOptions[x].getDeleteDirs();
104: if (dirs != null) {
105: for (int idx = 0; idx < dirs.length; ++idx) {
106: result = result && deleteDir(dirs[idx]);
107: }
108: }
109: }
110: } catch (Exception e) {
111: e.printStackTrace();
112: throw new MojoExecutionException(e.getMessage(), e);
113: }
114: }
115:
116: if (!result) {
117: throw new MojoExecutionException(
118: "Could not delete redundant dirs");
119: }
120: }
121:
122: if (project != null && sourceRoot != null) {
123: project.addCompileSourceRoot(sourceRoot);
124: }
125: if (project != null && testSourceRoot != null) {
126: project.addTestCompileSourceRoot(testSourceRoot);
127: }
128: }
129:
130: private boolean deleteDir(File f) {
131: if (f.isDirectory()) {
132: File files[] = f.listFiles();
133: for (int idx = 0; idx < files.length; ++idx) {
134: deleteDir(files[idx]);
135: }
136: }
137:
138: if (f.exists()) {
139: return f.delete();
140: }
141:
142: return true;
143: }
144: }
|