Java Code Examples for org.w3c.dom.Attr#setPrefix()

The following examples show how to use org.w3c.dom.Attr#setPrefix() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XmlFilterReader.java    From knox with Apache License 2.0 6 votes vote down vote up
private Attr bufferAttribute( Element element, Attribute attribute ) {
  QName name = attribute.getName();
  String prefix = name.getPrefix();
  String uri = name.getNamespaceURI();
  Attr node;
  if( uri == null || uri.isEmpty() ) {
    node = document.createAttribute( name.getLocalPart() );
    element.setAttributeNode( node );
  } else {
    node = document.createAttributeNS( uri, name.getLocalPart() );
    if( prefix != null && !prefix.isEmpty() ) {
      node.setPrefix( prefix );
    }
    element.setAttributeNodeNS( node );
  }
  node.setTextContent( attribute.getValue() );
  return node;
}
 
Example 2
Source File: SdkRepoSource.java    From java-n-IDE-for-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Helper method used by {@link #findAlternateToolsXml(InputStream)} to duplicate a node
 * and attach it to the given root in the new document.
 */
private Element duplicateNode(Element newRootNode, Element oldNode,
        String namespaceUri, String prefix) {
    // The implementation here is more or less equivalent to
    //
    //    newRoot.appendChild(newDoc.importNode(oldNode, deep=true))
    //
    // except we can't just use importNode() since we need to deal with the fact
    // that the old document is not namespace-aware yet the new one is.

    Document newDoc = newRootNode.getOwnerDocument();
    Element newNode = null;

    String nodeName = oldNode.getNodeName();
    int pos = nodeName.indexOf(':');
    if (pos > 0 && pos < nodeName.length() - 1) {
        nodeName = nodeName.substring(pos + 1);
        newNode = newDoc.createElementNS(namespaceUri, nodeName);
        newNode.setPrefix(prefix);
    } else {
        newNode = newDoc.createElement(nodeName);
    }

    newRootNode.appendChild(newNode);

    // Merge in all the attributes
    NamedNodeMap attrs = oldNode.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        Attr newAttr = null;

        String attrName = attr.getNodeName();
        pos = attrName.indexOf(':');
        if (pos > 0 && pos < attrName.length() - 1) {
            attrName = attrName.substring(pos + 1);
            newAttr = newDoc.createAttributeNS(namespaceUri, attrName);
            newAttr.setPrefix(prefix);
        } else {
            newAttr = newDoc.createAttribute(attrName);
        }

        newAttr.setNodeValue(attr.getNodeValue());

        if (pos > 0) {
            newNode.getAttributes().setNamedItemNS(newAttr);
        } else {
            newNode.getAttributes().setNamedItem(newAttr);
        }
    }

    // Merge all child elements and texts
    for (Node child = oldNode.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            duplicateNode(newNode, (Element) child, namespaceUri, prefix);

        } else if (child.getNodeType() == Node.TEXT_NODE) {
            Text newText = newDoc.createTextNode(child.getNodeValue());
            newNode.appendChild(newText);
        }
    }

    return newNode;
}
 
Example 3
Source File: SdkRepoSource.java    From javaide with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Helper method used by {@link #findAlternateToolsXml(InputStream)} to duplicate a node
 * and attach it to the given root in the new document.
 */
private Element duplicateNode(Element newRootNode, Element oldNode,
        String namespaceUri, String prefix) {
    // The implementation here is more or less equivalent to
    //
    //    newRoot.appendChild(newDoc.importNode(oldNode, deep=true))
    //
    // except we can't just use importNode() since we need to deal with the fact
    // that the old document is not namespace-aware yet the new one is.

    Document newDoc = newRootNode.getOwnerDocument();
    Element newNode = null;

    String nodeName = oldNode.getNodeName();
    int pos = nodeName.indexOf(':');
    if (pos > 0 && pos < nodeName.length() - 1) {
        nodeName = nodeName.substring(pos + 1);
        newNode = newDoc.createElementNS(namespaceUri, nodeName);
        newNode.setPrefix(prefix);
    } else {
        newNode = newDoc.createElement(nodeName);
    }

    newRootNode.appendChild(newNode);

    // Merge in all the attributes
    NamedNodeMap attrs = oldNode.getAttributes();
    for (int i = 0; i < attrs.getLength(); i++) {
        Attr attr = (Attr) attrs.item(i);
        Attr newAttr = null;

        String attrName = attr.getNodeName();
        pos = attrName.indexOf(':');
        if (pos > 0 && pos < attrName.length() - 1) {
            attrName = attrName.substring(pos + 1);
            newAttr = newDoc.createAttributeNS(namespaceUri, attrName);
            newAttr.setPrefix(prefix);
        } else {
            newAttr = newDoc.createAttribute(attrName);
        }

        newAttr.setNodeValue(attr.getNodeValue());

        if (pos > 0) {
            newNode.getAttributes().setNamedItemNS(newAttr);
        } else {
            newNode.getAttributes().setNamedItem(newAttr);
        }
    }

    // Merge all child elements and texts
    for (Node child = oldNode.getFirstChild(); child != null; child = child.getNextSibling()) {
        if (child.getNodeType() == Node.ELEMENT_NODE) {
            duplicateNode(newNode, (Element) child, namespaceUri, prefix);

        } else if (child.getNodeType() == Node.TEXT_NODE) {
            Text newText = newDoc.createTextNode(child.getNodeValue());
            newNode.appendChild(newText);
        }
    }

    return newNode;
}
 
Example 4
Source File: AttrImpl.java    From marklogic-contentpump with Apache License 2.0 4 votes vote down vote up
protected Node cloneNode(Document doc, boolean deep) {
    Attr attr = doc.createAttributeNS(getNamespaceURI(), getLocalName());
    attr.setValue(getValue());
    attr.setPrefix(getPrefix());
    return attr;
}
 
Example 5
Source File: XmlFilterReader.java    From knox with Apache License 2.0 4 votes vote down vote up
private void streamAttribute( Element element, Attribute attribute ) throws XPathExpressionException {
  Attr node;
  QName name = attribute.getName();
  String prefix = name.getPrefix();
  String uri = name.getNamespaceURI();
  if( uri == null || uri.isEmpty() ) {
    node = document.createAttribute( name.getLocalPart() );
    element.setAttributeNode( node );
  } else {
    node = document.createAttributeNS( uri, name.getLocalPart() );
    if( prefix != null && !prefix.isEmpty() ) {
      node.setPrefix( prefix );
    }
    element.setAttributeNodeNS( node );
  }

  String value = attribute.getValue();
  Level level = stack.peek();
  if( ( level.scopeConfig ) == null || ( level.scopeConfig.getSelectors().isEmpty() ) ) {
    value = filterAttribute( null, attribute.getName(), value, null );
    node.setValue( value );
  } else {
    UrlRewriteFilterPathDescriptor path = pickFirstMatchingPath( level );
    if( path instanceof UrlRewriteFilterApplyDescriptor ) {
      String rule = ((UrlRewriteFilterApplyDescriptor)path).rule();
      value = filterAttribute( null, attribute.getName(), value, rule );
      node.setValue( value );
    }
  }

  if( prefix == null || prefix.isEmpty() ) {
    writer.write( " " );
    writer.write( name.getLocalPart() );
  } else {
    writer.write( " " );
    writer.write( prefix );
    writer.write( ":" );
    writer.write( name.getLocalPart() );
  }
  writer.write( "=\"" );
  writer.write( value );
  writer.write( "\"" );
  element.removeAttributeNode( node );
}