Search in sources :

Example 96 with XMLStreamReader

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

the class XMLStreamReaderUtilsTest method testGetElementTextAsStreamWithForbiddenNonTextChildren.

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
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) XMLStreamReader(javax.xml.stream.XMLStreamReader) IOException(java.io.IOException)

Example 97 with XMLStreamReader

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

the class TestCloseWithoutCaching method runTest.

@Override
protected void runTest() throws Throwable {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    Writer writer = new OutputStreamWriter(baos, "UTF-8");
    writer.write("<root><a>");
    for (int i = 0; i < 20000; i++) {
        writer.write('a');
    }
    writer.write("</a></root>");
    writer.close();
    InstrumentedInputStream in = new InstrumentedInputStream(new ByteArrayInputStream(baos.toByteArray()));
    OMDocument doc = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), in).getDocument();
    XMLStreamReader reader = doc.getXMLStreamReaderWithoutCaching();
    reader.next();
    reader.next();
    long count = in.getCount();
    reader.close();
    assertEquals(count, in.getCount());
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InstrumentedInputStream(org.apache.axiom.testutils.io.InstrumentedInputStream) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) OMDocument(org.apache.axiom.om.OMDocument)

Example 98 with XMLStreamReader

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

the class TestDTDReader method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMDocument document = factory.createOMDocument();
    factory.createOMDocType(document, "root", "-//MY//DTD", "my.dtd", "<!ELEMENT root (#PCDATA)>");
    factory.createOMElement("root", null, document);
    XMLStreamReader reader = document.getXMLStreamReader();
    // Note that according to the specification of the DTDReader interface, it is
    // allowed to look up the extension before reaching the DTD event.
    DTDReader dtdReader = (DTDReader) reader.getProperty(DTDReader.PROPERTY);
    assertNotNull(dtdReader);
    assertEquals(XMLStreamReader.DTD, reader.next());
    assertEquals("root", dtdReader.getRootName());
    assertEquals("-//MY//DTD", dtdReader.getPublicId());
    assertEquals("my.dtd", dtdReader.getSystemId());
    assertEquals("<!ELEMENT root (#PCDATA)>", reader.getText());
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) DTDReader(org.apache.axiom.ext.stax.DTDReader) OMDocument(org.apache.axiom.om.OMDocument)

Example 99 with XMLStreamReader

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

the class TestGetReaderException method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    OMSourcedElement element = factory.createOMElement(new AbstractPullOMDataSource() {

        @Override
        public XMLStreamReader getReader() throws XMLStreamException {
            throw new XMLStreamException("Test exception");
        }

        @Override
        public boolean isDestructiveRead() {
            return true;
        }
    });
    try {
        element.getLocalName();
        fail("Expected OMException");
    } catch (OMException ex) {
        Throwable cause = ex.getCause();
        assertTrue(cause instanceof XMLStreamException);
        assertEquals("Test exception", cause.getMessage());
    }
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) XMLStreamReader(javax.xml.stream.XMLStreamReader) XMLStreamException(javax.xml.stream.XMLStreamException) AbstractPullOMDataSource(org.apache.axiom.om.ds.AbstractPullOMDataSource) OMSourcedElement(org.apache.axiom.om.OMSourcedElement) OMException(org.apache.axiom.om.OMException)

Example 100 with XMLStreamReader

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

the class TestStringOMDataSource method runTest.

@Override
protected void runTest() throws Throwable {
    OMFactory factory = metaFactory.getOMFactory();
    String localName = "myPayload";
    String payload1 = "<tns:myPayload xmlns:tns=\"urn://test\">Payload One</tns:myPayload>";
    OMNamespace ns = factory.createOMNamespace("urn://test", "tns");
    StringOMDataSource somds = new StringOMDataSource(payload1);
    OMElement parent = factory.createOMElement("root", null);
    OMSourcedElement omse = factory.createOMElement(somds, localName, ns);
    parent.addChild(omse);
    OMNode firstChild = parent.getFirstOMChild();
    assertTrue("Expected OMSourcedElement child", firstChild instanceof OMSourcedElement);
    OMSourcedElement child = (OMSourcedElement) firstChild;
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    assertThat(child.getDataSource()).isInstanceOf(StringOMDataSource.class);
    // A StringOMDataSource does not consume the backing object when read.
    // Thus getting the XMLStreamReader of the StringOMDataSource should not 
    // cause expansion of the OMSourcedElement.
    XMLStreamReader reader = child.getXMLStreamReader();
    reader.next();
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Likewise, a StringOMDataSource does not consume the backing object when 
    // written.  Thus serializing the OMSourcedElement should not cause the expansion
    // of the OMSourcedElement.
    StringWriter out = new StringWriter();
    parent.serialize(out);
    //        System.out.println(output);
    assertTrue("The payload was not present in the output", out.toString().indexOf(payload1) > 0);
    assertTrue("OMSourcedElement is expanded.  This is unexpected", !child.isExpanded());
    // Test getting the raw content from the StringOMDataSource.
    StringOMDataSource ds = (StringOMDataSource) child.getDataSource();
    assertThat(ds.getObject()).isEqualTo(payload1);
    // Validate close
    ds.close();
    assertTrue("Close should free the resource", ds.getObject() == null);
}
Also used : OMFactory(org.apache.axiom.om.OMFactory) OMNode(org.apache.axiom.om.OMNode) OMNamespace(org.apache.axiom.om.OMNamespace) XMLStreamReader(javax.xml.stream.XMLStreamReader) StringWriter(java.io.StringWriter) StringOMDataSource(org.apache.axiom.om.ds.StringOMDataSource) OMElement(org.apache.axiom.om.OMElement) OMSourcedElement(org.apache.axiom.om.OMSourcedElement)

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