📄 soapdocumentimpl.java
字号:
/* * Copyright 2001-2004 The Apache Software Foundation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.axis.message;import javax.xml.namespace.QName;import org.apache.axis.AxisFault;import org.apache.axis.Constants;import org.apache.axis.SOAPPart;import org.apache.axis.utils.Mapping;import org.apache.axis.utils.XMLUtils;import org.w3c.dom.Attr;import org.w3c.dom.CDATASection;import org.w3c.dom.Comment;import org.w3c.dom.DOMException;import org.w3c.dom.DOMImplementation;import org.w3c.dom.Document;import org.w3c.dom.DocumentFragment;import org.w3c.dom.DocumentType;import org.w3c.dom.Element;import org.w3c.dom.EntityReference;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.w3c.dom.ProcessingInstruction;import javax.xml.parsers.ParserConfigurationException;import javax.xml.soap.SOAPException;/** * SOAPDcoumentImpl implements the Document API for SOAPPART. At the moment, it * again delgate the XERCES DOM Implementation Here is my argument on it: I * guess that there is 3 way to implement this. - fully implement the DOM API * here myself. => This is too much and duplicated work. - extends XERCES * Implementation => this makes we are fixed to one Implementation - choose * delgate depends on the user's parser preference => This is the practically * best solution I have now * * @author Heejune Ahn (cityboy@tmax.co.kr) * */public class SOAPDocumentImplimplements org.w3c.dom.Document, java.io.Serializable { // Depending on the user's parser preference protected Document delegate = null; protected SOAPPart soapPart = null; /** * Construct the Document * * @param sp the soap part */ public SOAPDocumentImpl(SOAPPart sp) { try { delegate = XMLUtils.newDocument(); } catch (ParserConfigurationException e) { // Do nothing } soapPart = sp; } /** * @todo : link with SOAP * * @return */ public DocumentType getDoctype() { return delegate.getDoctype(); } public DOMImplementation getImplementation() { return delegate.getImplementation(); } /** * should not be called, the method will be handled in SOAPPart * * @return */ public Element getDocumentElement() { return soapPart.getDocumentElement(); } /** * based on the tagName, we will make different kind SOAP Elements Instance * Is really we can determine the Type by the Tagname??? * * @todo : verify this method * * @param tagName * @return @throws * DOMException */ public org.w3c.dom.Element createElement(String tagName) throws DOMException { int index = tagName.indexOf(":"); String prefix, localname; if (index < 0) { prefix = ""; localname = tagName; } else { prefix = tagName.substring(0, index); localname = tagName.substring(index + 1); } try { SOAPEnvelope soapenv = (org.apache.axis.message.SOAPEnvelope) soapPart.getEnvelope(); if (soapenv != null) { if (tagName.equalsIgnoreCase(Constants.ELEM_ENVELOPE)) new SOAPEnvelope(); if (tagName.equalsIgnoreCase(Constants.ELEM_HEADER)) return new SOAPHeader(soapenv, soapenv.getSOAPConstants()); if (tagName.equalsIgnoreCase(Constants.ELEM_BODY)) return new SOAPBody(soapenv, soapenv.getSOAPConstants()); if (tagName.equalsIgnoreCase(Constants.ELEM_FAULT)) return new SOAPEnvelope(); if (tagName.equalsIgnoreCase(Constants.ELEM_FAULT_DETAIL)) return new SOAPFault(new AxisFault(tagName)); else { return new MessageElement("", prefix, localname); } } else { return new MessageElement("", prefix, localname); } } catch (SOAPException se) { throw new DOMException(DOMException.INVALID_STATE_ERR, ""); } } /** * * Creates an empty <code>DocumentFragment</code> object. @todo not * implemented yet * * @return A new <code>DocumentFragment</code>. */ public DocumentFragment createDocumentFragment() { return delegate.createDocumentFragment(); } /** * Creates a <code>Text</code> node given the specified string. * * @param data * The data for the node. * @return The new <code>Text</code> object. */ public org.w3c.dom.Text createTextNode(String data) { org.apache.axis.message.Text me = new org.apache.axis.message.Text(delegate.createTextNode(data)); me.setOwnerDocument(soapPart); return me; } /** * Creates a <code>Comment</code> node given the specified string. * * @param data * The data for the node. * @return The new <code>Comment</code> object. */ public Comment createComment(String data) { return new org.apache.axis.message.CommentImpl(data); } /** * Creates a <code>CDATASection</code> node whose value is the specified * string. * * @param data * The data for the <code>CDATASection</code> contents. * @return The new <code>CDATASection</code> object. * @exception DOMException * NOT_SUPPORTED_ERR: Raised if this document is an HTML * document. */ public CDATASection createCDATASection(String data) throws DOMException { return new CDATAImpl(data); } /** * Creates a <code>ProcessingInstruction</code> node given the specified * name and data strings. * * @param target * The target part of the processing instruction. * @param data * The data for the node. * @return The new <code>ProcessingInstruction</code> object. * @exception DOMException * INVALID_CHARACTER_ERR: Raised if the specified target * contains an illegal character. <br>NOT_SUPPORTED_ERR: * Raised if this document is an HTML document. */ public ProcessingInstruction createProcessingInstruction( String target, String data) throws DOMException { throw new java.lang.UnsupportedOperationException( "createProcessingInstruction"); } /** * @todo: How Axis will maintain the Attribute representation ? */ public Attr createAttribute(String name) throws DOMException { return delegate.createAttribute(name); } /** * @param name * @return @throws * DOMException */ public EntityReference createEntityReference(String name) throws DOMException { throw new java.lang.UnsupportedOperationException( "createEntityReference"); } // implemented by yoonforh 2004-07-30 02:48:50 public Node importNode(Node importedNode, boolean deep) throws DOMException { Node targetNode = null; int type = importedNode.getNodeType(); switch (type) { case ELEMENT_NODE : Element el = (Element) importedNode; if (deep) { targetNode = new SOAPBodyElement(el); break; } SOAPBodyElement target = new SOAPBodyElement(); org.w3c.dom.NamedNodeMap attrs = el.getAttributes(); for (int i = 0; i < attrs.getLength(); i++) { org.w3c.dom.Node att = attrs.item(i); if (att.getNamespaceURI() != null && att.getPrefix() != null && att.getNamespaceURI().equals(Constants.NS_URI_XMLNS) && att.getPrefix().equals("xmlns")) { Mapping map = new Mapping(att.getNodeValue(), att.getLocalName()); target.addMapping(map); } if (att.getLocalName() != null) { target.addAttribute(att.getPrefix(), att.getNamespaceURI(), att.getLocalName(), att.getNodeValue()); } else if (att.getNodeName() != null) { target.addAttribute(att.getPrefix(), att.getNamespaceURI(), att.getNodeName(), att.getNodeValue()); } } if (el.getLocalName() == null) { target.setName(el.getNodeName()); } else { target.setQName(new QName(el.getNamespaceURI(), el.getLocalName())); } targetNode = target; break; case ATTRIBUTE_NODE : if (importedNode.getLocalName() == null) { targetNode = createAttribute(importedNode.getNodeName()); } else { targetNode = createAttributeNS(importedNode.getNamespaceURI(), importedNode.getLocalName()); } break; case TEXT_NODE : targetNode = createTextNode(importedNode.getNodeValue()); break; case CDATA_SECTION_NODE : targetNode = createCDATASection(importedNode.getNodeValue()); break; case COMMENT_NODE : targetNode = createComment(importedNode.getNodeValue()); break; case DOCUMENT_FRAGMENT_NODE : targetNode = createDocumentFragment(); if (deep) { org.w3c.dom.NodeList children = importedNode.getChildNodes(); for (int i = 0; i < children.getLength(); i++){ targetNode.appendChild(importNode(children.item(i), true)); } } break; case ENTITY_REFERENCE_NODE : targetNode = createEntityReference(importedNode.getNodeName()); break; case PROCESSING_INSTRUCTION_NODE : ProcessingInstruction pi = (ProcessingInstruction) importedNode; targetNode = createProcessingInstruction(pi.getTarget(), pi.getData()); break; case ENTITY_NODE : // TODO : ... throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Entity nodes are not supported."); case NOTATION_NODE : // TODO : any idea? throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Notation nodes are not supported."); case DOCUMENT_TYPE_NODE : throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "DocumentType nodes cannot be imported."); case DOCUMENT_NODE : throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Document nodes cannot be imported."); default : throw new DOMException(DOMException.NOT_SUPPORTED_ERR, "Node type (" + type + ") cannot be imported."); } return targetNode; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -