Java Code Examples for org.w3c.dom.Document#createComment()

The following examples show how to use org.w3c.dom.Document#createComment() . 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: test_XpathNodeTracker.java    From xmlunit with Apache License 2.0 5 votes vote down vote up
public void testNodes() throws Exception {
    Document doc = XMLUnit.newControlParser().newDocument();
    Element element = doc.createElementNS("http://example.com/xmlunit", "eg:root");
    xpathNodeTracker.visited(element);
    assertEquals("root element", "/root[1]", xpathNodeTracker.toXpathString());
            
    Attr attr = doc.createAttributeNS("http://example.com/xmlunit", "eg:type");
    attr.setValue("qwerty");
    element.setAttributeNodeNS(attr);
    xpathNodeTracker.visited(attr);
    assertEquals("root element attribute", "/root[1]/@type", xpathNodeTracker.toXpathString());             
            
    xpathNodeTracker.indent();
            
    Comment comment = doc.createComment("testing a comment");
    xpathNodeTracker.visited(comment);
    assertEquals("comment", "/root[1]/comment()[1]", xpathNodeTracker.toXpathString());

    ProcessingInstruction pi = doc.createProcessingInstruction("target","data");
    xpathNodeTracker.visited(pi);
    assertEquals("p-i", "/root[1]/processing-instruction()[1]", xpathNodeTracker.toXpathString());

    Text text = doc.createTextNode("some text");
    xpathNodeTracker.visited(text);
    assertEquals("text", "/root[1]/text()[1]", xpathNodeTracker.toXpathString());

    CDATASection cdata = doc.createCDATASection("some characters");
    xpathNodeTracker.visited(cdata);
    assertEquals("cdata", "/root[1]/text()[2]", xpathNodeTracker.toXpathString());
}
 
Example 2
Source File: DOMConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state and input values orientation
 * for public void setParameter(String name, Object value) throws
 * DOMException, <br>
 * <b>pre-conditions</b>: the root element has two Comment nodes, <br>
 * <b>name</b>: comments <br>
 * <b>value</b>: false. <br>
 * <b>Expected results</b>: the root element has no children
 */
@Test
public void testComments002() {
    DOMImplementation domImpl = null;
    try {
        domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
    } catch (ParserConfigurationException pce) {
        Assert.fail(pce.toString());
    } catch (FactoryConfigurationError fce) {
        Assert.fail(fce.toString());
    }

    Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);

    Comment comment1 = doc.createComment("comment1");
    Comment comment2 = doc.createComment("comment2");

    DOMConfiguration config = doc.getDomConfig();
    config.setParameter("comments", Boolean.FALSE);

    Element root = doc.getDocumentElement();
    root.appendChild(comment1);
    root.appendChild(comment2);

    doc.normalizeDocument();

    if (root.getFirstChild() != null) {
        Assert.fail("root has a child " + root.getFirstChild() + ", but expected to has none");
    }

    return; // Status.passed("OK");

}
 
Example 3
Source File: JavaActions.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private AntLocation handleInitials(Document doc, Lookup context) {
    ensurePropertiesCopied(doc.getDocumentElement());
    Comment comm = doc.createComment(" " + NbBundle.getMessage(JavaActions.class, "COMMENT_edit_target") + " ");
    doc.getDocumentElement().appendChild(comm);
    comm = doc.createComment(" " + NbBundle.getMessage(JavaActions.class, "COMMENT_more_info_run.single") + " ");
    doc.getDocumentElement().appendChild(comm);
    return findPackageRoot(context);
}
 
Example 4
Source File: DOMConfigurationTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Equivalence class partitioning with state and input values orientation
 * for public void setParameter(String name, Object value) throws
 * DOMException, <br>
 * <b>pre-conditions</b>: the root element has two Comment nodes, <br>
 * <b>name</b>: comments <br>
 * <b>value</b>: true. <br>
 * <b>Expected results</b>: the Comment nodes belong to the root element
 */
