Search in sources :

Example 26 with OMXMLParserWrapper

use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.

the class TestCloseWithStream method runTest.

@Override
protected void runTest() throws Throwable {
    InstrumentedStream in = streamType.instrumentStream(streamType.getStream(XMLSample.SIMPLE));
    try {
        OMXMLParserWrapper builder = streamType.getAdapter(StreamTypeAdapter.class).createOMBuilder(metaFactory.getOMFactory(), in);
        builder.getDocument().build();
        builder.close();
        // OMXMLParserWrapper#close() does _not_ close the underlying input stream
        assertFalse(in.isClosed());
    } finally {
        in.close();
    }
}
Also used : StreamTypeAdapter(org.apache.axiom.ts.StreamTypeAdapter) InstrumentedStream(org.apache.axiom.testutils.io.InstrumentedStream) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 27 with OMXMLParserWrapper

use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.

the class TestCreateOMBuilderFromSAXSource method runTest.

@Override
protected void runTest() throws Throwable {
    SAXParserFactory factory = implementation.newSAXParserFactory();
    factory.setNamespaceAware(true);
    factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
    SAXParser parser = factory.newSAXParser();
    SAXSource source = new SAXSource(parser.getXMLReader(), new InputSource(file.getUrl().toString()));
    OMXMLParserWrapper builder;
    if (expandEntityReferences == null) {
        builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), source);
    } else {
        builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), source, expandEntityReferences.booleanValue());
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    builder.getDocument().serialize(baos);
    InputSource actual = new InputSource();
    actual.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
    actual.setSystemId(file.getUrl().toString());
    assertAbout(xml()).that(actual).ignoringWhitespaceInPrologAndEpilog().expandingEntityReferences(expandEntityReferences == null ? false : expandEntityReferences.booleanValue()).hasSameContentAs(DOMImplementation.XERCES.parse(new InputSource(file.getUrl().toString()), expandEntityReferences == null || expandEntityReferences));
}
Also used : InputSource(org.xml.sax.InputSource) SAXSource(javax.xml.transform.sax.SAXSource) ByteArrayInputStream(java.io.ByteArrayInputStream) SAXParser(javax.xml.parsers.SAXParser) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 28 with OMXMLParserWrapper

use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.

the class TestCreateOMBuilderFromDOM method runTest.

@Override
protected void runTest() throws Throwable {
    // We never expand entity references during parsing, but we may do this later when
    // converting DOM to OM.
    Document document = loadDocument(false);
    OMXMLParserWrapper builder;
    if (expandEntityReferences == null) {
        builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), new DOMSource(document));
    } else {
        builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), document, expandEntityReferences.booleanValue());
    }
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    builder.getDocument().serialize(baos);
    InputSource actual = new InputSource();
    actual.setByteStream(new ByteArrayInputStream(baos.toByteArray()));
    actual.setSystemId(file.getUrl().toString());
    assertAbout(xml()).that(actual).ignoringWhitespaceInPrologAndEpilog().treatingElementContentWhitespaceAsText(!implementation.isDOM3()).hasSameContentAs(loadDocument(expandEntityReferences == null || expandEntityReferences));
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) InputSource(org.xml.sax.InputSource) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(org.w3c.dom.Document) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 29 with OMXMLParserWrapper

use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.

the class TestGetXMLStreamReaderWithCaching method runTest.

@Override
protected void runTest() throws Throwable {
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), TestGetChildElementsConsumed.class.getResourceAsStream("purchase-order.xml"));
    OMElement documentElement = builder.getDocumentElement();
    XMLStreamReader reader = documentElement.getXMLStreamReader();
    //tree to be fully built
    while (reader.hasNext()) {
        reader.next();
    }
    //try to find the children of the document element. This should *NOT* produce an
    //error even when the underlying stream is fully consumed , the object tree is already complete
    Iterator<OMElement> childElements = documentElement.getChildElements();
    int count = 0;
    while (childElements.hasNext()) {
        childElements.next();
        count++;
    }
    assertEquals("Number of elements need to be 2", count, 2);
    documentElement.close(false);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) OMElement(org.apache.axiom.om.OMElement) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 30 with OMXMLParserWrapper

use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.

the class TestSerializeAndConsumeConsumed method runTest.

@Override
protected void runTest() throws Throwable {
    OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), TestGetChildElementsConsumed.class.getResourceAsStream("purchase-order.xml"));
    OMElement documentElement = builder.getDocumentElement();
    XMLStreamReader reader = documentElement.getXMLStreamReaderWithoutCaching();
    //building the tree
    while (reader.hasNext()) {
        reader.next();
    }
    //error since the underlying stream is fully consumed without building the object tree
    try {
        documentElement.serializeAndConsume(StAX.createNullXMLStreamWriter());
        fail("Expected NodeUnavailableException");
    } catch (NodeUnavailableException e) {
    //wea re cool
    }
    documentElement.close(false);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) NodeUnavailableException(org.apache.axiom.om.NodeUnavailableException) OMElement(org.apache.axiom.om.OMElement) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Aggregations

OMXMLParserWrapper (org.apache.axiom.om.OMXMLParserWrapper)37 OMElement (org.apache.axiom.om.OMElement)21 StringReader (java.io.StringReader)12 XMLStreamReader (javax.xml.stream.XMLStreamReader)8 OMFactory (org.apache.axiom.om.OMFactory)8 InputSource (org.xml.sax.InputSource)8 OMNode (org.apache.axiom.om.OMNode)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)4 InputStream (java.io.InputStream)4 QName (javax.xml.namespace.QName)3 OMAttribute (org.apache.axiom.om.OMAttribute)3 OMDocument (org.apache.axiom.om.OMDocument)3 OMOutputFormat (org.apache.axiom.om.OMOutputFormat)3 SOAPEnvelope (org.apache.axiom.soap.SOAPEnvelope)3 DOMSource (javax.xml.transform.dom.DOMSource)2 SAXSource (javax.xml.transform.sax.SAXSource)2 NodeUnavailableException (org.apache.axiom.om.NodeUnavailableException)2 OMException (org.apache.axiom.om.OMException)2 OMNamespace (org.apache.axiom.om.OMNamespace)2