KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > maven_plugin > XSDToJavaMojo


1 package org.objectweb.celtix.maven_plugin;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
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 /**
14  * @goal xsdtojava
15  * @description Celtix XSD To Java Tool
16  */

17 public class XSDToJavaMojo extends AbstractMojo {
18     /**
19      * @parameter
20      */

21     String JavaDoc testSourceRoot;
22     
23     /**
24      * @parameter expression="${project.build.directory}/generated/src/main/java"
25      * @required
26      */

27     String JavaDoc sourceRoot;
28     
29     
30     /**
31      * @parameter expression="${project}"
32      * @required
33      */

34     MavenProject project;
35     
36     
37     /**
38      * @parameter
39      */

40     XSDOption xsdOptions[];
41
42     
43     public void execute() throws MojoExecutionException {
44         String JavaDoc outputDir = testSourceRoot == null ? sourceRoot : testSourceRoot;
45         File JavaDoc outputDirFile = new File JavaDoc(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 JavaDoc list = new ArrayList JavaDoc();
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 JavaDoc file = new File JavaDoc(xsdOptions[x].getXsd());
67             File JavaDoc doneFile = new File JavaDoc(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 JavaDoc 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 JavaDoc oldSm = System.getSecurityManager();
86                 try {
87                     try {
88                         System.setSecurityManager(new NoExitSecurityManager());
89                         
90                         com.sun.tools.xjc.Driver.main((String JavaDoc[])list.toArray(new String JavaDoc[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 JavaDoc 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 JavaDoc 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 JavaDoc f) {
128         if (f.isDirectory()) {
129             File JavaDoc 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