下面列出了怎么用org.w3c.dom.Comment的API类实例代码及写法,或者点击链接到github查看源代码。
private static boolean isNewFormatNode(Node node) {
// check for new node format - if the first non-whitespace node
// is an XML comment, and the comment includes
// one of the old element tags,
// then it is a generated node
NodeList children = node.getChildNodes();
int length = children.getLength();
for (int i = 0; i < length; i++) {
Node childNode = children.item(i);
if (childNode != null && childNode.getNodeType() == Node.COMMENT_NODE) {
String commentData = ((Comment) childNode).getData();
return MergeConstants.comentContainsTag(commentData);
}
}
return false;
}
public void serializeNode(Node node) throws XMLStreamException {
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
serializeDocument((Document) node);
break;
case Node.ELEMENT_NODE:
serializeElement((Element) node);
break;
case Node.CDATA_SECTION_NODE:
serializeCDATASection((CDATASection) node);
break;
case Node.TEXT_NODE:
serializeText((Text) node);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
serializeProcessingInstruction((ProcessingInstruction) node);
break;
case Node.COMMENT_NODE:
serializeComment((Comment) node);
break;
default:
throw new MarkLogicInternalException(
"Cannot process node type of: "+node.getClass().getName()
);
}
}
public static Element nextElement(Iterator iter) {
while (iter.hasNext()) {
Node n = (Node) iter.next();
if (n instanceof Text) {
Text t = (Text) n;
if (t.getData().trim().length() == 0)
continue;
fail("parsing.nonWhitespaceTextFound", t.getData().trim());
}
if (n instanceof Comment)
continue;
if (!(n instanceof Element))
fail("parsing.elementExpected");
return (Element) n;
}
return null;
}
public static Element nextElement(Iterator iter) {
while (iter.hasNext()) {
Node n = (Node) iter.next();
if (n instanceof Text) {
Text t = (Text) n;
if (t.getData().trim().length() == 0)
continue;
fail("parsing.nonWhitespaceTextFound", t.getData().trim());
}
if (n instanceof Comment)
continue;
if (!(n instanceof Element))
fail("parsing.elementExpected");
return (Element) n;
}
return null;
}
/**
* Extracts the text value from the given DOM element, ignoring XML comments.
* <p>Appends all CharacterData nodes and EntityReference nodes into a single
* String value, excluding Comment nodes. Only exposes actual user-specified
* text, no default values of any kind.
* @see CharacterData
* @see EntityReference
* @see Comment
*/
public static String getTextValue(Element valueEle) {
Assert.notNull(valueEle, "Element must not be null");
StringBuilder sb = new StringBuilder();
NodeList nl = valueEle.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node item = nl.item(i);
if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
sb.append(item.getNodeValue());
}
}
return sb.toString();
}
/**
* 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);
}
private void writeNodePlain(Writer out, Node node, int level) throws FHIRException, DOMException, IOException {
if (node.getNodeType() == Node.ELEMENT_NODE)
writeElementPlain(out, (Element) node, level);
else if (node.getNodeType() == Node.TEXT_NODE)
writeTextPlain(out, (Text) node, level);
else if (node.getNodeType() == Node.COMMENT_NODE)
writeCommentPlain(out, (Comment) node, level);
else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE)
writeProcessingInstructionPlain(out, (ProcessingInstruction) node);
else if (node.getNodeType() != Node.ATTRIBUTE_NODE)
throw new FHIRException("Unhandled node type");
}
public static Element nextElementIgnoringCharacterContent(Iterator iter) {
while (iter.hasNext()) {
Node n = (Node) iter.next();
if (n instanceof Text)
continue;
if (n instanceof Comment)
continue;
if (!(n instanceof Element))
fail("parsing.elementExpected");
return (Element) n;
}
return null;
}
private void testCommentContent(boolean expectDifference) {
Element control = document.createElement("foo");
Element test = document.createElement("foo");
Comment c = document.createComment("bar");
control.appendChild(c);
Comment c2 = document.createComment("baz");
test.appendChild(c2);
engine.compare(control, test, listener, null);
assertEquals(expectDifference, listener.different);
}
private boolean hasNewline(Node n) {
if (n instanceof Comment) {
return false;
}
CharacterData c = (CharacterData) n;
return (-1) != c.getData().indexOf('\n');
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @throws IOException
*/
private void outputCommentToWriter(Comment currentComment) throws IOException {
if (currentComment == null) {
return;
}
this.writer.write("<!--");
String data = currentComment.getData();
int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
switch (c) {
case 0x0D:
this.writer.write("&#xD;");
break;
case ' ':
this.writer.write("·");
break;
case '\n':
this.writer.write("¶\n");
break;
default:
this.writer.write(c);
break;
}
}
this.writer.write("-->");
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @param writer writer where to write the things
* @throws IOException
*/
protected void outputCommentToWriter(
Comment currentComment, OutputStream writer, int position
) throws IOException {
if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
writer.write('\n');
}
writer.write(BEGIN_COMM.clone());
final String data = currentComment.getData();
final int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
if (c == 0x0D) {
writer.write(XD.clone());
} else {
if (c < 0x80) {
writer.write(c);
} else {
UtfHelpper.writeCharToUtf8(c, writer);
}
}
}
writer.write(END_COMM.clone());
if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
writer.write('\n');
}
}
public void print(Node node) throws XMLStreamException {
switch (node.getNodeType()) {
case Node.DOCUMENT_NODE:
visitDocument((Document) node);
break;
case Node.DOCUMENT_FRAGMENT_NODE:
visitDocumentFragment((DocumentFragment) node);
break;
case Node.ELEMENT_NODE:
visitElement((Element) node);
break;
case Node.TEXT_NODE:
visitText((Text) node);
break;
case Node.CDATA_SECTION_NODE:
visitCDATASection((CDATASection) node);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
visitProcessingInstruction((ProcessingInstruction) node);
break;
case Node.ENTITY_REFERENCE_NODE:
visitReference((EntityReference) node);
break;
case Node.COMMENT_NODE:
visitComment((Comment) node);
break;
case Node.DOCUMENT_TYPE_NODE:
break;
case Node.ATTRIBUTE_NODE:
case Node.ENTITY_NODE:
default:
throw new XMLStreamException("Unexpected DOM Node Type "
+ node.getNodeType()
);
}
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @throws IOException
*/
private void outputCommentToWriter(Comment currentComment) throws IOException {
if (currentComment == null) {
return;
}
this.writer.write("<!--");
String data = currentComment.getData();
int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
switch (c) {
case 0x0D:
this.writer.write("&#xD;");
break;
case ' ':
this.writer.write("·");
break;
case '\n':
this.writer.write("¶\n");
break;
default:
this.writer.write(c);
break;
}
}
this.writer.write("-->");
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @param writer writer where to write the things
* @throws IOException
*/
protected void outputCommentToWriter(
Comment currentComment, OutputStream writer, int position
) throws IOException {
if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
writer.write('\n');
}
writer.write(BEGIN_COMM.clone());
final String data = currentComment.getData();
final int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
if (c == 0x0D) {
writer.write(XD.clone());
} else {
if (c < 0x80) {
writer.write(c);
} else {
UtfHelpper.writeCharToUtf8(c, writer);
}
}
}
writer.write(END_COMM.clone());
if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
writer.write('\n');
}
}
private void assertDifferentComments(Comment control, Comment test,
Difference difference) {
try {
engine.compareComment(control, test, listener);
} catch (DifferenceEngine.DifferenceFoundException e) {
}
assertEquals(difference.getId(), listener.comparingWhat);
assertEquals(true, listener.different);
resetListener();
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @param writer writer where to write the things
* @throws IOException
*/
protected void outputCommentToWriter(
Comment currentComment, OutputStream writer, int position
) throws IOException {
if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
writer.write('\n');
}
writer.write(BEGIN_COMM.clone());
final String data = currentComment.getData();
final int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
if (c == 0x0D) {
writer.write(XD.clone());
} else {
if (c < 0x80) {
writer.write(c);
} else {
UtfHelpper.writeCharToUtf8(c, writer);
}
}
}
writer.write(END_COMM.clone());
if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
writer.write('\n');
}
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @param writer writer where to write the things
* @throws IOException
*/
protected void outputCommentToWriter(
Comment currentComment, OutputStream writer, int position
) throws IOException {
if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
writer.write('\n');
}
writer.write(BEGIN_COMM.clone());
final String data = currentComment.getData();
final int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
if (c == 0x0D) {
writer.write(XD.clone());
} else {
if (c < 0x80) {
writer.write(c);
} else {
UtfHelpper.writeCharToUtf8(c, writer);
}
}
}
writer.write(END_COMM.clone());
if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
writer.write('\n');
}
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @throws IOException
*/
private void outputCommentToWriter(Comment currentComment) throws IOException {
if (currentComment == null) {
return;
}
this.writer.write("<!--");
String data = currentComment.getData();
int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
switch (c) {
case 0x0D:
this.writer.write("&#xD;");
break;
case ' ':
this.writer.write("·");
break;
case '\n':
this.writer.write("¶\n");
break;
default:
this.writer.write(c);
break;
}
}
this.writer.write("-->");
}
private void outputCoIw(Node p) throws DOMException, SAXException {
if (p instanceof Comment) {
String cmt = ((Comment) p).getData();
if (!lineEnd.equals("\n")) {
cmt = cmt.replace("\n", lineEnd);
}
cc.comment(cmt.toCharArray(), 0, cmt.length());
} else {
String s = p.getTextContent();
cc.characters(s.toCharArray(), 0, s.length());
}
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @throws IOException
*/
private void outputCommentToWriter(Comment currentComment) throws IOException {
if (currentComment == null) {
return;
}
this.writer.write("<!--");
String data = currentComment.getData();
int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
switch (c) {
case 0x0D:
this.writer.write("&#xD;");
break;
case ' ':
this.writer.write("·");
break;
case '\n':
this.writer.write("¶\n");
break;
default:
this.writer.write(c);
break;
}
}
this.writer.write("-->");
}
/**
* Unimplemented. See org.w3c.dom.Document
*
* @param data Data for comment
*
* @return null
*/
public Comment createComment(String data)
{
error(XMLErrorResources.ER_FUNCTION_NOT_SUPPORTED);
return null;
}
private static Element getOrCreateRootElement(AuxiliaryConfiguration conf, boolean shared) {
Element el = conf.getConfigurationFragment(ROOT, NAMESPACE, shared);
if (el == null) {
el = XMLUtil.createDocument(ROOT, NAMESPACE, null, null).getDocumentElement();
if (shared) {
Comment comment = el.getOwnerDocument().createComment("\nProperties that influence various parts of the IDE, especially code formatting and the like. \n" + //NOI18N
"You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.\n" + //NOI18N
"That way multiple projects can share the same settings (useful for formatting rules for example).\n" + //NOI18N
"Any value defined here will override the pom.xml file value but is only applicable to the current project.\n"); //NOI18N
el.appendChild(comment);
}
}
return el;
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @throws IOException
*/
private void outputCommentToWriter(Comment currentComment) throws IOException {
if (currentComment == null) {
return;
}
this.writer.write("<!--");
String data = currentComment.getData();
int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
switch (c) {
case 0x0D:
this.writer.write("&#xD;");
break;
case ' ':
this.writer.write("·");
break;
case '\n':
this.writer.write("¶\n");
break;
default:
this.writer.write(c);
break;
}
}
this.writer.write("-->");
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @throws IOException
*/
private void outputCommentToWriter(Comment currentComment) throws IOException {
if (currentComment == null) {
return;
}
this.writer.write("<!--");
String data = currentComment.getData();
int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
switch (c) {
case 0x0D:
this.writer.write("&#xD;");
break;
case ' ':
this.writer.write("·");
break;
case '\n':
this.writer.write("¶\n");
break;
default:
this.writer.write(c);
break;
}
}
this.writer.write("-->");
}
/**
* Method outputCommentToWriter
*
* @param currentComment
* @param writer writer where to write the things
* @throws IOException
*/
protected void outputCommentToWriter(
Comment currentComment, OutputStream writer, int position
) throws IOException {
if (position == NODE_AFTER_DOCUMENT_ELEMENT) {
writer.write('\n');
}
writer.write(BEGIN_COMM.clone());
final String data = currentComment.getData();
final int length = data.length();
for (int i = 0; i < length; i++) {
char c = data.charAt(i);
if (c == 0x0D) {
writer.write(XD.clone());
} else {
if (c < 0x80) {
writer.write(c);
} else {
UtfHelpper.writeCharToUtf8(c, writer);
}
}
}
writer.write(END_COMM.clone());
if (position == NODE_BEFORE_DOCUMENT_ELEMENT) {
writer.write('\n');
}
}
private void _writeComment (@Nonnull final XMLEmitter aXMLWriter, @Nonnull final Comment aComment)
{
if (m_aSettings.getSerializeComments ().isEmit ())
{
final String sComment = aComment.getData ();
aXMLWriter.onComment (sComment);
if (sComment.indexOf ('\n') >= 0)
{
// Newline only after multi-line comments
aXMLWriter.newLine ();
}
}
}
/**
* Compares the two given nodes. This method delegates to one of the given methods depending
* on the expected node type:
*
* <ul>
* <li>{@link #compareCDATASectionNode(CDATASection, Node)}</li>
* <li>{@link #compareTextNode(Text, Node)}</li>
* <li>{@link #compareCommentNode(Comment, Node)}</li>
* <li>{@link #compareProcessingInstructionNode(ProcessingInstruction, Node)}</li>
* <li>For all other types, {@link #compareNames(Node, Node)} and
* {@link #compareAttributes(Node, Node)}</li>
* </ul>
*
* Then this method invokes itself recursively for every children,
* by a call to {@link #compareChildren(Node, Node)}.
*
* @param expected the expected node.
* @param actual the node to compare.
*/
protected void compareNode(final Node expected, final Node actual) {
if (expected == null || actual == null) {
fail(formatErrorMessage(expected, actual));
return;
}
/*
* Check text value for types:
* TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE, PROCESSING_INSTRUCTION_NODE
*/
if (expected instanceof CDATASection) {
compareCDATASectionNode((CDATASection) expected, actual);
} else if (expected instanceof Text) {
compareTextNode((Text) expected, actual);
} else if (expected instanceof Comment) {
compareCommentNode((Comment) expected, actual);
} else if (expected instanceof ProcessingInstruction) {
compareProcessingInstructionNode((ProcessingInstruction) expected, actual);
} else if (expected instanceof Attr) {
compareAttributeNode((Attr) expected, actual);
} else {
compareNames(expected, actual);
compareAttributes(expected, actual);
}
/*
* Check child nodes recursivly if it's not an attribute.
*/
if (expected.getNodeType() != Node.ATTRIBUTE_NODE) {
compareChildren(expected, actual);
}
}
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);
}
/**
* 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");
}