下面列出了org.w3c.dom.Document#createComment ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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());
}
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);
}
/**
* 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);
}
/**
* 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");
}
/**
* 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");
}
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;
}
/**
* 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();
}
}
}
}
/**
* 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();
}
}
}
}
/**
* 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;
}
/**
* 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();
}
}
}
}
/**
* 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();
}
}
}
}
private static void appendXmlFragment(Node parent, String fragment){
Document doc = parent.getOwnerDocument();
Comment comment = doc.createComment(fragment);
parent.appendChild(comment);
}
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;
}
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;
}
/**
* 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();
}
}
}
}
/**
* 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;
}
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;
}
/**
* 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();
}
}
}
}
/**
* 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();
}
}
}
}
private static void appendXmlFragment(Node parent, String fragment) {
Document doc = parent.getOwnerDocument();
Comment comment = doc.createComment(fragment);
parent.appendChild(comment);
}