use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.
the class TestDetachWithSAXSource method runTest.
@Override
protected void runTest() throws Throwable {
DummyXMLReader xmlReader = new DummyXMLReader();
OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(metaFactory.getOMFactory(), new SAXSource(xmlReader, new InputSource()), false);
assertThat(xmlReader.isParsed()).isFalse();
builder.detach();
assertThat(xmlReader.isParsed()).isTrue();
}
use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.
the class TestGetDocumentElement method runTest.
@Override
protected void runTest() throws Throwable {
OMXMLParserWrapper builder = builderFactory.getBuilder(metaFactory, new InputSource(new StringReader("<!--comment1--><root/><!--comment2-->")));
OMElement element;
if (discardDocument == null) {
element = builder.getDocumentElement();
} else {
element = builder.getDocumentElement(discardDocument.booleanValue());
}
assertNotNull("Document element can not be null", element);
assertEquals("Name of the document element is wrong", "root", element.getLocalName());
if (Boolean.TRUE.equals(discardDocument)) {
if (builderFactory.isDeferredParsing()) {
assertFalse(element.isComplete());
}
assertNull(element.getParent());
// Note: we can't test getNextOMSibling here because this would build the element
assertNull(element.getPreviousOMSibling());
OMElement newParent = element.getOMFactory().createOMElement("newParent", null);
newParent.addChild(element);
if (builderFactory.isDeferredParsing()) {
assertFalse(element.isComplete());
assertFalse(builder.isCompleted());
}
assertAbout(xml()).that(xml(OMElement.class, newParent)).hasSameContentAs("<newParent><root/></newParent>");
assertTrue(element.isComplete());
// Since we discarded the document, the nodes in the epilog will not be accessible.
// Therefore we expect that when the document element changes its completion status,
// the builder will consume the epilog and change its completion status as well.
// This gives the underlying parser a chance to release some resources.
assertTrue(builder.isCompleted());
} else {
// The getDocumentElement doesn't detach the document element from the document:
assertSame(builder.getDocument(), element.getParent());
assertSame(builder.getDocument().getOMDocumentElement(), element);
assertTrue(element.getPreviousOMSibling() instanceof OMComment);
assertTrue(element.getNextOMSibling() instanceof OMComment);
}
}
use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.
the class TestGetDocumentElementWithDiscardDocumentIllFormedEpilog method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<root/> there shouldn't be text here!"));
OMElement element = builder.getDocumentElement(true);
try {
element.build();
fail("Expected OMException");
} catch (OMException ex) {
// Expected
}
}
use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.
the class TestRootPartStreaming method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
// Programmatically create the message
OMElement orgRoot = factory.createOMElement("root", null);
for (int i = 0; i < 10000; i++) {
factory.createOMElement("child", null, orgRoot).setText("Some text content");
}
// Serialize the message as XOP even if there will be no attachment parts
OMOutputFormat format = new OMOutputFormat();
format.setDoOptimize(true);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
orgRoot.serialize(baos, format);
// Parse the message and monitor the number of bytes read
InstrumentedInputStream in = new InstrumentedInputStream(new ByteArrayInputStream(baos.toByteArray()));
OMXMLParserWrapper builder = OMXMLBuilderFactory.createOMBuilder(factory, StAXParserConfiguration.DEFAULT, MultipartBody.builder().setInputStream(in).setContentType(format.getContentType()).build());
OMElement root = builder.getDocumentElement();
long count1 = in.getCount();
XMLStreamReader reader = root.getXMLStreamReader(false);
while (reader.hasNext()) {
reader.next();
}
long count2 = in.getCount();
// We expect that after requesting the document element, only a small part (corresponding to
// the size of the parser buffer) should have been read:
assertTrue(count1 < count2 / 2);
}
use of org.apache.axiom.om.OMXMLParserWrapper in project webservices-axiom by apache.
the class TestCommentEvent method runTest.
@Override
protected void runTest() throws Throwable {
OMXMLParserWrapper builder = builderFactory.getBuilder(metaFactory, new InputSource(new StringReader("<a><!--comment text--></a>")));
OMElement element = builder.getDocumentElement();
XMLStreamReader reader = element.getXMLStreamReader(cache);
assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
assertEquals(XMLStreamReader.COMMENT, reader.next());
assertEquals("comment text", reader.getText());
assertEquals("comment text", new String(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength()));
StringBuffer text = new StringBuffer();
char[] buf = new char[5];
for (int sourceStart = 0; ; sourceStart += buf.length) {
int nCopied = reader.getTextCharacters(sourceStart, buf, 0, buf.length);
text.append(buf, 0, nCopied);
if (nCopied < buf.length) {
break;
}
}
assertEquals("comment text", text.toString());
element.close(false);
}
Aggregations