Skip to content

Commit dc45137

Browse files
committed
Added defaultValue support.
1 parent 3941d3a commit dc45137

File tree

11 files changed

+123
-23
lines changed

11 files changed

+123
-23
lines changed

basic/src/main/java/org/jvnet/jaxb2_commons/plugin/elementwrapper/ElementWrapperPlugin.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ protected void processWrappedElementPropertyInfo(
185185
wrappedPropertyInfo.getTypeInfo(),
186186
wrappedPropertyInfo.getElementName(),
187187
wrapperPropertyInfo.getWrapperElementName(),
188-
wrappedPropertyInfo.isNillable());
188+
wrappedPropertyInfo.isNillable(),
189+
wrappedPropertyInfo.getDefaultValue());
189190

190191
rootClassInfo.addProperty(propertyInfo);
191192

@@ -244,7 +245,8 @@ protected void processWrappedElementRefPropertyInfo(
244245
wrapperPropertyInfo.getElementName(),
245246
wrappedPropertyInfo.isMixed(),
246247
wrappedPropertyInfo.isDomAllowed(),
247-
wrappedPropertyInfo.isTypedObjectAllowed());
248+
wrappedPropertyInfo.isTypedObjectAllowed(),
249+
wrappedPropertyInfo.getDefaultValue());
248250

249251
rootClassInfo.addProperty(propertyInfo);
250252

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package org.jvnet.jaxb2_commons.xml.bind.model;
2+
3+
public interface MDefaultValue {
4+
5+
public String getDefaultValue();
6+
}

runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/MElementTypeInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import javax.xml.namespace.QName;
44

