⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlutil.java

📁 jbpm-bpel-1.1.Beta3 JBoss jBPM Starters Kit  是一个综合包
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
    }
    else {
      prefixedValue = value.getLocalPart();
    }
    XmlUtil.setStringValue(elem, prefixedValue);
  }

  public static void setQNameValue(SOAPElement elem, QName value)
      throws SOAPException {
    String codeNamespace = value.getNamespaceURI();
    String prefixedValue;
    if (codeNamespace.length() > 0) {
      String codePrefix = XmlUtil.getPrefix(codeNamespace, elem);
      if (codePrefix == null) {
        codePrefix = "valueNS";
        elem.addNamespaceDeclaration(codePrefix, codeNamespace);
      }
      prefixedValue = codePrefix + ':' + value.getLocalPart();
    }
    else {
      prefixedValue = value.getLocalPart();
    }
    elem.setValue(prefixedValue);
  }

  public static void copy(Element target, Element source) {
    if (traceEnabled)
      log.trace("copying from: " + toTraceString(source));
    // attributes
    removeAttributes(target);
    copyAttributes(target, source);
    // all namespaces
    copyVisibleNamespaces(target, source);
    // child nodes
    removeChildNodes(target);
    copyChildNodes(target, source);
    if (traceEnabled)
      log.trace("copied to: " + toTraceString(target));
  }

  private static String toTraceString(Element elem) {
    String namespace = elem.getNamespaceURI();
    String localName = elem.getLocalName();
    // easy way out: no namespace
    if (StringUtils.isEmpty(namespace))
      return localName;

    StringBuffer traceBuffer = new StringBuffer(namespace.length()
        + localName.length());
    traceBuffer.append('{').append(namespace).append('}');
    String prefix = elem.getPrefix();
    if (!StringUtils.isEmpty(prefix)) {
      traceBuffer.append(prefix).append(':');
    }
    return traceBuffer.append(localName).toString();
  }

  public static void copyVisibleNamespaces(final Element target, Element source) {
    // copy namespaces declared at 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(final Element target, Element source) {
    // 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();
      // default namespace declaration?
      if ("xmlns".equals(prefix)) {
        // BPEL-195: prevent addition matching visible declaration at target
        if ("".equals(getPrefix(namespaceURI, target)))
          continue;
        addNamespaceDeclaration(target, namespaceURI);
        if (traceEnabled)
          log.trace("added default namespace declaration: " + namespaceURI);
      }
      else {
        // BPEL-195: prevent addition matching visible declaration at target
        if (prefix.equals(getPrefix(namespaceURI, target)))
          continue;
        addNamespaceDeclaration(target, namespaceURI, prefix);
        if (traceEnabled)
          log.trace("added namespace declaration: "
              + prefix
              + "->"
              + namespaceURI);
      }
    }
  }

  public static void copyAttributes(Element 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)) {
        target.setAttributeNS(namespaceURI, attribute.getNodeName(),
            attribute.getNodeValue());
      }
    }
  }

  public static void copyChildNodes(Element target, Element source) {
    Document targetDoc = target.getOwnerDocument();
    for (Node child = source.getFirstChild(); child != null; child = child.getNextSibling()) {
      switch (child.getNodeType()) {
      case Node.ELEMENT_NODE:
        target.appendChild(targetDoc.importNode(child, true));
        if (traceEnabled)
          log.trace("appended element: " + toTraceString((Element) child));
        break;
      case Node.TEXT_NODE:
      case Node.CDATA_SECTION_NODE:
        target.appendChild(targetDoc.createTextNode(child.getNodeValue()));
        if (traceEnabled)
          log.trace("appended text: " + child.getNodeValue());
        break;
      default:
        log.debug("discarding child: " + child);
      }
    }
  }

  public static void copy(Element target, SOAPElement source) {
    if (traceEnabled)
      log.trace("copying from: " + toTraceString(source));
    // attributes
    removeAttributes(target);
    copyAttributes(target, source);
    // all namespaces
    copyVisibleNamespaces(target, source);
    // child nodes
    removeChildNodes(target);
    copyChildNodes(target, source);
    if (traceEnabled)
      log.trace("copied to: " + 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(getPrefix(namespaceURI, target)))
        continue;
      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();
      String value = source.getAttributeValue(attrName);
      if (StringUtils.isEmpty(namespaceURI)) {
        String localName = attrName.getLocalName();
        target.setAttribute(localName, value);
        if (traceEnabled)
          log.trace("set attribute: " + localName);
      }
      else {
        String qualifiedName = attrName.getQualifiedName();
        target.setAttributeNS(namespaceURI, qualifiedName, value);
        if (traceEnabled)
          log.trace("set attribute: " + qualifiedName);
      }
    }
  }

  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);
      }
    }
  }

  private static void copyChildElement(Element parent, SOAPElement source) {
    String namespaceURI = source.getNamespaceURI();
    String name = source.getNodeName();
    // create a child DOM element with the same name
    Element target = parent.getOwnerDocument().createElementNS(namespaceURI,
        name);
    parent.appendChild(target);
    if (traceEnabled)
      log.trace("appended element: {" + namespaceURI + '}' + name);
    // namespaces
    copyNamespaces(target, source);
    // 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: " + toTraceString(source));
    // attributes
    removeAttributes(target);
    copyAttributes(target, source);
    // all namespaces
    removeNamespaces(target);
    copyVisibleNamespaces(target, source);
    // child nodes
    target.removeContents();
    copyChildNodes(target, source);
    if (traceEnabled)
      log.trace("copied to: " + toTraceString(target));
  }

  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 = generatePrefix(source, DEFAULT_NAMESPACE_PREFIX);
        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)) {
        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();

    SOAPElement target;
    // no prefix?
    if (prefix == null) {

⌨️ 快捷键说明

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