@Test
public void testComments001() {
    DOMImplementation domImpl = null;
    try {
        domImpl = DocumentBuilderFactory.newInstance().newDocumentBuilder().getDOMImplementation();
    } catch (ParserConfigurationException pce) {
        Assert.fail(pce.toString());
    } catch (FactoryConfigurationError fce) {
        Assert.fail(fce.toString());
    }

    Document doc = domImpl.createDocument("namespaceURI", "ns:root", null);

    Comment comment1 = doc.createComment("comment1");
    Comment comment2 = doc.createComment("comment2");

    DOMConfiguration config = doc.getDomConfig();
    config.setParameter("comments", Boolean.TRUE);

    Element root = doc.getDocumentElement();
    root.appendChild(comment1);
    root.appendChild(comment2);

    setHandler(doc);
    doc.normalizeDocument();

    if (comment1.getParentNode() != root) {
        Assert.fail("comment1 is attached to " + comment1.getParentNode() + ", but expected to be a child of root");
    }

    if (comment2.getParentNode() != root) {
        Assert.fail("comment1 is attached to " + comment2.getParentNode() + ", but expected to be a child of root");
    }

    return; // Status.passed("OK");

}
 
Example 5
Source File: WebFreeFormActionProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
 * Appends the comments to script.
 * @param script Script to write to.
 */
private void writeComments(Document script) {
    Comment comm4Edit = script.createComment(" " + NbBundle.getMessage(WebFreeFormActionProvider.class, "COMMENT_edit_target") + " "); // NOI18N
    Comment comm4Info = script.createComment(" " + NbBundle.getMessage(WebFreeFormActionProvider.class, "COMMENT_more_info_debug") + " "); // NOI18N

    Element scriptRoot = script.getDocumentElement();
    scriptRoot.appendChild(comm4Edit);
    scriptRoot.appendChild(comm4Info);
}
 
Example 6
Source File: DynamicClientFactory.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Node cloneNode(Document document, Node node, boolean deep) throws DOMException {
    if (document == null || node == null) {
        return null;
    }
    int type = node.getNodeType();

    if (node.getOwnerDocument() == document) {
        return node.cloneNode(deep);
    }
    Node clone;
    switch (type) {
    case Node.CDATA_SECTION_NODE:
        clone = document.createCDATASection(node.getNodeValue());
        break;
    case Node.COMMENT_NODE:
        clone = document.createComment(node.getNodeValue());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        clone = document.createEntityReference(node.getNodeName());
        break;
    case Node.ELEMENT_NODE:
        clone = document.createElement(node.getNodeName());
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            ((Element)clone).setAttribute(attributes.item(i).getNodeName(), attributes.item(i)
                .getNodeValue());
        }
        break;

    case Node.TEXT_NODE:
        clone = document.createTextNode(node.getNodeValue());
        break;
    default:
        return null;
    }
    if (deep && type == Node.ELEMENT_NODE) {
        Node child = node.getFirstChild();
        while (child != null) {
            clone.appendChild(cloneNode(document, child, true));
            child = child.getNextSibling();
        }
    }
    return clone;
}
 
Example 7
Source File: ExtensionModuleTrimmer.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 4 votes vote down vote up
private static void appendXmlFragment(Node parent, String fragment) {
	Document doc = parent.getOwnerDocument();
	Comment comment = doc.createComment(fragment);
	parent.appendChild(comment);
}
 
Example 8
Source File: DOMUtil.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
            }
        }

    }

}
 
Example 9
Source File: DOMUtil.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
            }
        }

    }

}
 
Example 10
Source File: ProcessorUtil.java    From cxf with Apache License 2.0 4 votes vote down vote up
public static Node cloneNode(Document document, Node node, boolean deep) throws DOMException {
    if (document == null || node == null) {
        return null;
    }
    int type = node.getNodeType();

    if (node.getOwnerDocument() == document) {
        return node.cloneNode(deep);
    }
    Node clone;
    switch (type) {
    case Node.CDATA_SECTION_NODE:
        clone = document.createCDATASection(node.getNodeValue());
        break;
    case Node.COMMENT_NODE:
        clone = document.createComment(node.getNodeValue());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        clone = document.createEntityReference(node.getNodeName());
        break;
    case Node.ELEMENT_NODE:
        clone = document.createElementNS(node.getNamespaceURI(), node.getNodeName());
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Attr attr = (Attr)attributes.item(i);
            Attr attrnew =
                ((Element)clone).getOwnerDocument().createAttributeNS(attr.getNamespaceURI(),
                                                                  attr.getNodeName());
            attrnew.setValue(attr.getNodeValue());
            ((Element)clone).setAttributeNodeNS(attrnew);
        }
        break;

    case Node.TEXT_NODE:
        clone = document.createTextNode(node.getNodeValue());
        break;
    default:
        return null;
    }
    if (deep && type == Node.ELEMENT_NODE) {
        Node child = node.getFirstChild();
        while (child != null) {
            clone.appendChild(cloneNode(document, child, true));
            child = child.getNextSibling();
        }
    }
    return clone;
}
 
