use of org.apache.axiom.om.OMContainer in project webservices-axiom by apache.
the class AxiomTraverser method next.
@Override
public Event next() throws TraverserException {
if (node == null) {
if (root instanceof OMDocument) {
node = ((OMDocument) root).getFirstOMChild();
} else {
node = (OMElement) root;
}
} else if (!visited && node instanceof OMElement) {
OMNode firstChild = ((OMElement) node).getFirstOMChild();
if (firstChild != null) {
node = firstChild;
} else {
visited = true;
}
} else {
OMNode nextSibling = node.getNextOMSibling();
if (node == root) {
return null;
} else if (nextSibling != null) {
node = nextSibling;
visited = false;
} else {
OMContainer parent = node.getParent();
if (parent instanceof OMDocument) {
return null;
} else {
node = (OMElement) parent;
visited = true;
}
}
}
switch(node.getType()) {
case OMNode.DTD_NODE:
return Event.DOCUMENT_TYPE;
case OMNode.ELEMENT_NODE:
return visited ? Event.END_ELEMENT : Event.START_ELEMENT;
case OMNode.TEXT_NODE:
return Event.TEXT;
case OMNode.SPACE_NODE:
return Event.WHITESPACE;
case OMNode.ENTITY_REFERENCE_NODE:
if (expandEntityReferences) {
throw new UnsupportedOperationException();
} else {
return Event.ENTITY_REFERENCE;
}
case OMNode.COMMENT_NODE:
return Event.COMMENT;
case OMNode.CDATA_SECTION_NODE:
return Event.CDATA_SECTION;
case OMNode.PI_NODE:
return Event.PROCESSING_INSTRUCTION;
default:
throw new IllegalStateException();
}
}
use of org.apache.axiom.om.OMContainer in project webservices-axiom by apache.
the class TestAddChildWithIncompleteSibling method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMContainer container = containerFactory.create(factory);
container.addChild(OMXMLBuilderFactory.createOMBuilder(factory, new StringReader("<a>test</a>")).getDocumentElement(true));
assertThat(container.isComplete()).isFalse();
container.addChild(factory.createOMText("test"));
assertThat(container).hasNumberOfChildren(2);
}
use of org.apache.axiom.om.OMContainer in project webservices-axiom by apache.
the class TestRegisterCustomBuilder method runTest.
@Override
protected void runTest() throws Throwable {
SOAPModelBuilder builder = SOAPSampleSet.WSA.getMessage(spec).getAdapter(SOAPSampleAdapter.class).getBuilder(metaFactory);
((CustomBuilderSupport) builder).registerCustomBuilder(new CustomBuilder.Selector() {
@Override
public boolean accepts(OMContainer parent, int depth, String namespaceURI, String localName) {
return depth == 3 && namespaceURI.equals("http://www.w3.org/2005/08/addressing") && localName.equals("To");
}
}, new BlobOMDataSourceCustomBuilder(MemoryBlob.FACTORY, "utf-8"));
SOAPHeader header = builder.getSOAPEnvelope().getHeader();
ArrayList al = header.getHeaderBlocksWithNSURI("http://www.w3.org/2005/08/addressing");
assertEquals(al.size(), 4);
for (int i = 0; i < al.size(); i++) {
SOAPHeaderBlock shb = (SOAPHeaderBlock) al.get(i);
if ("To".equals(shb.getLocalName())) {
assertNotNull(shb.getDataSource());
}
}
}
use of org.apache.axiom.om.OMContainer in project webservices-axiom by apache.
the class PushOMDataSourceReader method proceed.
@Override
public boolean proceed() throws StreamException {
// TODO: we might want to unwrap the NamespaceRepairingFilter (and some other filters) here
XmlHandler handler = this.handler;
OMOutputFormat format = null;
XmlHandler current = handler;
while (current instanceof XmlHandlerWrapper) {
if (current instanceof XmlDeclarationRewriterHandler) {
format = ((XmlDeclarationRewriterHandler) current).getFormat();
break;
}
current = ((XmlHandlerWrapper) current).getParent();
}
if (format == null) {
// This is for the OMSourcedElement expansion case
format = new OMOutputFormat();
format.setDoOptimize(true);
handler = new PushOMDataSourceXOPHandler(handler);
}
try {
XMLStreamWriter writer = new XmlHandlerStreamWriter(handler, null, AxiomXMLStreamWriterExtensionFactory.INSTANCE);
// Seed the namespace context with the namespace context from the parent
OMContainer parent = root.getParent();
if (parent instanceof OMElement) {
for (Iterator<OMNamespace> it = ((OMElement) parent).getNamespacesInScope(); it.hasNext(); ) {
OMNamespace ns = it.next();
writer.setPrefix(ns.getPrefix(), ns.getNamespaceURI());
}
}
handler.startFragment();
dataSource.serialize(new MTOMXMLStreamWriterImpl(new PushOMDataSourceStreamWriter(writer), format));
handler.completed();
} catch (XMLStreamException ex) {
Throwable cause = ex.getCause();
if (cause instanceof StreamException) {
throw (StreamException) cause;
} else {
throw new StreamException(ex);
}
}
return true;
}
use of org.apache.axiom.om.OMContainer in project webservices-axiom by apache.
the class TestSerialize method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMSourcedElement element = TestDocument.DOCUMENT1.createOMSourcedElement(factory, push, destructive);
OMDataSource ds = element.getDataSource();
OMContainer parent = elementContext.wrap(element);
boolean parentComplete = parent != null && parent.isComplete();
expansionStrategy.apply(element);
boolean consuming = expansionStrategy.isConsumedAfterSerialization(push, destructive, serializationStrategy);
for (int iteration = 0; iteration < count; iteration++) {
boolean expectException = iteration != 0 && (consuming || serializeParent && !serializationStrategy.isCaching() && !parentComplete);
XML result;
try {
result = serializationStrategy.serialize(serializeParent ? parent : element);
if (expectException) {
fail("Expected exception");
}
} catch (Exception ex) {
if (!expectException) {
throw ex;
} else {
continue;
}
}
InputSource expectedXML = new InputSource(new StringReader(TestDocument.DOCUMENT1.getContent()));
if (serializeParent) {
expectedXML = elementContext.getControl(expectedXML);
}
assertAbout(xml()).that(result.getInputSource()).hasSameContentAs(expectedXML);
// the sourced element should be expanded.
if (expansionStrategy.isExpandedAfterSerialization(push, destructive, serializationStrategy)) {
assertTrue(element.isExpanded());
assertEquals("OMSourcedElement completion status", !consuming, element.isComplete());
} else {
assertFalse(element.isExpanded());
}
if (parent != null && !serializeParent) {
// Operations on the OMSourcedElement should have no impact on the parent
assertEquals("Parent completion status", parentComplete, parent.isComplete());
}
}
if (ds instanceof PullOMDataSource) {
assertFalse(((PullOMDataSource) ds).hasUnclosedReaders());
}
}
Aggregations