soapbodyimpl.java
来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 617 行 · 第 1/2 页
JAVA
617 行
* Creates a new <code>SOAPBodyElement</code> object with the specified name and adds it to this
* <code>SOAPBody</code> object.
*
* @param qname a <code>QName</code> object with the name for the new <code>SOAPBodyElement</code>
* object
* @return the new <code>SOAPBodyElement</code> object
* @throws SOAPException if a SOAP error occurs
*/
public SOAPBodyElement addBodyElement(QName qname) throws SOAPException {
return (SOAPBodyElement)addChildElement(qname);
}
public SOAPFault addFault(QName faultcode, String faultString) throws SOAPException {
return addFault(faultcode, faultString, null);
}
/**
* Creates a new <code>SOAPFault</code> object and adds it to this <code>SOAPBody</code> object.
* The new <code>SOAPFault</code> will have a <code>faultcode</code> element that is set to the
* <code>faultCode</code> parameter and a <code>faultstring</code> set to
* <code>faultstring</code> and localized to <code>locale</code>.
*
* @param faultCode a <code>QName</code> object giving the fault code to be
* @param faultString a <code>String</code> giving an explanation of the fault
* @param locale a <code>Locale</code> object indicating the native language of the
* <ocde>faultString</code>
* @return the new <code>SOAPFault</code> object
* @throws SOAPException if there is a SOAP error
*/
public SOAPFault addFault(QName faultCode, String faultString, Locale locale)
throws SOAPException {
SOAPFaultImpl faultImpl = null;
if (this.element.getOMFactory() instanceof SOAP11Factory) {
SOAP11FaultImpl fault = new SOAP11FaultImpl(omSOAPBody, new Exception(
faultString), (SOAPFactory)this.element.getOMFactory());
faultImpl = new SOAPFaultImpl(fault);
} else if (this.element.getOMFactory() instanceof SOAP12Factory) {
SOAP12FaultImpl fault = new SOAP12FaultImpl(omSOAPBody, new Exception(
faultString), (SOAPFactory)this.element.getOMFactory());
faultImpl = new SOAPFaultImpl(fault);
}
if (faultImpl != null) {
faultImpl.setFaultCode(faultCode);
if (locale != null) {
faultImpl.setFaultString(faultString, locale);
} else {
faultImpl.setFaultString(faultString);
}
}
return faultImpl;
}
/**
* Creates a new DOM org.w3c.dom.Document and sets the first child of this SOAPBody as its
* document element. The child SOAPElement is removed as part of the process.
*
* @return The org.w3c.dom.Document representation of the SOAPBody content.
* @throws SOAPException - if there is not exactly one child SOAPElement of the SOAPBody.
*/
public Document extractContentAsDocument() throws SOAPException {
Iterator childElements = this.getChildElements();
org.w3c.dom.Node domNode = null;
int childCount = 0;
while (childElements.hasNext()) {
domNode = (org.w3c.dom.Node)childElements.next();
childCount++;
if (childCount > 1) {
throw new SOAPException("SOAPBody contains more than one child element");
}
}
//The child SOAPElement is removed as part of the process
this.removeContents();
Document document;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
Element element =
document.createElementNS(domNode.getNamespaceURI(), domNode.getLocalName());
element.setNodeValue(domNode.getNodeValue());
document.appendChild(element);
} catch (ParserConfigurationException e) {
throw new SOAPException(e);
}
return document;
}
private javax.xml.soap.Node toSAAJNode(org.w3c.dom.Node node,
SOAPElement parent) throws SOAPException {
if (node == null) {
return null;
}
if (node instanceof org.w3c.dom.Text) {
org.w3c.dom.Text domText = (org.w3c.dom.Text)node;
return new TextImplEx(domText.getData(), parent);
}
if (node instanceof org.w3c.dom.Comment) {
org.w3c.dom.Comment domText = (org.w3c.dom.Comment)node;
return new TextImplEx("<!--" + domText.getData() + "-->", parent);
}
Element domEle = ((Element)node);
int indexOfColon = domEle.getTagName().indexOf(":");
NamespaceImpl ns;
String localName;
if (indexOfColon != -1) {
localName = domEle.getTagName().substring(indexOfColon + 1);
ns = new NamespaceImpl(domEle.getNamespaceURI(),
domEle.getTagName().substring(0, indexOfColon));
} else {
localName = domEle.getLocalName();
if (localName == null) { //it is possible that localname isn't set but name is set
localName = domEle.getTagName();
}
if (domEle.getNamespaceURI() != null) {
ns = new NamespaceImpl(domEle.getNamespaceURI(), domEle.getPrefix());
} else {
if (domEle.getPrefix() != null) {
ns = new NamespaceImpl("", domEle.getPrefix());
} else {
ns = new NamespaceImpl("", "");
}
}
}
ElementImpl eleImpl =
new ElementImpl((DocumentImpl)this.getOwnerDocument(),
localName, ns, this.element.getOMFactory());
SOAPElementImpl saajEle = new SOAPElementImpl(eleImpl);
saajEle.setParentElement(parent);
NamedNodeMap domAttrs = domEle.getAttributes();
for (int i = 0; i < domAttrs.getLength(); i++) {
org.w3c.dom.Node attrNode = domAttrs.item(i);
String attrLocalName = attrNode.getLocalName();
if (attrLocalName == null) {
attrLocalName = attrNode.getNodeName();
}
if (attrLocalName == null) {
//local part is required. "" is allowed to preserve compatibility with QName 1.0
attrLocalName = "";
}
saajEle.addAttribute(new PrefixedQName(attrNode.getNamespaceURI(),
attrLocalName,
attrNode.getPrefix()),
attrNode.getNodeValue());
}
NodeList childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node childSAAJNode = toSAAJNode(childNodes.item(i), saajEle);
if (childSAAJNode instanceof javax.xml.soap.Text) {
saajEle.addTextNode(childSAAJNode.getValue());
} else {
saajEle.addChildElement((javax.xml.soap.SOAPElement)childSAAJNode);
}
}
return saajEle;
}
public void detachNode() {
this.detach();
}
public OMNode detach() {
this.parentElement = null;
return this.element.detach();
}
public Iterator getChildElements(Name name) {
QName qName = new QName(name.getURI(), name.getLocalName());
return getChildren(element.getChildrenWithName(qName));
}
public SOAPElement addAttribute(QName qname, String value) throws SOAPException {
OMNamespace omNamespace = null;
SOAPFactory soapFactory;
if (this.element.getOMFactory() instanceof SOAP11Factory) {
soapFactory = new SOAP11Factory();
omNamespace = soapFactory.createOMNamespace(qname.getNamespaceURI(), qname.getPrefix());
} else if (this.element.getOMFactory() instanceof SOAP12Factory) {
soapFactory = new SOAP12Factory();
omNamespace = soapFactory.createOMNamespace(qname.getNamespaceURI(), qname.getPrefix());
}
this.element.addAttribute(qname.getLocalPart(), value, omNamespace);
return this;
}
public SOAPElement addChildElement(QName qname) throws SOAPException {
if (omSOAPBody.hasFault()) {
throw new SOAPException("A SOAPFault has been already added to this SOAPBody");
}
SOAPBodyElementImpl childEle;
if (qname.getNamespaceURI() == null || "".equals(qname.getNamespaceURI())) {
childEle = new SOAPBodyElementImpl(
(ElementImpl)getOwnerDocument().createElement(qname.getLocalPart()));
}else if(null == qname.getPrefix() || "".equals(qname.getPrefix().trim())) {
childEle = new SOAPBodyElementImpl(
(ElementImpl)getOwnerDocument().createElementNS(qname.getNamespaceURI(),
qname.getLocalPart()));
}else {
childEle = new SOAPBodyElementImpl(
(ElementImpl)getOwnerDocument().createElementNS(qname.getNamespaceURI(),
qname.getPrefix() + ":" +
qname.getLocalPart()));
}
childEle.element.setUserData(SAAJ_NODE, childEle, null);
childEle.element.setNamespace(childEle.element.declareNamespace(
qname.getNamespaceURI(), qname.getPrefix()));
element.appendChild(childEle.element);
((NodeImpl)childEle.element.getParentNode()).setUserData(SAAJ_NODE, this, null);
isBodyElementAdded = true;
childEle.setParentElement(this);
return childEle;
}
public QName createQName(String localName, String prefix) throws SOAPException {
if (this.element.getOMFactory() instanceof SOAP11Factory) {
return super.createQName(localName, prefix);
} else if (this.element.getOMFactory() instanceof SOAP12Factory) {
if (this.element.findNamespaceURI(prefix) == null) {
throw new SOAPException("Only Namespace Qualified elements are allowed");
} else {
return super.createQName(localName, prefix);
}
} else {
throw new UnsupportedOperationException();
}
}
public Iterator getAllAttributesAsQNames() {
return super.getAllAttributesAsQNames();
}
public String getAttributeValue(QName qname) {
return super.getAttributeValue(qname);
}
public Iterator getChildElements(QName qname) {
return super.getChildElements(qname);
}
public QName getElementQName() {
return super.getElementQName();
}
public boolean removeAttribute(QName qname) {
return super.removeAttribute(qname);
}
public SOAPElement setElementQName(QName qname) throws SOAPException {
return super.setElementQName(qname);
}
public Iterator getChildElements() {
return getChildren(element.getChildren());
}
public SOAPElement addTextNode(String text) throws SOAPException {
return super.addTextNode(text);
}
private Iterator getChildren(Iterator childIter) {
Collection childElements = new ArrayList();
while (childIter.hasNext()) {
org.w3c.dom.Node domNode = (org.w3c.dom.Node)childIter.next();
Node saajNode = toSAAJNode(domNode);
if (saajNode instanceof javax.xml.soap.Text) {
childElements.add(saajNode);
} else if (!(saajNode instanceof SOAPBodyElement)) {
// silently replace node, as per saaj 1.2 spec
if (domNode instanceof ElementImpl) {
if (omSOAPBody.hasFault()) {
SOAPFactory omFactory = (SOAPFactory)this.element.getOMFactory();
org.apache.axiom.soap.SOAPFault fault;
if (omFactory instanceof SOAP11Factory) {
fault = new SOAP11FaultImpl(omSOAPBody, omFactory);
} else {
fault = new SOAP12FaultImpl(omSOAPBody, omFactory);
}
SOAPFaultImpl saajSOAPFault = new SOAPFaultImpl(fault);
((NodeImpl)omSOAPBody.getFault())
.setUserData(SAAJ_NODE, saajSOAPFault, null);
childElements.add(saajSOAPFault);
} else {
SOAPBodyElement saajBodyEle = new SOAPBodyElementImpl((ElementImpl)domNode);
((NodeImpl)domNode).setUserData(SAAJ_NODE, saajBodyEle, null);
childElements.add(saajBodyEle);
}
}
} else {
childElements.add(saajNode);
}
}
return childElements.iterator();
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?