55
public interface MElementTypeInfo<T, C extends T> extends MTyped<T, C>,
6-
MNillable {
6+
MNillable, MDefaultValue {
77

88
public QName getElementName();
99

runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMClassInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public MElementInfo<T, C> createElementInfo(MTypeInfo<T, C> scope,
8686
QName substitutionHead) {
8787
return new CMElementInfo<T, C>(getOrigin().createElementInfoOrigin(),
8888
getPackageInfo(), getContainer(), getLocalName(),
89-
getElementName(), scope, this, substitutionHead);
89+
getElementName(), scope, this, substitutionHead, null);
9090
}
9191

9292
public MPackageInfo getPackageInfo() {

runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementInfo.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ public class CMElementInfo<T, C extends T> implements MElementInfo<T, C> {
2929

3030
private final QName substitutionHead;
3131

32+
private final String defaultValue;
33+
3234
public CMElementInfo(MElementInfoOrigin origin, MPackageInfo _package,
3335
MContainer container, String localName, QName elementName,
3436
MTypeInfo<T, C> scope, MTypeInfo<T, C> typeInfo,
35-
QName substitutionHead) {
37+
QName substitutionHead, String defaultValue) {
3638
super();
3739
Validate.notNull(origin);
3840
Validate.notNull(elementName);
@@ -45,6 +47,7 @@ public CMElementInfo(MElementInfoOrigin origin, MPackageInfo _package,
4547
this.scope = scope;
4648
this.typeInfo = typeInfo;
4749
this.substitutionHead = substitutionHead;
50+
this.defaultValue = defaultValue;
4851
}
4952

5053
public MElementInfoOrigin getOrigin() {
@@ -95,12 +98,17 @@ public MTypeInfo<T, C> getTypeInfo() {
9598
public QName getSubstitutionHead() {
9699
return substitutionHead;
97100
}
98-
101+
99102
@Override
100103
public boolean isNillable() {
101104
return true;
102105
}
103106

107+
@Override
108+
public String getDefaultValue() {
109+
return defaultValue;
110+
}
111+
104112
public String toString() {
105113
return MessageFormat.format("ElementInfo [{0}: {1}]", getElementName(),
106114
getTypeInfo());

runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementPropertyInfo.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,25 @@
88
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo;
99
import org.jvnet.jaxb2_commons.xml.bind.model.origin.MPropertyInfoOrigin;
1010

11-
public class CMElementPropertyInfo<T, C extends T> extends CMPropertyInfo<T, C> implements
12-
MElementPropertyInfo<T, C> {
11+
public class CMElementPropertyInfo<T, C extends T> extends CMPropertyInfo<T, C>
12+
implements MElementPropertyInfo<T, C> {
1313

1414
private final MTypeInfo<T, C> typeInfo;
1515
private final QName elementName;
1616
private final QName wrapperElementName;
1717
private final boolean nillable;
18+
private final String defaultValue;
1819

1920
public CMElementPropertyInfo(MPropertyInfoOrigin origin,
2021
MClassInfo<T, C> classInfo, String privateName, boolean collection,
2122
MTypeInfo<T, C> typeInfo, QName elementName,
22-
QName wrapperElementName, boolean nillable) {
23+
QName wrapperElementName, boolean nillable, String defaultValue) {
2324
super(origin, classInfo, privateName, collection);
2425
this.typeInfo = typeInfo;
2526
this.elementName = elementName;
2627
this.wrapperElementName = wrapperElementName;
2728
this.nillable = nillable;
29+
this.defaultValue = defaultValue;
2830
}
2931

3032
public MTypeInfo<T, C> getTypeInfo() {
@@ -38,12 +40,17 @@ public QName getElementName() {
3840
public QName getWrapperElementName() {
3941
return wrapperElementName;
4042
}
41-
43+
4244
@Override
4345
public boolean isNillable() {
4446
return nillable;
4547
}
4648

49+
@Override
50+
public String getDefaultValue() {
51+
return defaultValue;
52+
}
53+
4754
public <V> V acceptPropertyInfoVisitor(MPropertyInfoVisitor<T, C, V> visitor) {
4855
return visitor.visitElementPropertyInfo(this);
4956
}

runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementRefPropertyInfo.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,21 @@ public class CMElementRefPropertyInfo<T, C extends T> extends CMPropertyInfo<T,
1818
private final boolean mixed;
1919
private final boolean domAllowed;
2020
private final boolean typedObjectAllowed;
21+
private final String defaultValue;
2122

2223
public CMElementRefPropertyInfo(MPropertyInfoOrigin origin,
2324
MClassInfo<T, C> classInfo, String privateName, boolean collection,
2425
MTypeInfo<T, C> typeInfo, QName elementName,
2526
QName wrapperElementName, boolean mixed, boolean domAllowed,
26-
boolean typedObjectAllowed) {
27+
boolean typedObjectAllowed, String defaultValue) {
2728
super(origin, classInfo, privateName, collection);
2829
this.typeInfo = typeInfo;
2930
this.elementName = elementName;
3031
this.wrapperElementName = wrapperElementName;
3132
this.mixed = mixed;
3233
this.domAllowed = domAllowed;
3334
this.typedObjectAllowed = typedObjectAllowed;
35+
this.defaultValue = defaultValue;
3436
}
3537

3638
public MTypeInfo<T, C> getTypeInfo() {
@@ -62,6 +64,11 @@ public boolean isNillable() {
6264
return true;
6365
}
6466

67+
@Override
68+
public String getDefaultValue() {
69+
return defaultValue;
70+
}
71+
6572
public <V> V acceptPropertyInfoVisitor(MPropertyInfoVisitor<T, C, V> visitor) {
6673
return visitor.visitElementRefPropertyInfo(this);
6774
}

runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMElementTypeInfo.java

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,25 @@
66
import org.jvnet.jaxb2_commons.xml.bind.model.MElementTypeInfo;
77
import org.jvnet.jaxb2_commons.xml.bind.model.MTypeInfo;
88

9-
public class CMElementTypeInfo<T, C extends T> implements MElementTypeInfo<T, C> {
9+
public class CMElementTypeInfo<T, C extends T> implements
10+
MElementTypeInfo<T, C> {
1011

1112
private final QName elementName;
1213

1314
private final MTypeInfo<T, C> typeInfo;
1415

1516
private final boolean nillable;
1617

17-
public CMElementTypeInfo(QName elementName, MTypeInfo<T, C> typeInfo, boolean nillable) {
18+
private final String defaultValue;
19+
20+
public CMElementTypeInfo(QName elementName, MTypeInfo<T, C> typeInfo,
21+
boolean nillable, String defaultValue) {
1822
Validate.notNull(elementName);
1923
Validate.notNull(typeInfo);
2024
this.elementName = elementName;
2125
this.typeInfo = typeInfo;
2226
this.nillable = nillable;
27+
this.defaultValue = defaultValue;
2328
}
2429

2530
public QName getElementName() {
@@ -29,12 +34,16 @@ public QName getElementName() {
2934
public MTypeInfo<T, C> getTypeInfo() {
3035
return typeInfo;
3136
}
32-
33-
public boolean isNillable()
34-
{
37+
38+
public boolean isNillable() {
3539
return this.nillable;
3640
}
37-
41+
42+
@Override
43+
public String getDefaultValue() {
44+
return defaultValue;
45+
}
46+
3847
@Override
3948
public String toString() {
4049
return "Element [" + getElementName() + ":" + getTypeInfo() + "]";

runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMEnumLeafInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public MElementInfo<T, C> createElementInfo(MTypeInfo<T, C> scope,
9393
QName substitutionHead) {
9494
return new CMElementInfo<T, C>(getOrigin().createElementInfoOrigin(),
9595
getPackageInfo(), getContainer(), getLocalName(),
96-
getElementName(), scope, this, substitutionHead);
96+
getElementName(), scope, this, substitutionHead, null);
9797
}
9898

9999
public String getName() {

runtime/src/main/java/org/jvnet/jaxb2_commons/xml/bind/model/concrete/CMInfoFactory.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,8 @@ protected MPropertyInfo<T, C> createElementPropertyInfo(
369369
createPropertyInfoOrigin((PI) ep), classInfo, ep.getName(),
370370
ep.isCollection() && !ep.isValueList(),
371371
getTypeInfo(ep, typeRef), typeRef.getTagName(),
372-
ep.getXmlName(), typeRef.isNillable());
372+
ep.getXmlName(), typeRef.isNillable(),
373+
typeRef.getDefaultValue());
373374
}
374375

375376
protected MPropertyInfo<T, C> createElementsPropertyInfo(
@@ -379,7 +380,8 @@ protected MPropertyInfo<T, C> createElementsPropertyInfo(
379380
types.size());
380381
for (TypeRef<T, C> typeRef : types) {
381382
typedElements.add(new CMElementTypeInfo<T, C>(typeRef.getTagName(),
382-
getTypeInfo(ep, typeRef), typeRef.isNillable()));
383+
getTypeInfo(ep, typeRef), typeRef.isNillable(), typeRef
384+
.getDefaultValue()));
383385
}
384386
return new CMElementsPropertyInfo<T, C>(
385387
createPropertyInfoOrigin((PI) ep), classInfo, ep.getName(),
@@ -406,15 +408,17 @@ protected MPropertyInfo<T, C> createElementRefPropertyInfo(
406408
rp.isMixed(), rp.getWildcard() == null ? false
407409
: rp.getWildcard().allowDom,
408410
rp.getWildcard() == null ? true
409-
: rp.getWildcard().allowTypedObject);
411+
: rp.getWildcard().allowTypedObject,
412+
getDefaultValue(element));
410413
}
411414

412415
protected MPropertyInfo<T, C> createElementRefsPropertyInfo(
413416
final MClassInfo<T, C> classInfo, final RPI rp) {
414417
final List<MElementTypeInfo<T, C>> typedElements = new ArrayList<MElementTypeInfo<T, C>>();
415418
for (Element<T, C> element : rp.getElements()) {
416419
typedElements.add(new CMElementTypeInfo<T, C>(element
417-
.getElementName(), getTypeInfo(rp, element), true));
420+
.getElementName(), getTypeInfo(rp, element), true,
421+
getDefaultValue(element)));
418422
}
419423
return new CMElementRefsPropertyInfo<T, C>(
420424
createPropertyInfoOrigin((PI) rp), classInfo, rp.getName(),
@@ -457,6 +461,22 @@ protected MTypeInfo<T, C> getTypeInfo(final ReferencePropertyInfo<T, C> rp,
457461
rp.getExpectedMimeType());
458462
}
459463

464+
private String getDefaultValue(Element<T, C> element) {
465+
if (element instanceof ElementInfo) {
466+
final ElementInfo<T, C> elementInfo = (ElementInfo<T, C>) element;
467+
final ElementPropertyInfo<T, C> property = elementInfo
468+
.getProperty();
469+
if (property != null) {
470+
final List<? extends TypeRef<T, C>> types = property.getTypes();
471+
if (types.size() == 1) {
472+
final TypeRef<T, C> typeRef = types.get(0);
473+
return typeRef.getDefaultValue();
474+
}
475+
}
476+
}
477+
return null;
478+
}
479+
460480
protected abstract MPackageInfo getPackage(CI info);
461481

462482
protected abstract String getLocalName(CI info);
@@ -511,7 +531,7 @@ protected MElementInfo<T, C> createElementInfo(EI element) {
511531
createElementInfoOrigin(element), getPackage(element),
512532
getContainer(element), getLocalName(element),
513533
element.getElementName(), scope, getTypeInfo(element),
514-
substitutionHead);
534+
substitutionHead, getDefaultValue(element));
515535
return elementInfo;
516536
}
517537

0 commit comments

Comments
 (0)