Skip to content

Commit a34416f

Browse files
committed
Fixed #26.
1 parent 0cdaec5 commit a34416f

File tree

13 files changed

+699
-96
lines changed

13 files changed

+699
-96
lines changed

README.md

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
jaxb2-annotate-plugin
22
=====================
33

4-
JAXB2 Annotate Plugin is capable of adding arbitrary annotations to the generated sources.
4+
JAXB2 Annotate Plugin is capable of adding or removing arbitrary annotations to/from the generated sources.
55

66
Usage overview
77
--------------
@@ -131,11 +131,106 @@ jaxb:extensionBindingPrefixes="xjc annox"
131131
Note: yes, I know that `http://annox.dev.java.net` no longer exists. Changing this namespace would break old builds.
132132
This is just a namespace, there must not necessarily be content there. Treat it as a logical identifier, nothing else.
133133

134+
Removing annotations
135+
--------------
136+
137+
* Customize your schema using binding files or directly in schema
138+
* Add the plugin to the XJC classpath.
139+
* Activate the plugin using `-XremoveAnnotation`-switch.
140+
141+
You can remove annotations using customizations directly in schema:
142+
143+
````xml
144+
<xsd:schema
145+
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
146+
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
147+
jaxb:version="2.1"
148+
xmlns:annox="http://annox.dev.java.net"
149+
jaxb:extensionBindingPrefixes="annox">
150+
151+
152+
153+
<xsd:complexType name="FooType">
154+
<xsd:annotation>
155+
<xsd:appinfo>
156+
<annox:removeAnnotation class="javax.xml.bind.annotation.XmlType" />
157+
</xsd:appinfo>
158+
</xsd:annotation>
159+
<xsd:sequence>
160+
<xsd:element name="bar" type="xsd:string"/>
161+
<xsd:element name="foobar" type="xsd:string">
162+
<xsd:annotation>
163+
<xsd:appinfo>
164+
<annox:removeAnnotation class="javax.xml.bind.annotation.XmlElement" target="field" />
165+
</xsd:appinfo>
166+
</xsd:annotation>
167+
</xsd:element>
168+
</xsd:sequence>
169+
</xsd:complexType>
170+
171+
</xsd:schema>
172+
````
173+
174+
Or in binding files:
175+
176+
````xml
177+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
178+
<jaxb:bindings
179+
xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema"
180+
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
181+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
182+
xmlns:annox="http://annox.dev.java.net"
183+
xsi:schemaLocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd"
184+
jaxb:extensionBindingPrefixes="xjc annox"
185+
version="2.1">
186+
187+
<jaxb:bindings schemaLocation="schema.xsd" node="/xs:schema">
188+
<jaxb:bindings node="xs:complexType[@name='FooType']">
189+
<annox:removeAnnotation class="javax.xml.bind.annotation.XmlType" />
190+
</jaxb:bindings>
191+
<jaxb:bindings node="xs:complexType[@name='FooType']//xs:element[@name='foobar']">
192+
<annox:removeAnnotation class="javax.xml.bind.annotation.XmlElement" target="field" />
193+
</jaxb:bindings>
194+
</jaxb:bindings>
195+
196+
</jaxb:bindings>
197+
````
198+
199+
You can use the following customization elements in the `http://annox.dev.java.net` namespace:
200+
* `removeAnnotation` with the optional `target` attribute:
201+
* `package`
202+
* `class`
203+
* `getter`
204+
* `setter`
205+
* `setter-parameter`
206+
* `field`
207+
* `enum-value-method`
208+
* `enum-fromValue-method`
209+
* `removeAnnotationFromProperty`
210+
* `removeAnnotationFromPackage`
211+
* `removeAnnotationFromClass`
212+
* `removeAnnotationFromElement`
213+
* `removeAnnotationFromeEnum`
214+
* `removeAnnotationFromEnumConstant`
215+
* `removeAnnotationFromEnumValueMethod` - removes annotation from the `value()` method of the enum
216+
* `removeAnnotationFromEnumFromValueMethod` - removes annotation from the `fromValue(String)` method of the enum
217+
218+
The `http://annox.dev.java.net` namespace must be declared in the `jaxb:extensionBindingPrefixes` attribute via prefix, ex.:
219+
220+
````
221+
xmlns:annox="http://annox.dev.java.net"
222+
jaxb:extensionBindingPrefixes="xjc annox"
223+
````
224+
225+
Note: yes, I know that `http://annox.dev.java.net` no longer exists. Changing this namespace would break old builds.
226+
This is just a namespace, there must not necessarily be content there. Treat it as a logical identifier, nothing else.
227+
228+
134229
Using JAXB2 Annotate Plugin with Maven
135230
--------------------------------------
136231

