Skip to content

Commit 47e0caa

Browse files
hansonrhansonr
authored andcommitted
JAXB code cleanup; JSSAXParser fix for top-level-only "parsing"
1 parent ed91e5f commit 47e0caa

File tree

7 files changed

+70
-97
lines changed

7 files changed

+70
-97
lines changed
-268 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20190924101854
1+
20190924231443
-268 Bytes
Binary file not shown.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
20190924101854
1+
20190924231443
-268 Bytes
Binary file not shown.

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

Lines changed: 24 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,18 @@ public JSJAXBUnmarshaller(JAXBContext context) {
6767

6868
@Override
6969
public Object unmarshal(Node node) throws JAXBException {
70-
// TODO Auto-generated method stub
7170
return null;
7271
}
7372

7473
@Override
7574
public UnmarshallerHandler getUnmarshallerHandler() {
76-
// TODO Auto-generated method stub
7775
return null;
7876
}
7977

8078
@Override
8179
public <T> JAXBElement<T> unmarshal(XMLStreamReader reader, Class<T> expectedType) throws JAXBException {
8280
parser = new JSSAXParser();
8381
xmlSource = ((JSXMLStreamReader) reader).getSource();
84-
@SuppressWarnings("unchecked")
8582
JAXBElement<T> o = (JAXBElement<T>) doUnmarshal(null, expectedType, MODE_RETURN_ELEMENT);
8683
clearStatics();
8784
return o;
@@ -133,7 +130,6 @@ private Class<?> findClassForNode(DOMNode doc, Class<?>[] classes) {
133130
if (nodeName.equals(name))
134131
return classes[i];
135132
}
136-
// TODO Auto-generated method stub
137133
return null;
138134
}
139135

@@ -154,7 +150,6 @@ private Object unmarshalField(JSJAXBField field, DOMNode node, String className)
154150
* @param javaClass
155151
* @return an instance of this class
156152
*/
157-
@SuppressWarnings({ "unchecked", "rawtypes" })
158153
private Object doUnmarshal(DOMNode node, Class<?> javaClass, int mode) {
159154
if (jaxbClass != null)
160155
addSeeAlso(javaClass);
@@ -175,6 +170,8 @@ private Object doUnmarshal(DOMNode node, Class<?> javaClass, int mode) {
175170
this.javaObject = javaObject;
176171
jaxbClass = newUnmarshalledInstance(javaClass, javaObject);
177172
try {
173+
// Note that we only ever parse the top level.
174+
// This method iterates to generate subsequent levels.
178175
parser.setContentHandler(this);
179176
switch (mode) {
180177
case MODE_RESTARTING:
@@ -232,13 +229,12 @@ private void processArraysAndLists() {
232229
// System.out.println("Filling List " + field.javaName);
233230
String type = (field.isArray ? field.javaClassName.replace("[]", "") : field.listClassName);
234231
boolean holdsObject = (field.holdsObjects != JSJAXBField.NO_OBJECT);
235-
field.setValue(fillArrayData(field, null, null, (holdsObject ? null : type), !field.isArray), this.javaObject);
232+
field.setValue(fillArrayData(field, null, null, (holdsObject ? null : type)), this.javaObject);
236233
}
237234
field.boundListNodes = null;
238235
}
239236
}
240237

241-
@SuppressWarnings("unchecked")
242238
private void processMaps() {
243239
for (int j = jaxbClass.fields.size(); --j >= 0;) {
244240
JSJAXBField field = jaxbClass.fields.get(j);
@@ -265,8 +261,8 @@ private void processMaps() {
265261
valueType = className;
266262
JSJAXBField valueFieldToUnmarshal = (className == null ? null : field);
267263
for (int i = 1, n = nodes.size(); i < n;) {
268-
Object key = getNodeObject(keyFieldToUnmarshal, (DOMNode) nodes.get(i++), keyType, null, true);
269-
Object value = getNodeObject(valueFieldToUnmarshal, (DOMNode) nodes.get(i++), valueType, null, true);
264+
Object key = getNodeObject(keyFieldToUnmarshal, (DOMNode) nodes.get(i++), keyType, null);
265+
Object value = getNodeObject(valueFieldToUnmarshal, (DOMNode) nodes.get(i++), valueType, null);
270266
// System.out.println("map.put " + key + " = " + value);
271267
map.put(key, value);
272268
}
@@ -303,7 +299,7 @@ private Object[] getArrayOfType(JSJAXBField field, String type, int len) {
303299
return new Object[len];
304300
}
305301

306-
private Object[] fillArrayData(JSJAXBField field, DOMNode node, Object[] data, String arrayType, boolean asObject) {
302+
private Object[] fillArrayData(JSJAXBField field, DOMNode node, Object[] data, String arrayType) {
307303
boolean haveData = (data != null);
308304
int n = (haveData ? data.length : field.boundListNodes != null ? field.boundListNodes.size() : field.listValues.size());
309305
Object[] a = getArrayOfType(field, arrayType, n);
@@ -315,12 +311,11 @@ private Object[] fillArrayData(JSJAXBField field, DOMNode node, Object[] data, S
315311
if (className != null)
316312
arrayType = className;
317313
for (int i = 0; i < n; i++)
318-
a[i] = (field.listValues != null ? field.listValues.get(i) : getNodeObject(fieldToUnmarshal, (node == null ? (DOMNode) field.boundListNodes.get(i) : node), arrayType, data[i],
319-
asObject));
314+
a[i] = (field.listValues != null ? field.listValues.get(i) : getNodeObject(fieldToUnmarshal, (node == null ? (DOMNode) field.boundListNodes.get(i) : node), arrayType, data[i]));
320315
return a;
321316
}
322317

323-
private Object getNodeObject(JSJAXBField fieldToUnmarshal, DOMNode node, String type, Object data, boolean asObject) {
318+
private Object getNodeObject(JSJAXBField fieldToUnmarshal, DOMNode node, String type, Object data) {
324319
if (fieldToUnmarshal != null) {
325320
return unmarshalField(fieldToUnmarshal, node, type);
326321
}
@@ -339,7 +334,7 @@ private Object getNodeObject(JSJAXBField fieldToUnmarshal, DOMNode node, String
339334
return unmarshalField(field, node, null);
340335
}
341336
}
342-
return convertFromType(null, data, type, asObject);
337+
return convertFromType(null, data, type);
343338
}
344339

345340
private void start(DOMNode node, QName qName, Attributes atts) {
@@ -510,14 +505,9 @@ private void bindQName(QName q, JSJAXBField field, boolean isSeeAlso) {
510505
JSJAXBField getFieldFromQName(QName qName) {
511506
String key = qName.getNamespaceURI() + ":" + qName.getLocalPart();
512507
JSJAXBField f = jaxbClass.unmarshallerFieldMap.get(key);
513-
// if (f == null)
514-
// f = jaxbClass.unmarshallerFieldMap.get(qName.getLocalPart());
515508
if (f == null)
516509
f = seeAlsoMap.get(key);
517-
// if (f == null)
518-
// f = seeAlsoMap.get(qName.getLocalPart());
519-
520-
// desparately doing this because I cannot find the capitalization algorithm in JAXB!
510+
// desperately doing this because I cannot find the capitalization algorithm in JAXB!
521511
if (f == null)
522512
f = jaxbClass.unmarshallerFieldMap.get("/lc/" + key.toLowerCase());
523513
if (f == null)
@@ -541,7 +531,7 @@ private void setFieldValue(JSJAXBField field) {
541531
// char data for field
542532
if (field.asList) {
543533
field.setValue(fillArrayData(field, field.boundNode, data.split(" "),
544-
getArrayType(field), false), javaObject);
534+
getArrayType(field)), javaObject);
545535
return;
546536
}
547537

@@ -581,7 +571,7 @@ private void setFieldValue(JSJAXBField field) {
581571
}
582572

583573
String dataType = (field.xmlType == null ? field.javaClassName : field.xmlType);
584-
field.setValue(convertFromType(field, data, dataType, field.xmlType != null), javaObject);
574+
field.setValue(convertFromType(field, data, dataType), javaObject);
585575
}
586576

587577
private DOMNode addXmlns(DOMNode node) {
@@ -606,11 +596,9 @@ private boolean isPrimitive(String type) {
606596
* @param field
607597
* @param objVal
608598
* @param type
609-
* @param asObject TODO
610599
* @return
611600
*/
612-
@SuppressWarnings({ "unchecked", "deprecation" })
613-
private Object convertFromType(JSJAXBField field, Object objVal, String type, boolean asObject) {
601+
private Object convertFromType(JSJAXBField field, Object objVal, String type) {
614602
@SuppressWarnings("unused")
615603
Object newVal = null;
616604
try {
@@ -622,7 +610,6 @@ private Object convertFromType(JSJAXBField field, Object objVal, String type, bo
622610

623611
if (field != null) {
624612
if (field.typeAdapter != null) {
625-
@SuppressWarnings("rawtypes")
626613
XmlAdapter adapter = field.getAdapter();
627614
try {
628615
return newVal = adapter.unmarshal(val);
@@ -788,23 +775,17 @@ private Object getNanInf(Object objVal) {
788775
return null;
789776
}
790777

791-
/////////// JSSAXParser callbacks ////////
778+
/////////// ContentHandler (JSSAXParser callback) ////////
792779

793780
@Override
794781
public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
795782
start(parser.getNode(), new QName(uri, localName, ""), atts);
796783
}
797784

798-
/////////// unused XMLReader /////////
799-
800-
@Override
801-
public void startDocument() throws SAXException {
802-
}
785+
/////////// unused ContentHandler /////////
803786

804787
@Override
805788
public void characters(char[] ch, int start, int length) throws SAXException {
806-
// TODO Auto-generated method stub
807-
808789
}
809790

810791
@Override
@@ -816,39 +797,31 @@ public void endDocument() throws SAXException {
816797
}
817798

818799
@Override
819-
public void setDocumentLocator(Locator locator) {
820-
// TODO Auto-generated method stub
821-
800+
public void endPrefixMapping(String prefix) throws SAXException {
822801
}
823802

824803
@Override
825-
public void startPrefixMapping(String prefix, String uri) throws SAXException {
826-
// TODO Auto-generated method stub
827-
804+
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
828805
}
829806

830807
@Override
831-
public void endPrefixMapping(String prefix) throws SAXException {
832-
// TODO Auto-generated method stub
833-
808+
public void processingInstruction(String target, String data) throws SAXException {
834809
}
835810

836811
@Override
837-
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
838-
// TODO Auto-generated method stub
839-
812+
public void setDocumentLocator(Locator locator) {
840813
}
841814

842815
@Override
843-
public void processingInstruction(String target, String data) throws SAXException {
844-
// TODO Auto-generated method stub
845-
816+
public void skippedEntity(String name) throws SAXException {
846817
}
847818

848819
@Override
849-
public void skippedEntity(String name) throws SAXException {
850-
// TODO Auto-generated method stub
820+
public void startDocument() throws SAXException {
821+
}
851822

823+
@Override
824+
public void startPrefixMapping(String prefix, String uri) throws SAXException {
852825
}
853826

854827
}

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

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@
99
import java.util.Locale;
1010
import java.util.Map;
1111

12-
import javajs.util.AU;
13-
import javajs.util.PT;
14-
import javajs.util.Rdr;
15-
import swingjs.JSUtil;
16-
import swingjs.api.js.DOMNode;
17-
1812
import org.xml.sax.ContentHandler;
1913
import org.xml.sax.DTDHandler;
2014
import org.xml.sax.DocumentHandler;
@@ -29,7 +23,11 @@
2923
import org.xml.sax.XMLReader;
3024
import org.xml.sax.helpers.DefaultHandler;
3125

32-
@SuppressWarnings({"deprecation"})
26+
import javajs.util.AU;
27+
import javajs.util.Rdr;
28+
import swingjs.JSUtil;
29+
import swingjs.api.js.DOMNode;
30+
3331
public class JSSAXParser implements Parser, XMLReader {
3432

3533
private EntityResolver resolver;
@@ -78,28 +76,31 @@ public void parse(InputSource source) throws SAXException, IOException {
7876
parse(source, PARSE_ALL);
7977
}
8078

79+
public DOMNode parseToDOM(InputSource is) throws SAXException, IOException {
80+
return parseXML(getString(is));
81+
}
82+
83+
public void parse(InputSource source, int mode) throws SAXException, IOException {
84+
parseXMLString(getString(source), mode);
85+
}
86+
8187
public void parseXMLString(String data) throws SAXException, IOException {
88+
parseXMLString(data, PARSE_ALL);
89+
}
90+
91+
public void parseXMLString(String data, int mode) throws SAXException, IOException {
8292
try {
83-
parseDocument(parseXML(data), PARSE_ALL);
93+
parseDocument(parseXML(data), mode);
8494
} catch (Exception e) {
8595
error(e);
8696
}
8797
}
8898

89-
90-
public DOMNode parseToDOM(InputSource is) throws SAXException, IOException {
91-
return parseXML(getString(is));
92-
}
93-
9499
public void parse(InputSource source, DefaultHandler handler) throws SAXException, IOException {
95100
setContentHandler(handler);
96101
parse(source, PARSE_ALL);
97102
}
98103

99-
public void parse(InputSource source, int mode) throws SAXException, IOException {
100-
parseXMLString(getString(source));
101-
}
102-
103104
private String getString(InputSource source) throws IOException {
104105
Reader rdr = source.getCharacterStream();
105106
String[] data = new String[1];
@@ -129,25 +130,25 @@ public DOMNode parseXML(String data) {
129130
* @return reconfigured data
130131
*/
131132
private String removeProcessing(String data) {
132-
if (false && data.indexOf("<?") >= 0) { // doesn't seem to be necessary?
133-
getUniqueSequence(data);
134-
data = PT.rep(PT.rep(data, "<?", "<![CDATA[" + uniqueSeq), "?>", "]]>");
135-
if (data.startsWith("<!")) {
136-
data = "<pre>" + data + "</pre>";
137-
havePre = true;
138-
}
139-
}
133+
// if (data.indexOf("<?") >= 0) { // doesn't seem to be necessary?
134+
// getUniqueSequence(data);
135+
// data = PT.rep(PT.rep(data, "<?", "<![CDATA[" + uniqueSeq), "?>", "]]>");
136+
// if (data.startsWith("<!")) {
137+
// data = "<pre>" + data + "</pre>";
138+
// havePre = true;
139+
// }
140+
// }
140141
return data;
141142
}
142143

143-
private String uniqueSeq;
144-
145-
private void getUniqueSequence(String data) {
146-
String s = "~";
147-
while (data.indexOf("<![CDATA["+s) >= 0)
148-
s += "~";
149-
uniqueSeq = s;
150-
}
144+
// private String uniqueSeq;
145+
//
146+
// private void getUniqueSequence(String data) {
147+
// String s = "~";
148+
// while (data.indexOf("<![CDATA["+s) >= 0)
149+
// s += "~";
150+
// uniqueSeq = s;
151+
// }
151152

152153
private void error(Exception e) throws SAXException {
153154
SAXParseException ee = new SAXParseException("Invalid Document", null);
@@ -159,7 +160,6 @@ private void error(Exception e) throws SAXException {
159160

160161
private boolean ver2;
161162

162-
private static final int ELEMENT_TYPE = 1;
163163
public static final int PARSE_ALL = 0;
164164
public static final int PARSE_TOP_LEVEL_ONLY = 1;
165165
public static final int PARSE_GET_DOC_ONLY = 2;
@@ -312,7 +312,7 @@ private String getTextData(DOMNode node, boolean doProcess) throws SAXException
312312
String data = (String) DOMNode.getAttr(node, "textContent");
313313
if (!doProcess)
314314
return data;
315-
if (isText || uniqueSeq == null || !data.startsWith(uniqueSeq)) {
315+
// if (isText || uniqueSeq == null || !data.startsWith(uniqueSeq)) {
316316
int len = data.length();
317317
char[] ch = tempChars;
318318
if (len > ch.length)
@@ -325,15 +325,15 @@ private String getTextData(DOMNode node, boolean doProcess) throws SAXException
325325
else
326326
docHandler.characters(ch, 0, len);
327327
return null;
328-
}
329-
data = data.substring(uniqueSeq.length());
330-
String target = data + " ";
331-
target = target.substring(0, target.indexOf(" "));
332-
data = data.substring(target.length()).trim();
333-
if (ver2)
334-
contentHandler.processingInstruction(target, data);
335-
else
336-
docHandler.processingInstruction(target, data);
328+
// }
329+
// data = data.substring(uniqueSeq.length());
330+
// String target = data + " ";
331+
// target = target.substring(0, target.indexOf(" "));
332+
// data = data.substring(target.length()).trim();
333+
// if (ver2)
334+
// contentHandler.processingInstruction(target, data);
335+
// else
336+
// docHandler.processingInstruction(target, data);
337337
}
338338
return null;
339339
}

0 commit comments

Comments
 (0)