org.w3c.dom.Attr#getNodeName ( )源码实例Demo

下面列出了org.w3c.dom.Attr#getNodeName ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: jdk8u-dev-jdk   文件: SetAttributeNode.java
public static void test2() {
    String name = "attr";
    String oldValue = "old value";
    String newValue = "new value";
    Attr retAttr;

    IIOMetadataNode parent = new IIOMetadataNode("parent");
    MyAttrNode attrNode1 = new MyAttrNode(name, oldValue);
    MyAttrNode attrNode2 = new MyAttrNode(name, newValue);

    retAttr = parent.setAttributeNode(attrNode1);
    retAttr = parent.setAttributeNode(attrNode2);

    String actName = retAttr.getNodeName();
    String actValue = retAttr.getValue();

    if (!actName.equals(name) || !actValue.equals(oldValue)) {
        throw new RuntimeException("Test 2 failed: Invalid attribute " +
                                   "returned: " +
                                   "(name: " + actName +
                                   ", value: " + actValue + ")");
    }
}
 
源代码2 项目: hottub   文件: SetAttributeNode.java
public static void test2() {
    String name = "attr";
    String oldValue = "old value";
    String newValue = "new value";
    Attr retAttr;

    IIOMetadataNode parent = new IIOMetadataNode("parent");
    MyAttrNode attrNode1 = new MyAttrNode(name, oldValue);
    MyAttrNode attrNode2 = new MyAttrNode(name, newValue);

    retAttr = parent.setAttributeNode(attrNode1);
    retAttr = parent.setAttributeNode(attrNode2);

    String actName = retAttr.getNodeName();
    String actValue = retAttr.getValue();

    if (!actName.equals(name) || !actValue.equals(oldValue)) {
        throw new RuntimeException("Test 2 failed: Invalid attribute " +
                                   "returned: " +
                                   "(name: " + actName +
                                   ", value: " + actValue + ")");
    }
}
 
源代码3 项目: TencentKona-8   文件: SetAttributeNode.java
public static void test2() {
    String name = "attr";
    String oldValue = "old value";
    String newValue = "new value";
    Attr retAttr;

    IIOMetadataNode parent = new IIOMetadataNode("parent");
    MyAttrNode attrNode1 = new MyAttrNode(name, oldValue);
    MyAttrNode attrNode2 = new MyAttrNode(name, newValue);

    retAttr = parent.setAttributeNode(attrNode1);
    retAttr = parent.setAttributeNode(attrNode2);

    String actName = retAttr.getNodeName();
    String actValue = retAttr.getValue();

    if (!actName.equals(name) || !actValue.equals(oldValue)) {
        throw new RuntimeException("Test 2 failed: Invalid attribute " +
                                   "returned: " +
                                   "(name: " + actName +
                                   ", value: " + actValue + ")");
    }
}
 
源代码4 项目: jdk8u-jdk   文件: SetAttributeNode.java
public static void test2() {
    String name = "attr";
    String oldValue = "old value";
    String newValue = "new value";
    Attr retAttr;

    IIOMetadataNode parent = new IIOMetadataNode("parent");
    MyAttrNode attrNode1 = new MyAttrNode(name, oldValue);
    MyAttrNode attrNode2 = new MyAttrNode(name, newValue);

    retAttr = parent.setAttributeNode(attrNode1);
    retAttr = parent.setAttributeNode(attrNode2);

    String actName = retAttr.getNodeName();
    String actValue = retAttr.getValue();

    if (!actName.equals(name) || !actValue.equals(oldValue)) {
        throw new RuntimeException("Test 2 failed: Invalid attribute " +
                                   "returned: " +
                                   "(name: " + actName +
                                   ", value: " + actValue + ")");
    }
}
 
