use of org.apache.axiom.om.OMAttribute in project ballerina by ballerina-lang.
the class BXMLItem method removeAttribute.
/**
* {@inheritDoc}
*/
@Override
public void removeAttribute(String qname) {
if (nodeType != XMLNodeType.ELEMENT || qname.isEmpty()) {
return;
}
OMElement omElement = (OMElement) omNode;
OMAttribute attribute = omElement.getAttribute(getQname(qname));
if (attribute == null) {
return;
}
omElement.removeAttribute(attribute);
}
use of org.apache.axiom.om.OMAttribute in project ballerina by ballerina-lang.
the class BXMLItem method setAttributes.
/**
* {@inheritDoc}
*/
@Override
public void setAttributes(BMap<String, ?> attributes) {
if (nodeType != XMLNodeType.ELEMENT || attributes == null) {
return;
}
// Remove existing attributes
OMElement omElement = ((OMElement) omNode);
Iterator<OMAttribute> attrIterator = omElement.getAllAttributes();
while (attrIterator.hasNext()) {
omElement.removeAttribute(attrIterator.next());
}
// Remove existing namespace declarations
Iterator<OMNamespace> namespaceIterator = omElement.getAllDeclaredNamespaces();
while (namespaceIterator.hasNext()) {
namespaceIterator.next();
namespaceIterator.remove();
}
String localName, uri;
Set<String> attributeQNames = attributes.keySet();
for (String qname : attributeQNames) {
if (qname.startsWith("{") && qname.indexOf('}') > 0) {
localName = qname.substring(qname.indexOf('}') + 1, qname.length());
uri = qname.substring(1, qname.indexOf('}'));
} else {
localName = qname;
uri = STRING_NULL_VALUE;
}
// Validate whether the attribute name is an XML supported qualified name,
// according to the XML recommendation.
XMLValidationUtils.validateXMLName(localName);
setAttribute(localName, uri, STRING_NULL_VALUE, attributes.get(qname).stringValue());
}
}
use of org.apache.axiom.om.OMAttribute in project ofbiz-framework by apache.
the class SOAPEventHandler method createAndSendSOAPResponse.
private void createAndSendSOAPResponse(Map<String, Object> serviceResults, String serviceName, HttpServletResponse response) throws EventHandlerException {
try {
// setup the response
if (Debug.verboseOn())
Debug.logVerbose("[EventHandler] : Setting up response message", module);
String xmlResults = SoapSerializer.serialize(serviceResults);
// Debug.logInfo("xmlResults ==================" + xmlResults, module);
XMLStreamReader reader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xmlResults));
StAXOMBuilder resultsBuilder = (StAXOMBuilder) OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), reader);
OMElement resultSer = resultsBuilder.getDocumentElement();
// create the response soap
SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope resEnv = factory.createSOAPEnvelope();
SOAPBody resBody = factory.createSOAPBody();
OMElement resService = factory.createOMElement(new QName(serviceName + "Response"));
resService.addChild(resultSer.getFirstElement());
resBody.addChild(resService);
resEnv.addChild(resBody);
// The declareDefaultNamespace method doesn't work see (https://issues.apache.org/jira/browse/AXIS2-3156)
// so the following doesn't work:
// resService.declareDefaultNamespace(ModelService.TNS);
// instead, create the xmlns attribute directly:
OMAttribute defaultNS = factory.createOMAttribute("xmlns", null, ModelService.TNS);
resService.addAttribute(defaultNS);
// log the response message
if (Debug.verboseOn()) {
try {
Debug.logInfo("Response Message:\n" + resEnv + "\n", module);
} catch (Throwable t) {
}
}
resEnv.serialize(response.getOutputStream());
response.getOutputStream().flush();
} catch (Exception e) {
Debug.logError(e, module);
throw new EventHandlerException(e.getMessage(), e);
}
}
use of org.apache.axiom.om.OMAttribute in project ofbiz-framework by apache.
the class SOAPEventHandler method sendError.
private void sendError(HttpServletResponse res, Object object, String serviceName) throws EventHandlerException {
try {
// setup the response
res.setContentType("text/xml");
String xmlResults = SoapSerializer.serialize(object);
XMLStreamReader xmlReader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(xmlResults));
StAXOMBuilder resultsBuilder = (StAXOMBuilder) OMXMLBuilderFactory.createStAXOMBuilder(OMAbstractFactory.getOMFactory(), xmlReader);
OMElement resultSer = resultsBuilder.getDocumentElement();
// create the response soap
SOAPFactory factory = OMAbstractFactory.getSOAP11Factory();
SOAPEnvelope resEnv = factory.createSOAPEnvelope();
SOAPBody resBody = factory.createSOAPBody();
OMElement errMsg = factory.createOMElement(new QName((serviceName != null ? serviceName : "") + "Response"));
errMsg.addChild(resultSer.getFirstElement());
resBody.addChild(errMsg);
resEnv.addChild(resBody);
// The declareDefaultNamespace method doesn't work see (https://issues.apache.org/jira/browse/AXIS2-3156)
// so the following doesn't work:
// resService.declareDefaultNamespace(ModelService.TNS);
// instead, create the xmlns attribute directly:
OMAttribute defaultNS = factory.createOMAttribute("xmlns", null, ModelService.TNS);
errMsg.addAttribute(defaultNS);
// log the response message
if (Debug.verboseOn()) {
try {
Debug.logInfo("Response Message:\n" + resEnv + "\n", module);
} catch (Throwable t) {
}
}
resEnv.serialize(res.getOutputStream());
res.getOutputStream().flush();
} catch (Exception e) {
throw new EventHandlerException(e.getMessage(), e);
}
}
use of org.apache.axiom.om.OMAttribute in project webservices-axiom by apache.
the class OMAttributeHelperTest method testDetachedElement.
public void testDetachedElement() {
OMNamespace top = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory().createOMNamespace("urn:test1", "t1");
OMElement ome = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory().createOMElement("test", top);
OMElement child = OMAbstractFactory.getMetaFactory(OMAbstractFactory.FEATURE_DOM).getOMFactory().createOMElement(new QName("test"), ome);
OMAttribute oma = child.addAttribute("attr", "value", top);
OMElement target = OMAbstractFactory.getOMFactory().createOMElement("test2", "urn:test2", "t2");
AttributeHelper.importOMAttribute(oma, target);
}
Aggregations