soaputil.java
来自「ejb3 java session bean」· Java 代码 · 共 503 行 · 第 1/2 页
JAVA
503 行
// attributes removeAttributes(target); copyAttributes(target, source); // all namespaces removeNamespaces(target); copyVisibleNamespaces(target, source); ensureOwnNamespaceDeclared(target); // child nodes target.removeContents(); copyChildNodes(target, source); if (traceEnabled) log.trace("copied to: " + XmlUtil.toTraceString(target)); } public static void ensureOwnNamespaceDeclared(SOAPElement elem) throws SOAPException { ensureNamespaceDeclared(elem, elem.getNamespaceURI(), elem.getPrefix()); } public static void ensureNamespaceDeclared(SOAPElement elem, String namespaceURI, String prefix) throws SOAPException { if (prefix == null || prefix.length() == 0) { if (namespaceURI == null || namespaceURI.length() == 0) return; // do not declare the empty namespace // verify the given URI is the default namespace if (!namespaceURI.equals(elem.getNamespaceURI(""))) { // the given URI is not the default namespace, declare locally elem.addNamespaceDeclaration("", namespaceURI); } } else { if (namespaceURI == null || namespaceURI.length() == 0) throw new IllegalArgumentException("namespaceURI cannot be empty unless prefix is empty"); // verify given prefix is associated to given URI if (!namespaceURI.equals(elem.getNamespaceURI(prefix))) { // prefix is associated with other/no URI, declare locally elem.addNamespaceDeclaration(prefix, namespaceURI); } } } public static void copyVisibleNamespaces(SOAPElement target, Element source) throws SOAPException { // copy the namespaces declared at the source element copyNamespaces(target, source); // go up the element hierarchy for (Node parent = source.getParentNode(); parent instanceof Element; parent = parent.getParentNode()) copyNamespaces(target, (Element) parent); } public static void copyNamespaces(SOAPElement target, Element source) throws SOAPException { // easy way out: no attributes if (!source.hasAttributes()) return; // traverse attributes to discover namespace declarations NamedNodeMap attributes = source.getAttributes(); for (int i = 0, n = attributes.getLength(); i < n; i++) { Node attribute = attributes.item(i); // is attribute a namespace declaration? if (!BpelConstants.NS_XMLNS.equals(attribute.getNamespaceURI())) continue; // namespace declaration format xmlns:prefix="namespaceURI" | // xmlns="defaultNamespaceURI" String namespaceURI = attribute.getNodeValue(); String prefix = attribute.getLocalName(); // non-default namespace declaration? if (!"xmlns".equals(prefix)) { // BPEL-195: prevent addition matching visible declaration at target if (namespaceURI.equals(target.getNamespaceURI(prefix))) continue; target.addNamespaceDeclaration(prefix, namespaceURI); if (traceEnabled) log.trace("added namespace declaration: " + prefix + "->" + namespaceURI); } // non-empty default namespace declaration else if (namespaceURI.length() > 0) { prefix = XmlUtil.generatePrefix(DEFAULT_NAMESPACE_PREFIX, source); target.addNamespaceDeclaration(prefix, namespaceURI); if (traceEnabled) log.trace("reassigned default namespace declaration: " + prefix + "->" + namespaceURI); } } } public static void copyAttributes(SOAPElement target, Element source) { // easy way out: no attributes if (!source.hasAttributes()) return; // traverse attributes NamedNodeMap attributes = source.getAttributes(); for (int i = 0, n = attributes.getLength(); i < n; i++) { Node attribute = attributes.item(i); String namespaceURI = attribute.getNamespaceURI(); // isn't the attribute a namespace declaration? if (BpelConstants.NS_XMLNS.equals(namespaceURI)) continue; String name = attribute.getNodeName(); String value = attribute.getNodeValue(); if (namespaceURI == null) { /* * use the DOM level 1 method as some SAAJ implementations complain when presented a null * namespace URI */ target.setAttribute(name, value); } else target.setAttributeNS(namespaceURI, name, value); if (traceEnabled) log.trace("set attribute: " + name); } } public static void copyChildNodes(SOAPElement target, Element source) throws SOAPException { // easy way out: no child nodes if (!source.hasChildNodes()) return; // traverse child nodes for (Node child = source.getFirstChild(); child != null; child = child.getNextSibling()) { switch (child.getNodeType()) { case Node.ELEMENT_NODE: { copyChildElement(target, (Element) child); break; } case Node.TEXT_NODE: case Node.CDATA_SECTION_NODE: { String text = child.getNodeValue(); // drop whitespace-only text nodes if (!StringUtils.isWhitespace(text)) { target.addTextNode(text); if (traceEnabled) log.trace("appended text: " + text); } break; } default: log.debug("discarding child: " + child); } } } public static void copyChildElement(SOAPElement parent, Element source) throws SOAPException { String localName = source.getLocalName(); String prefix = source.getPrefix(); String namespaceURI = source.getNamespaceURI(); Name targetName; SOAPEnvelope envelope = findEnvelope(parent); if (prefix == null || prefix.length() == 0) { // source has no prefix, distinguish between no namespace and default namespace if (namespaceURI == null || namespaceURI.length() == 0) { // no namespace targetName = envelope.createName(localName); if (traceEnabled) log.trace("appended element: " + localName); } else { // default namespace, look for existing prefix at target prefix = getPrefix(namespaceURI, parent); // no prefix for that namespace? if (prefix == null) { prefix = XmlUtil.generatePrefix(DEFAULT_NAMESPACE_PREFIX, source); } // BPEL-195 source maps prefix to another URI? else if (!namespaceURI.equals(source.getAttributeNS(BpelConstants.NS_XMLNS, prefix))) { prefix = XmlUtil.generatePrefix(prefix, source); } targetName = envelope.createName(localName, prefix, namespaceURI); if (traceEnabled) log.trace("added child element: {" + namespaceURI + '}' + prefix + ':' + localName); } } else { // source has prefix targetName = envelope.createName(localName, prefix, namespaceURI); if (traceEnabled) log.trace("added child element: {" + namespaceURI + '}' + prefix + ':' + localName); } SOAPElement target; if (parent instanceof SOAPBody) { /* * jboss-ws4ee throws ClassCastException upon calling the remote endpoint if child elements * other than SOAPBodyElements are added to SOAPBody */ SOAPBody body = (SOAPBody) parent; target = body.addBodyElement(targetName); } else target = parent.addChildElement(targetName); // namespaces copyNamespaces(target, source); ensureOwnNamespaceDeclared(target); // attributes copyAttributes(target, source); // child nodes copyChildNodes(target, source); } public static SOAPEnvelope findEnvelope(SOAPElement element) throws SOAPException { do { if (element instanceof SOAPEnvelope) return (SOAPEnvelope) element; element = element.getParentElement(); } while (element != null); return null; } public static SOAPElement addChildElement(SOAPElement parent, String localName) throws SOAPException { // the proper call is addChildElement(localName); however, the WS4EE stack // mistakenly adds a child element with parent's namespace URI return parent.addChildElement(localName, "", ""); } public static void removeAttributes(SOAPElement elem) { if (elem.hasAttributes()) { Iterator attrNameIt = elem.getAllAttributes(); while (attrNameIt.hasNext()) { Name attrName = (Name) attrNameIt.next(); elem.removeAttribute(attrName); } } } public static void removeNamespaces(SOAPElement elem) { Iterator prefixIt = elem.getNamespacePrefixes(); while (prefixIt.hasNext()) { String prefix = (String) prefixIt.next(); elem.removeNamespaceDeclaration(prefix); } } public static String getPrefix(String namespaceURI, SOAPElement contextElem) { Iterator prefixIt = contextElem.getVisibleNamespacePrefixes(); while (prefixIt.hasNext()) { String prefix = (String) prefixIt.next(); if (namespaceURI.equals(contextElem.getNamespaceURI(prefix))) return prefix; } return null; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?