From 30018621989576cd4249ff39b9674e61f9b0aadc Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Sun, 24 May 2020 09:56:37 -0700 Subject: [PATCH] Minor clean up, preparing to test #405 --- .../dataformat/xml/deser/XmlTokenStream.java | 42 +++++++++++------- .../{deser => stream}/XmlTokenStreamTest.java | 44 ++++++++++++++++++- 2 files changed, 67 insertions(+), 19 deletions(-) rename src/test/java/com/fasterxml/jackson/dataformat/xml/{deser => stream}/XmlTokenStreamTest.java (75%) diff --git a/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java b/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java index 9989ed9a7..947894efb 100644 --- a/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java +++ b/src/main/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStream.java @@ -173,7 +173,8 @@ public int next() throws XMLStreamException System.out.printf(" XmlTokenStream.next(): XML_START_ELEMENT '%s' %s\n", _localName, _loc()); break; case XML_END_ELEMENT: - System.out.printf(" XmlTokenStream.next(): XML_END_ELEMENT '%s' %s\n", _localName, _loc()); + // 24-May-2020, tatu: no name available for end element so do not print + System.out.printf(" XmlTokenStream.next(): XML_END_ELEMENT %s\n", _loc()); break; case XML_ATTRIBUTE_NAME: System.out.printf(" XmlTokenStream.next(): XML_ATTRIBUTE_NAME '%s' %s\n", _localName, _loc()); @@ -219,7 +220,15 @@ public void skipEndElement() throws IOException, XMLStreamException public int getCurrentToken() { return _currentState; } public String getText() { return _textValue; } + + /** + * Accessor for local name of current named event (that is, + * {@code XML_START_ELEMENT} or {@code XML_ATTRIBUTE_NAME}). + *

+ * NOTE: name NOT accessible on {@code XML_END_ELEMENT} + */ public String getLocalName() { return _localName; } + public String getNamespaceURI() { return _namespaceURI; } public boolean hasXsiNil() { @@ -509,15 +518,6 @@ private final String _getText(XMLStreamReader2 r) throws XMLStreamException /********************************************************************** */ - /* - _xmlReader = Stax2ReaderAdapter.wrapIfNecessary(xmlReader); - _currentState = XML_START_ELEMENT; - _localName = _xmlReader.getLocalName(); - _namespaceURI = _xmlReader.getNamespaceURI(); - _attributeCount = _xmlReader.getAttributeCount(); - _formatFeatures = formatFeatures; - */ - private final int _initStartElement() throws XMLStreamException { final String ns = _xmlReader.getNamespaceURI(); @@ -525,11 +525,10 @@ private final int _initStartElement() throws XMLStreamException _checkXsiAttributes(); - /* Support for virtual wrapping: in wrapping, may either - * create a new wrapper scope (if in sub-tree, or matches - * wrapper element itself), or implicitly close existing - * scope. - */ + // Support for virtual wrapping: in wrapping, may either create a new + // wrapper scope (if in sub-tree, or matches wrapper element itself), + // or implicitly close existing scope. + if (_currentWrapper != null) { if (_currentWrapper.matchesWrapper(localName, ns)) { _currentWrapper = _currentWrapper.intermediateWrapper(); @@ -631,11 +630,21 @@ private final int _handleEndElement() //System.out.println(" XMLTokenStream._handleEndElement(): IMPLICIT requestRepeat of END_ELEMENT '"+_localName); } else { _currentWrapper = _currentWrapper.getParent(); + // 23-May-2020, tatu: Let's clear _localName since it's value is unlikely + // to be correct and we may or may not be able to get real one (for + // END_ELEMENT could) -- FromXmlParser does NOT use this info + _localName = ""; + _namespaceURI = ""; + } + } else { + // Not (necessarily) known, as per above, so: + _localName = ""; + _namespaceURI = ""; } return (_currentState = XML_END_ELEMENT); } - + private JsonLocation _extractLocation(XMLStreamLocation2 location) { if (location == null) { // just for impls that might pass null... @@ -647,7 +656,6 @@ private JsonLocation _extractLocation(XMLStreamLocation2 location) location.getColumnNumber()); } - protected boolean _allWs(String str) { final int len = (str == null) ? 0 : str.length(); diff --git a/src/test/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStreamTest.java b/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlTokenStreamTest.java similarity index 75% rename from src/test/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStreamTest.java rename to src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlTokenStreamTest.java index 22e2efa40..613ff6eff 100644 --- a/src/test/java/com/fasterxml/jackson/dataformat/xml/deser/XmlTokenStreamTest.java +++ b/src/test/java/com/fasterxml/jackson/dataformat/xml/stream/XmlTokenStreamTest.java @@ -1,4 +1,4 @@ -package com.fasterxml.jackson.dataformat.xml.deser; +package com.fasterxml.jackson.dataformat.xml.stream; import java.io.*; @@ -31,7 +31,10 @@ public void testSimple() throws Exception assertEquals(XmlTokenStream.XML_TEXT, tokens.next()); assertEquals("abc", tokens.getText()); assertEquals(XmlTokenStream.XML_END_ELEMENT, tokens.next()); + // 23-May-2020, tatu: Not known for END_ELEMENT, alas, so: + assertEquals("", tokens.getLocalName()); assertEquals(XmlTokenStream.XML_END_ELEMENT, tokens.next()); + assertEquals("", tokens.getLocalName()); assertEquals(XmlTokenStream.XML_END, tokens.next()); } @@ -124,5 +127,42 @@ public void testNested() throws Exception assertEquals(XmlTokenStream.XML_END_ELEMENT, tokens.next()); assertEquals(XmlTokenStream.XML_END, tokens.next()); } - + + // For [dataformat-xml#402] +/* public void testMixedContent() throws Exception + { + String XML = "first123 and second abclast & final"; + XMLStreamReader sr = _staxInputFactory.createXMLStreamReader(new StringReader(XML)); + sr.nextTag(); + XmlTokenStream tokens = new XmlTokenStream(sr, XML, FromXmlParser.Feature.collectDefaults()); + + assertEquals(XmlTokenStream.XML_START_ELEMENT, tokens.getCurrentToken()); + assertEquals("root", tokens.getLocalName()); + + assertEquals(XmlTokenStream.XML_TEXT, tokens.next()); + assertEquals("first", tokens.getText()); + + assertEquals(XmlTokenStream.XML_START_ELEMENT, tokens.next()); + assertEquals("a", tokens.getLocalName()); + assertEquals(XmlTokenStream.XML_TEXT, tokens.next()); + assertEquals("123", tokens.getText()); + assertEquals(XmlTokenStream.XML_END_ELEMENT, tokens.next()); + + assertEquals(XmlTokenStream.XML_TEXT, tokens.next()); + assertEquals(" and second ", tokens.getText()); + + assertEquals(XmlTokenStream.XML_START_ELEMENT, tokens.next()); + assertEquals("b", tokens.getLocalName()); + assertEquals(XmlTokenStream.XML_TEXT, tokens.next()); + assertEquals("abc", tokens.getText()); + assertEquals(XmlTokenStream.XML_END_ELEMENT, tokens.next()); + + assertEquals(XmlTokenStream.XML_TEXT, tokens.next()); + assertEquals("last & final", tokens.getText()); + + assertEquals(XmlTokenStream.XML_END_ELEMENT, tokens.next()); + assertEquals(XmlTokenStream.XML_END, tokens.next()); + sr.close(); + } + */ }