Skip to content

Commit 3dde6e2

Browse files
hansonrhansonr
authored andcommitted
Java2Script/SwingJS 3.2.4.02 further support for JAXB
1 parent 8b6f1f0 commit 3dde6e2

File tree

15 files changed

+314
-218
lines changed

15 files changed

+314
-218
lines changed
142 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20181103123755
1+
20181104122354
142 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20181103123755
1+
20181104122354

sources/net.sf.j2s.core/src/net/sf/j2s/core/CorePlugin.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ public class CorePlugin extends Plugin {
1919
* register the bundle version properly. So we use VERSION here instead.
2020
*
2121
*/
22-
public static String VERSION = "3.2.4.01";
22+
public static String VERSION = "3.2.4.02";
23+
// BH 11/04/2018 -- 3.2.4.02 support for JAXB - all accessor types, ObjectFactory, package-level namespace, but not yet accessor type
2324
// BH 10/27/2018 -- 3.2.4.01 support for JAXB FIELD+propOrder and NONE types
2425
// BH 9/28/2018 -- 3.2.4.00 adds minimal support for JAXB
2526
// BH 9/23/2018 -- 3.2.3.00 new transpiler + SwingJS-site.zip - support for java.applet.Applet and java.awt.* controls without use of a2s.*

sources/net.sf.j2s.core/src/net/sf/j2s/core/Java2ScriptVisitor.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@
135135
import org.eclipse.jdt.core.dom.WhileStatement;
136136
import org.eclipse.jdt.core.dom.WildcardType;
137137

138+
// BH 11/4/2018 -- 3.2.4.02 broad JAXB support
138139
// BH 10/27/2018 -- 3.2.4.01 support for JAXB FIELD+propOrder and NONE types
139140
// BH 9/28/2018 -- 3.2.4.00 adds minimal support for JAXB
140141
// BH 9/23/2018 -- 3.2.3.00 adds support for java.applet.Applet and java.awt.* controls without use of a2s.*
@@ -6019,7 +6020,8 @@ static String checkClassReplacement(String className) {
60196020
private final static String defaultNonQualified
60206021
// Math and Date both are minor extensions
60216022
// of JavaScript, so they are not qualified
6022-
= "java.lang.Math;" + "java.util.Date;"
6023+
= "java.lang.Math;"
6024+
// MAYBE NOT! + "java.util.Date;"
60236025
// swingjs.api.js and javajs.api.js contain
60246026
// interfaces to JavaScript methods and so
60256027
// are not parameterized.
@@ -6346,9 +6348,14 @@ public static void addClassAnnotations(int accessType, List<ClassAnnotation> cla
63466348
}
63476349

63486350
private static String annotationNameValue(String name, Object value) {
6351+
System.out.println(">>>value " + value + " " + value.getClass().getName() + " = " + value.toString());
63496352
String str = (name == null ? "" : name + "=");
6350-
if (value instanceof TypeLiteral) {
6353+
if (value instanceof TypeLiteral) {
63516354
str += "\"" + ((TypeLiteral) value).getType().resolveBinding().getQualifiedName() + ".class\"";
6355+
} else if (value instanceof IVariableBinding) {
6356+
str += "\"" + ((IVariableBinding) value).getType().getQualifiedName() + "." + ((IVariableBinding) value).getName() + "\"";
6357+
} else if (value instanceof Expression) {
6358+
str += "\"" + value + "\"";
63526359
} else if (value instanceof ITypeBinding) {
63536360
str += "\"" + ((ITypeBinding) value).getQualifiedName() + ".class\"";
63546361
} else if (value instanceof Object[]){

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -278,19 +278,18 @@ static boolean hasAnnotations(Object value) {
278278
}
279279
}
280280

281-
// public JSJAXBClass clone() {
282-
// try {
283-
// JSJAXBClass jaxbClass = (JSJAXBClass) super.clone();
284-
// for (int i = 0, n = fields.size(); i < n; i++) {
285-
// jaxbClass.addField((JSJAXBField) fields.get(i).clone());
286-
// }
287-
// return jaxbClass;
288-
// } catch (CloneNotSupportedException e) {
289-
// return null;
290-
// }
291-
//
292-
//
293-
// }
281+
public JSJAXBClass clone() {
282+
try {
283+
JSJAXBClass jaxbClass = (JSJAXBClass) super.clone();
284+
jaxbClass.fields = new ArrayList<JSJAXBField>();
285+
for (int i = 0, n = fields.size(); i < n; i++) {
286+
jaxbClass.addField((JSJAXBField) fields.get(i).clone());
287+
}
288+
return jaxbClass;
289+
} catch (CloneNotSupportedException e) {
290+
return null;
291+
}
292+
}
294293

295294
QName finalizeFieldQName(QName qName, String defaultName) {
296295
if (qName == null)

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

Lines changed: 94 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,92 @@ class JSJAXBField implements Cloneable {
3030

3131
// Unmarshaller only
3232

33+
final static int SIMPLE = 0;
34+
final static int LIST = 1;
35+
final static int MAP = 2;
36+
37+
int fieldType = SIMPLE;
38+
3339
DOMNode boundNode;
3440
List<Object> boundListNodes;
41+
QName qualifiedTypeName;
42+
3543
String xmlCharacterData = "";
3644
String xmlAttributeData;
37-
Attributes xmlAttributes;
45+
// Attributes xmlAttributes;
3846
String xmlType;
3947
boolean isNil;
4048

49+
// marshaller and unmarshaller
50+
51+
final static int NO_OBJECT = 0;
52+
final static int SIMPLE_OBJECT = 1;
53+
final static int ARRAY_OBJECT = 2;
54+
final static int LIST_OBJECT = 3;
55+
final static int MAP_KEY_OBJECT = 4;
56+
final static int MAP_VALUE_OBJECT = 8;
57+
58+
int holdsObjects = NO_OBJECT;
59+
60+
QName qualifiedName;
61+
QName qualifiedWrapName;
62+
63+
String text;
64+
String javaName;
65+
String javaClassName;
66+
String xmlSchemaType;
67+
String typeAdapter;
68+
String mimeType;
69+
String enumValue;
70+
String mapClassNameKey;
71+
String mapClassNameValue;
72+
String listClassName;
73+
74+
75+
boolean isTransient;
76+
boolean isAttribute;
77+
boolean isXmlID;
78+
boolean isXmlIDREF;
79+
boolean isXmlValue;
80+
boolean asList;
81+
boolean isNillable;
82+
boolean isArray;
83+
boolean isByteArray;
84+
boolean isContainer;
85+
86+
/// private only
87+
88+
private Object methodSet;
89+
private Object methodGet;
90+
private boolean isMethod;
91+
92+
/// debugging only
93+
94+
private int index;
95+
private Object clazz; // for debugging only
96+
97+
4198
/**
4299
* prior to re-use in unmarshalling
43100
*
44101
*/
45-
public JSJAXBField clear() {
46-
// try {
47-
// JSJAXBField f = (JSJAXBField) super.clone();
102+
public JSJAXBField clone() {
103+
try {
104+
JSJAXBField f = (JSJAXBField) super.clone();
48105
// marshaller
49-
this.mapEntryValue = null;
106+
f.mapEntryValue = null;
50107
// unmarshaller
51-
this.boundNode = null;
52-
this.boundListNodes = null;
53-
this.xmlCharacterData = null;
54-
this.xmlAttributeData = null;
55-
this.xmlAttributes = null;
56-
this.xmlType = null;
57-
this.isNil = false;
58-
return this;
59-
// return f;
60-
// } catch (CloneNotSupportedException e) {
61-
// return null;
62-
// }
108+
f.boundNode = null;
109+
f.boundListNodes = null;
110+
f.xmlCharacterData = null;
111+
f.xmlAttributeData = null;
112+
// f.xmlAttributes = null;
113+
f.xmlType = null;
114+
f.isNil = false;
115+
return f;
116+
} catch (CloneNotSupportedException e) {
117+
return null;
118+
}
63119
}
64120

65121
void setCharacters(String ch) {
@@ -71,7 +127,7 @@ void setAttributeData(String val) {
71127
}
72128

73129
void setAttributes(Attributes attr) {
74-
xmlAttributes = attr;
130+
// xmlAttributes = attr;
75131
xmlType = attr.getValue("xsi:type");
76132
isNil = (attr.getIndex("xsi:nil") >= 0);
77133
}
@@ -86,63 +142,6 @@ void setNode(DOMNode node) {
86142
boundNode = node;
87143
}
88144

89-
// marshaller and unmarshaller
90-
91-
int index;
92-
93-
String text;
94-
String javaName;
95-
String javaClassName;
96-
97-
Map<String, String> attr;
98-
99-
boolean isTransient;
100-
boolean isAttribute;
101-
boolean isXmlID;
102-
boolean isXmlIDREF;
103-
boolean isXmlValue;
104-
boolean asList;
105-
boolean isNillable;
106-
boolean isArray;
107-
boolean isByteArray;
108-
boolean isContainer;
109-
110-
QName qualifiedName = null;
111-
QName qualifiedWrapName;
112-
113-
String xmlSchemaType;
114-
String typeAdapter;
115-
String mimeType;
116-
String enumValue;
117-
118-
private Object methodSet;
119-
private Object methodGet;
120-
121-
String mapClassNameKey, mapClassNameValue, listClassName;
122-
123-
QName qualifiedTypeName;
124-
125-
final static int SEE_ALSO = -1;
126-
final static int SIMPLE = 0;
127-
final static int LIST = 1;
128-
final static int MAP = 2;
129-
130-
final static int NO_OBJECT = 0;
131-
final static int SIMPLE_OBJECT = 1;
132-
final static int ARRAY_OBJECT = 2;
133-
final static int LIST_OBJECT = 3;
134-
final static int MAP_KEY_OBJECT = 4;
135-
final static int MAP_VALUE_OBJECT = 8;
136-
final static int MAP_KEY_VALUE_OBJECT = 12;
137-
138-
int fieldType = SIMPLE;
139-
140-
int holdsObjects = NO_OBJECT;
141-
142-
private boolean isMethod;
143-
144-
private Object clazz;
145-
146145
/**
147146
* @param jclass
148147
* @param adata
@@ -189,10 +188,10 @@ void setNode(DOMNode node) {
189188
holdsObjects = ARRAY_OBJECT;
190189
isContainer |= isArray;
191190
if (isMethod)
192-
getMethods(jaxbClass.getJavaObject());
193-
attr = new Hashtable<String, String>();
191+
getMethods(jaxbClass.getJavaObject(), clazz);
192+
Map<String, String> attr = new Hashtable<String, String>();
194193
text = "";
195-
readAnnotations(jaxbClass, (String[]) adata[1], propOrder);
194+
readAnnotations(jaxbClass, (String[]) adata[1], propOrder, attr);
196195
// ensure that we have a qualified name if appropriate
197196
setDefaults();
198197
if (qualifiedWrapName != null) {
@@ -214,7 +213,7 @@ private static boolean isObject(String javaClassName) {
214213
|| javaClassName.equals("Object[]");
215214
}
216215

217-
private void readAnnotations(JSJAXBClass jaxbClass, String[] javaAnnotations, List<String> propOrder) {
216+
private void readAnnotations(JSJAXBClass jaxbClass, String[] javaAnnotations, List<String> propOrder, Map<String, String> attr) {
218217
for (int i = 0; i < javaAnnotations.length; i++) {
219218
String data = javaAnnotations[i];
220219
text += data + ";";
@@ -225,9 +224,9 @@ private void readAnnotations(JSJAXBClass jaxbClass, String[] javaAnnotations, Li
225224
if (pt >= 0 && data.indexOf("=") >= 0)
226225
addXMLAttributes(tag, data, attr);
227226
if (javaName == null)
228-
processTypeAnnotation(jaxbClass, tag, data, propOrder);
227+
processTypeAnnotation(jaxbClass, tag, data, propOrder, attr);
229228
else
230-
processFieldAnnotation(jaxbClass, tag, data);
229+
processFieldAnnotation(jaxbClass, tag, data, attr);
231230
}
232231
}
233232

@@ -254,8 +253,9 @@ private void setDefaults() {
254253
* @param tag
255254
* @param data
256255
* @param propOrder
256+
* @param attr TODO
257257
*/
258-
private void processTypeAnnotation(JSJAXBClass jaxbClass, String tag, String data, List<String> propOrder) {
258+
private void processTypeAnnotation(JSJAXBClass jaxbClass, String tag, String data, List<String> propOrder, Map<String, String> attr) {
259259
// check package annotations
260260
switch (tag) {
261261
case "@XmlSchema":
@@ -265,10 +265,10 @@ private void processTypeAnnotation(JSJAXBClass jaxbClass, String tag, String dat
265265
// check type annotations:
266266
switch (tag) {
267267
case "@XmlRootElement":
268-
qualifiedName = getName(tag);
268+
qualifiedName = getName(tag, attr);
269269
return;
270270
case "@XmlType":
271-
qualifiedTypeName = getName(tag);
271+
qualifiedTypeName = getName(tag, attr);
272272
String order = attr.get("@XmlType:propOrder");
273273
if (order != null) {
274274
int[] pt = new int[1];
@@ -308,8 +308,9 @@ private void processTypeAnnotation(JSJAXBClass jaxbClass, String tag, String dat
308308
* @param jaxbClass
309309
* @param tag
310310
* @param data what is in the (...)
311+
* @param attr
311312
*/
312-
private void processFieldAnnotation(JSJAXBClass jaxbClass, String tag, String data) {
313+
private void processFieldAnnotation(JSJAXBClass jaxbClass, String tag, String data, Map<String, String> attr) {
313314
switch (tag) {
314315
case "!XmlInner":
315316
jaxbClass.addSeeAlso(javaClassName);
@@ -320,10 +321,10 @@ private void processFieldAnnotation(JSJAXBClass jaxbClass, String tag, String da
320321
return;
321322
case "@XmlAttribute":
322323
isAttribute = true;
323-
qualifiedName = getName(tag);
324+
qualifiedName = getName(tag, attr);
324325
return;
325326
case "@XmlElement":
326-
qualifiedName = getName(tag);
327+
qualifiedName = getName(tag, attr);
327328
isNillable = "true".equals(attr.get("@XmlElement:nillable"));
328329
return;
329330
case "@XmlSchemaType":
@@ -377,7 +378,7 @@ private void processFieldAnnotation(JSJAXBClass jaxbClass, String tag, String da
377378
// @XmlMimeType("text/xml; charset=iso-8859-1")
378379
return;
379380
case "@XmlElementWrapper":
380-
qualifiedWrapName = getName(tag);
381+
qualifiedWrapName = getName(tag, attr);
381382
return;
382383
}
383384
System.out.println("JSJAXBField Unprocessed field annotation: " + text);
@@ -403,9 +404,10 @@ private static String getQuotedClass(String s) {
403404
* combining all annotations into one.
404405
*
405406
* @param tag
407+
* @param attr TODO
406408
* @return
407409
*/
408-
private QName getName(String tag) {
410+
private QName getName(String tag, Map<String, String> attr) {
409411
String name, namespace;
410412
name = attr.get(tag + ":name");
411413
namespace = attr.get(tag + ":namespace");
@@ -509,10 +511,10 @@ private Object findSetMethod(String m, Object[] pm, Object[] jo) {
509511
/**
510512
* Check for methods in C$.$P$[] (private) and object[] (all others)
511513
*/
512-
private void getMethods(Object javaObject) {
514+
private void getMethods(Object javaObject, Object clazz) {
513515
String methodName = javaName.substring(2);
514-
Object[] pm = /** @j2sNative this.clazz.$P$ || */null;
515-
Object[] jo = /** @j2sNative this.clazz.prototype || */null;
516+
Object[] pm = /** @j2sNative clazz.$P$ || */null;
517+
Object[] jo = /** @j2sNative clazz.prototype || */null;
516518
// annotation can be on set... or get... or is...
517519
// so we start by using get instead of set
518520
// qualifications getXxxx$() and setXxxx$mytype_escaped(xxx)

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
@@ -112,6 +112,7 @@ private static Map<String, Integer> newQualifierMap(Map<String, Integer> newMap)
112112
mapQualifierLevel = new Hashtable<String, Integer>(oldMap);
113113
return oldMap;
114114
}
115+
115116
private static void clearQualifierMap() {
116117
// not thread safe
117118
mapQualifierLevel.clear();
@@ -166,6 +167,7 @@ private static String escapeString(String str, boolean isAttribute) {
166167
/**
167168
* @j2sNative str= (this.textarea.innerHTML=str,this.textarea.innerHTML);
168169
*/
170+
str = str.replace("&", "&amp;");
169171
if (isAttribute)
170172
str = str.replace("\"", "&quot;");
171173

0 commit comments

Comments
 (0)