-
Notifications
You must be signed in to change notification settings - Fork 155
/
AbstractScalaxbMojo.java
320 lines (289 loc) · 12 KB
/
AbstractScalaxbMojo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
package org.scalaxb.maven;
/*
* Copyright (c) 2011-2013 Martin Ellis
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import static java.util.Collections.emptyMap;
import static java.util.Collections.unmodifiableList;
import java.io.File;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugins.annotations.Parameter;
public abstract class AbstractScalaxbMojo extends AbstractMojo {
/**
* The directory containing the XSD files. If the specified directory does
* not exist or is empty, then it is ignored.
*/
@Parameter(property = "scalaxb.xsdDirectory",
defaultValue = "${project.basedir}/src/main/xsd",
required = true)
private File xsdDirectory;
/**
* The directory containing the WSDL files. If the specified directory does
* not exist or is empty, then it is ignored.
*/
@Parameter(property = "scalaxb.wsdlDirectory",
defaultValue = "${project.basedir}/src/main/wsdl",
required = true)
private File wsdlDirectory;
/**
* The output directory.
*/
@Parameter(property = "scalaxb.outputDirectory",
defaultValue = "${project.build.directory}/generated-sources/scalaxb",
required = true)
private File outputDirectory;
/**
* The package in which to generate classes. Classes are generated in the
* package specified, unless the <code>packageNames</code> parameter is used
* to override this value.
*/
@Parameter(property = "scalaxb.packageName",
defaultValue = "generated")
private String packageName;
/**
* Map of namespace URIs to package names for generated classes.
* <br/>
* This option can be used to override the <code>packageName</code>
* parameter (see above) for elements in specific namespaces. The mapping
* between namespace URIs and package names can be specifying any number of
* <code>packageName</code> elements within the <code>packageNames</code>
* element. For example:
* <pre>
* <packageNames>
* <packageName>
* <uri>http://example.com/service1</uri>
* <package>com.example.service1<package>
* </packageName>
* <packageName>
* <uri>http://example.com/service2</uri>
* <package>com.example.service2<package>
* </packageName>
* </packageNames>
* </pre>
*/
@Parameter
private PackageName[] packageNames;
/**
* The prefix to use on generated classes.
*/
@Parameter(property = "scalaxb.classPrefix")
private String classPrefix;
/**
* The prefix to use on generated parameter names.
*/
@Parameter(property = "scalaxb.parameterPrefix")
private String parameterPrefix;
/**
* Wraps the content of specified complex types into separate case classes.
* <br/>
* By default, when a type inherits from a complex type, the case class
* generated for that type will include fields for the content of the
* inherited type. With some XML schemas, this cause case classes with many
* fields to be generated. Many fields can cause case classes to become
* inconvenient to use, and there is a limit to the number of fields that
* can appear in a case class (see <code>chunkSize</code>).
* <br/>
* The <code><wrapContents></code> option causes a separate case class
* to be generated for the named complex types. Where a type inherits from
* the complex type, it will have a reference to the case class for that
* complex type, instead of a number of fields for the content of that
* complex type.
* <br/>
* This option is useful when inheriting from complexTypes would cause a
* case class to contain too many fields. It may be a better alternative to
* using the <code>chunkSize</code> option, because that simply limits the
* number of fields in every generated case class. In contrast, this option
* provides more fine-grained control: it allows related fields to be
* grouped for a given set of types.
* <br/>
* For example, the following will generate a separate case class for the
* complex type named in the <wrapContent> element.
* <pre>
* <wrapContents>
* <wrapContent>{http://example.com/schema}ComplexType</wrapContent>
* </wrapContents>
* </pre>
*/
@Parameter
private List<String> wrapContents;
/**
* Generate the scalaxb classes required to use the generated bindings.
* This option is useful for preventing duplicate copies of the scalaxb
* runtime being present on the classpath when more than one jar contains
* scalaxb bindings. To prevent the scalaxb runtime sources being
* generated, this option should be set to false.
*/
@Parameter(defaultValue = "true")
private boolean generateRuntime;
/**
* Maximum number of parameters to use in generated case class constructors.
* This allows parameters sequences to be separated into chunks of the given
* size.
*/
@Parameter
private Integer chunkSize;
/**
* Determines whether generated Scala files will be written into a directory
* corresponding to their package name. By default, the generated files are
* written in the output directory under a sub-directory that corresponds to
* the package name. For example, if the generated classes are in package
* 'foo', they will be generated in ${scalaxb.outputDirectory}/foo. Setting
* this value to false will cause the generated sources to be written
* directly into the output directory, without creating a directory for the
* package.
*/
@Parameter(property = "scalaxb.package-dir",
defaultValue = "true")
private boolean packageDir;
/**
* The name of the file to generate that includes the protocol
* implementation; that is, the code that marshals values to and from XML.
*/
@Parameter(defaultValue = "xmlprotocol.scala")
private String protocolFile;
/**
* The package in which to generate the 'protocol' code; that is, the code
* that marshals values to and from XML. The generated code defines a package
* object for the named package. The package object defines implicit values
* required for using the <code>scalaxb.toXML</code> and
* <code>scalaxb.fromXML</code> functions. If unspecified, the protocol code
* is generated in the same package as the generated classes that define the
* values marshalled to and from XML.
*/
@Parameter
private String protocolPackage;
/**
* Relaxes namespace constraints of <code>xs:any</code>.
* <br/>
* This option allows <code>xs:any</code> elements declared with a namespace
* attribute of <code>##local</code> to contain qualified XML elements.
* According to the W3C XML Schema recommendation, an XML element that is
* declared to be in a namespace is not permitted content for an
* <code>xs:any</code> element with a namespace of <code>##local</code>.
* By default, this option is false, thus enforcing this requirement. Setting
* this option to true allows namespaced content to be used.
*/
@Parameter(property = "scalaxb.lax-any",
defaultValue = "false")
private boolean laxAny;
/**
* Prefix to prepend to the names of generated parameters for XML attributes.
* <br/>
* This option sets a prefix to be used in the names of parameters for XML
* attributes. It is useful when a schema defines both an element and an
* attribute of the same name within a complex type.
*/
@Parameter(property = "scalaxb.attributePrefix")
private String attributePrefix;
@Parameter(property = "scalaxb.prependFamily",
defaultValue = "false")
private boolean prependFamily;
/**
* The version of
* <a href="http://dispatch.databinder.net/Dispatch.html">Dispatch</a>
* to be used when generating code from WSDL files.
*/
@Parameter(property = "scalaxb.dispatch.version")
private String dispatchVersion;
/**
* Generate non-blocking client code from WSDL sources.
*/
@Parameter(property = "scalaxb.async", defaultValue = "true")
private boolean async;
@Parameter(property = "scalaxb.verbose")
private boolean verbose;
/**
* Returns the directory in which to search for XSD schema files.
* @return The directory containing the XSD files.
*/
public File getXsdDirectory() {
return xsdDirectory;
}
/**
* Returns the directory in which to search for WSDL files.
* @return The directory containing WSDL files.
*/
public File getWsdlDirectory() {
return wsdlDirectory;
}
/**
* Returns the directory into which generated sources should be written.
* @return The output directory.
*/
public File getOutputDirectory() {
return outputDirectory;
}
/**
* When this option is activated, lenses are generated for each case class allowing the user to modify easily
* an XML file.
*/
@Parameter
private boolean generateLens;
/**
* Generate case class repeated parameters as varargs if true or Seq if false
*/
@Parameter(defaultValue = "true")
private boolean useVarArgs;
/**
* Returns the command line options to be used for scalaxb, excluding the
* input file names.
*/
protected List<String> arguments() {
List<String> args = new ArgumentsBuilder()
.flag("-v", verbose)
.flag("--package-dir", packageDir)
.param("-d", outputDirectory.getPath())
.param("-p", packageName)
.map("--package:", packageNameMap())
.param("--class-prefix", classPrefix)
.param("--param-prefix", parameterPrefix)
.param("--chunk-size", chunkSize)
.param("--dispatch-version", dispatchVersion)
.flag("--no-runtime", !generateRuntime)
.intersperse("--wrap-contents", wrapContents)
.param("--protocol-file", protocolFile)
.param("--protocol-package", protocolPackage)
.param("--attribute-prefix", attributePrefix)
.flag("--prepend-family", prependFamily)
.flag("--blocking", !async)
.flag("--lax-any", laxAny)
.flag("--generate-lens", generateLens)
.flag("--use-varargs", useVarArgs)
.getArguments();
return unmodifiableList(args);
}
/**
* Returns a map of URIs to package name, as specified by the packageNames
* parameter.
*/
Map<String, String> packageNameMap() {
if (packageNames == null) {
return emptyMap();
}
Map<String, String> names = new LinkedHashMap<String, String>();
for (PackageName name : packageNames) {
names.put(name.getUri(), name.getPackage());
}
return names;
}
}