19
19
* actual higher-level conversion to JSON tokens.
20
20
*<p>
21
21
* Beyond initial idea there are also couple of other detours like ability
22
- * to "replay" some tokens, add virtual wrappers (ironically to support "unwrapped"
22
+ * to "replay" some tokens, add virtual wrappers (ironically to support "unwrapped"_currentStateDesc
23
23
* array values), and to unroll "Objects" into String values in some cases.
24
24
*/
25
25
public class XmlTokenStream
26
26
{
27
27
// // // main token states:
28
-
28
+
29
29
public final static int XML_START_ELEMENT = 1 ;
30
30
public final static int XML_END_ELEMENT = 2 ;
31
31
public final static int XML_ATTRIBUTE_NAME = 3 ;
@@ -227,8 +227,8 @@ public void skipEndElement() throws IOException, XMLStreamException
227
227
int type = next ();
228
228
if (type != XML_END_ELEMENT ) {
229
229
throw new IOException (String .format (
230
- "Internal error: Expected END_ELEMENT (%d) , got event of type %d " ,
231
- XML_END_ELEMENT , type ));
230
+ "Internal error: Expected END_ELEMENT, got event of type %s " ,
231
+ _stateDesc ( type ) ));
232
232
}
233
233
}
234
234
@@ -284,16 +284,15 @@ public JsonLocation getTokenLocation() {
284
284
*/
285
285
protected void repeatStartElement ()
286
286
{
287
- //System.out.println(" -> repeatStartElement for "+_localName+", _currentWrapper was: "+_currentWrapper);
287
+ //System.out.println(" XmlTokenStream. repeatStartElement() for < "+_localName+"> , _currentWrapper was: "+_currentWrapper);
288
288
// sanity check: can only be used when just returned START_ELEMENT:
289
289
if (_currentState != XML_START_ELEMENT ) {
290
290
// 14-May-2020, tatu: Looks like we DO end up here with empty Lists; if so,
291
291
// should NOT actually wrap.
292
292
if (_currentState == XML_END_ELEMENT ) {
293
293
return ;
294
294
}
295
- throw new IllegalStateException ("Current state not XML_START_ELEMENT ("
296
- +XML_START_ELEMENT +") but " +_currentState );
295
+ throw new IllegalStateException ("Current state not XML_START_ELEMENT but " +_currentStateDesc ());
297
296
}
298
297
// Important: add wrapper, to keep track...
299
298
if (_currentWrapper == null ) {
@@ -324,20 +323,20 @@ protected void pushbackCurrentToken()
324
323
*/
325
324
protected void skipAttributes ()
326
325
{
326
+ //System.out.println(" XmlTokenStream.skipAttributes(), state: "+_currentStateDesc());
327
327
if (_currentState == XML_ATTRIBUTE_NAME ) {
328
328
_attributeCount = 0 ;
329
329
_currentState = XML_START_ELEMENT ;
330
330
} else if (_currentState == XML_START_ELEMENT ) {
331
- /* 06-Jan-2012, tatu: As per [#47] it looks like we should NOT do anything
332
- * in this particular case, because it occurs when original element had
333
- * no attributes and we now point to the first child element.
334
- */
331
+ // 06-Jan-2012, tatu: As per [#47] it looks like we should NOT do anything
332
+ // in this particular case, because it occurs when original element had
333
+ // no attributes and we now point to the first child element.
335
334
// _attributeCount = 0;
336
335
} else if (_currentState == XML_TEXT ) {
337
336
; // nothing to do... is it even legal?
338
337
} else {
339
- throw new IllegalStateException ("Current state not XML_START_ELEMENT or XML_ATTRIBUTE_NAME ("
340
- + XML_START_ELEMENT + ") but " +_currentState );
338
+ throw new IllegalStateException (
339
+ "Current state not XML_START_ELEMENT or XML_ATTRIBUTE_NAME but " +_currentStateDesc () );
341
340
}
342
341
}
343
342
@@ -347,6 +346,7 @@ protected void skipAttributes()
347
346
*/
348
347
protected String convertToString () throws XMLStreamException
349
348
{
349
+ //System.out.println(" XmlTokenStream.convertToString(), state: "+_currentStateDesc());
350
350
// only applicable to cases where START_OBJECT was induced by attributes
351
351
if (_currentState != XML_ATTRIBUTE_NAME || _nextAttributeIndex != 0 ) {
352
352
return null ;
@@ -381,6 +381,7 @@ protected String convertToString() throws XMLStreamException
381
381
382
382
private final int _next () throws XMLStreamException
383
383
{
384
+ //System.out.println(" XmlTokenStream._next(), state: "+_currentStateDesc());
384
385
switch (_currentState ) {
385
386
case XML_ATTRIBUTE_VALUE :
386
387
++_nextAttributeIndex ;
@@ -747,7 +748,30 @@ protected static boolean _allWs(String str)
747
748
return true ;
748
749
}
749
750
751
+ protected String _currentStateDesc () {
752
+ return _stateDesc (_currentState );
753
+ }
754
+
755
+ protected String _stateDesc (int state ) {
756
+ switch (state ) {
757
+ case XML_START_ELEMENT :
758
+ return "XML_START_ELEMENT" ;
759
+ case XML_END_ELEMENT :
760
+ return "XML_END_ELEMENT" ;
761
+ case XML_ATTRIBUTE_NAME :
762
+ return "XML_ATTRIBUTE_NAME" ;
763
+ case XML_ATTRIBUTE_VALUE :
764
+ return "XML_ATTRIBUTE_VALUE" ;
765
+ case XML_TEXT :
766
+ return "XML_TEXT" ;
767
+ case XML_END :
768
+ return "XML_END" ;
769
+ }
770
+ return "N/A (" +_currentState +")" ;
771
+ }
772
+
750
773
// for DEBUGGING
774
+ /*
751
775
@Override
752
776
public String toString()
753
777
{
@@ -756,4 +780,5 @@ public String toString()
756
780
_currentState, _attributeCount, _nextAttributeIndex,
757
781
_localName, _textValue, _repeatElement, _currentWrapper, _repeatElement, _nextLocalName);
758
782
}
783
+ */
759
784
}
0 commit comments