Author: veithen
Date: Mon May 24 16:50:08 2010
New Revision: 947712
URL: http://svn.apache.org/viewvc?rev=947712&view=rev
Log:
Refactored the TextFromElementReader class and related code so that it can be
used in a wider range of use cases.
Removed:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/TextFromElementReaderTest.java
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/TextFromElementReader.java
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/XMLStreamReaderUtilsTest.java
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/ElementHelperTest.java
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java?rev=947712&r1=947711&r2=947712&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.java
Mon May 24 16:50:08 2010
@@ -31,7 +31,7 @@ import org.apache.axiom.om.ds.ByteArrayD
import org.apache.axiom.om.impl.builder.StAXOMBuilder;
import org.apache.axiom.soap.SOAPFactory;
import org.apache.axiom.soap.SOAPHeaderBlock;
-import org.apache.axiom.util.stax.TextFromElementReader;
+import org.apache.axiom.util.stax.XMLStreamReaderUtils;
import org.apache.axiom.util.stax.xop.XOPUtils;
import javax.xml.namespace.QName;
@@ -243,8 +243,11 @@ public class ElementHelper {
}
// In all other cases, extract the data from the XMLStreamReader
try {
- return new TextFromElementReader(cache ?
element.getXMLStreamReader()
- : element.getXMLStreamReaderWithoutCaching());
+ XMLStreamReader reader = element.getXMLStreamReader(cache);
+ if (reader.getEventType() == XMLStreamReader.START_DOCUMENT) {
+ reader.next();
+ }
+ return XMLStreamReaderUtils.getElementTextAsStream(reader, true);
} catch (XMLStreamException ex) {
throw new OMException(ex);
}
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/TextFromElementReader.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/TextFromElementReader.java?rev=947712&r1=947711&r2=947712&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/TextFromElementReader.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/TextFromElementReader.java
Mon May 24 16:50:08 2010
@@ -25,8 +25,6 @@ import java.io.Reader;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
-import org.apache.axiom.om.OMException;
-
/**
* {...@link Reader} implementation that extracts the text nodes from an
element given by an
* {...@link XMLStreamReader}. The expected input is a document with only a
document
@@ -45,8 +43,10 @@ import org.apache.axiom.om.OMException;
* {...@link
org.apache.axiom.om.util.ElementHelper#getTextAsStream(org.apache.axiom.om.OMElement)}
* should be called to get the most efficient stream implementation for a
given an element.
*/
-public class TextFromElementReader extends Reader {
+// This class has package access -> use
XMLStreamReaderUtils#getElementTextAsStream
+class TextFromElementReader extends Reader {
private final XMLStreamReader stream;
+ private final boolean allowNonTextChildren;
/**
* Flag indicating that we have reached the end of the document and that
the underlying
@@ -66,25 +66,9 @@ public class TextFromElementReader exten
*/
private int sourceStart = -1;
- /**
- * Constructor.
- *
- * @param stream
- * the stream to extract the text nodes from
- * @throws IllegalStateException
- * if the stream doesn't start with the expected events
- * @throws XMLStreamException
- * if there was a parser error when attempting to position the
- * stream to the right event
- */
- public TextFromElementReader(XMLStreamReader stream) throws
XMLStreamException {
+ TextFromElementReader(XMLStreamReader stream, boolean
allowNonTextChildren) {
this.stream = stream;
- if (stream.getEventType() != XMLStreamReader.START_DOCUMENT) {
- throw new IllegalStateException("Expected START_DOCUMENT as first
event from parser");
- }
- if (stream.next() != XMLStreamReader.START_ELEMENT) {
- throw new IllegalStateException("Expected START_ELEMENT event");
- }
+ this.allowNonTextChildren = allowNonTextChildren;
}
public int read(char[] cbuf, int off, int len) throws IOException {
@@ -106,18 +90,16 @@ public class TextFromElementReader exten
}
break;
case XMLStreamReader.START_ELEMENT:
- skipDepth++;
+ if (allowNonTextChildren) {
+ skipDepth++;
+ } else {
+ throw new IOException("Unexpected
START_ELEMENT event");
+ }
break;
case XMLStreamReader.END_ELEMENT:
if (skipDepth == 0) {
- if (stream.next() ==
XMLStreamReader.END_DOCUMENT) {
- endOfStream = true;
- stream.close();
- return read == 0 ? -1 : read;
- } else {
- throw new IOException(
- "End of document expected
after element");
- }
+ endOfStream = true;
+ return read == 0 ? -1 : read;
} else {
skipDepth--;
}
@@ -136,22 +118,11 @@ public class TextFromElementReader exten
}
}
} catch (XMLStreamException ex) {
- IOException ex2 = new IOException("Got an exception from the
underlying parser " +
- "while reading the content of an element");
- ex2.initCause(ex);
- throw ex2;
+ throw new XMLStreamIOException(ex);
}
}
public void close() throws IOException {
- if (!endOfStream) {
- try {
- stream.close();
- } catch (XMLStreamException ex) {
- IOException ex2 = new IOException("Error when trying to close
underlying parser");
- ex2.initCause(ex);
- throw ex2;
- }
- }
+ // Do nothing
}
}
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java?rev=947712&r1=947711&r2=947712&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/XMLStreamReaderUtils.java
Mon May 24 16:50:08 2010
@@ -20,6 +20,7 @@
package org.apache.axiom.util.stax;
import java.io.IOException;
+import java.io.Reader;
import java.io.Writer;
import javax.activation.DataHandler;
@@ -149,6 +150,34 @@ public class XMLStreamReaderUtils {
}
/**
+ * Get the text content of the current element as a {...@link Reader}
object.
+ *
+ * @param reader
+ * The XML stream reader to read the element text from. The
reader must be positioned
+ * on a {...@link XMLStreamConstants#START_ELEMENT} event.
+ * @param allowNonTextChildren
+ * If set to <code>true</code>, non text child nodes are
allowed and skipped. If set
+ * to <code>false</code> only text nodes are allowed and the
presence of any other
+ * type of child node will trigger an exception.
+ * @return The reader from which the element text can be read. After the
reader has reported the
+ * end of the stream, the XML stream reader will be positioned on
the
+ * {...@link XMLStreamConstants#END_ELEMENT} event corresponding
to the initial
+ * {...@link XMLStreamConstants#START_ELEMENT} event. Calling
{...@link Reader#close()} on the
+ * returned reader has no effect. Any parser exception will be
reported by the reader
+ * using {...@link XMLStreamIOException}.
+ * @throws IllegalStateException
+ * if the XML stream reader is not positioned on a
+ * {...@link XMLStreamConstants#START_ELEMENT} event
+ */
+ public static Reader getElementTextAsStream(XMLStreamReader reader,
+ boolean allowNonTextChildren) {
+ if (reader.getEventType() != XMLStreamReader.START_ELEMENT) {
+ throw new IllegalStateException("Reader must be on a START_ELEMENT
event");
+ }
+ return new TextFromElementReader(reader, allowNonTextChildren);
+ }
+
+ /**
* Searches the wrapper and delegate classes to find the original
{...@link XMLStreamReader}.
* This method should only be used when a consumer of Axiom really needs
to
* access the original stream reader.
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/XMLStreamReaderUtilsTest.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/XMLStreamReaderUtilsTest.java?rev=947712&r1=947711&r2=947712&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/XMLStreamReaderUtilsTest.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/XMLStreamReaderUtilsTest.java
Mon May 24 16:50:08 2010
@@ -18,6 +18,8 @@
*/
package org.apache.axiom.util.stax;
+import java.io.IOException;
+import java.io.Reader;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Random;
@@ -185,4 +187,31 @@ public class XMLStreamReaderUtilsTest ex
reader.close();
}
}
+
+ public void testGetElementTextAsStream() throws Exception {
+ XMLStreamReader reader = StAXUtils.createXMLStreamReader(new
StringReader("<a>test</a>"));
+ reader.next();
+ Reader in = XMLStreamReaderUtils.getElementTextAsStream(reader, false);
+ assertEquals("test", IOUtils.toString(in));
+ assertEquals(XMLStreamReader.END_ELEMENT, reader.getEventType());
+ }
+
+ public void testGetElementTextAsStreamWithAllowedNonTextChildren() throws
Exception {
+ XMLStreamReader reader = StAXUtils.createXMLStreamReader(new
StringReader("<a>xxx<b>yyy</b>zzz</a>"));
+ reader.next();
+ Reader in = XMLStreamReaderUtils.getElementTextAsStream(reader, true);
+ assertEquals("xxxzzz", IOUtils.toString(in));
+ }
+
+ public void testGetElementTextAsStreamWithForbiddenNonTextChildren()
throws Exception {
+ XMLStreamReader reader = StAXUtils.createXMLStreamReader(new
StringReader("<a>xxx<b>yyy</b>zzz</a>"));
+ reader.next();
+ Reader in = XMLStreamReaderUtils.getElementTextAsStream(reader, false);
+ try {
+ IOUtils.toString(in);
+ fail("Expected exception");
+ } catch (IOException ex) {
+ // Expected
+ }
+ }
}
Modified:
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/ElementHelperTest.java
URL:
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/ElementHelperTest.java?rev=947712&r1=947711&r2=947712&view=diff
==============================================================================
---
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/ElementHelperTest.java
(original)
+++
webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/util/ElementHelperTest.java
Mon May 24 16:50:08 2010
@@ -48,7 +48,6 @@ import org.apache.axiom.om.util.ElementH
import org.apache.axiom.testutils.activation.RandomDataSource;
import org.apache.axiom.testutils.io.CharacterStreamComparator;
import org.apache.axiom.testutils.io.IOTestUtils;
-import org.apache.axiom.util.stax.TextFromElementReader;
import org.apache.commons.io.IOUtils;
public class ElementHelperTest extends TestCase {
@@ -63,7 +62,7 @@ public class ElementHelperTest extends T
public void testGetTextAsStreamWithNonTextChildren() throws Exception {
OMElement element = AXIOMUtil.stringToOM("<a>A<b>B</b>C</a>");
- Reader in = new TextFromElementReader(element.getXMLStreamReader());
+ Reader in = ElementHelper.getTextAsStream(element, true);
assertEquals(element.getText(), IOUtils.toString(in));
}