use of org.apache.axiom.om.ds.custombuilder.CustomBuilder 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.ds.custombuilder.CustomBuilder in project webservices-axiom by apache.
the class FirstElementNameWithParserTestCase method runTest.
@Override
protected final void runTest() throws Throwable {
SOAPEnvelope orgEnvelope = soapFactory.getDefaultEnvelope();
orgEnvelope.getBody().addChild(soapFactory.createOMElement(qname.getLocalPart(), qname.getNamespaceURI(), qname.getPrefix()));
SOAPModelBuilder builder = OMXMLBuilderFactory.createSOAPModelBuilder(metaFactory, new StringReader(orgEnvelope.toString()));
SOAPBody body = builder.getSOAPEnvelope().getBody();
runTest(body);
if (supportsOptimization) {
// The expectation is that even after looking at the payload element name, registering
// a custom builder still transforms the element.
((CustomBuilderSupport) builder).registerCustomBuilder(CustomBuilder.Selector.PAYLOAD, new CustomBuilder() {
@Override
public OMDataSource create(OMElement element) throws OMException {
try {
element.getXMLStreamReaderWithoutCaching().close();
} catch (XMLStreamException ex) {
throw new OMException(ex);
}
return new AbstractPushOMDataSource() {
@Override
public void serialize(XMLStreamWriter xmlWriter) throws XMLStreamException {
xmlWriter.writeEmptyElement(qname.getPrefix(), qname.getLocalPart(), qname.getNamespaceURI());
}
@Override
public boolean isDestructiveWrite() {
return false;
}
};
}
});
assertThat(body.getFirstElement()).isInstanceOf(OMSourcedElement.class);
}
}
Aggregations