soaputil.java

来自「ejb3 java session bean」· Java 代码 · 共 503 行 · 第 1/2 页

JAVA
503
字号
/* * JBoss, Home of Professional Open Source * Copyright 2005, JBoss Inc., and individual contributors as indicated * by the @authors tag. * * This is free software; you can redistribute it and/or modify it * under the terms of the JBPM BPEL PUBLIC LICENSE AGREEMENT as * published by JBoss Inc.; either version 1.0 of the License, or * (at your option) any later version. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. */package org.jbpm.bpel.integration.soap;import java.util.Iterator;import javax.xml.namespace.QName;import javax.xml.soap.Name;import javax.xml.soap.SOAPBody;import javax.xml.soap.SOAPElement;import javax.xml.soap.SOAPEnvelope;import javax.xml.soap.SOAPException;import javax.xml.soap.Text;import org.apache.commons.lang.StringUtils;import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.w3c.dom.Attr;import org.w3c.dom.Element;import org.w3c.dom.NamedNodeMap;import org.w3c.dom.Node;import org.jbpm.bpel.xml.BpelConstants;import org.jbpm.bpel.xml.util.XmlUtil;/** * Utility methods for dealing with SAAJ objects. * @author Alejandro Guizar * @version $Revision: 1.6 $ $Date: 2007/10/13 02:53:28 $ */public class SoapUtil {  static final String DEFAULT_NAMESPACE_PREFIX = "defaultNS";  static final String QUALIFIED_VALUE_PREFIX = "valueNS";  private static final Log log = LogFactory.getLog(SoapUtil.class);  private static final boolean traceEnabled = log.isTraceEnabled();  // suppress default constructor, ensuring non-instantiability  private SoapUtil() {  }  /**   * Gets the first child element of the given SOAP element with the specified local name and a   * <code>null</code> or empty namespace URI. This overload is necessary as method   * {@link Node#getNextSibling()} does not behave as expected under some SAAJ implementations.   * @param parent the parent SOAP element to examine   * @param localName the local name of the desired child element   * @return the corresponding child element, or <code>null</code> if there is no match   */  public static SOAPElement getElement(SOAPElement parent, String localName) {    return getElement(parent, null, localName);  }  /**   * Gets the first child element of the given SOAP element with the specified namespace URI and   * local name. This overload is necessary as method {@link Node#getNextSibling()} does not behave   * as expected under some SAAJ implementations.   * @param parent the parent node to examine   * @param namespaceURI the namespace URI of the desired child element; if <code>null</code>,   * only elements with a <code>null</code> or empty namespace URI will be considered   * @param localName the local name of the desired child element   * @return the corresponding child element, or <code>null</code> if there is no match   */  public static SOAPElement getElement(SOAPElement parent, String namespaceURI, String localName) {    Iterator childIt = parent.getChildElements();    while (childIt.hasNext()) {      javax.xml.soap.Node child = (javax.xml.soap.Node) childIt.next();      if (XmlUtil.nodeNameEquals(child, namespaceURI, localName))        return (SOAPElement) child;    }    return null;  }  /**   * Gets the first child element of the given SOAP element with the specified qualified name. This   * overload is necessary as method {@link Node#getNextSibling()} does not behave as expected under   * some SAAJ implementations.   * @param parent the parent node to examine   * @param name the qualified name of the desired child element; if   * <code>name.getNamespaceURI()</code> is empty, only elements with a <code>null</code> or   * empty namespace URI will be considered   * @return the corresponding child element, or <code>null</code> if there is no match   */  public static SOAPElement getElement(SOAPElement parent, QName name) {    return getElement(parent, name.getNamespaceURI(), name.getLocalPart());  }  /**   * Gets the first child element of the given SOAP element.   * @param parent the parent SOAP element to examine   * @return the corresponding child element, or <code>null</code> if there is no match   */  public static SOAPElement getElement(SOAPElement parent) {    Iterator childIt = parent.getChildElements();    while (childIt.hasNext()) {      Object child = childIt.next();      if (child instanceof SOAPElement)        return (SOAPElement) child;    }    return null;  }  public static void setQNameValue(SOAPElement elem, QName value) throws SOAPException {    elem.setValue(formatQName(value, elem));  }  private static String formatQName(QName value, SOAPElement elem) throws SOAPException {    String namespaceURI = value.getNamespaceURI();    String localName = value.getLocalPart();    // easy way out: no namespace    if (namespaceURI.length() == 0)      return localName;    String prefix = SoapUtil.getPrefix(namespaceURI, elem);    if (prefix == null) {      String givenPrefix = value.getPrefix();      prefix = XmlUtil.generatePrefix(givenPrefix.length() > 0 ? givenPrefix          : QUALIFIED_VALUE_PREFIX, elem);      elem.addNamespaceDeclaration(prefix, namespaceURI);    }    return prefix + ':' + localName;  }  public static void copy(Element target, SOAPElement source) {    if (traceEnabled)      log.trace("copying from: " + XmlUtil.toTraceString(source));    // attributes    XmlUtil.removeAttributes(target);    copyAttributes(target, source);    // all namespaces    copyVisibleNamespaces(target, source);    XmlUtil.ensureOwnNamespaceDeclared(target);    // child nodes    XmlUtil.removeChildNodes(target);    copyChildNodes(target, source);    if (traceEnabled)      log.trace("copied to: " + XmlUtil.toTraceString(target));  }  public static void copyVisibleNamespaces(Element target, SOAPElement source) {    copyNamespaces(target, source, source.getVisibleNamespacePrefixes());  }  public static void copyNamespaces(Element target, SOAPElement source) {    copyNamespaces(target, source, source.getNamespacePrefixes());  }  private static void copyNamespaces(Element target, SOAPElement source, Iterator prefixIt) {    // namespace declarations appear as attributes in the target element    while (prefixIt.hasNext()) {      String prefix = (String) prefixIt.next();      String namespaceURI = source.getNamespaceURI(prefix);      // BPEL-195: prevent addition matching visible declaration at target      if (prefix.equals(XmlUtil.getPrefix(namespaceURI, target)))        continue;      XmlUtil.addNamespaceDeclaration(target, namespaceURI, prefix);      if (traceEnabled)        log.trace("added namespace declaration: " + prefix + "->" + namespaceURI);    }  }  public static void copyAttributes(Element target, SOAPElement source) {    // easy way out: no attributes to copy    if (!source.hasAttributes())      return;    // traverse attributes    Iterator attrNameIt = source.getAllAttributes();    while (attrNameIt.hasNext()) {      Name attrName = (Name) attrNameIt.next();      String namespaceURI = attrName.getURI();      // isn't the attribute a namespace declaration?      if (BpelConstants.NS_XMLNS.equals(namespaceURI))        continue;      // unqualified?      if (namespaceURI == null || namespaceURI.length() == 0) {        String localName = attrName.getLocalName();        target.setAttribute(localName, source.getAttributeValue(attrName));        if (traceEnabled)          log.trace("set attribute: " + localName);      }      // qualified      else {        Attr attr = target.getOwnerDocument().createAttributeNS(namespaceURI,            attrName.getQualifiedName());        attr.setValue(source.getAttributeValue(attrName));        target.setAttributeNodeNS(attr);        XmlUtil.ensureNamespaceDeclared(attr, namespaceURI, attrName.getPrefix());        if (traceEnabled)          log.trace("set attribute: {" + namespaceURI + '}' + attrName.getQualifiedName());      }    }  }  public static void copyChildNodes(Element target, SOAPElement source) {    // easy way out: no child nodes to copy    if (!source.hasChildNodes())      return;    // traverse child nodes    Iterator childIt = source.getChildElements();    while (childIt.hasNext()) {      Object child = childIt.next();      if (child instanceof SOAPElement) {        copyChildElement(target, (SOAPElement) child);      }      else if (child instanceof Text) {        Text childText = (Text) child;        String value = childText.getValue();        target.appendChild(target.getOwnerDocument().createTextNode(value));        if (traceEnabled)          log.trace("appended text: " + value);      }      else        log.debug("discarding child: " + child);    }  }  public static void copyChildElement(Element parent, SOAPElement source) {    // create a DOM element with the same name as the source    String namespaceURI = source.getNamespaceURI();    String qualifiedName = source.getNodeName();    Element target = parent.getOwnerDocument().createElementNS(namespaceURI, qualifiedName);    parent.appendChild(target);    if (traceEnabled)      log.trace("appended element: {" + namespaceURI + '}' + qualifiedName);    // namespaces    copyNamespaces(target, source);    XmlUtil.ensureOwnNamespaceDeclared(target);    // attributes    copyAttributes(target, source);    // child nodes    copyChildNodes(target, source);  }  public static void copy(SOAPElement target, Element source) throws SOAPException {    if (traceEnabled)      log.trace("copying from: " + XmlUtil.toTraceString(source));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?