Skip to content
Permalink
Browse files
Update xjc plugin to work with newer JAXB's, detect the version of ja…
…xb-api found and adjust accordingly.
  • Loading branch information
dkulp committed May 28, 2014
1 parent 864321f commit 34372d4b6ea72548796b0e5c69be5610104587a3
Showing 12 changed files with 74 additions and 7 deletions.
@@ -40,12 +40,14 @@
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.12</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.1.12</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>

<dependency>
@@ -41,6 +41,11 @@
<artifactId>jaxb-xjc</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
@@ -20,6 +20,7 @@
package org.apache.cxf.xjc.bug671;


import com.sun.codemodel.JJavaName;
import com.sun.tools.xjc.BadCommandLineException;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.Plugin;
@@ -59,7 +60,7 @@ public String toPackageName(String nsUri) {
if (idx2 == -1) {
idx2 = s.length();
}
if (!isJavaIdentifier(s.substring(idx, idx2))) {
if (!JJavaName.isJavaIdentifier(s.substring(idx, idx2))) {
s = s.substring(0, idx) + "_" + s.substring(idx);
}
idx = s.indexOf('.', idx);
@@ -84,14 +84,22 @@
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.2.10-b140310.1920</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.2.10-b140310.1920</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.10-b140310.1920</version>
</dependency>
<dependency>
<groupId>xml-resolver</groupId>
@@ -27,13 +27,16 @@
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.annotation.XmlElementRef;

import org.xml.sax.InputSource;

import com.sun.codemodel.CodeWriter;
import com.sun.codemodel.JCodeModel;
import com.sun.tools.xjc.Language;
import com.sun.tools.xjc.ModelLoader;
import com.sun.tools.xjc.Options;
import com.sun.tools.xjc.api.SpecVersion;
import com.sun.tools.xjc.model.Model;
import com.sun.tools.xjc.outline.Outline;

@@ -108,6 +111,9 @@ public void addCatalog(File catalogFile) throws IOException {
catResolver.getCatalog().parseCatalog(catalogFile.getPath());
}
};
if (checkXmlElementRef()) {
opt.target = SpecVersion.V2_1;
}
opt.setSchemaLanguage(Language.XMLSCHEMA);
opt.parseArguments(args);
Model model = ModelLoader.load(opt, new JCodeModel(), listener);
@@ -131,6 +137,16 @@ public void addCatalog(File catalogFile) throws IOException {
}
return 0;
}
private boolean checkXmlElementRef() {
try {
//check the version of JAXB-API that is actually being picked up
//so we can set target=2.1 if the 2.1 version of XmlElementRef is picked up
XmlElementRef.class.getMethod("required");
} catch (Throwable t) {
return true;
}
return false;
}
public static void main(String[] args) throws Exception {
BuildContext context = new XJCBuildContext();
XJCErrorListener listener = new XJCErrorListener(context);
@@ -42,6 +42,10 @@
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
@@ -41,6 +41,11 @@
<artifactId>jaxb-xjc</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
@@ -125,6 +125,15 @@ private boolean containsDefaultValue(Outline outline, FieldOutline field) {
return false;
}

private int getMinOccurs(XSParticle particle) {
try {
Number o = (Number)particle.getClass().getMethod("getMinOccurs").invoke(particle);
return o.intValue();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) {
if (!active) {
return true;
@@ -164,7 +173,7 @@ public boolean run(Outline outline, Options opt, ErrorHandler errorHandler) {
if (xsType != null
&& xsType.isComplexType()
&& ((containsDefaultValue(outline, f) && complexTypes)
|| (particle != null && particle.getMinOccurs() != 0))) {
|| (particle != null && getMinOccurs(particle) != 0))) {
String varName = f.getPropertyInfo().getName(false);
JFieldVar var = co.implClass.fields().get(varName);
if (var != null) {
@@ -40,6 +40,10 @@
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
11 pom.xml
@@ -86,17 +86,22 @@
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.1</version>
<version>2.2.9</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.1.13</version>
<version>2.2.10-b140310.1920</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.1.13</version>
<version>2.2.10-b140310.1920</version>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.2.10-b140310.1920</version>
</dependency>
<dependency>
<groupId>junit</groupId>
@@ -44,6 +44,10 @@
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
@@ -44,6 +44,10 @@
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>

0 comments on commit 34372d4

Please sign in to comment.