Skip to content

Commit e0a8a81

Browse files
committed
JAXB update for 3.2.7
1 parent d6758c2 commit e0a8a81

File tree

5 files changed

+153
-70
lines changed

5 files changed

+153
-70
lines changed

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

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@ class JSJAXBClass implements Cloneable {
2626
* This class's accessorType. We need explicit propOrder or XmlAttribute or
2727
* XmlElement.
2828
*
29-
* This field is not used, because all this is determined during transpilation.
30-
* BUT... Note that the Java2Script transpiler currently does not honor
31-
* package-info.java annotations for this. TODO
29+
* The java2script transpiler will add !XmlPublic(true|false) to the annotation information.
3230
*
3331
*/
3432
int accessorType = TYPE_PUBLIC_MEMBER;
@@ -83,8 +81,7 @@ class JSJAXBClass implements Cloneable {
8381
QName declaredTypeName;
8482

8583
private static String packageNamespace;
86-
// TODO: allow for package accessor type.
87-
private static String packageAccessorType;
84+
private static int packageAccessorType = TYPE_PUBLIC_MEMBER;
8885

8986
private final static Map<String, String> marshallerNamespacePrefixes = new Hashtable<String, String>();
9087
@SuppressWarnings("rawtypes")
@@ -93,21 +90,32 @@ class JSJAXBClass implements Cloneable {
9390
static void clearStatics() {
9491
prefixIndex = 1;
9592
packageNamespace = null;
96-
packageAccessorType = null;
93+
packageAccessorType = TYPE_PUBLIC_MEMBER;
9794
marshallerNamespacePrefixes.clear();
9895
adapterMap.clear();
9996
}
10097

101-
private static void getDefaultNamespaceFromPackageInfo(Class<?> javaClass) {
102-
if (packageNamespace != null)
103-
return;
104-
packageNamespace = "";
105-
InputStream is = javaClass.getResourceAsStream("_$.js");
106-
if (is != null) {
107-
String data = Rdr.streamToUTF8String(new BufferedInputStream(is));
108-
data = PT.getQuotedAttribute(data, "namespace");
109-
if (data != null)
110-
packageNamespace = data;
98+
static int parseAccessorType(String data) {
99+
return (data.indexOf("XmlAccessType.FIELD") >= 0 ? TYPE_FIELD
100+
: data.indexOf("XmlAccessType.PUBLIC_MEMBER") >= 0 ? TYPE_PUBLIC_MEMBER
101+
: data.indexOf("XmlAccessType.PROPERTY") >= 0 ? TYPE_PROPERTY : TYPE_NONE);
102+
}
103+
104+
private static void getPackageInfo(Class<?> javaClass) {
105+
if (packageNamespace == null) {
106+
// just keeping this simple; could really read the _$ class and do this
107+
// properly.
108+
packageNamespace = "";
109+
packageAccessorType = TYPE_PUBLIC_MEMBER;
110+
// Keeping this simple for now.
111+
InputStream is = javaClass.getResourceAsStream("_$.js");
112+
if (is != null) {
113+
String data = Rdr.streamToUTF8String(new BufferedInputStream(is));
114+
packageAccessorType = parseAccessorType(data);
115+
data = PT.getQuotedAttribute(data, "namespace");
116+
if (data != null)
117+
packageNamespace = data;
118+
}
111119
}
112120
}
113121

@@ -132,7 +140,8 @@ private static void getDefaultNamespaceFromPackageInfo(Class<?> javaClass) {
132140
@SuppressWarnings("unused")
133141
static boolean checkC$__ANN__(JSJAXBClass jsjaxbClass, Class<?> javaClass, boolean haveJavaObject,
134142
boolean isXmlIDREF) {
135-
getDefaultNamespaceFromPackageInfo(javaClass);
143+
getPackageInfo(javaClass);
144+
jsjaxbClass.accessorType = packageAccessorType;
136145
boolean isTop = true;
137146
while (javaClass != null) {
138147

@@ -184,14 +193,33 @@ JSJAXBClass addTypeData(Object[][][] jsdata, Object clazz, boolean haveJavaObjec
184193
else
185194
lastClassName = (String) adata[0][1];
186195
JSJAXBField field = new JSJAXBField(this, adata, clazz, fields.size(), propOrder);
187-
if (i == 0 && !isSuperclass || field.javaName != null)
196+
if (field.ignore == true) {
197+
removeField(field.javaName);
198+
} else if (i == 0 && !isSuperclass || field.javaName != null) {
188199
addField(field);
200+
}
189201
}
190202
// if (isMarshaller)
191203
// processPropOrder(javaObject);
192204
return this;
193205
}
194206

207+
/**
208+
* !XMLpublic?
209+
*
210+
* @param javaName
211+
*/
212+
private void removeField(String javaName) {
213+
for (int i = fields.size(); --i >= 0;) {
214+
if (javaName.equals(fields.get(i).javaName)) {
215+
fields.remove(i);
216+
System.out.println("jsjaxbclass (ignored)");
217+
marshallerFieldMap.remove(javaName);
218+
break;
219+
}
220+
}
221+
}
222+
195223
public void addSeeAlso(String... javaClassName) {
196224
if (seeAlso == null)
197225
seeAlso = new ArrayList<>();
@@ -205,8 +233,11 @@ private void addField(JSJAXBField field) {
205233
addField(field.listFields.get(i));
206234
}
207235
fields.add(field);
208-
if (isMarshaller && field.javaName != null)
236+
if (isMarshaller && field.javaName != null) {
237+
System.out.println("jsjaxbclass adding "
238+
+ (field.methodName == null ? "field " + field.javaName : field.methodName + "()"));
209239
marshallerFieldMap.put(field.javaName, field);
240+
}
210241
}
211242

212243
JSJAXBField getFieldFromJavaNameForMarshaller(String javaName) {

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

Lines changed: 58 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ class JSJAXBField implements Cloneable {
9292
boolean isArray;
9393
boolean isByteArray;
9494
boolean isContainer;
95+
boolean isNotPublic;
96+
97+
boolean ignore;
9598

9699
/// private only
97100

@@ -104,6 +107,9 @@ class JSJAXBField implements Cloneable {
104107
private int index;
105108
private Object clazz; // for debugging only
106109

110+
String methodName;
111+
112+
107113
JSJAXBField(JSJAXBField listOwner) {
108114
this.listOwner = listOwner;
109115
}
@@ -156,7 +162,16 @@ class JSJAXBField implements Cloneable {
156162
getMethods(jaxbClass.getJavaObject(), clazz);
157163
Map<String, String> attr = new Hashtable<String, String>();
158164
text = "";
159-
readAnnotations(jaxbClass, (String[]) adata[1], propOrder, attr);
165+
String[] attrs = (String[]) adata[1];
166+
if (adata[0].length == 4) {
167+
// new style has the full class name, not just @XmlRootElement
168+
String[] classes = (String[]) adata[0][3];
169+
for (int i = classes.length; --i >= 0;) {
170+
attrs[i] = "@" + classes[i] + "(" + attrs[i] + ")";
171+
}
172+
}
173+
readAnnotations(jaxbClass, attrs, propOrder, attr);
174+
160175
// ensure that we have a qualified name if appropriate
161176
finalizeNames(index, jaxbClass);
162177
}
@@ -280,7 +295,6 @@ private void processTypeAnnotation(JSJAXBClass jaxbClass, String tag, String dat
280295
case "@XmlSchema":
281296
return;
282297
}
283-
284298
// check type annotations:
285299
switch (tag) {
286300
case "@XmlRootElement":
@@ -300,9 +314,7 @@ private void processTypeAnnotation(JSJAXBClass jaxbClass, String tag, String dat
300314
}
301315
return;
302316
case "@XmlAccessorType":
303-
jaxbClass.accessorType = (data.indexOf("FIELD") >= 0 ? JSJAXBClass.TYPE_FIELD
304-
: data.indexOf("MEMBER") >= 0 ? JSJAXBClass.TYPE_PUBLIC_MEMBER
305-
: data.indexOf("PROPERTY") >= 0 ? JSJAXBClass.TYPE_PROPERTY : JSJAXBClass.TYPE_NONE);
317+
jaxbClass.accessorType = JSJAXBClass.parseAccessorType(data);
306318
return;
307319
case "@XmlSeeAlso":
308320
// @XmlSeeAlso({Dog.class,Cat.class})
@@ -330,39 +342,60 @@ private void processTypeAnnotation(JSJAXBClass jaxbClass, String tag, String dat
330342
*/
331343
private void processFieldAnnotation(JSJAXBClass jaxbClass, String tag, String data, Map<String, String> attr) {
332344
switch (tag) {
345+
case "!XmlPublic":
346+
// transpiler has added this tag for each field and method because XmlAccessorType was not indicated in
347+
// the class file
348+
isNotPublic = data.equals("false");
349+
switch (jaxbClass.accessorType) {
350+
case JSJAXBClass.TYPE_PUBLIC_MEMBER:
351+
ignore = isNotPublic;
352+
break;
353+
case JSJAXBClass.TYPE_NONE:
354+
ignore = true;
355+
break;
356+
case JSJAXBClass.TYPE_FIELD:
357+
ignore = isMethod;
358+
break;
359+
case JSJAXBClass.TYPE_PROPERTY:
360+
ignore = !isMethod;
361+
break;
362+
}
363+
if (!ignore)
364+
javaName = null;
365+
break;
333366
case "!XmlInner":
334367
jaxbClass.addSeeAlso(javaClassName);
335368
javaName = null;
336369
return;
337370
case "@XmlTransient":
338371
isTransient = true;
339-
return;
372+
break;
340373
case "@XmlAttribute":
341374
isAttribute = true;
342375
if (isContainer)
343376
asList = true;
344377
qualifiedName = getName(tag, attr);
345-
return;
378+
break;
346379
case "@XmlElements":
347380
listFields = new ArrayList<JSJAXBField>();
348-
return;
381+
break;
349382
case "@XmlElement":
350383
if (listFields != null) {
351384
JSJAXBField f = new JSJAXBField(this);
352385
listFields.add(f);
353386
f.processFieldAnnotation(jaxbClass, tag, data, attr);
354-
f.javaClassName = stripJavaLang(f.javaClassName).replace(".class", "");
387+
f.javaClassName = stripJavaLang(f.javaClassName).replace(".class", "");
355388
f.javaName = javaName + "::" + f.javaClassName;
356389
f.finalizeNames(index, jaxbClass);
357-
return;
390+
break;
358391
}
359392
qualifiedName = getName(tag, attr);
360393
isNillable = "true".equals(attr.get("@XmlElement:nillable"));
361394
defaultValue = attr.get("@XmlElement:defaultValue");
362395
String type = attr.get("@XmlElement:type");
363396
if (type != null)
364397
javaClassName = type;
365-
return;
398+
break;
366399
case "@XmlSchemaType":
367400
xmlSchemaType = attr.get("@XmlSchemaType:name");
368401
if (xmlSchemaType.equals("hexBinary")) {
@@ -379,45 +412,48 @@ private void processFieldAnnotation(JSJAXBClass jaxbClass, String tag, String da
379412
//
380413
// @XmlSchemaType(name="base64Binary")
381414
// public byte[] base64Bytes;
382-
return;
415+
break;
383416
case "@XmlJavaTypeAdapter":
384417
// typically CollapsedStringAdapter.class
385418
typeAdapter = attr.get("@XmlJavaTypeAdapter:name");
386419
if (typeAdapter == null)
387420
typeAdapter = data;
388421
typeAdapter = getQuotedClass(data);
389-
return;
422+
break;
390423
case "@XmlValue":
391424
jaxbClass.xmlValueField = this;
392425
isXmlValue = true;
393-
return;
426+
break;
394427
case "@XmlEnumValue":
395428
enumValue = data = PT.trim(data, "\"");
396429
jaxbClass.enumMap.put("/" + javaName, data); // for marshaller
397430
jaxbClass.enumMap.put("//" + data, javaName); // for unmarshaller
398431
jaxbClass.enumMap.put(data, this); // for unmarshaller
399-
return;
432+
break;
400433
case "@XmlList":
401434
asList = true;
402-
return;
435+
break;
403436
case "@XmlID":
404437
isXmlID = true;
405438
jaxbClass.xmlIDField = this;
406-
return;
439+
break;
407440
case "@XmlIDREF":
408441
isXmlIDREF = true;
409-
return;
442+
break;
410443
case "@XmlMimeType":
411444
mimeType = attr.get("@XmlMimeType:name");
412445
// e.g. @XmlMimeType("application/octet-stream")
413446
// @XmlMimeType("image/jpeg")
414447
// @XmlMimeType("text/xml; charset=iso-8859-1")
415-
return;
448+
break;
416449
case "@XmlElementWrapper":
417450
qualifiedWrapName = getName(tag, attr);
418-
return;
451+
break;
452+
default:
453+
System.out.println("JSJAXBField Unprocessed field annotation: " + text);
454+
ignore = true;
455+
break;
419456
}
420-
System.out.println("JSJAXBField Unprocessed field annotation: " + text);
421457
}
422458

423459
private static String[] getSeeAlso(String data) {
@@ -551,7 +587,7 @@ private Object findSetMethod(String m, Object[] pm, Object[] jo) {
551587
* Check for methods in C$.$P$[] (private) and object[] (all others)
552588
*/
553589
private void getMethods(Object javaObject, Object clazz) {
554-
String methodName = javaName.substring(2);
590+
methodName = javaName.substring(2);
555591
Object[] pm = /** @j2sNative clazz.$P$ || */
556592
null;
557593
Object[] jo = /** @j2sNative clazz.prototype || */

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ private void writeXML(JSJAXBClass jaxbClass, boolean isRoot, boolean addXsiType)
225225
if (isRoot)
226226
output("\n");
227227
writeTagClose(qname, true);
228+
if (isRoot)
229+
output("\n");
228230
}
229231

230232
private static boolean hasElements(List<JSJAXBField> fields) {

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

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package test;
2+
3+
import java.io.ByteArrayOutputStream;
4+
5+
import javax.xml.bind.JAXBContext;
6+
import javax.xml.bind.JAXBException;
7+
import javax.xml.bind.Marshaller;
8+
9+
public class Test_JAXB_Marshall extends Test_ {
10+
11+
public static void main(String[] args) {
12+
test.jaxb.publicmember.PhoneNumber phoneNumber = new test.jaxb.publicmember.PhoneNumber();
13+
phoneNumber.setType("work");
14+
phoneNumber.number = "555-1234";
15+
marshal(phoneNumber, true);
16+
test.jaxb.field.PhoneNumber pnf = new test.jaxb.field.PhoneNumber();
17+
pnf.setType("work");
18+
pnf.setNumber("555-1234");
19+
marshal(pnf, false);
20+
test.jaxb.property.PhoneNumber pnp = new test.jaxb.property.PhoneNumber();
21+
pnp.setType("work");
22+
pnp.number = "555-1234";
23+
marshal(pnp, true);
24+
System.out.println("Test_JAXB2 OK");
25+
26+
}
27+
28+
private static void marshal(Object o, boolean isProperty) {
29+
try {
30+
JAXBContext jc = JAXBContext.newInstance(o.getClass());
31+
Marshaller marshaller = jc.createMarshaller();
32+
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
33+
ByteArrayOutputStream os = new ByteArrayOutputStream();
34+
marshaller.marshal(o, os);
35+
String xml = new String(os.toString());
36+
System.out.println(xml);
37+
assert ((xml.indexOf("(property)") >= 0) == isProperty);
38+
} catch (JAXBException e) {
39+
e.printStackTrace();
40+
assert (false);
41+
}
42+
}
43+
}

0 commit comments

Comments
 (0)