Java Code Examples for org.w3c.dom.Comment

The following examples show how to use org.w3c.dom.Comment. These examples are extracted from open source projects. 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 Project: mapper-generator-javafx   Source File: XmlFileMergerJaxp.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source Project: java-client-api   Source File: DOMWriter.java    License: Apache License 2.0 6 votes vote down vote up
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()
      );
  }
}
 
Example 3
Source Project: jdk8u60   Source File: Util.java    License: GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 4
Source Project: openjdk-jdk9   Source File: Util.java    License: GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example 5
Source Project: spring-analysis-note   Source File: DomUtils.java    License: MIT License 5 votes vote down vote up
/**
 * 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();
}
 
Example 6
Source Project: netbeans   Source File: WebFreeFormActionProvider.java    License: 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 7
Source Project: org.hl7.fhir.core   Source File: XhtmlGenerator.java    License: Apache License 2.0 5 votes vote down vote up
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");
}
 
Example 8
Source Project: openjdk-jdk9   Source File: Util.java    License: GNU General Public License v2.0 5 votes vote down vote up
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;
}
 
Example 9
Source Project: xmlunit   Source File: test_NewDifferenceEngine.java    License: Apache License 2.0 5 votes vote down vote up
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);
}
 
Example 10
private boolean hasNewline(Node n) {
  if (n instanceof Comment) {
    return false;
  }
  CharacterData c = (CharacterData) n;
  return (-1) != c.getData().indexOf('\n');
}
 
Example 11
/**
 * Method outputCommentToWriter
 *
 * @param currentComment
 * @throws IOException
 */
private void outputCommentToWriter(Comment currentComment) throws IOException {

    if (currentComment == null) {
        return;
    }

    this.writer.write("&lt;!--");

    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("&amp;#xD;");
            break;

        case ' ':
            this.writer.write("&middot;");
            break;

        case '\n':
            this.writer.write("&para;\n");
            break;

        default:
            this.writer.write(c);
            break;
        }
    }

    this.writer.write("--&gt;");
}
 
Example 12
Source Project: jdk1.8-source-analysis   Source File: CanonicalizerBase.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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');
    }
}
 
Example 13
Source Project: openjdk-jdk8u   Source File: DOMPrinter.java    License: GNU General Public License v2.0 5 votes vote down vote up
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()
        );
    }
}
 
Example 14
/**
 * Method outputCommentToWriter
 *
 * @param currentComment
 * @throws IOException
 */
private void outputCommentToWriter(Comment currentComment) throws IOException {

    if (currentComment == null) {
        return;
    }

    this.writer.write("&lt;!--");

    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("&amp;#xD;");
            break;

        case ' ':
            this.writer.write("&middot;");
            break;

        case '\n':
            this.writer.write("&para;\n");
            break;

        default:
            this.writer.write(c);
            break;
        }
    }

    this.writer.write("--&gt;");
}
 
Example 15
/**
 * 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');
    }
}
 
Example 16
Source Project: xmlunit   Source File: test_DifferenceEngine.java    License: Apache License 2.0 5 votes vote down vote up
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();
}
 
Example 17
/**
 * 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');
    }
}
 
Example 18
Source Project: openjdk-8   Source File: CanonicalizerBase.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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');
    }
}
 
Example 19
/**
 * Method outputCommentToWriter
 *
 * @param currentComment
 * @throws IOException
 */
private void outputCommentToWriter(Comment currentComment) throws IOException {

    if (currentComment == null) {
        return;
    }

    this.writer.write("&lt;!--");

    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("&amp;#xD;");
            break;

        case ' ':
            this.writer.write("&middot;");
            break;

        case '\n':
            this.writer.write("&para;\n");
            break;

        default:
            this.writer.write(c);
            break;
        }
    }

    this.writer.write("--&gt;");
}
 
Example 20
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());
  }

}
 
Example 21
/**
 * Method outputCommentToWriter
 *
 * @param currentComment
 * @throws IOException
 */
private void outputCommentToWriter(Comment currentComment) throws IOException {

    if (currentComment == null) {
        return;
    }

    this.writer.write("&lt;!--");

    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("&amp;#xD;");
            break;

        case ' ':
            this.writer.write("&middot;");
            break;

        case '\n':
            this.writer.write("&para;\n");
            break;

        default:
            this.writer.write(c);
            break;
        }
    }

    this.writer.write("--&gt;");
}
 
Example 22
Source Project: TencentKona-8   Source File: UnImplNode.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example 23
Source Project: netbeans   Source File: MavenProjectPropsImpl.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 24
/**
 * Method outputCommentToWriter
 *
 * @param currentComment
 * @throws IOException
 */
private void outputCommentToWriter(Comment currentComment) throws IOException {

    if (currentComment == null) {
        return;
    }

    this.writer.write("&lt;!--");

    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("&amp;#xD;");
            break;

        case ' ':
            this.writer.write("&middot;");
            break;

        case '\n':
            this.writer.write("&para;\n");
            break;

        default:
            this.writer.write(c);
            break;
        }
    }

    this.writer.write("--&gt;");
}
 
Example 25
/**
 * Method outputCommentToWriter
 *
 * @param currentComment
 * @throws IOException
 */
private void outputCommentToWriter(Comment currentComment) throws IOException {

    if (currentComment == null) {
        return;
    }

    this.writer.write("&lt;!--");

    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("&amp;#xD;");
            break;

        case ' ':
            this.writer.write("&middot;");
            break;

        case '\n':
            this.writer.write("&para;\n");
            break;

        default:
            this.writer.write(c);
            break;
        }
    }

    this.writer.write("--&gt;");
}
 
Example 26
Source Project: jdk8u60   Source File: CanonicalizerBase.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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');
    }
}
 
Example 27
Source Project: ph-commons   Source File: XMLSerializer.java    License: Apache License 2.0 5 votes vote down vote up
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 ();
    }
  }
}
 
Example 28
Source Project: sis   Source File: DocumentComparator.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}
 
Example 29
Source Project: netbeans   Source File: JavaActions.java    License: 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 30
/**
 * 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");

}