源代码5 项目: openjdk-jdk8u-backup   文件: SetAttributeNode.java
public static void test2() {
    String name = "attr";
    String oldValue = "old value";
    String newValue = "new value";
    Attr retAttr;

    IIOMetadataNode parent = new IIOMetadataNode("parent");
    MyAttrNode attrNode1 = new MyAttrNode(name, oldValue);
    MyAttrNode attrNode2 = new MyAttrNode(name, newValue);

    retAttr = parent.setAttributeNode(attrNode1);
    retAttr = parent.setAttributeNode(attrNode2);

    String actName = retAttr.getNodeName();
    String actValue = retAttr.getValue();

    if (!actName.equals(name) || !actValue.equals(oldValue)) {
        throw new RuntimeException("Test 2 failed: Invalid attribute " +
                                   "returned: " +
                                   "(name: " + actName +
                                   ", value: " + actValue + ")");
    }
}
 
源代码6 项目: cxf   文件: W3CNamespaceContext.java
private String getPrefix(Element e, String uri) {
    NamedNodeMap attributes = e.getAttributes();
    if (attributes != null) {
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr a = (Attr)attributes.item(i);

            String val = a.getValue();
            if (val != null && val.equals(uri)) {
                String name = a.getNodeName();
                if ("xmlns".equals(name)) {
                    return "";
                }
                return name.substring(6);
            }
        }
    }

    Node n = e.getParentNode();
    if (n instanceof Element && n != e) {
        return getPrefix((Element)n, uri);
    }

    return null;
}
 
源代码7 项目: hottub   文件: DOMStreamReader.java
/**
 * If the given attribute is a namespace declaration for the given namespace URI,
 * return its prefix. Otherwise null.
 */
private static String getPrefixForAttr(Attr attr, String nsUri) {
    String attrName = attr.getNodeName();
    if (!attrName.startsWith("xmlns:") && !attrName.equals("xmlns"))
        return null;    // not nsdecl

    if(attr.getValue().equals(nsUri)) {
        if(attrName.equals("xmlns"))
            return "";
        String localName = attr.getLocalName();
        return (localName != null) ? localName :
            QName.valueOf(attrName).getLocalPart();
    }

    return null;
}
 
源代码8 项目: openjdk-8   文件: SetAttributeNode.java
public static void test2() {
    String name = "attr";
    String oldValue = "old value";
    String newValue = "new value";
    Attr retAttr;

    IIOMetadataNode parent = new IIOMetadataNode("parent");
    MyAttrNode attrNode1 = new MyAttrNode(name, oldValue);
    MyAttrNode attrNode2 = new MyAttrNode(name, newValue);

    retAttr = parent.setAttributeNode(attrNode1);
    retAttr = parent.setAttributeNode(attrNode2);

    String actName = retAttr.getNodeName();
    String actValue = retAttr.getValue();

    if (!actName.equals(name) || !actValue.equals(oldValue)) {
        throw new RuntimeException("Test 2 failed: Invalid attribute " +
                                   "returned: " +
                                   "(name: " + actName +
                                   ", value: " + actValue + ")");
    }
}
 
源代码9 项目: openjdk-jdk8u-backup   文件: DOMStreamReader.java
/**
 * If the given attribute is a namespace declaration for the given namespace URI,
 * return its prefix. Otherwise null.
 */
private static String getPrefixForAttr(Attr attr, String nsUri) {
    String attrName = attr.getNodeName();
    if (!attrName.startsWith("xmlns:") && !attrName.equals("xmlns"))
        return null;    // not nsdecl

    if(attr.getValue().equals(nsUri)) {
        if(attrName.equals("xmlns"))
            return "";
        String localName = attr.getLocalName();
        return (localName != null) ? localName :
            QName.valueOf(attrName).getLocalPart();
    }

    return null;
}
 
源代码10 项目: openjdk-jdk8u-backup   文件: C14nHelper.java
/**
 * This method throws an exception if the Attribute value contains
 * a relative URI.
 *
 * @param attr
 * @throws CanonicalizationException
 */
