use of org.apache.axiom.om.OMDataSource in project webservices-axiom by apache.
the class TestGetDocumentFromBuilder method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMDataSource ds = new StringOMDataSource("<root><a/></root>");
OMSourcedElement element = factory.createOMElement(ds);
// Force expansion
element.getFirstOMChild();
OMXMLParserWrapper builder = element.getBuilder();
try {
builder.getDocument();
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException ex) {
// Expected
}
try {
builder.getDocumentElement();
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException ex) {
// Expected
}
}
use of org.apache.axiom.om.OMDataSource in project webservices-axiom by apache.
the class CustomBuilderManager method getAction.
private Runnable getAction(CoreNode node, int depth, int firstCustomBuilder) {
lastCandidateElement = null;
lastCandidateDepth = -1;
if (node instanceof AxiomElement && (node instanceof AxiomSOAPHeaderBlock || !(node instanceof AxiomSOAPElement))) {
final AxiomElement element = (AxiomElement) node;
if (registrations != null) {
for (int i = firstCustomBuilder; i < registrations.size(); i++) {
CustomBuilderRegistration registration = registrations.get(i);
final String namespaceURI = element.coreGetNamespaceURI();
final String localName = element.coreGetLocalName();
if (registration.getSelector().accepts(element.getParent(), depth, namespaceURI, localName)) {
final CustomBuilder customBuilder = registration.getCustomBuilder();
if (log.isDebugEnabled()) {
log.debug("Custom builder " + customBuilder + " accepted element {" + namespaceURI + "}" + localName + " at depth " + depth);
}
return new Runnable() {
@Override
public void run() {
if (log.isDebugEnabled()) {
log.debug("Invoking custom builder " + customBuilder);
}
OMDataSource dataSource = customBuilder.create(element);
Class<? extends AxiomSourcedElement> type;
if (element instanceof AxiomSOAP11HeaderBlock) {
type = AxiomSOAP11HeaderBlock.class;
} else if (element instanceof AxiomSOAP12HeaderBlock) {
type = AxiomSOAP12HeaderBlock.class;
} else {
type = AxiomSourcedElement.class;
}
if (log.isDebugEnabled()) {
log.debug("Replacing element with new sourced element of type " + type);
}
AxiomSourcedElement newElement = element.coreCreateNode(type);
newElement.init(localName, new OMNamespaceImpl(namespaceURI, null), dataSource);
try {
element.coreReplaceWith(newElement, AxiomSemantics.INSTANCE);
} catch (CoreModelException ex) {
throw AxiomExceptionTranslator.translate(ex);
}
}
};
}
}
}
// Save a reference to the element so that we can process it when another custom builder is registered
lastCandidateElement = element;
lastCandidateDepth = depth;
}
return null;
}
use of org.apache.axiom.om.OMDataSource in project webservices-axiom by apache.
the class StreamingOMSerializer method serializeNode.
protected void serializeNode(XMLStreamReader reader, XMLStreamWriter writer, boolean startAtNext) throws XMLStreamException {
// TODO We get the StAXWriter at this point and uses it hereafter
// assuming that this is the only entry point to this class.
// If there can be other classes calling methodes of this we might
// need to change methode signatures to OMOutputer
// If not startAtNext, then seed the processing with the current element.
boolean useCurrentEvent = !startAtNext;
while (reader.hasNext() || useCurrentEvent) {
int event = 0;
OMDataSource ds = null;
if (useCurrentEvent) {
event = reader.getEventType();
useCurrentEvent = false;
} else {
event = reader.next();
}
// DataSource
if (reader instanceof OMXMLStreamReaderEx) {
ds = ((OMXMLStreamReaderEx) reader).getDataSource();
}
if (ds != null) {
ds.serialize(writer);
} else {
switch(event) {
case START_ELEMENT:
serializeElement(reader, writer);
depth++;
break;
case ATTRIBUTE:
serializeAttributes(reader, writer);
break;
case CHARACTERS:
if (dataHandlerReader != null && dataHandlerReader.isBinary()) {
serializeDataHandler();
break;
}
// Fall through
case SPACE:
serializeText(reader, writer);
break;
case COMMENT:
serializeComment(reader, writer);
break;
case CDATA:
serializeCData(reader, writer);
break;
case PROCESSING_INSTRUCTION:
serializeProcessingInstruction(reader, writer);
break;
case END_ELEMENT:
serializeEndElement(writer);
depth--;
break;
case START_DOCUMENT:
//if a start document is found then increment the depth
depth++;
break;
case END_DOCUMENT:
//for the end document - reduce the depth
if (depth != 0)
depth--;
try {
serializeEndElement(writer);
} catch (Exception e) {
//TODO: log exceptions
}
break;
case DTD:
serializeDTD(reader, writer);
break;
case ENTITY_REFERENCE:
writer.writeEntityRef(reader.getLocalName());
}
}
if (depth == 0) {
break;
}
}
}
use of org.apache.axiom.om.OMDataSource in project webservices-axiom by apache.
the class TestCloneNonDestructive method runTest.
@Override
protected void runTest() throws Throwable {
OMFactory factory = metaFactory.getOMFactory();
OMDataSource ds = new WrappedTextNodeOMDataSourceFromDataSource(new QName("wrapper"), new ByteArrayDataSource("test".getBytes("utf-8")), Charset.forName("utf-8"));
OMSourcedElement element = factory.createOMElement(ds);
OMCloneOptions options = new OMCloneOptions();
options.setCopyOMDataSources(copyOMDataSources);
OMElement clone = (OMElement) element.clone(options);
if (copyOMDataSources) {
assertTrue(clone instanceof OMSourcedElement);
assertFalse(element.isExpanded());
} else {
assertFalse(clone instanceof OMSourcedElement);
assertTrue(clone.isComplete());
}
assertEquals("test", clone.getText());
assertEquals("wrapper", clone.getLocalName());
}
use of org.apache.axiom.om.OMDataSource in project webservices-axiom by apache.
the class TestCloneWithSourcedElement2 method runTest.
@Override
protected void runTest() throws Throwable {
SOAPEnvelope sourceEnv = soapFactory.getDefaultEnvelope();
SOAPBody body = sourceEnv.getBody();
SOAPHeader header = sourceEnv.getHeader();
// Create a header OMSE
OMDataSource dsHdr = new StringOMDataSource("<hdr:myheader xmlns:hdr=\"urn://test\">Hello World</hdr:myheader>");
OMNamespace hdrNS = header.getOMFactory().createOMNamespace("urn://test", "hdr");
SOAPFactory sf = (SOAPFactory) header.getOMFactory();
SOAPHeaderBlock shb = sf.createSOAPHeaderBlock("myheader", hdrNS, dsHdr);
// test setting processing flag
shb.setProcessed();
header.addChild(shb);
// Create a payload
OMDataSource ds = new StringOMDataSource("<tns:payload xmlns:tns=\"urn://test\">Hello World</tns:payload>");
OMNamespace ns = body.getOMFactory().createOMNamespace("urn://test", "tns");
OMSourcedElement omse = body.getOMFactory().createOMElement(ds, "payload", ns);
body.addChild(omse);
copyAndCheck(sourceEnv);
// The source SOAPHeaderBlock should not be expanded in the process
assertFalse(shb.isExpanded());
}
Aggregations