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