Example 11
Source File: Configuration.java    From RDFS with Apache License 2.0 4 votes vote down vote up
/**
 * Return the XML DOM corresponding to this Configuration.
 */
private synchronized Document asXmlDocument() throws IOException {
  Document doc;
  try {
    doc =
      DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
  } catch (ParserConfigurationException pe) {
    throw new IOException(pe);
  }
  Element conf = doc.createElement("configuration");
  doc.appendChild(conf);
  conf.appendChild(doc.createTextNode("\n"));
  Properties properties = getProps();
  for (Enumeration e = properties.keys(); e.hasMoreElements();) {
    String name = (String)e.nextElement();
    Object object = properties.get(name);
    String value = null;
    if (object instanceof String) {
      value = (String) object;
    }else {
      continue;
    }
    Element propNode = doc.createElement("property");
    conf.appendChild(propNode);

    if (updatingResource != null) {
      Comment commentNode = doc.createComment(
        "Loaded from " + updatingResource.get(name));
      propNode.appendChild(commentNode);
    }
    Element nameNode = doc.createElement("name");
    nameNode.appendChild(doc.createTextNode(name));
    propNode.appendChild(nameNode);

    Element valueNode = doc.createElement("value");
    valueNode.appendChild(doc.createTextNode(value));
    propNode.appendChild(valueNode);

    conf.appendChild(doc.createTextNode("\n"));
  }
  return doc;
}
 
Example 12
Source File: DOMUtil.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
            }
        }

    }

}
 
Example 13
Source File: XmlGenerator.java    From ecs-sync with Apache License 2.0 4 votes vote down vote up
private static <C> Element createDefaultElement(Document document, ConfigWrapper<C> configWrapper, String name, boolean addComments, boolean advancedOptions)
        throws IllegalAccessException, InstantiationException {

    // create main element
    if (name == null) name = initialLowerCase(configWrapper.getTargetClass().getSimpleName());
    Element mainElement = document.createElement(name);

    // create object instance for defaults
    C object = configWrapper.getTargetClass().newInstance();
    BeanWrapper beanWrapper = PropertyAccessorFactory.forBeanPropertyAccess(object);

    List<ConfigPropertyWrapper> propertyWrappers = new ArrayList<>();
    for (String property : configWrapper.propertyNames()) {
        propertyWrappers.add(configWrapper.getPropertyWrapper(property));
    }
    Collections.sort(propertyWrappers, new Comparator<ConfigPropertyWrapper>() {
        @Override
        public int compare(ConfigPropertyWrapper o1, ConfigPropertyWrapper o2) {
            return o1.getOrderIndex() - o2.getOrderIndex();
        }
    });

    for (ConfigPropertyWrapper propertyWrapper : propertyWrappers) {
        if (propertyWrapper.isAdvanced() && !advancedOptions) continue;

        Object defaultValue = beanWrapper.getPropertyValue(propertyWrapper.getName());

        // create XMl comment[s]
        if (addComments) {
            Comment comment = document.createComment(" " + propertyWrapper.getDescription() + " ");
            mainElement.appendChild(comment);
            String specString = propertyWrapper.getDescriptor().getPropertyType().getSimpleName();
            if (propertyWrapper.isRequired()) specString += " - Required";
            if (propertyWrapper.getDescriptor().getPropertyType().isArray())
                specString += " - Repeat for multiple values";
            if (propertyWrapper.getValueList() != null && propertyWrapper.getValueList().length > 0)
                specString += " - Values: " + Arrays.toString(propertyWrapper.getValueList());
            else if (propertyWrapper.getDescriptor().getPropertyType().isEnum())
                specString += " - Values: " + Arrays.toString(propertyWrapper.getDescriptor().getPropertyType().getEnumConstants());
            if (defaultValue != null) specString += " - Default: " + defaultValue;
            comment = document.createComment(" " + specString + " ");
            mainElement.appendChild(comment);
        }

        // create XMl element
        Element propElement = document.createElement(propertyWrapper.getName());

        // set default value
        String defaultValueStr = propertyWrapper.getValueHint();
        if (defaultValue != null) defaultValueStr = conversionService.convert(defaultValue, String.class);
        if (defaultValueStr == null || defaultValueStr.length() == 0) defaultValueStr = propertyWrapper.getName();
        propElement.appendChild(document.createTextNode(defaultValueStr));

        // add to parent element
        mainElement.appendChild(propElement);
    }

    return mainElement;
}
 
