Author: veithen Date: Sun Apr 25 19:06:27 2010 New Revision: 937856 URL: http://svn.apache.org/viewvc?rev=937856&view=rev Log: WSCOMMONS-452: Moved some code that was intended for inclusion into Axiom.
Added: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/TextFromElementReader.java - copied, changed from r909787, axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/format/TextFromElementReader.java webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/TextFromElementReaderTest.java - copied, changed from r909787, axis/axis2/java/transports/trunk/modules/base/src/test/java/org/apache/axis2/format/TextFromElementReaderTest.java Modified: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/om/util/ElementHelper.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=937856&r1=937855&r2=937856&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 Sun Apr 25 19:06:27 2010 @@ -25,16 +25,24 @@ import org.apache.axiom.om.OMException; import org.apache.axiom.om.OMFactory; import org.apache.axiom.om.OMNamespace; import org.apache.axiom.om.OMNode; +import org.apache.axiom.om.OMSourcedElement; +import org.apache.axiom.om.OMText; import org.apache.axiom.om.ds.ByteArrayDataSource; 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 javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.Reader; +import java.io.StringReader; import java.io.UnsupportedEncodingException; +import java.io.Writer; import java.net.URLDecoder; import java.util.Iterator; @@ -219,4 +227,74 @@ public class ElementHelper { return block; } + + /** + * Returns a stream representing the concatenation of the text nodes that are children of a + * given element. + * The stream returned by this method produces exactly the same character sequence as the + * the stream created by the following expression: + * <pre>new StringReader(element.getText())</pre> + * The difference is that the stream implementation returned by this method is guaranteed + * to have constant memory usage and is optimized for performance. + * + * @param element the element to read the text nodes from + * @param cache whether to enable caching when accessing the element + * @return a stream representing the concatenation of the text nodes + * + * @see OMElement#getText() + */ + public static Reader getTextAsStream(OMElement element, boolean cache) { + // If the element is not an OMSourcedElement and has not more than one child, then the most + // efficient way to get the Reader is to build a StringReader + if (!(element instanceof OMSourcedElement) && (!cache || element.isComplete())) { + OMNode child = element.getFirstOMChild(); + if (child == null) { + return new StringReader(""); + } else if (child.getNextOMSibling() == null) { + return new StringReader(child instanceof OMText ? ((OMText)child).getText() : ""); + } + } + // In all other cases, extract the data from the XMLStreamReader + return new TextFromElementReader(cache ? element.getXMLStreamReader() + : element.getXMLStreamReaderWithoutCaching()); + } + + /** + * Write the content of the text nodes that are children of a given element to a + * {...@link Writer}. + * If <code>cache</code> is true, this method has the same effect as the following instruction: + * <pre>out.write(element.getText())</pre> + * The difference is that this method is guaranteed to have constant memory usage and is + * optimized for performance. + * + * @param element the element to read the text nodes from + * @param out the stream to write the content to + * @param cache whether to enable caching when accessing the element + * @throws XMLStreamException if an error occurs when reading from the element + * @throws IOException if an error occurs when writing to the stream + * + * @see OMElement#getText() + */ + public static void writeTextTo(OMElement element, Writer out, boolean cache) + throws XMLStreamException, IOException { + + XMLStreamReader reader = cache ? element.getXMLStreamReader() + : element.getXMLStreamReaderWithoutCaching(); + int depth = 0; + while (reader.hasNext()) { + switch (reader.next()) { + case XMLStreamReader.CHARACTERS: + case XMLStreamReader.CDATA: + if (depth == 1) { + out.write(reader.getText()); + } + break; + case XMLStreamReader.START_ELEMENT: + depth++; + break; + case XMLStreamReader.END_ELEMENT: + depth--; + } + } + } } Copied: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/TextFromElementReader.java (from r909787, axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/format/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?p2=webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/TextFromElementReader.java&p1=axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/format/TextFromElementReader.java&r1=909787&r2=937856&rev=937856&view=diff ============================================================================== --- axis/axis2/java/transports/trunk/modules/base/src/main/java/org/apache/axis2/format/TextFromElementReader.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/util/stax/TextFromElementReader.java Sun Apr 25 19:06:27 2010 @@ -1,23 +1,23 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ -package org.apache.axis2.format; +package org.apache.axiom.util.stax; import java.io.IOException; import java.io.Reader; @@ -44,8 +44,6 @@ import org.apache.axiom.om.OMException; * Note that this class should in general not be used directly. Instead, * {...@link ElementHelper#getTextAsStream(org.apache.axiom.om.OMElement)} * should be called to get the most efficient stream implementation for a given an element. - * <p> - * NOTICE: The code in this class will be moved to Axiom (or somewhere else). Use with care! */ public class TextFromElementReader extends Reader { private final XMLStreamReader stream; @@ -88,7 +86,6 @@ public class TextFromElementReader exten } } - @Override public int read(char[] cbuf, int off, int len) throws IOException { if (endOfStream) { return -1; @@ -145,7 +142,6 @@ public class TextFromElementReader exten } } - @Override public void close() throws IOException { if (!endOfStream) { try { Copied: webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/TextFromElementReaderTest.java (from r909787, axis/axis2/java/transports/trunk/modules/base/src/test/java/org/apache/axis2/format/TextFromElementReaderTest.java) URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/TextFromElementReaderTest.java?p2=webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/TextFromElementReaderTest.java&p1=axis/axis2/java/transports/trunk/modules/base/src/test/java/org/apache/axis2/format/TextFromElementReaderTest.java&r1=909787&r2=937856&rev=937856&view=diff ============================================================================== --- axis/axis2/java/transports/trunk/modules/base/src/test/java/org/apache/axis2/format/TextFromElementReaderTest.java (original) +++ webservices/commons/trunk/modules/axiom/modules/axiom-api/src/test/java/org/apache/axiom/util/stax/TextFromElementReaderTest.java Sun Apr 25 19:06:27 2010 @@ -1,23 +1,23 @@ /* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. */ -package org.apache.axis2.format; +package org.apache.axiom.util.stax; import java.io.Reader; import java.io.StringReader; @@ -27,6 +27,7 @@ import javax.xml.stream.XMLStreamReader; import junit.framework.TestCase; import org.apache.axiom.om.util.StAXUtils; +import org.apache.axiom.util.stax.TextFromElementReader; import org.apache.commons.io.IOUtils; // Note: this class only contains a single test method because most aspects of TextFromElementReader