Search in sources :

Example 76 with XMLStreamReader

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

the class TestCloseWithXMLStreamReader method runTest.

@Override
protected void runTest() throws Throwable {
    InputStream in = XMLSample.SIMPLE.getInputStream();
    try {
        XMLStreamReader reader = StAXUtils.createXMLStreamReader(in);
        OMXMLParserWrapper builder = OMXMLBuilderFactory.createStAXOMBuilder(metaFactory.getOMFactory(), reader);
        WeakReference<XMLStreamReader> readerWeakRef = new WeakReference<XMLStreamReader>(reader);
        reader = null;
        builder.getDocument().build();
        builder.close();
        for (int i = 0; i < 10; i++) {
            Thread.sleep(500);
            if (readerWeakRef.get() == null) {
                return;
            }
            System.gc();
        }
        fail("Builder didn't release reference to the underlying parser");
    } finally {
        in.close();
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStream(java.io.InputStream) WeakReference(java.lang.ref.WeakReference) OMXMLParserWrapper(org.apache.axiom.om.OMXMLParserWrapper)

Example 77 with XMLStreamReader

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

the class WrappedTextNodeStreamReaderTest method testUsingBuilder.

//
// Tests that construct the Axiom tree and check the result
//
private void testUsingBuilder(QName wrapperElementName, String testString, int chunkSize) {
    StringReader reader = new StringReader(testString);
    XMLStreamReader xmlStreamReader = new WrappedTextNodeStreamReader(wrapperElementName, reader, chunkSize);
    OMElement element = OMXMLBuilderFactory.createStAXOMBuilder(xmlStreamReader).getDocumentElement();
    assertEquals(wrapperElementName, element.getQName());
    assertEquals(wrapperElementName.getPrefix(), element.getQName().getPrefix());
    assertEquals(testString, element.getText());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) OMElement(org.apache.axiom.om.OMElement) WrappedTextNodeStreamReader(org.apache.axiom.util.stax.WrappedTextNodeStreamReader)

Example 78 with XMLStreamReader

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

the class StAXTraverserTest method testFragment.

@Test
public void testFragment() throws Exception {
    XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader("<root><a><b/></a></root>"));
    reader.next();
    reader.next();
    Traverser t = new StAXTraverser(reader);
    assertThat(t.next()).isEqualTo(Event.START_ELEMENT);
    assertThat(t.getQName()).isEqualTo(new QName("a"));
    assertThat(t.next()).isEqualTo(Event.START_ELEMENT);
    assertThat(t.getQName()).isEqualTo(new QName("b"));
    assertThat(t.next()).isEqualTo(Event.END_ELEMENT);
    assertThat(t.next()).isEqualTo(Event.END_ELEMENT);
    assertThat(t.next()).isNull();
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) QName(javax.xml.namespace.QName) Traverser(org.apache.axiom.truth.xml.spi.Traverser) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 79 with XMLStreamReader

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

the class TestBlobOMDataSource method runTest.

@Override
protected void runTest() throws Throwable {
    SOAPEnvelope soapEnvelope = soapFactory.createSOAPEnvelope();
    SOAPHeader soapHeader = soapFactory.createSOAPHeader(soapEnvelope);
    String localName = "myPayload";
    String encoding = "utf-8";
    String payload = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
    OMNamespace ns = soapFactory.createOMNamespace("urn://test", "tns");
    BlobOMDataSource ds = new BlobOMDataSource(Blobs.createBlob(payload.getBytes(encoding)), encoding);
    // Set an empty MustUnderstand property on the data source
    ds.setProperty(SOAPHeaderBlock.MUST_UNDERSTAND_PROPERTY, null);
    OMSourcedElement omse = soapFactory.createSOAPHeaderBlock(localName, ns, ds);
    soapHeader.addChild(omse);
    OMNode firstChild = soapHeader.getFirstOMChild();
    assertTrue("Expected OMSourcedElement child", firstChild instanceof SOAPHeaderBlock);
    SOAPHeaderBlock child = (SOAPHeaderBlock) firstChild;
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    assertThat(child.getDataSource()).isSameAs(ds);
    // Make sure that getting the MustUnderstand property does not cause expansion.
    assertTrue(!child.getMustUnderstand());
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    assertThat(child.getDataSource()).isSameAs(ds);
    // A BlobOMDataSource does not consume the backing object when read.
    // Thus getting the XMLStreamReader of the BlobOMDataSource should not 
    // cause expansion of the OMSourcedElement.
    XMLStreamReader reader = child.getXMLStreamReader();
    reader.next();
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Likewise, a BlobOMDataSource does not consume the backing object when 
    // written.  Thus serializing the OMSourcedElement should not cause the expansion
    // of the OMSourcedElement.
    assertTrue("The payload was not present in the output", soapHeader.toString().indexOf(payload) > 0);
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    assertThat(child.getDataSource()).isSameAs(ds);
}
Also used : OMNode(org.apache.axiom.om.OMNode) OMNamespace(org.apache.axiom.om.OMNamespace) XMLStreamReader(javax.xml.stream.XMLStreamReader) BlobOMDataSource(org.apache.axiom.om.ds.BlobOMDataSource) SOAPHeaderBlock(org.apache.axiom.soap.SOAPHeaderBlock) SOAPEnvelope(org.apache.axiom.soap.SOAPEnvelope) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) SOAPHeader(org.apache.axiom.soap.SOAPHeader)

Example 80 with XMLStreamReader

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

the class BEAInputFactoryWrapper method createXMLStreamReader.

@Override
public XMLStreamReader createXMLStreamReader(String systemId, InputStream stream) throws XMLStreamException {
    // The getEncoding() method of the stream reader produced by the reference implementation
    // doesn't return complete information about the effective encoding. To work around this,
    // we need to implement the detection algorithm described in Appendix F.1 of the
    // XML 1.0 specifications (Fifth Edition). Note that the encoding determined here may be
    // overridden by the XML encoding declaration, if present in the XML document. This
    // information is already available from the stream reader, so that we don't need to
    // reimplement this part.
    // TODO: this needs some more unit testing!
    EncodingDetectionHelper helper = new EncodingDetectionHelper(stream);
    stream = helper.getInputStream();
    String encoding = helper.detectEncoding();
    XMLStreamReader reader;
    if (systemId == null) {
        reader = super.createXMLStreamReader(stream);
    } else {
        reader = super.createXMLStreamReader(systemId, stream);
    }
    return new BEAStreamReaderWrapper(reader, encoding);
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader)

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