137232
* Add `org.jvnet.jaxb2_commons:jaxb2-basics-annotate` as XJC plugin
138-
* Turn on the plugin using `-Xannotate` switch
233+
* Turn on the plugin using `-Xannotate` or `-XremoveAnnotation`switch
139234
* Add artifact with your annotations as another XJC plugin
140235

141236
Example:
@@ -148,6 +243,7 @@ Example:
148243
<extension>true</extension>
149244
<args>
150245
<arg>-Xannotate</arg>
246+
<arg>-XremoveAnnotation</arg>
151247
</args>
152248
<plugins>
153249
<plugin>

plugin/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@
7272
<artifactId>jaxb-xjc</artifactId>
7373
<scope>provided</scope>
7474
</dependency>
75-
<dependency>
75+
<!--dependency>
7676
<groupId>org.jvnet.jaxb2.maven2</groupId>
7777
<artifactId>maven-jaxb2-plugin-testing</artifactId>
7878
<scope>test</scope>
79-
</dependency>
79+
</dependency-->
8080
<!-- Annox -->
8181
<dependency>
8282
<groupId>org.jvnet.annox</groupId>

plugin/src/main/java/org/jvnet/jaxb2_commons/plugin/annotate/AnnotationTarget.java renamed to plugin/src/main/java/org/jvnet/jaxb2_commons/plugin/AnnotationTarget.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@
2626
* of the authors and should not be interpreted as representing official policies,
2727
* either expressed or implied, of the FreeBSD Project.
2828
*/
29-
package org.jvnet.jaxb2_commons.plugin.annotate;
29+
package org.jvnet.jaxb2_commons.plugin;
3030

3131
import java.text.MessageFormat;
32+
import java.util.Arrays;
33+
import java.util.Collections;
34+
import java.util.HashSet;
35+
import java.util.Set;
3236

3337
import javax.xml.namespace.QName;
3438

3539
import org.apache.commons.lang3.Validate;
40+
import org.jvnet.jaxb2_commons.plugin.annotate.AnnotatePlugin;
41+
import org.jvnet.jaxb2_commons.plugin.removeannotation.RemoveAnnotationPlugin;
3642
import org.jvnet.jaxb2_commons.util.FieldAccessorUtils;
3743
import org.jvnet.jaxb2_commons.util.OutlineUtils;
3844
import org.w3c.dom.Element;
@@ -258,15 +264,15 @@ public JAnnotatable getAnnotatable(Outline outline,
258264
};
259265

260266
private final String target;
261-
private final QName name;
267+
private final Set<QName> names;
262268

