Search in sources :

Example 86 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.

the class TestGetDataHandlerFromElement method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    RandomDataSource orgDS = new RandomDataSource(64 * 1024);
    OMElement orgRoot = factory.createOMElement(new QName("root"));
    OMElement orgChild = factory.createOMElement(new QName("child"), orgRoot);
    orgChild.addChild(factory.createOMText(new DataHandler(orgDS), false));
    OMElement root = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader(orgRoot.toString())).getDocumentElement();
    XMLStreamReader reader = root.getXMLStreamReader(cache);
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    DataSource ds = XMLStreamReaderUtils.getDataHandlerFromElement(reader).getDataSource();
    IOTestUtils.compareStreams(orgDS.getInputStream(), ds.getInputStream());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) DataHandler(javax.activation.DataHandler) RandomDataSource(org.apache.axiom.testutils.activation.RandomDataSource) DataSource(javax.activation.DataSource)

Example 87 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.

the class TestGetElementTextFromParser method runTest.

@Override
protected void runTest() throws Throwable {
    // Note: We test getElementText on a child element ("b") of the element from which we request
    //       the XMLStreamReader ("a"). This is to make sure that the XMLStreamReader implementation actually
    //       delegates to the underlying parser (which is not necessarily the case on "a").
    OMXMLParserWrapper builder = builderFactory.getBuilder(metaFactory, new InputSource(new StringReader("<a><b>AB<!--comment text-->CD</b></a>")));
    OMElement element = builder.getDocumentElement();
    // Build a certain number of descendants. This is used to test scenarios where the XMLStreamReader
    // needs to switch to pull through mode in the middle of the element from which we attempt to
    // get the text.
    Iterator<OMNode> it = element.getDescendants(true);
    for (int i = 0; i < build; i++) {
        it.next();
    }
    XMLStreamReader reader = element.getXMLStreamReader(cache);
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals("ABCD", reader.getElementText());
}
Also used : OMNode(org.apache.axiom.om.OMNode) InputSource(org.xml.sax.InputSource) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 88 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.

the class TestGetNamespaceContext method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement element = AXIOMUtil.stringToOM(metaFactory.getOMFactory(), "<a xmlns='urn:ns1' xmlns:ns2='urn:ns2'><b xmlns:ns3='urn:ns3'/></a>");
    XMLStreamReader stream = cache ? element.getXMLStreamReader() : element.getXMLStreamReaderWithoutCaching();
    stream.next();
    assertEquals(XMLStreamReader.START_ELEMENT, stream.next());
    assertEquals("b", stream.getLocalName());
    NamespaceContext context = stream.getNamespaceContext();
    assertEquals("urn:ns1", context.getNamespaceURI(""));
    assertEquals("urn:ns2", context.getNamespaceURI("ns2"));
    assertEquals("urn:ns3", context.getNamespaceURI("ns3"));
    assertEquals("ns2", context.getPrefix("urn:ns2"));
    element.close(false);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) NamespaceContext(javax.xml.namespace.NamespaceContext) OMElement(org.apache.axiom.om.OMElement)

Example 89 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project webservices-axiom by apache.

the class TestCloseAndContinueBuilding method runTest.

@Override
protected void runTest() throws Throwable {
    OMElement root = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), new StringReader("<root><a><b>some text</b></a><c>content</c></root>")).getDocumentElement();
    OMElement a = (OMElement) root.getFirstOMChild();
    XMLStreamReader reader = a.getXMLStreamReader(cache);
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
    assertEquals(new QName("b"), reader.getName());
    reader.close();
    assertFalse(root.isComplete());
    OMElement c = (OMElement) a.getNextOMSibling();
    assertEquals("content", c.getText());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement)

Example 90 with XMLStreamReader

use of javax.xml.stream.XMLStreamReader in project tomee by apache.

the class Sxc method prepareReader.

public static XMLStreamReader prepareReader(final InputStream inputStream) throws XMLStreamException {
    final Source source = new StreamSource(inputStream);
    final XMLStreamReader streamReader = getXmlInputFactory().createXMLStreamReader(source);
    return new JavaeeNamespaceFilter(streamReader);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) XoXMLStreamReader(org.metatype.sxc.util.XoXMLStreamReader) StreamSource(javax.xml.transform.stream.StreamSource) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source)

Aggregations

XMLStreamReader (javax.xml.stream.XMLStreamReader)243 XMLInputFactory (javax.xml.stream.XMLInputFactory)98 StringReader (java.io.StringReader)85 XMLStreamException (javax.xml.stream.XMLStreamException)78 InputStream (java.io.InputStream)61 IOException (java.io.IOException)43 OMElement (org.apache.axiom.om.OMElement)37 ByteArrayInputStream (java.io.ByteArrayInputStream)27 Test (org.junit.Test)25 JAXBException (javax.xml.bind.JAXBException)16 QName (javax.xml.namespace.QName)16 StAXSource (javax.xml.transform.stax.StAXSource)16 StreamSource (javax.xml.transform.stream.StreamSource)16 FileInputStream (java.io.FileInputStream)14 OMFactory (org.apache.axiom.om.OMFactory)14 Unmarshaller (javax.xml.bind.Unmarshaller)13 InputStreamReader (java.io.InputStreamReader)12 DeploymentUnitProcessingException (org.jboss.as.server.deployment.DeploymentUnitProcessingException)12 Source (javax.xml.transform.Source)11 InputSource (org.xml.sax.InputSource)11