Skip to content

Commit 438c90f

Browse files
committed
JAXB upgrade for 3.2.7
1 parent f01ecea commit 438c90f

File tree

8 files changed

+107
-68
lines changed

8 files changed

+107
-68
lines changed

sources/net.sf.j2s.java.core/src/swingjs/xml/JSJAXBClass.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,8 @@ private static void getPackageInfo(Class<?> javaClass) {
141141
static boolean checkC$__ANN__(JSJAXBClass jsjaxbClass, Class<?> javaClass, boolean haveJavaObject,
142142
boolean isXmlIDREF) {
143143
getPackageInfo(javaClass);
144-
jsjaxbClass.accessorType = packageAccessorType;
144+
if (jsjaxbClass != null)
145+
jsjaxbClass.accessorType = packageAccessorType;
145146
boolean isTop = true;
146147
while (javaClass != null) {
147148

@@ -313,10 +314,10 @@ static boolean hasAnnotations(Object value) {
313314
@SuppressWarnings("unused")
314315
Class<?> cl = value.getClass();
315316
return (/**
316-
* @j2sNative value.$clazz$
317-
* && value.$clazz$.$getAnn$
318-
* && cl.$clazz$
319-
* && !!cl.$clazz$.$getAnn$ ||
317+
* @j2sNative (value.$clazz$ ?
318+
* !!value.$clazz$.$getAnn$
319+
* : cl.$clazz$ ?
320+
* !!cl.$clazz$.$getAnn$: 0) ||
320321
*/
321322
false);
322323
} catch (Throwable t) {

sources/net.sf.j2s.java.core/src/swingjs/xml/JSJAXBField.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private void finalizeNames(int index, JSJAXBClass jaxbClass) {
199199
}
200200
}
201201

202-
private static String stripJavaLang(String name) {
202+
static String stripJavaLang(String name) {
203203
return (name.startsWith("java.lang.")? name.substring(10) : name);
204204
}
205205

@@ -236,6 +236,11 @@ void setAttributeData(String val) {
236236
xmlAttributeData = val;
237237
}
238238

239+
/**
240+
* Unmarshaller from DOM node
241+
*
242+
* @param attr
243+
*/
239244
void setAttributes(Attributes attr) {
240245
// xmlAttributes = attr;
241246
xmlType = attr.getValue("xsi:type");
@@ -384,8 +389,10 @@ private void processFieldAnnotation(JSJAXBClass jaxbClass, String tag, String da
384389
JSJAXBField f = new JSJAXBField(this);
385390
listFields.add(f);
386391
f.processFieldAnnotation(jaxbClass, tag, data, attr);
387-
f.javaClassName = stripJavaLang(f.javaClassName).replace(".class", "");
392+
f.javaClassName = f.javaClassName.replace(".class", "");
388393
f.javaName = javaName + "::" + f.javaClassName;
394+
// some issue here?
395+
f.javaClassName = stripJavaLang(f.javaClassName);
389396
f.finalizeNames(index, jaxbClass);
390397
break;
391398
}
@@ -394,10 +401,12 @@ private void processFieldAnnotation(JSJAXBClass jaxbClass, String tag, String da
394401
defaultValue = attr.get("@XmlElement:defaultValue");
395402
String type = attr.get("@XmlElement:type");
396403
if (type != null)
397-
javaClassName = type;
404+
javaClassName = type.replace(".class", "");
398405
break;
399406
case "@XmlSchemaType":
400407
xmlSchemaType = attr.get("@XmlSchemaType:name");
408+
if (xmlSchemaType.startsWith("xs:"))
409+
xmlSchemaType = xmlSchemaType.substring(3);
401410
if (xmlSchemaType.equals("hexBinary")) {
402411
xmlSchemaType = null;
403412
typeAdapter = "javax.xml.bind.annotation.adapters.HexBinaryAdapter";
@@ -413,9 +422,10 @@ private void processFieldAnnotation(JSJAXBClass jaxbClass, String tag, String da
413422
// @XmlSchemaType(name="base64Binary")
414423
// public byte[] base64Bytes;
415424
break;
416-
case "@XmlJavaTypeAdapter":
425+
case "@adapters.XmlJavaTypeAdapter":
426+
case "@XmlJavaTypeAdapter":
417427
// typically CollapsedStringAdapter.class
418-
typeAdapter = attr.get("@XmlJavaTypeAdapter:name");
428+
typeAdapter = attr.get(tag + ":name");
419429
if (typeAdapter == null)
420430
typeAdapter = data;
421431
typeAdapter = getQuotedClass(data);
@@ -450,7 +460,7 @@ private void processFieldAnnotation(JSJAXBClass jaxbClass, String tag, String da
450460
qualifiedWrapName = getName(tag, attr);
451461
break;
452462
default:
453-
System.out.println("JSJAXBField Unprocessed field annotation: " + text);
463+
System.out.println("JSJAXBField Unprocessed field annotation: " +tag + " "+ text);
454464
ignore = true;
455465
break;
456466
}
@@ -776,4 +786,27 @@ public static boolean isknownSchemaType(String xmlSchemaType) {
776786
return PT.isOneOf(xmlSchemaType, XML_SCHEMA_TYPES);
777787
}
778788

789+
/**
790+
* Arrays may need forcing.
791+
*
792+
* @param type
793+
* @return
794+
*/
795+
public static String boxPrimitive(String type) {
796+
switch (type) {
797+
case "int":
798+
return "Integer";
799+
case "boolean":
800+
case "float":
801+
case "double":
802+
case "integer":
803+
case "long":
804+
case "short":
805+
case "byte":
806+
return type.substring(0,1).toUpperCase() + type.substring(1);
807+
default:
808+
return type;
809+
}
810+
}
811+
779812
}

sources/net.sf.j2s.java.core/src/swingjs/xml/JSJAXBMarshaller.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ private static boolean isArray(Object value) {
155155
}
156156
}
157157

158-
private static JSJAXBField getField(JSJAXBClass jaxbClass, String javaName) {
158+
private static JSJAXBField getField(JSJAXBClass jaxbClass, String javaName) {
159159
return jaxbClass.getFieldFromJavaNameForMarshaller(javaName);
160160
}
161161

sources/net.sf.j2s.java.core/src/swingjs/xml/JSJAXBUnmarshaller.java

Lines changed: 54 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ private void processArraysAndLists() {
226226
&& (field.boundListNodes != null || field.listValues != null)
227227
&& field.fieldType != JSJAXBField.MAP) {
228228
// unwrapped set - array or list or map?
229-
// System.out.println("Filling List " + field.javaName);
230229
String type = (field.isArray ? field.javaClassName.replace("[]", "") : field.listClassName);
231230
boolean holdsObject = (field.holdsObjects != JSJAXBField.NO_OBJECT);
232231
field.setValue(fillArrayData(field, null, null, (holdsObject ? null : type)), this.javaObject);
@@ -240,7 +239,6 @@ private void processMaps() {
240239
JSJAXBField field = jaxbClass.fields.get(j);
241240
if (field.fieldType != JSJAXBField.MAP)
242241
continue;
243-
// System.out.println("Filling Map " + field.javaName);
244242
List<Object> nodes = field.boundListNodes;
245243
Map<Object, Object> map = (Map<Object, Object>) field.getObject(javaObject);
246244
if (map == null) {
@@ -261,9 +259,8 @@ private void processMaps() {
261259
valueType = className;
262260
JSJAXBField valueFieldToUnmarshal = (className == null ? null : field);
263261
for (int i = 1, n = nodes.size(); i < n;) {
264-
Object key = getNodeObject(keyFieldToUnmarshal, (DOMNode) nodes.get(i++), keyType, null);
265-
Object value = getNodeObject(valueFieldToUnmarshal, (DOMNode) nodes.get(i++), valueType, null);
266-
// System.out.println("map.put " + key + " = " + value);
262+
Object key = getNodeObject(keyFieldToUnmarshal, (DOMNode) nodes.get(i++), keyType, null, true);
263+
Object value = getNodeObject(valueFieldToUnmarshal, (DOMNode) nodes.get(i++), valueType, null, true);
267264
map.put(key, value);
268265
}
269266
}
@@ -308,14 +305,15 @@ private Object[] fillArrayData(JSJAXBField field, DOMNode node, Object[] data, S
308305
// TODO: what if arrayType is Object?
309306
String className = needsUnmarshalling(field, arrayType);
310307
JSJAXBField fieldToUnmarshal = (className == null ? null : field);
308+
boolean asObject = (arrayType == null);
311309
if (className != null)
312310
arrayType = className;
313311
for (int i = 0; i < n; i++)
314-
a[i] = (field.listValues != null ? field.listValues.get(i) : getNodeObject(fieldToUnmarshal, (node == null ? (DOMNode) field.boundListNodes.get(i) : node), arrayType, data[i]));
312+
a[i] = (field.listValues != null ? field.listValues.get(i) : getNodeObject(fieldToUnmarshal, (node == null ? (DOMNode) field.boundListNodes.get(i) : node), arrayType, data[i], asObject));
315313
return a;
316314
}
317315

318-
private Object getNodeObject(JSJAXBField fieldToUnmarshal, DOMNode node, String type, Object data) {
316+
private Object getNodeObject(JSJAXBField fieldToUnmarshal, DOMNode node, String type, Object data, boolean asObject) {
319317
if (fieldToUnmarshal != null) {
320318
return unmarshalField(fieldToUnmarshal, node, type);
321319
}
@@ -327,28 +325,27 @@ private Object getNodeObject(JSJAXBField fieldToUnmarshal, DOMNode node, String
327325
type = JSSAXParser.getAttribute(node, "xsi:type");
328326
if (type == null)
329327
return null;
330-
if (type.indexOf(":") >= 0 && !type.startsWith("xs:")) {
331-
// this is a SeeAlso entry
332-
QName qname = getQnameForAttribute(null, null, type);
333-
JSJAXBField field = getFieldFromQName(qname);
334-
return unmarshalField(field, node, null);
328+
if (type.indexOf(":") >= 0) {
329+
if (!type.startsWith("xs:")) {
330+
// this is a SeeAlso entry
331+
QName qname = getQnameForAttribute(null, null, type);
332+
JSJAXBField field = getFieldFromQName(qname);
333+
return unmarshalField(field, node, null);
334+
}
335+
} else if (asObject) {
336+
type = JSJAXBField.boxPrimitive(type);
335337
}
336338
}
337339
return convertFromType(null, data, type);
338340
}
339341

340342
private void start(DOMNode node, QName qName, Attributes atts) {
341-
//System.out.println(">>JSJAXBUnmarshaller start:" + qName);
342343
String text = JSSAXParser.getSimpleInnerText(node);
343344
if (doc == null) {
344345
doc = node;
345346
setDocAttributes(qName, text, atts);
346347
return;
347348
}
348-
// /**
349-
// * @j2sNative
350-
// System.out.println("start:" +node.outerHTML);
351-
// */
352349
JSJAXBField field = getFieldFromQName(qName);
353350
if (field != null) {
354351
bindNode(node, field, atts);
@@ -465,7 +462,6 @@ void prepareForUnmarshalling(String defaultNamespace) {
465462
String cl = seeAlso.get(i);
466463
try {
467464
addSeeAlso(Class.forName(cl));
468-
//System.out.println("JSJAXBClass seeAlso: " + cl);
469465
} catch (ClassNotFoundException e) {
470466
System.out.println("JSJAXBClass seeAlso[" + i + "] not found: " + cl);
471467
}
@@ -498,8 +494,6 @@ private void bindQName(QName q, JSJAXBField field, boolean isSeeAlso) {
498494
String qn = q.getNamespaceURI() + ":" + q.getLocalPart();
499495
map.put(qn, field);
500496
map.put("/lc/" + qn.toLowerCase(), field);
501-
// System.out.println("JSJAXBClass#binding " + namespace + ":" +
502-
// q.getLocalPart() + "->" + field.javaName);
503497
}
504498

505499
JSJAXBField getFieldFromQName(QName qName) {
@@ -570,7 +564,12 @@ private void setFieldValue(JSJAXBField field) {
570564
return;
571565
}
572566

573-
String dataType = (field.xmlType == null ? field.javaClassName : field.xmlType);
567+
String dataType;
568+
if ("java.lang.Object".equals(field.javaClassName) && field.xmlType != null) {
569+
dataType = JSJAXBField.boxPrimitive(field.xmlType);
570+
} else {
571+
dataType = field.javaClassName;
572+
}
574573
field.setValue(convertFromType(field, data, dataType), javaObject);
575574
}
576575

@@ -633,8 +632,9 @@ private Object convertFromType(JSJAXBField field, Object objVal, String type) {
633632
null);
634633
default:
635634
if (haventUnmarshalled.indexOf(field.xmlSchemaType) < 0) {
636-
haventUnmarshalled += ";" + field.xmlSchemaType;
637-
System.out.println("JSJAXBUnmarhsaller using target type " + type + " for " + field.xmlSchemaType);
635+
haventUnmarshalled += ";" + field.xmlSchemaType;
636+
System.out.println(
637+
"JSJAXBUnmarhsaller using target type " + type + " for " + field.xmlSchemaType);
638638
}
639639
// fall through //
640640
case "xsd:ID":
@@ -647,31 +647,35 @@ private Object convertFromType(JSJAXBField field, Object objVal, String type) {
647647
String lctype = type.substring(3);
648648
// must be an OBJECT for a List, Map, or Object field
649649
type = type.substring(3, 4).toUpperCase() + lctype.substring(1);
650-
switch (type) {
651-
case "Decimal":
652-
type = "java.math.BigDecimal";
653-
break;
654-
case "Unsignedlong":
655-
case "Integer":
656-
type = "java.math.BigInteger";
657-
break;
658-
case "Int":
659-
case "Unsignedshort":
660-
type = "Integer";
661-
break;
662-
case "Unsignedbyte":
663-
type = "Short";
664-
break;
665-
case "Unsignedint":
666-
type = "Long";
667-
break;
668-
default:
669-
if (isPrimitive(lctype)) {
670-
/**
671-
* return newVal = eval(type + ".valueOf$S(" + objVal + ")");
672-
*/
650+
if (field == null || field.javaClassName == null || field.javaClassName.equals("java.lang.Object")) {
651+
switch (type) {
652+
case "Decimal":
653+
type = "java.math.BigDecimal";
654+
break;
655+
case "Unsignedlong":
656+
case "Integer":
657+
type = "java.math.BigInteger";
658+
break;
659+
case "Int":
660+
case "Unsignedshort":
661+
type = "Integer";
662+
break;
663+
case "Unsignedbyte":
664+
type = "Short";
665+
break;
666+
case "Unsignedint":
667+
type = "Long";
668+
break;
669+
default:
670+
if (isPrimitive(lctype)) {
671+
/**
672+
* return newVal = eval(type + ".valueOf$S(" + objVal + ")");
673+
*/
674+
}
675+
break;
673676
}
674-
break;
677+
} else {
678+
type = field.javaClassName;
675679
}
676680
}
677681

@@ -720,7 +724,7 @@ private Object convertFromType(JSJAXBField field, Object objVal, String type) {
720724
return newVal = DatatypeConverter.parseQName(val, null);
721725
case "unsignedlong":
722726
type = "java.math.BigInteger";
723-
break;
727+
break;
724728
}
725729
Class<?> cl = null;
726730
try {
@@ -738,8 +742,8 @@ private Object convertFromType(JSJAXBField field, Object objVal, String type) {
738742

739743
// all Numbers and Enum
740744
/**
741-
* @j2sNative if (cl.$clazz$.valueOf$S) return newVal =
742-
* cl.$clazz$.valueOf$S(objVal);
745+
* @j2sNative if (cl.$clazz$.valueOf$S) return (objVal == null || objVal == "" ? null :
746+
* cl.$clazz$.valueOf$S(objVal));
743747
*/
744748

745749
// BigInteger, BigDecimal

sources/net.sf.j2s.java.core/src/test/Test_JAXB_Marshall.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static void main(String[] args) {
2121
pnp.setType("work");
2222
pnp.number = "555-1234";
2323
marshal(pnp, true);
24-
System.out.println("Test_JAXB2 OK");
24+
System.out.println("Test_JAXB_Marshall OK");
2525

2626
}
2727

sources/net.sf.j2s.java.core/src/test/Test_JAXB_Obj.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ public static void main(String[] args) {
3737
Date d = new Date(1373360175539L);
3838
System.out.println(d);
3939

40-
Root_FIELD root = new Root_FIELD();
40+
Root_FIELD root = new Root_FIELD(true);
4141
System.out.println("c is " + root.C());
4242
System.out.println("getPropertyC is " + root.getPropertyC());
4343
System.out.println("DEFVAL is " + root.DEFVAL);
44+
System.out.println("Sym is " + root.Ang);
4445
Marshaller marshaller = jc.createMarshaller();
4546
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
4647
ByteArrayOutputStream bos = new ByteArrayOutputStream();

sources/net.sf.j2s.java.core/src/test/jaxb/Root_FIELD.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ public String pC() {
203203
// <item xsi:nil="true"/>
204204
// </lst>
205205

206-
String Ang;
206+
public String Ang;
207207
// <Ang>[3 bytes here]</Ang>
208208
public String getPropertyAng() {
209209
return Ang;

sources/net.sf.j2s.java.core/src/test/jaxb/Root_ORDERED.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
@XmlAccessorType(XmlAccessType.FIELD)
4040
@XmlType(propOrder = {
4141
// "qname",
42-
"position",
42+
"f5", "position",
4343
"iii",
4444
"IFArray", "IFList",
4545
"hm","hm2",
@@ -59,7 +59,7 @@
5959
"Ilist",
6060
"list",
6161
"bi","bd",
62-
"f","f2","f3","f4","f6","Ilist2","f5",
62+
"f","f2","f3","f4","f6","Ilist2",
6363
"extraElem", "date"
6464
})
6565
public class Root_ORDERED {
@@ -168,7 +168,7 @@ public void validate() {
168168
assert(position.toString().equals("12345678910"));
169169
assert(extraAttr.equals("more&amp;"));
170170
assert(extraElem.equals("more&amp;"));
171-
assert(bd.equals(new BigDecimal("123.456")));
171+
assert(bd.equals(new BigDecimal("123.456")));
172172
assert(f2 == 1.2f);
173173
assert(f3[0] == 1.2f);
174174
assert(f4 == 1.3f);

0 commit comments

Comments
 (0)