public static void assertNotRelativeNS(Attr attr) throws CanonicalizationException {
    if (attr == null) {
        return;
    }

    String nodeAttrName = attr.getNodeName();
    boolean definesDefaultNS = nodeAttrName.equals("xmlns");
    boolean definesNonDefaultNS = nodeAttrName.startsWith("xmlns:");

    if ((definesDefaultNS || definesNonDefaultNS) && namespaceIsRelative(attr)) {
        String parentName = attr.getOwnerElement().getTagName();
        String attrValue = attr.getValue();
        Object exArgs[] = { parentName, nodeAttrName, attrValue };

        throw new CanonicalizationException(
            "c14n.Canonicalizer.RelativeNamespace", exArgs
        );
    }
}
 
源代码11 项目: openjdk-jdk8u   文件: C14nHelper.java
/**
 * This method throws an exception if the Attribute value contains
 * a relative URI.
 *
 * @param attr
 * @throws CanonicalizationException
 */
public static void assertNotRelativeNS(Attr attr) throws CanonicalizationException {
    if (attr == null) {
        return;
    }

    String nodeAttrName = attr.getNodeName();
    boolean definesDefaultNS = nodeAttrName.equals("xmlns");
    boolean definesNonDefaultNS = nodeAttrName.startsWith("xmlns:");

    if ((definesDefaultNS || definesNonDefaultNS) && namespaceIsRelative(attr)) {
        String parentName = attr.getOwnerElement().getTagName();
        String attrValue = attr.getValue();
        Object exArgs[] = { parentName, nodeAttrName, attrValue };

        throw new CanonicalizationException(
            "c14n.Canonicalizer.RelativeNamespace", exArgs
        );
    }
}
 
源代码12 项目: openjdk-jdk9   文件: DOMStreamReader.java
/**
 * Called when the current node is {@link Element} to look at attribute list
 * (which contains both ns decl and attributes in DOM) and split them
 * to attributes-proper and namespace decls.
 */
protected void splitAttributes() {
    // Clear attribute and namespace lists
    _currentAttributes.clear();

    Scope scope = allocateScope();

    _namedNodeMap = _current.getAttributes();
    if (_namedNodeMap != null) {
        final int n = _namedNodeMap.getLength();
        for (int i = 0; i < n; i++) {
            final Attr attr = (Attr) _namedNodeMap.item(i);
            final String attrName = attr.getNodeName();
            if (attrName.startsWith("xmlns:") || attrName.equals("xmlns")) {     // NS decl?
                scope.currentNamespaces.add(attr);
            }
            else {
                _currentAttributes.add(attr);
            }
        }
    }

    // verify that all the namespaces used in element and attributes are indeed available
    ensureNs(_current);
    for( int i=_currentAttributes.size()-1; i>=0; i-- ) {
        Attr a = _currentAttributes.get(i);
        if(fixNull(a.getNamespaceURI()).length()>0)
            ensureNs(a);    // no need to declare "" for attributes in the default namespace
    }
}
 
源代码13 项目: openjdk-jdk8u-backup   文件: DOMStreamReader.java
/**
 * Called when the current node is {@link Element} to look at attribute list
 * (which contains both ns decl and attributes in DOM) and split them
 * to attributes-proper and namespace decls.
 */
protected void splitAttributes() {
    // Clear attribute and namespace lists
    _currentAttributes.clear();

    Scope scope = allocateScope();

    _namedNodeMap = _current.getAttributes();
    if (_namedNodeMap != null) {
        final int n = _namedNodeMap.getLength();
        for (int i = 0; i < n; i++) {
            final Attr attr = (Attr) _namedNodeMap.item(i);
            final String attrName = attr.getNodeName();
            if (attrName.startsWith("xmlns:") || attrName.equals("xmlns")) {     // NS decl?
                scope.currentNamespaces.add(attr);
            }
            else {
                _currentAttributes.add(attr);
            }
        }
    }

    // verify that all the namespaces used in element and attributes are indeed available
    ensureNs(_current);
    for( int i=_currentAttributes.size()-1; i>=0; i-- ) {
        Attr a = _currentAttributes.get(i);
        if(fixNull(a.getNamespaceURI()).length()>0)
            ensureNs(a);    // no need to declare "" for attributes in the default namespace
    }
}
 