263-
AnnotationTarget(String target, QName name) {
269+
AnnotationTarget(String target, QName... names) {
264270
this.target = target;
265-
this.name = name;
271+
this.names = Collections.unmodifiableSet(new HashSet<QName>(Arrays.asList(names)));
266272
}
267273

268-
public QName getName() {
269-
return name;
274+
public Set<QName> getNames() {
275+
return names;
270276
}
271277

272278
public String getTarget() {
@@ -322,7 +328,9 @@ public static AnnotationTarget getAnnotationTarget(final Element element,
322328
element.getLocalName());
323329

324330
if (AnnotatePlugin.ANNOTATE_QNAME.equals(name)
325-
|| AnnotatePlugin.ANNOTATE_PROPERTY_QNAME.equals(name)) {
331+
|| AnnotatePlugin.ANNOTATE_PROPERTY_QNAME.equals(name)
332+
|| RemoveAnnotationPlugin.REMOVE_ANNOTATION_QNAME.equals(name)
333+
|| RemoveAnnotationPlugin.REMOVE_ANNOTATION_FROM_PROPERTY_QNAME.equals(name)) {
326334
final String target = element.getAttribute("target");
327335
if (target == null || "".equals(target)) {
328336
return defaultAnnotationTarget;
@@ -332,7 +340,7 @@ public static AnnotationTarget getAnnotationTarget(final Element element,
332340
} else {
333341
for (AnnotationTarget possibleAnnotationTarget : AnnotationTarget
334342
.values()) {
335-
if (possibleAnnotationTarget.getName().equals(name)) {
343+
if (possibleAnnotationTarget.names.contains(name)) {
336344
return possibleAnnotationTarget;
337345
}
338346
}

plugin/src/main/java/org/jvnet/jaxb2_commons/plugin/annotate/AnnotatePlugin.java

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030

3131
import java.util.Arrays;
3232
import java.util.Collection;
33+
import java.util.Collections;
34+
import java.util.HashSet;
35+
import java.util.Set;
3336

3437
import javax.xml.namespace.QName;
3538

@@ -38,6 +41,8 @@
3841
import org.jvnet.annox.model.XAnnotation;
3942
import org.jvnet.annox.parser.XAnnotationParser;
4043
import org.jvnet.jaxb2_commons.plugin.AbstractParameterizablePlugin;
44+
import org.jvnet.jaxb2_commons.plugin.AnnotationTarget;
45+
import org.jvnet.jaxb2_commons.plugin.removeannotation.RemoveAnnotationPlugin;
4146
import org.jvnet.jaxb2_commons.util.CustomizationUtils;
4247
import org.w3c.dom.Element;
4348
import org.w3c.dom.Node;
@@ -87,6 +92,23 @@ public class AnnotatePlugin extends AbstractParameterizablePlugin {
8792
Constants.NAMESPACE_URI, "annotateEnumFromValueMethod");
8893
public static final QName ANNOTATE_QNAME = new QName(
8994
Constants.NAMESPACE_URI, "annotate");
95+
96+
public static final Set<QName> CUSTOMIZATION_ELEMENT_QNAMES = Collections.unmodifiableSet(
97+
new HashSet<QName>(Arrays.asList(
98+
ANNOTATE_QNAME,
99+
ANNOTATE_PACKAGE_QNAME,
100+
ANNOTATE_CLASS_QNAME,
101+
ANNOTATE_ELEMENT_QNAME,
102+
ANNOTATE_PROPERTY_QNAME,
103+
ANNOTATE_PROPERTY_FIELD_QNAME,
104+
ANNOTATE_PROPERTY_GETTER_QNAME,
105+
ANNOTATE_PROPERTY_SETTER_QNAME,
106+
ANNOTATE_PROPERTY_SETTER_PARAMETER_QNAME,
107+
ANNOTATE_ENUM_QNAME,
108+
ANNOTATE_ENUM_CONSTANT_QNAME,
109+
ANNOTATE_ENUM_VALUE_METHOD_QNAME,
110+
ANNOTATE_ENUM_FROM_VALUE_METHOD_QNAME)));
111+
90112

91113
@Override
92114
public String getOptionName() {
@@ -202,7 +224,7 @@ protected void processFieldOutline(ClassOutline classOutline,
202224
ErrorHandler errorHandler) {
203225
final CCustomizations customizations = CustomizationUtils
204226
.getCustomizations(fieldOutline);
205-
annotate(fieldOutline.parent().ref.owner(), fieldOutline,
227+
annotateFieldOutline(fieldOutline.parent().ref.owner(), fieldOutline,
206228
customizations, errorHandler);
207229
}
208230

@@ -226,7 +248,7 @@ protected void annotateElementOutline(final JCodeModel codeModel,
226248
final Element element = customization.element;
227249
final QName name = new QName(element.getNamespaceURI(),
228250
element.getLocalName());
229-
if (Constants.NAMESPACE_URI.equals(name.getNamespaceURI())) {
251+
if (isCustomizationElementName(name)) {
230252
customization.markAsAcknowledged();
231253
final AnnotationTarget annotationTarget = AnnotationTarget
232254
.getAnnotationTarget(element, AnnotationTarget.ELEMENT);
@@ -251,7 +273,7 @@ protected void annotateEnumOutline(final JCodeModel codeModel,
251273
final Element element = customization.element;
252274
final QName name = new QName(element.getNamespaceURI(),
253275
element.getLocalName());
254-
if (Constants.NAMESPACE_URI.equals(name.getNamespaceURI())) {
276+
if (isCustomizationElementName(name)) {
255277
customization.markAsAcknowledged();
256278
final AnnotationTarget annotationTarget = AnnotationTarget
257279
.getAnnotationTarget(element, AnnotationTarget.ENUM);
@@ -276,7 +298,7 @@ protected void annotateEnumConstantOutline(final JCodeModel codeModel,
276298
final Element element = customization.element;
277299
final QName name = new QName(element.getNamespaceURI(),
278300
element.getLocalName());
279-
if (Constants.NAMESPACE_URI.equals(name.getNamespaceURI())) {
301+
if (isCustomizationElementName(name)) {
280302
customization.markAsAcknowledged();
281303
final AnnotationTarget annotationTarget = AnnotationTarget
282304
.getAnnotationTarget(element,
@@ -301,7 +323,7 @@ protected void annotateClassOutline(final JCodeModel codeModel,
301323
final Element element = customization.element;
302324
final QName name = new QName(element.getNamespaceURI(),
303325
element.getLocalName());
304-
if (Constants.NAMESPACE_URI.equals(name.getNamespaceURI())) {
326+
if (isCustomizationElementName(name)) {
305327
customization.markAsAcknowledged();
306328
final AnnotationTarget annotationTarget = AnnotationTarget
307329
.getAnnotationTarget(element, AnnotationTarget.CLASS);
@@ -317,14 +339,14 @@ protected void annotateClassOutline(final JCodeModel codeModel,
317339
}
318340
}
319341

320-
protected void annotate(final JCodeModel codeModel,
342+
protected void annotateFieldOutline(final JCodeModel codeModel,
321343
final FieldOutline fieldOutline,
322344
final CCustomizations customizations, ErrorHandler errorHandler) {
323345
for (final CPluginCustomization customization : customizations) {
324346
final Element element = customization.element;
325347
final QName name = new QName(element.getNamespaceURI(),
326348
element.getLocalName());
327-
if (Constants.NAMESPACE_URI.equals(name.getNamespaceURI())) {
349+
if (isCustomizationElementName(name)) {
328350
customization.markAsAcknowledged();
329351

330352
final AnnotationTarget annotationTarget = AnnotationTarget
@@ -390,23 +412,16 @@ private void annotate(final JCodeModel codeModel,
390412
}
391413
}
392414
}
415+
416+
private boolean isCustomizationElementName(final QName name) {
417+
return name != null &&
418+
Constants.NAMESPACE_URI.equals(name.getNamespaceURI()) &&
419+
!RemoveAnnotationPlugin.CUSTOMIZATION_ELEMENT_QNAMES.contains(name);
420+
}
393421

394422
@Override
395423
public Collection<QName> getCustomizationElementNames() {
396-
return Arrays.asList(
397-
ANNOTATE_QNAME,
398-
ANNOTATE_PACKAGE_QNAME,
399-
ANNOTATE_CLASS_QNAME,
400-
ANNOTATE_ELEMENT_QNAME,
401-
ANNOTATE_PROPERTY_QNAME,
402-
ANNOTATE_PROPERTY_FIELD_QNAME,
403-
ANNOTATE_PROPERTY_GETTER_QNAME,
404-
ANNOTATE_PROPERTY_SETTER_QNAME,
405-
ANNOTATE_PROPERTY_SETTER_PARAMETER_QNAME,
406-
ANNOTATE_ENUM_QNAME,
407-
ANNOTATE_ENUM_CONSTANT_QNAME,
408-
ANNOTATE_ENUM_VALUE_METHOD_QNAME,
409-
ANNOTATE_ENUM_FROM_VALUE_METHOD_QNAME);
424+
return CUSTOMIZATION_ELEMENT_QNAMES;
410425
}
411426

412427
}

0 commit comments

Comments
 (0)