1 package xuml.tools.maven.plugin;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
43
44
45
46
47
48 public class GenerateJpaMojo extends AbstractMojo {
49
50 private static final String DEFAULT_IMPLEMENTATION_SUB_PACKAGE = "behaviour";
51
52
53
54
55
56
57
58 private MavenProject project;
59
60
61
62
63
64
65 private File outputSourceDirectory;
66
67
68
69
70
71
72
73
74 private String domainsXml;
75
76
77
78
79
80
81
82 private String domain;
83
84
85
86
87
88
89
90 private String schema;
91
92
93
94
95
96
97
98 private String implementationPackageName;
99
100
101
102
103
104
105 private File implementationSourceDirectory;
106
107
108
109
110
111
112 private boolean implementationOverwrite;
113
114
115
116
117
118
119 private File resourcesDirectory;
120
121
122
123
124
125
126
127 private boolean generatePersistenceXml;
128
129
130
131
132
133
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
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 }