源代码14 项目: hottub   文件: DOMStreamReader.java
/**
 * Called when the current node is {@link Element} to look at attribute list
 * (which contains both ns decl and attributes in DOM) and split them
 * to attributes-proper and namespace decls.
 */
protected void splitAttributes() {
    // Clear attribute and namespace lists
    _currentAttributes.clear();

    Scope scope = allocateScope();

    _namedNodeMap = _current.getAttributes();
    if (_namedNodeMap != null) {
        final int n = _namedNodeMap.getLength();
        for (int i = 0; i < n; i++) {
            final Attr attr = (Attr) _namedNodeMap.item(i);
            final String attrName = attr.getNodeName();
            if (attrName.startsWith("xmlns:") || attrName.equals("xmlns")) {     // NS decl?
                scope.currentNamespaces.add(attr);
            }
            else {
                _currentAttributes.add(attr);
            }
        }
    }

    // verify that all the namespaces used in element and attributes are indeed available
    ensureNs(_current);
    for( int i=_currentAttributes.size()-1; i>=0; i-- ) {
        Attr a = _currentAttributes.get(i);
        if(fixNull(a.getNamespaceURI()).length()>0)
            ensureNs(a);    // no need to declare "" for attributes in the default namespace
    }
}
 
源代码15 项目: Bytecoder   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
源代码16 项目: javaide   文件: SdkRepoSource.java
/**
 * 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;
}
 
源代码17 项目: openjdk-jdk8u   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
源代码18 项目: TencentKona-8   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
源代码19 项目: openjdk-jdk9   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}
 
源代码20 项目: JDKSourceCode1.8   文件: DOMUtil.java
/**
 * Copies the source tree into the specified place in a destination
 * tree. The source node and its children are appended as children
 * of the destination node.
 * <p>
 * <em>Note:</em> This is an iterative implementation.
 */
public static void copyInto(Node src, Node dest) throws DOMException {

    // get node factory
    Document factory = dest.getOwnerDocument();
    boolean domimpl = factory instanceof DocumentImpl;

    // placement variables
    Node start  = src;
    Node parent = src;
    Node place  = src;

    // traverse source tree
    while (place != null) {

        // copy this node
        Node node = null;
        int  type = place.getNodeType();
        switch (type) {
        case Node.CDATA_SECTION_NODE: {
            node = factory.createCDATASection(place.getNodeValue());
            break;
        }
        case Node.COMMENT_NODE: {
            node = factory.createComment(place.getNodeValue());
            break;
        }
        case Node.ELEMENT_NODE: {
            Element element = factory.createElement(place.getNodeName());
            node = element;
            NamedNodeMap attrs  = place.getAttributes();
            int attrCount = attrs.getLength();
            for (int i = 0; i < attrCount; i++) {
                Attr attr = (Attr)attrs.item(i);
                String attrName = attr.getNodeName();
                String attrValue = attr.getNodeValue();
                element.setAttribute(attrName, attrValue);
                if (domimpl && !attr.getSpecified()) {
                    ((AttrImpl)element.getAttributeNode(attrName)).setSpecified(false);
                }
            }
            break;
        }
        case Node.ENTITY_REFERENCE_NODE: {
            node = factory.createEntityReference(place.getNodeName());
            break;
        }
        case Node.PROCESSING_INSTRUCTION_NODE: {
            node = factory.createProcessingInstruction(place.getNodeName(),
                    place.getNodeValue());
            break;
        }
        case Node.TEXT_NODE: {
            node = factory.createTextNode(place.getNodeValue());
            break;
        }
        default: {
            throw new IllegalArgumentException("can't copy node type, "+
                    type+" ("+
                    place.getNodeName()+')');
        }
        }
        dest.appendChild(node);

        // iterate over children
        if (place.hasChildNodes()) {
            parent = place;
            place  = place.getFirstChild();
            dest   = node;
        }

        // advance
        else {
            place = place.getNextSibling();
            while (place == null && parent != start) {
                place  = parent.getNextSibling();
                parent = parent.getParentNode();
                dest   = dest.getParentNode();
            }
        }

    }

}