diff --git a/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/elementwrapper/ElementWrapperPlugin.java b/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/elementwrapper/ElementWrapperPlugin.java index ae78d2d9..0cd139d3 100644 --- a/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/elementwrapper/ElementWrapperPlugin.java +++ b/basic/src/main/java/org/jvnet/jaxb2_commons/plugin/elementwrapper/ElementWrapperPlugin.java @@ -187,7 +187,8 @@ protected void processWrappedElementPropertyInfo( wrappedPropertyInfo.getElementName(), wrapperPropertyInfo.getWrapperElementName(), wrappedPropertyInfo.isNillable(), - wrappedPropertyInfo.getDefaultValue()); + wrappedPropertyInfo.getDefaultValue(), + wrappedPropertyInfo.getDefaultValueNamespaceContext()); rootClassInfo.addProperty(propertyInfo); @@ -248,7 +249,8 @@ protected void processWrappedElementRefPropertyInfo( wrappedPropertyInfo.isMixed(), wrappedPropertyInfo.isDomAllowed(), wrappedPropertyInfo.isTypedObjectAllowed(), - wrappedPropertyInfo.getDefaultValue()); + wrappedPropertyInfo.getDefaultValue(), + wrappedPropertyInfo.getDefaultValueNamespaceContext()); rootClassInfo.addProperty(propertyInfo); diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/MDefaultValue.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/MDefaultValue.java index cbd27e9c..624a643a 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/MDefaultValue.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/MDefaultValue.java @@ -1,6 +1,10 @@ package org.jvnet.jaxb2_commons.xml.bind.model; +import javax.xml.namespace.NamespaceContext; + public interface MDefaultValue { + public NamespaceContext getDefaultValueNamespaceContext(); + public String getDefaultValue(); } diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/MSingleTypePropertyInfo.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/MSingleTypePropertyInfo.java index 8443cfe6..0030e013 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/MSingleTypePropertyInfo.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/MSingleTypePropertyInfo.java @@ -1,6 +1,6 @@ package org.jvnet.jaxb2_commons.xml.bind.model; public interface MSingleTypePropertyInfo extends MPropertyInfo, - MTyped { + MTyped, MDefaultValue { } diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMAttributePropertyInfo.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMAttributePropertyInfo.java index 116ef432..d88ad606 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMAttributePropertyInfo.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMAttributePropertyInfo.java @@ -1,5 +1,6 @@ package org.jvnet.jaxb2_commons.xml.bind.model.concrete; +import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import org.jvnet.jaxb2_commons.lang.Validate; @@ -13,11 +14,13 @@ public class CMAttributePropertyInfo extends CMSingleTypePropertyInfo implements MAttributePropertyInfo { private final QName attributeName; - + public CMAttributePropertyInfo(MPropertyInfoOrigin origin, MClassInfo classInfo, String privateName, - MTypeInfo typeInfo, QName attributeName, boolean required) { - super(origin, classInfo, privateName, false, typeInfo, required); + MTypeInfo typeInfo, QName attributeName, boolean required, + String defaultValue, NamespaceContext defaultValueNamespaceContext) { + super(origin, classInfo, privateName, false, typeInfo, required, + defaultValue, defaultValueNamespaceContext); Validate.notNull(attributeName); this.attributeName = attributeName; } diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMClassInfo.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMClassInfo.java index c4bbb499..91608b17 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMClassInfo.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMClassInfo.java @@ -89,7 +89,7 @@ public MElementInfo createElementInfo(MTypeInfo scope, QName substitutionHead) { return new CMElementInfo(getOrigin().createElementInfoOrigin(), getPackageInfo(), getContainer(), getLocalName(), - getElementName(), scope, this, substitutionHead, null); + getElementName(), scope, this, substitutionHead, null, null); } public MPackageInfo getPackageInfo() { @@ -136,7 +136,7 @@ public MClassTypeInfo getBaseTypeInfo() { public List> getProperties() { return unmodifiableProperties; } - + @Override public MPropertyInfo getProperty(String privateName) { return this.propertiesMap.get(privateName); diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementInfo.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementInfo.java index 3825c3e7..02938706 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementInfo.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementInfo.java @@ -2,6 +2,7 @@ import java.text.MessageFormat; +import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import org.jvnet.jaxb2_commons.lang.Validate; @@ -31,10 +32,13 @@ public class CMElementInfo implements MElementInfo { private final String defaultValue; + private final NamespaceContext defaultValueNamespaceContext; + public CMElementInfo(MElementInfoOrigin origin, MPackageInfo _package, MContainer container, String localName, QName elementName, MTypeInfo scope, MTypeInfo typeInfo, - QName substitutionHead, String defaultValue) { + QName substitutionHead, String defaultValue, + NamespaceContext defaultValueNamespaceContext) { super(); Validate.notNull(origin); Validate.notNull(elementName); @@ -48,6 +52,7 @@ public CMElementInfo(MElementInfoOrigin origin, MPackageInfo _package, this.typeInfo = typeInfo; this.substitutionHead = substitutionHead; this.defaultValue = defaultValue; + this.defaultValueNamespaceContext = defaultValueNamespaceContext; } public MElementInfoOrigin getOrigin() { @@ -109,6 +114,11 @@ public String getDefaultValue() { return defaultValue; } + @Override + public NamespaceContext getDefaultValueNamespaceContext() { + return defaultValueNamespaceContext; + } + public String toString() { return MessageFormat.format("ElementInfo [{0}: {1}]", getElementName(), getTypeInfo()); diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementPropertyInfo.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementPropertyInfo.java index 47cce6ed..0154d15b 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementPropertyInfo.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementPropertyInfo.java @@ -1,5 +1,6 @@ package org.jvnet.jaxb2_commons.xml.bind.model.concrete; +import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo; @@ -16,17 +17,20 @@ public class CMElementPropertyInfo extends CMPropertyInfo private final QName wrapperElementName; private final boolean nillable; private final String defaultValue; + private final NamespaceContext defaultValueNamespaceContext; public CMElementPropertyInfo(MPropertyInfoOrigin origin, MClassInfo classInfo, String privateName, boolean collection, boolean required, MTypeInfo typeInfo, QName elementName, - QName wrapperElementName, boolean nillable, String defaultValue) { + QName wrapperElementName, boolean nillable, String defaultValue, + NamespaceContext defaultValueNamespaceContext) { super(origin, classInfo, privateName, collection, required); this.typeInfo = typeInfo; this.elementName = elementName; this.wrapperElementName = wrapperElementName; this.nillable = nillable; this.defaultValue = defaultValue; + this.defaultValueNamespaceContext = defaultValueNamespaceContext; } public MTypeInfo getTypeInfo() { @@ -51,6 +55,11 @@ public String getDefaultValue() { return defaultValue; } + @Override + public NamespaceContext getDefaultValueNamespaceContext() { + return defaultValueNamespaceContext; + } + public V acceptPropertyInfoVisitor(MPropertyInfoVisitor visitor) { return visitor.visitElementPropertyInfo(this); } diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementRefPropertyInfo.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementRefPropertyInfo.java index 4b125d3f..c70897a3 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementRefPropertyInfo.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementRefPropertyInfo.java @@ -1,5 +1,6 @@ package org.jvnet.jaxb2_commons.xml.bind.model.concrete; +import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo; @@ -19,12 +20,14 @@ public class CMElementRefPropertyInfo extends private final boolean domAllowed; private final boolean typedObjectAllowed; private final String defaultValue; + private final NamespaceContext defaultValueNamespaceContext; public CMElementRefPropertyInfo(MPropertyInfoOrigin origin, MClassInfo classInfo, String privateName, boolean collection, boolean required, MTypeInfo typeInfo, QName elementName, QName wrapperElementName, boolean mixed, boolean domAllowed, - boolean typedObjectAllowed, String defaultValue) { + boolean typedObjectAllowed, String defaultValue, + NamespaceContext defaultValueNamespaceContext) { super(origin, classInfo, privateName, collection, required); this.typeInfo = typeInfo; this.elementName = elementName; @@ -33,6 +36,7 @@ public CMElementRefPropertyInfo(MPropertyInfoOrigin origin, this.domAllowed = domAllowed; this.typedObjectAllowed = typedObjectAllowed; this.defaultValue = defaultValue; + this.defaultValueNamespaceContext = defaultValueNamespaceContext; } public MTypeInfo getTypeInfo() { @@ -69,6 +73,11 @@ public String getDefaultValue() { return defaultValue; } + @Override + public NamespaceContext getDefaultValueNamespaceContext() { + return defaultValueNamespaceContext; + } + public V acceptPropertyInfoVisitor(MPropertyInfoVisitor visitor) { return visitor.visitElementRefPropertyInfo(this); } diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementTypeInfo.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementTypeInfo.java index f7f78519..a76e0994 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementTypeInfo.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementTypeInfo.java @@ -1,5 +1,6 @@ package org.jvnet.jaxb2_commons.xml.bind.model.concrete; +import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import org.jvnet.jaxb2_commons.lang.Validate; @@ -17,14 +18,18 @@ public class CMElementTypeInfo implements private final String defaultValue; + private final NamespaceContext defaultValueNamespaceContext; + public CMElementTypeInfo(QName elementName, MTypeInfo typeInfo, - boolean nillable, String defaultValue) { + boolean nillable, String defaultValue, + NamespaceContext defaultValueNamespaceContext) { Validate.notNull(elementName); Validate.notNull(typeInfo); this.elementName = elementName; this.typeInfo = typeInfo; this.nillable = nillable; this.defaultValue = defaultValue; + this.defaultValueNamespaceContext = defaultValueNamespaceContext; } public QName getElementName() { @@ -43,6 +48,11 @@ public boolean isNillable() { public String getDefaultValue() { return defaultValue; } + + @Override + public NamespaceContext getDefaultValueNamespaceContext() { + return defaultValueNamespaceContext; + } @Override public String toString() { diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMEnumLeafInfo.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMEnumLeafInfo.java index 9bdb2982..d14306c7 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMEnumLeafInfo.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMEnumLeafInfo.java @@ -93,7 +93,7 @@ public MElementInfo createElementInfo(MTypeInfo scope, QName substitutionHead) { return new CMElementInfo(getOrigin().createElementInfoOrigin(), getPackageInfo(), getContainer(), getLocalName(), - getElementName(), scope, this, substitutionHead, null); + getElementName(), scope, this, substitutionHead, null, null); } public String getName() { diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMInfoFactory.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMInfoFactory.java index 624a31d3..0d7f52db 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMInfoFactory.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMInfoFactory.java @@ -8,6 +8,7 @@ import java.util.Set; import javax.activation.MimeType; +import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import org.jvnet.jaxb2_commons.lang.Validate; @@ -349,17 +350,20 @@ private MPropertyInfo createPropertyInfo( protected MPropertyInfo createAttributePropertyInfo( final MClassInfo classInfo, final API propertyInfo) { + return new CMAttributePropertyInfo( createPropertyInfoOrigin((PI) propertyInfo), classInfo, propertyInfo.getName(), getTypeInfo(propertyInfo), - propertyInfo.getXmlName(), propertyInfo.isRequired()); + propertyInfo.getXmlName(), propertyInfo.isRequired(), + getDefaultValue(propertyInfo), + getDefaultValueNamespaceContext(propertyInfo)); } protected MPropertyInfo createValuePropertyInfo( final MClassInfo classInfo, final VPI propertyInfo) { return new CMValuePropertyInfo( createPropertyInfoOrigin((PI) propertyInfo), classInfo, - propertyInfo.getName(), getTypeInfo(propertyInfo)); + propertyInfo.getName(), getTypeInfo(propertyInfo), null, null); } protected MPropertyInfo createElementPropertyInfo( @@ -370,7 +374,8 @@ protected MPropertyInfo createElementPropertyInfo( ep.isCollection() && !ep.isValueList(), ep.isRequired(), getTypeInfo(ep, typeRef), typeRef.getTagName(), ep.getXmlName(), typeRef.isNillable(), - typeRef.getDefaultValue()); + getDefaultValue(typeRef), + getDefaultValueNamespaceContext(typeRef)); } protected MPropertyInfo createElementsPropertyInfo( @@ -380,8 +385,9 @@ protected MPropertyInfo createElementsPropertyInfo( types.size()); for (TypeRef typeRef : types) { typedElements.add(new CMElementTypeInfo(typeRef.getTagName(), - getTypeInfo(ep, typeRef), typeRef.isNillable(), typeRef - .getDefaultValue())); + getTypeInfo(ep, typeRef), typeRef.isNillable(), + getDefaultValue(typeRef), + getDefaultValueNamespaceContext(typeRef))); } return new CMElementsPropertyInfo( createPropertyInfoOrigin((PI) ep), classInfo, ep.getName(), @@ -409,7 +415,8 @@ protected MPropertyInfo createElementRefPropertyInfo( : rp.getWildcard().allowDom, rp.getWildcard() == null ? true : rp.getWildcard().allowTypedObject, - getDefaultValue(element)); + getDefaultValue(element), + getDefaultValueNamespaceContext(element)); } protected MPropertyInfo createElementRefsPropertyInfo( @@ -418,7 +425,8 @@ protected MPropertyInfo createElementRefsPropertyInfo( for (Element element : rp.getElements()) { typedElements.add(new CMElementTypeInfo(element .getElementName(), getTypeInfo(rp, element), true, - getDefaultValue(element))); + getDefaultValue(element), + getDefaultValueNamespaceContext(element))); } return new CMElementRefsPropertyInfo( createPropertyInfoOrigin((PI) rp), classInfo, rp.getName(), @@ -470,7 +478,24 @@ private String getDefaultValue(Element element) { final List> types = property.getTypes(); if (types.size() == 1) { final TypeRef typeRef = types.get(0); - return typeRef.getDefaultValue(); + return getDefaultValue(typeRef); + } + } + } + return null; + } + + private NamespaceContext getDefaultValueNamespaceContext( + Element element) { + if (element instanceof ElementInfo) { + final ElementInfo elementInfo = (ElementInfo) element; + final ElementPropertyInfo property = elementInfo + .getProperty(); + if (property != null) { + final List> types = property.getTypes(); + if (types.size() == 1) { + final TypeRef typeRef = types.get(0); + return getDefaultValueNamespaceContext(typeRef); } } } @@ -531,7 +556,8 @@ protected MElementInfo createElementInfo(EI element) { createElementInfoOrigin(element), getPackage(element), getContainer(element), getLocalName(element), element.getElementName(), scope, getTypeInfo(element), - substitutionHead, getDefaultValue(element)); + substitutionHead, getDefaultValue(element), + getDefaultValueNamespaceContext(element)); return elementInfo; } @@ -587,4 +613,13 @@ protected MWildcardTypeInfoOrigin createWildcardTypeInfoOrigin(WTI info) { */ protected abstract Class loadClass(T referencedType); + protected abstract String getDefaultValue(API propertyInfo); + + protected abstract NamespaceContext getDefaultValueNamespaceContext( + API propertyInfo); + + protected abstract String getDefaultValue(TypeRef typeRef); + + protected abstract NamespaceContext getDefaultValueNamespaceContext( + TypeRef typeRef); } diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMSingleTypePropertyInfo.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMSingleTypePropertyInfo.java index 457bd9ce..7ed63bc7 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMSingleTypePropertyInfo.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMSingleTypePropertyInfo.java @@ -1,5 +1,7 @@ package org.jvnet.jaxb2_commons.xml.bind.model.concrete; +import javax.xml.namespace.NamespaceContext; + import org.jvnet.jaxb2_commons.lang.Validate; import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo; import org.jvnet.jaxb2_commons.xml.bind.model.MSingleTypePropertyInfo; @@ -10,13 +12,28 @@ public abstract class CMSingleTypePropertyInfo extends CMPropertyInfo implements MSingleTypePropertyInfo { private final MTypeInfo typeInfo; + private final String defaultValue; + private final NamespaceContext defaultValueNamespaceContext; public CMSingleTypePropertyInfo(MPropertyInfoOrigin origin, MClassInfo classInfo, String privateName, boolean collection, - MTypeInfo typeInfo, boolean required) { + MTypeInfo typeInfo, boolean required, String defaultValue, + NamespaceContext defaultValueNamespaceContext) { super(origin, classInfo, privateName, collection, required); Validate.notNull(typeInfo); this.typeInfo = typeInfo; + this.defaultValue = defaultValue; + this.defaultValueNamespaceContext = defaultValueNamespaceContext; + } + + @Override + public String getDefaultValue() { + return this.defaultValue; + } + + @Override + public NamespaceContext getDefaultValueNamespaceContext() { + return this.defaultValueNamespaceContext; } public MTypeInfo getTypeInfo() { diff --git a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMValuePropertyInfo.java b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMValuePropertyInfo.java index fd636b13..838d7143 100644 --- a/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMValuePropertyInfo.java +++ b/runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMValuePropertyInfo.java @@ -1,18 +1,22 @@ package org.jvnet.jaxb2_commons.xml.bind.model.concrete; +import javax.xml.namespace.NamespaceContext; + import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo; import org.jvnet.jaxb2_commons.xml.bind.model.MPropertyInfoVisitor; import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo; import org.jvnet.jaxb2_commons.xml.bind.model.MValuePropertyInfo; import org.jvnet.jaxb2_commons.xml.bind.model.origin.MPropertyInfoOrigin; -public class CMValuePropertyInfo extends CMSingleTypePropertyInfo - implements MValuePropertyInfo { +public class CMValuePropertyInfo extends + CMSingleTypePropertyInfo implements MValuePropertyInfo { public CMValuePropertyInfo(MPropertyInfoOrigin origin, MClassInfo classInfo, String privateName, - MTypeInfo typeInfo) { - super(origin, classInfo, privateName, false, typeInfo, /* required */ true); + MTypeInfo typeInfo, String defaultValue, + NamespaceContext defaultValueNamespaceContext) { + super(origin, classInfo, privateName, false, typeInfo, /* required */ + true, defaultValue, defaultValueNamespaceContext); } public V acceptPropertyInfoVisitor(MPropertyInfoVisitor visitor) { diff --git a/tests/issues/src/main/resources/schema.xsd b/tests/issues/src/main/resources/schema.xsd index 4c80e2a9..c89bf58c 100644 --- a/tests/issues/src/main/resources/schema.xsd +++ b/tests/issues/src/main/resources/schema.xsd @@ -284,4 +284,19 @@ + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/tests/issues/src/test/java/org/jvnet/jaxb2_commons/tests/issues/GH26Test.java b/tests/issues/src/test/java/org/jvnet/jaxb2_commons/tests/issues/GH26Test.java new file mode 100644 index 00000000..a1c88cdc --- /dev/null +++ b/tests/issues/src/test/java/org/jvnet/jaxb2_commons/tests/issues/GH26Test.java @@ -0,0 +1,90 @@ +package org.jvnet.jaxb2_commons.tests.issues; + +import java.io.File; +import java.net.URL; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.jvnet.jaxb2_commons.xjc.model.concrete.XJCCMInfoFactory; +import org.jvnet.jaxb2_commons.xml.bind.model.MAttributePropertyInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MClassInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MElementPropertyInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MElementRefPropertyInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MElementRefsPropertyInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MElementsPropertyInfo; +import org.jvnet.jaxb2_commons.xml.bind.model.MModelInfo; + +import com.sun.codemodel.JCodeModel; +import com.sun.tools.xjc.ConsoleErrorReporter; +import com.sun.tools.xjc.ModelLoader; +import com.sun.tools.xjc.Options; +import com.sun.tools.xjc.model.Model; +import com.sun.tools.xjc.model.nav.NClass; +import com.sun.tools.xjc.model.nav.NType; + +public class GH26Test { + + @Before + public void setUp() { + System.setProperty("javax.xml.accessExternalSchema", "all"); + } + + @Test + public void compilesSchema() throws Exception { + + new File("target/generated-sources/xjc").mkdirs(); + + URL schema = getClass().getResource("/schema.xsd"); + URL binding = getClass().getResource("/binding.xjb"); + final String[] arguments = new String[] { "-xmlschema", + schema.toExternalForm(), "-b", binding.toExternalForm(), "-d", + "target/generated-sources/xjc", "-extension", "-XhashCode", + "-Xequals", "-XtoString", "-Xcopyable", "-Xmergeable", + "-Xinheritance", "-Xsetters", "-Xsetters-mode=direct", + "-Xwildcard", "-XenumValue" + // "-XsimpleToString" + + }; + + Options options = new Options(); + options.parseArguments(arguments); + ConsoleErrorReporter receiver = new ConsoleErrorReporter(); + Model model = ModelLoader.load(options, new JCodeModel(), receiver); + final XJCCMInfoFactory factory = new XJCCMInfoFactory(model); + final MModelInfo mmodel = factory.createModel(); + + final MClassInfo classInfo = mmodel + .getClassInfo("org.jvnet.jaxb2_commons.tests.issues.IssueGH26Type"); + Assert.assertNotNull(classInfo); + + final MElementPropertyInfo a = (MElementPropertyInfo) classInfo + .getProperty("a"); + Assert.assertEquals("a", a.getDefaultValue()); + Assert.assertNotNull(a.getDefaultValueNamespaceContext()); + final MElementsPropertyInfo bOrC = (MElementsPropertyInfo) classInfo + .getProperty("bOrC"); + +// Assert.assertEquals("b", bOrC.getElementTypeInfos().get(0).getDefaultValue()); + Assert.assertNotNull(bOrC.getElementTypeInfos().get(0).getDefaultValueNamespaceContext()); + +// Assert.assertEquals("3", bOrC.getElementTypeInfos().get(1).getDefaultValue()); + Assert.assertNotNull(bOrC.getElementTypeInfos().get(1).getDefaultValueNamespaceContext()); + + final MElementRefsPropertyInfo dOrE = (MElementRefsPropertyInfo) classInfo + .getProperty("dOrE"); +// Assert.assertEquals("e", dOrE.getElementTypeInfos().get(0).getDefaultValue()); + Assert.assertNotNull(dOrE.getElementTypeInfos().get(0).getDefaultValueNamespaceContext()); + +// Assert.assertEquals("d", dOrE.getElementTypeInfos().get(1).getDefaultValue()); + Assert.assertNotNull(dOrE.getElementTypeInfos().get(1).getDefaultValueNamespaceContext()); + + final MAttributePropertyInfo z = (MAttributePropertyInfo) classInfo + .getProperty("z"); + Assert.assertEquals("z", z.getDefaultValue()); + Assert.assertNotNull(z.getDefaultValueNamespaceContext()); + // model.generateCode(options, receiver); + // com.sun.codemodel.CodeWriter cw = options.createCodeWriter(); + // model.codeModel.build(cw); + } +} diff --git a/tools/src/main/java/org/jvnet/jaxb2_commons/xjc/model/concrete/XJCCMInfoFactory.java b/tools/src/main/java/org/jvnet/jaxb2_commons/xjc/model/concrete/XJCCMInfoFactory.java index 339462f6..db25fd03 100644 --- a/tools/src/main/java/org/jvnet/jaxb2_commons/xjc/model/concrete/XJCCMInfoFactory.java +++ b/tools/src/main/java/org/jvnet/jaxb2_commons/xjc/model/concrete/XJCCMInfoFactory.java @@ -5,6 +5,8 @@ import java.util.List; import java.util.Map; +import javax.xml.namespace.NamespaceContext; + import org.jvnet.jaxb2_commons.xjc.model.concrete.origin.XJCCMClassInfoOrigin; import org.jvnet.jaxb2_commons.xjc.model.concrete.origin.XJCCMElementInfoOrigin; import org.jvnet.jaxb2_commons.xjc.model.concrete.origin.XJCCMEnumConstantInfoOrigin; @@ -46,12 +48,18 @@ import com.sun.tools.xjc.model.CPropertyInfo; import com.sun.tools.xjc.model.CReferencePropertyInfo; import com.sun.tools.xjc.model.CTypeInfo; +import com.sun.tools.xjc.model.CTypeRef; import com.sun.tools.xjc.model.CValuePropertyInfo; import com.sun.tools.xjc.model.CWildcardTypeInfo; import com.sun.tools.xjc.model.Model; import com.sun.tools.xjc.model.nav.NClass; import com.sun.tools.xjc.model.nav.NType; import com.sun.tools.xjc.outline.Outline; +import com.sun.tools.xjc.util.NamespaceContextAdapter; +import com.sun.xml.bind.v2.model.core.TypeRef; +import com.sun.xml.xsom.XSAttributeUse; +import com.sun.xml.xsom.XSComponent; +import com.sun.xml.xsom.XmlString; public class XJCCMInfoFactory extends @@ -402,4 +410,54 @@ protected Class loadClass(NType referencedType) { return null; } } + + private XSAttributeUse getAttributeUse(CAttributePropertyInfo propertyInfo) { + final XSComponent schemaComponent = propertyInfo.getSchemaComponent(); + if (schemaComponent instanceof XSAttributeUse) { + return (XSAttributeUse) schemaComponent; + } else { + return null; + } + } + + protected String getDefaultValue(CAttributePropertyInfo propertyInfo) { + + final XSAttributeUse attributeUse = getAttributeUse(propertyInfo); + if (attributeUse != null) { + final XmlString defaultValue = attributeUse.getDefaultValue(); + if (defaultValue != null) { + return defaultValue.value; + } + } + return null; + } + + protected NamespaceContext getDefaultValueNamespaceContext( + CAttributePropertyInfo propertyInfo) { + final XSAttributeUse attributeUse = getAttributeUse(propertyInfo); + if (attributeUse != null) { + final XmlString defaultValue = attributeUse.getDefaultValue(); + if (defaultValue != null) { + return new NamespaceContextAdapter(defaultValue); + } + } + return null; + + } + + @Override + protected String getDefaultValue(TypeRef typeRef) { + return typeRef == null ? null : typeRef.getDefaultValue(); + } + + @Override + protected NamespaceContext getDefaultValueNamespaceContext( + TypeRef typeRef) { + if (typeRef instanceof CTypeRef) { + final CTypeRef cTypeRef = (CTypeRef) typeRef; + return new NamespaceContextAdapter(cTypeRef.defaultValue); + } else { + return null; + } + } }