org.w3c.dom.Comment Java Examples

The following examples show how to use org.w3c.dom.Comment. 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: DOMWriter.java    From java-client-api with 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 #2
Source File: Util.java    From openjdk-jdk9 with 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 #3
Source File: XmlFileMergerJaxp.java    From mapper-generator-javafx with 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 #4
Source File: Util.java    From jdk8u60 with 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 File: CanonicalizerBase.java    From jdk1.8-source-analysis with 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 #6
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 #7
Source File: CanonicalizerBase.java    From openjdk-8 with 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 #8
Source File: XMLSignatureInputDebugger.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #9
Source File: MetaDataObjectSerializer_indent.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
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 #10
Source File: XMLSignatureInputDebugger.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #11
Source File: UnImplNode.java    From TencentKona-8 with 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 #12
Source File: MavenProjectPropsImpl.java    From netbeans with 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 #13
Source File: XMLSignatureInputDebugger.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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 #14
Source File: XMLSignatureInputDebugger.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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
Source File: CanonicalizerBase.java    From jdk8u60 with 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 #16
Source File: XMLSerializer.java    From ph-commons with 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 #17
Source File: DocumentComparator.java    From sis with 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 #18
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 #19
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 #20
Source File: SVGDocument.java    From latexdraw with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Comment createComment(final String data) {
	if(data == null) {
		throw new DOMException(DOMException.INVALID_CHARACTER_ERR, "Invalid data.");
	}

	return new SVGComment(data, this);
}
 
Example #21
Source File: XMLHelper.java    From stategen with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Extract 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.
 *
 * @see CharacterData
 * @see EntityReference
 * @see Comment
 */
public static String getTextValue(Element valueEle) {
    if(valueEle == null) throw new IllegalArgumentException("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());
        }else if(item instanceof Element) {
            sb.append(getTextValue((Element)item));
        }
    }
    return sb.toString();
}
 
Example #22
Source File: DOMPrinter.java    From openjdk-jdk8u-backup with 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 #23
Source File: LookupProviderImpl.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void copyXMLTree(Document doc, Element from, Element to, String newNamespace) {
    NodeList nl = from.getChildNodes();
    int length = nl.getLength();
    for (int i = 0; i < length; i++) {
        org.w3c.dom.Node node = nl.item(i);
        org.w3c.dom.Node newNode;
        switch (node.getNodeType()) {
            case org.w3c.dom.Node.ELEMENT_NODE:
                Element oldElement = (Element) node;
                newNode = doc.createElementNS(newNamespace, oldElement.getTagName());
                NamedNodeMap attrs = oldElement.getAttributes();
                int alength = attrs.getLength();
                for (int j = 0; j < alength; j++) {
                    org.w3c.dom.Attr oldAttr = (org.w3c.dom.Attr) attrs.item(j);
                    ((Element)newNode).setAttributeNS(oldAttr.getNamespaceURI(), oldAttr.getName(), oldAttr.getValue());
                }
                copyXMLTree(doc, oldElement, (Element) newNode, newNamespace);
                break;
            case org.w3c.dom.Node.TEXT_NODE:
                newNode = doc.createTextNode(((Text) node).getData());
                break;
            case org.w3c.dom.Node.COMMENT_NODE:
                newNode = doc.createComment(((Comment) node).getData());
                break;
            default:
                // Other types (e.g. CDATA) not yet handled.
                throw new AssertionError(node);
        }
        to.appendChild(newNode);
    }
}
 
Example #24
Source File: CajaTreeBuilder.java    From caja with Apache License 2.0 5 votes vote down vote up
@Override
protected void appendComment(Node el, char[] buf, int start, int length) {
  Comment comment = doc.createComment(new String(buf, start, length));
  comment.setUserData("COMMENT_TYPE", startTok.type.toString(), null);
  el.appendChild(comment);
  if (needsDebugData) {
    Nodes.setFilePositionFor(comment, startTok.pos);
  }
}
 
Example #25
Source File: W3CDOMStreamReader.java    From cxf with Apache License 2.0 5 votes vote down vote up
public String getText() {
    if (content instanceof Text) {
        return ((Text)content).getData();
    } else if (content instanceof Comment) {
        return ((Comment)content).getData();
    }
    return DOMUtils.getRawContent(getCurrentNode());
}
 
Example #26
Source File: UnImplNode.java    From JDKSourceCode1.8 with MIT License 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 #27
Source File: HtmlElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void checkChildHierarchy(final Node childNode) throws DOMException {
    if (!((childNode instanceof Element) || (childNode instanceof Text)
        || (childNode instanceof Comment) || (childNode instanceof ProcessingInstruction)
        || (childNode instanceof CDATASection) || (childNode instanceof EntityReference))) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
            "The Element may not have a child of this type: " + childNode.getNodeType());
    }
    super.checkChildHierarchy(childNode);
}
 
Example #28
Source File: UnImplNode.java    From openjdk-jdk8u with 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 #29
Source File: UnImplNode.java    From hottub with 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 #30
Source File: HtmlElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
protected void checkChildHierarchy(final Node childNode) throws DOMException {
    if (!((childNode instanceof Element) || (childNode instanceof Text)
        || (childNode instanceof Comment) || (childNode instanceof ProcessingInstruction)
        || (childNode instanceof CDATASection) || (childNode instanceof EntityReference))) {
        throw new DOMException(DOMException.HIERARCHY_REQUEST_ERR,
            "The Element may not have a child of this type: " + childNode.getNodeType());
    }
    super.checkChildHierarchy(childNode);
}