Example 14
Source File: JAXBDataBinding.java    From cxf with Apache License 2.0 4 votes vote down vote up
public Node cloneNode(Document document, Node node, boolean deep) throws DOMException {
    if (document == null || node == null) {
        return null;
    }
    int type = node.getNodeType();

    if (node.getOwnerDocument() == document) {
        return node.cloneNode(deep);
    }
    Node clone;
    switch (type) {
    case Node.CDATA_SECTION_NODE:
        clone = document.createCDATASection(node.getNodeValue());
        break;
    case Node.COMMENT_NODE:
        clone = document.createComment(node.getNodeValue());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        clone = document.createEntityReference(node.getNodeName());
        break;
    case Node.ELEMENT_NODE:
        clone = document.createElement(node.getNodeName());
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            ((Element)clone).setAttributeNS(attributes.item(i).getNamespaceURI(),
                                            attributes.item(i).getNodeName(),
                                            attributes.item(i).getNodeValue());
        }
        try {
            clone.setUserData("location", node.getUserData("location"), null);
        } catch (Throwable t) {
            //non DOM level 3
        }
        break;

    case Node.TEXT_NODE:
        clone = document.createTextNode(node.getNodeValue());
        break;
    default:
        return null;
    }
    if (deep && type == Node.ELEMENT_NODE) {
        Node child = node.getFirstChild();
        while (child != null) {
            clone.appendChild(cloneNode(document, child, true));
            child = child.getNextSibling();
        }
    }
    return clone;
}
 
Example 15
Source File: FixProjectsUtils.java    From hybris-commerce-eclipse-plugin with Apache License 2.0 4 votes vote down vote up
private static void appendXmlFragment(Node parent, String fragment){
	Document doc = parent.getOwnerDocument();
	Comment comment = doc.createComment(fragment);
	parent.appendChild(comment);
}
 
Example 16
Source File: DOMUtil.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
            }
        }

    }

}
 
Example 17
Source File: DOMUtil.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
            }
        }

    }

}
 
Example 18
Source File: Configuration.java    From anima with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Return the XML DOM corresponding to this Configuration.
 */
private synchronized Document asXmlDocument() throws IOException {
	Document doc;
	Properties properties = getProps();
	try {
		doc = DocumentBuilderFactory.newInstance().newDocumentBuilder()
				.newDocument();
	} catch (ParserConfigurationException pe) {
		throw new IOException(pe);
	}
	Element conf = doc.createElement("configuration");
	doc.appendChild(conf);
	conf.appendChild(doc.createTextNode("\n"));
	for (Enumeration<Object> e = properties.keys(); e.hasMoreElements();) {
		String name = (String) e.nextElement();
		Object object = properties.get(name);
		String value = null;
		if (object instanceof String) {
			value = (String) object;
		} else {
			continue;
		}
		Element propNode = doc.createElement("property");
		conf.appendChild(propNode);
		if (updatingResource != null) {
			org.w3c.dom.Comment commentNode = doc.createComment("Loaded from "
					+ updatingResource.get(name));
			propNode.appendChild(commentNode);
		}
		Element nameNode = doc.createElement("name");
		nameNode.appendChild(doc.createTextNode(name));
		propNode.appendChild(nameNode);

		Element valueNode = doc.createElement("value");
		valueNode.appendChild(doc.createTextNode(value));
		propNode.appendChild(valueNode);

		conf.appendChild(doc.createTextNode("\n"));
	}
	return doc;
}
 
Example 19
Source File: DOMUtil.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
            }
        }

    }

}
 
Example 20
Source File: DOMUtil.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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();
            }
        }

    }

}