1 package org.objectweb.celtix.maven_plugin; 2 3 import java.io.File ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 7 import org.apache.maven.plugin.AbstractMojo; 8 import org.apache.maven.plugin.MojoExecutionException; 9 import org.apache.maven.project.MavenProject; 10 import org.apache.tools.ant.ExitException; 11 import org.apache.tools.ant.util.optional.NoExitSecurityManager; 12 13 17 public class XSDToJavaMojo extends AbstractMojo { 18 21 String testSourceRoot; 22 23 27 String sourceRoot; 28 29 30 34 MavenProject project; 35 36 37 40 XSDOption xsdOptions[]; 41 42 43 public void execute() throws MojoExecutionException { 44 String outputDir = testSourceRoot == null ? sourceRoot : testSourceRoot; 45 File outputDirFile = new File (outputDir); 46 outputDirFile.mkdirs(); 47 48 long timestamp = CodegenUtils.getCodegenTimestamp(); 49 boolean result = true; 50 51 for (int x = 0; x < xsdOptions.length; x++) { 52 List list = new ArrayList (); 53 if (xsdOptions[x].getPackagename() != null) { 54 list.add("-p"); 55 list.add(xsdOptions[x].getPackagename()); 56 } 57 if (xsdOptions[x].getBindingFile() != null) { 58 list.add("-b"); 59 list.add(xsdOptions[x].getBindingFile()); 60 } 61 list.add("-quiet"); 62 list.add("-d"); 63 list.add(outputDir); 64 list.add(xsdOptions[x].getXsd()); 65 66 File file = new File (xsdOptions[x].getXsd()); 67 File doneFile = new File (outputDirFile, "." + file.getName() + ".DONE"); 68 boolean doWork = timestamp > doneFile.lastModified(); 69 if (!doneFile.exists()) { 70 doWork = true; 71 } else if (file.lastModified() > doneFile.lastModified()) { 72 doWork = true; 73 } else { 74 File files[] = xsdOptions[x].getDependencies(); 75 if (files != null) { 76 for (int z = 0; z < files.length; ++z) { 77 if (files[z].lastModified() > doneFile.lastModified()) { 78 doWork = true; 79 } 80 } 81 } 82 } 83 84 if (doWork) { 85 SecurityManager oldSm = System.getSecurityManager(); 86 try { 87 try { 88 System.setSecurityManager(new NoExitSecurityManager()); 89 90 com.sun.tools.xjc.Driver.main((String [])list.toArray(new String [list.size()])); 91 92 } catch (ExitException e) { 93 if (e.getStatus() == 0) { 94 doneFile.delete(); 95 doneFile.createNewFile(); 96 } else { 97 throw e; 98 } 99 } finally { 100 System.setSecurityManager(oldSm); 101 File dirs[] = xsdOptions[x].getDeleteDirs(); 102 if (dirs != null) { 103 for (int idx = 0; idx < dirs.length; ++idx) { 104 result = result && deleteDir(dirs[idx]); 105 } 106 } 107 } 108 } catch (Exception e) { 109 e.printStackTrace(); 110 throw new MojoExecutionException(e.getMessage(), e); 111 } 112 } 113 114 if (!result) { 115 throw new MojoExecutionException("Could not delete redundant dirs"); 116 } 117 } 118 119 if (project != null && sourceRoot != null) { 120 project.addCompileSourceRoot(sourceRoot); 121 } 122 if (project != null && testSourceRoot != null) { 123 project.addTestCompileSourceRoot(testSourceRoot); 124 } 125 } 126 127 private boolean deleteDir(File f) { 128 if (f.isDirectory()) { 129 File files[] = f.listFiles(); 130 for (int idx = 0; idx < files.length; ++idx) { 131 deleteDir(files[idx]); 132 } 133 } 134 135 if (f.exists()) { 136 return f.delete(); 137 } 138 139 return true; 140 } 141 } | Popular Tags |