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
}
}
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());
}
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());
}
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());
}
}
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);
}
Aggregations