View Javadoc
1   package xuml.tools.maven.plugin;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.FileInputStream;
21  import java.io.FileOutputStream;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.Set;
25  
26  import org.apache.maven.artifact.Artifact;
27  import org.apache.maven.plugin.AbstractMojo;
28  import org.apache.maven.plugin.MojoExecutionException;
29  import org.apache.maven.project.MavenProject;
30  
31  import com.google.common.base.Optional;
32  
33  import jakarta.xml.bind.JAXBElement;
34  import xuml.tools.diagram.ClassDiagramGenerator;
35  import xuml.tools.miuml.metamodel.jaxb.Domain;
36  import xuml.tools.miuml.metamodel.jaxb.Marshaller;
37  import xuml.tools.miuml.metamodel.jaxb.ModeledDomain;
38  import xuml.tools.miuml.metamodel.jaxb.Subsystem;
39  import xuml.tools.model.compiler.CodeGeneratorJava;
40  
41  /**
42   * Generates JPA classes from a miUML schema specified domain.
43   * 
44   * @goal generate-jpa
45   * 
46   * @phase process-resources
47   */
48  public class GenerateJpaMojo extends AbstractMojo {
49  
50      private static final String DEFAULT_IMPLEMENTATION_SUB_PACKAGE = "behaviour";
51  
52      /**
53       * @parameter default-value="${project}"
54       * @required
55       * @readonly
56       * @since 1.0
57       */
58      private MavenProject project;
59  
60      /**
61       * Location to place generated java source.
62       * 
63       * @parameter default-value="${project.build.directory}/generated-sources"
64       */
65      private File outputSourceDirectory;
66  
67      /**
68       * Location of miUML schema compliant xml (classpath checked first then
69       * filesystem)
70       * 
71       * @parameter default-value="/domains.xml"
72       * @required
73       */
74      private String domainsXml;
75  
76      /**
77       * Specific domain in domains xml to generate from.
78       * 
79       * @parameter default-value="/domains.xml"
80       * @required
81       */
82      private String domain;
83  
84      /**
85       * Schema name.
86       * 
87       * @parameter default-value="xuml"
88       * @required
89       */
90      private String schema;
91  
92      /**
93       * Package name for generated implementation source. Defaults to packageName
94       * + ".behaviour".
95       * 
96       * @parameter
97       */
98      private String implementationPackageName;
99  
100     /**
101      * Implementation generated source directory.
102      * 
103      * @parameter default-value="${project.build.directory}/generated-sources"
104      */
105     private File implementationSourceDirectory;
106 
107     /**
108      * Implementation generated source directory.
109      * 
110      * @parameter default-value="false"
111      */
112     private boolean implementationOverwrite;
113 
114     /**
115      * Resources directory.
116      * 
117      * @parameter default-value="${project.build.directory}/generated-resources"
118      */
119     private File resourcesDirectory;
120 
121     /**
122      * If and only if true generate META-INF/persistence.xml in
123      * resourcesDirectory.
124      * 
125      * @parameter default-value="true"
126      */
127     private boolean generatePersistenceXml;
128 
129     /**
130      * Root package name of the generated classes.
131      * 
132      * @parameter default-value="xuml"
133      * @required
134      */
135     private String packageName;
136 
137     @Override
138     public void execute() throws MojoExecutionException {
139 
140         createDirectory(outputSourceDirectory);
141         createDirectory(implementationSourceDirectory);
142 
143         xuml.tools.miuml.metamodel.jaxb.Domains domains = getDomains();
144 
145         if (implementationPackageName == null)
146             implementationPackageName = packageName + "." + DEFAULT_IMPLEMENTATION_SUB_PACKAGE;
147 
148         generate(domains);
149 
150         project.addCompileSourceRoot(outputSourceDirectory.getAbsolutePath());
151         // TODO add resourcesDirectory to resources
152 
153         generateClassDiagrams(domains);
154     }
155 
156     private void generate(xuml.tools.miuml.metamodel.jaxb.Domains domains) {
157         new CodeGeneratorJava(domains, domain, packageName, schema, outputSourceDirectory,
158                 resourcesDirectory, implementationPackageName, implementationSourceDirectory,
159                 generatePersistenceXml, implementationOverwrite).generate();
160     }
161 
162     private void generateClassDiagrams(xuml.tools.miuml.metamodel.jaxb.Domains domains) {
163         String localRepo = project.getProperties().getProperty("settings.localRepository");
164 
165         @SuppressWarnings("unchecked")
166         Set<Artifact> artifacts = project.getDependencyArtifacts();
167         for (Artifact artifact : artifacts) {
168             if (artifact.getArtifactId().equals("xuml-diagrams")
169                     && artifact.getGroupId().equals("org.github.davidmoten")) {
170                 String version = artifact.getVersion();
171                 String name = "org/github/davidmoten/xuml-diagrams/" + version + "/xuml-diagrams-"
172                         + version + ".war";
173                 File war = new File(localRepo, name);
174                 getLog().info("found war: " + war.getAbsolutePath());
175             }
176         }
177 
178         int domainIndex = 0;
179         for (JAXBElement<? extends Domain> domain : domains.getDomain()) {
180             if (domain.getValue() instanceof ModeledDomain) {
181                 ModeledDomain md = (ModeledDomain) domain.getValue();
182                 int ssIndex = 0;
183                 for (@SuppressWarnings("unused")
184                 Subsystem ss : md.getSubsystem()) {
185                     String s = new ClassDiagramGenerator().generate(domains, domainIndex, ssIndex,
186                             Optional.<String> absent());
187                     String name = md.getName().replaceAll(" ", "_") + "_" + ssIndex + ".html";
188                     writeToFile(resourcesDirectory, name, s);
189                     ssIndex++;
190                 }
191             }
192             domainIndex++;
193         }
194     }
195 
196     private xuml.tools.miuml.metamodel.jaxb.Domains getDomains() throws MojoExecutionException {
197         InputStream is = getClass().getResourceAsStream(domainsXml);
198         if (is == null)
199             try {
200                 getLog().info("domains xml file not found on classpath: " + domainsXml);
201                 File file = new File(domainsXml);
202                 if (!file.exists())
203                     file = new File(project.getBasedir(), domainsXml);
204                 getLog().info("loading domains xml from file system: " + file.getCanonicalPath());
205                 is = new FileInputStream(file);
206             } catch (IOException e) {
207                 throw new MojoExecutionException(e.getMessage(), e);
208             }
209         xuml.tools.miuml.metamodel.jaxb.Domains domains = new Marshaller().unmarshal(is);
210         return domains;
211     }
212 
213     private void createDirectory(File directory) {
214         if (!directory.exists()) {
215             if (directory.mkdirs())
216                 getLog().info("created directory " + directory);
217         }
218     }
219 
220     private void writeToFile(File resources, String name, String html) {
221         try {
222             FileOutputStream fos = new FileOutputStream(new File(resources, name));
223             fos.write(html.getBytes());
224             fos.close();
225         } catch (IOException e) {
226             throw new RuntimeException(e);
227         }
228     }
229 }