Search in sources :

Example 6 with Comment

use of org.w3c.dom.Comment in project aries by apache.

the class CmNamespaceHandler method getTextValue.

private static String getTextValue(Element element) {
    StringBuffer value = new StringBuffer();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);
        if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
            value.append(item.getNodeValue());
        }
    }
    return value.toString();
}
Also used : Comment(org.w3c.dom.Comment) CharacterData(org.w3c.dom.CharacterData) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) EntityReference(org.w3c.dom.EntityReference)

Example 7 with Comment

use of org.w3c.dom.Comment in project karaf by apache.

the class NamespaceHandler method getTextValue.

private static String getTextValue(Element element) {
    StringBuffer value = new StringBuffer();
    NodeList nl = element.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node item = nl.item(i);
        if ((item instanceof CharacterData && !(item instanceof Comment)) || item instanceof EntityReference) {
            value.append(item.getNodeValue());
        }
    }
    return value.toString();
}
Also used : Comment(org.w3c.dom.Comment) CharacterData(org.w3c.dom.CharacterData) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) EntityReference(org.w3c.dom.EntityReference)

Example 8 with Comment

use of org.w3c.dom.Comment in project sling by apache.

the class MyErrorHandler method convert.

// ------------------------------------------------------ Protected Methods
/**
     * Create and return a TreeNode that corresponds to the specified Node,
     * including processing all of the attributes and children nodes.
     *
     * @param parent The parent TreeNode (if any) for the new TreeNode
     * @param node The XML document Node to be converted
     */
protected TreeNode convert(TreeNode parent, Node node) {
    // Construct a new TreeNode for this node
    TreeNode treeNode = new TreeNode(node.getNodeName(), parent);
    // Convert all attributes of this node
    NamedNodeMap attributes = node.getAttributes();
    if (attributes != null) {
        int n = attributes.getLength();
        for (int i = 0; i < n; i++) {
            Node attribute = attributes.item(i);
            treeNode.addAttribute(attribute.getNodeName(), attribute.getNodeValue());
        }
    }
    // Create and attach all children of this node
    NodeList children = node.getChildNodes();
    if (children != null) {
        int n = children.getLength();
        for (int i = 0; i < n; i++) {
            Node child = children.item(i);
            if (child instanceof Comment)
                continue;
            if (child instanceof Text) {
                String body = ((Text) child).getData();
                if (body != null) {
                    body = body.trim();
                    if (body.length() > 0)
                        treeNode.setBody(body);
                }
            } else {
                TreeNode treeChild = convert(treeNode, child);
            }
        }
    }
    // Return the completed TreeNode graph
    return (treeNode);
}
Also used : Comment(org.w3c.dom.Comment) NamedNodeMap(org.w3c.dom.NamedNodeMap) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Text(org.w3c.dom.Text)

Example 9 with Comment

use of org.w3c.dom.Comment in project tomee by apache.

the class SuperProperties method loadFromXML.

public synchronized void loadFromXML(final InputStream in) throws IOException {
    if (in == null) {
        throw new NullPointerException();
    }
    final DocumentBuilder builder = getDocumentBuilder();
    try {
        final Document doc = builder.parse(in);
        final NodeList entries = doc.getElementsByTagName("entry");
        if (entries == null) {
            return;
        }
        final int entriesListLength = entries.getLength();
        for (int i = 0; i < entriesListLength; i++) {
            final Element entry = (Element) entries.item(i);
            final String key = entry.getAttribute("key");
            final String value = entry.getTextContent();
            put(key, value);
            // search backwards for a comment
            for (Node node = entry.getPreviousSibling(); node != null && !(node instanceof Element); node = node.getPreviousSibling()) {
                if (node instanceof Comment) {
                    final InputStream cin = new ByteArrayInputStream(((Comment) node).getData().getBytes());
                    // read comment line by line
                    final StringBuilder comment = new StringBuilder();
                    final LinkedHashMap<String, String> attributes = new LinkedHashMap<String, String>();
                    int nextByte;
                    char nextChar;
                    boolean firstLine = true;
                    int commentIndent = Integer.MAX_VALUE;
                    do {
                        // read one line
                        final StringBuilder commentLine = new StringBuilder();
                        int commentLineIndent = 0;
                        boolean inIndent = true;
                        while (true) {
                            nextByte = cin.read();
                            if (nextByte < 0) {
                                break;
                            }
                            // & 0xff
                            nextChar = (char) nextByte;
                            if (inIndent && nextChar == ' ') {
                                commentLineIndent++;
                                commentLine.append(' ');
                            } else if (inIndent && nextChar == '\t') {
                                commentLineIndent += 4;
                                commentLine.append("    ");
                            } else if (nextChar == '\r' || nextChar == '\n') {
                                break;
                            } else {
                                inIndent = false;
                                commentLine.append(nextChar);
                            }
                        }
                        // Determine indent
                        if (!firstLine && commentIndent == Integer.MAX_VALUE && commentLine.length() > 0) {
                            // if this is a new comment block, the comment indent size for this
                            // block is based the first full line of the comment (ignoring the
                            // line with the <!--
                            commentIndent = commentLineIndent;
                        }
                        commentLineIndent = Math.min(commentIndent, commentLineIndent);
                        if (commentLine.toString().trim().startsWith("@")) {
                            // process property attribute
                            final String attribute = commentLine.toString().trim().substring(1);
                            final String[] parts = attribute.split("=", 2);
                            final String attributeName = parts[0].trim();
                            final String attributeValue = parts.length == 2 ? parts[1].trim() : "";
                            attributes.put(attributeName, attributeValue);
                        } else {
                            // append comment
                            if (comment.length() != 0) {
                                comment.append(lineSeparator);
                            }
                            comment.append(commentLine.toString().substring(commentLineIndent));
                        }
                        firstLine = false;
                    } while (nextByte > 0);
                    if (comment.length() > 0) {
                        setComment(key, comment.toString());
                    }
                    this.attributes.put(normalize(key), attributes);
                    break;
                }
            }
        }
    } catch (final SAXException e) {
        throw new InvalidPropertiesFormatException(e);
    }
}
Also used : Comment(org.w3c.dom.Comment) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NodeList(org.w3c.dom.NodeList) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) Document(org.w3c.dom.Document) LinkedHashMap(java.util.LinkedHashMap) SAXException(org.xml.sax.SAXException) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) InvalidPropertiesFormatException(java.util.InvalidPropertiesFormatException)

Example 10 with Comment

use of org.w3c.dom.Comment in project stanbol by apache.

the class DomSerializer2 method createSubnodes.

private void createSubnodes(Document document, Element element, List tagChildren) {
    if (tagChildren != null) {
        Iterator it = tagChildren.iterator();
        while (it.hasNext()) {
            Object item = it.next();
            if (item instanceof CommentToken) {
                CommentToken commentNode = (CommentToken) item;
                Comment comment = document.createComment(commentNode.getContent().toString());
                element.appendChild(comment);
            } else if (item instanceof ContentToken) {
                ContentToken contentToken = (ContentToken) item;
                String content = contentToken.getContent();
                String nodeName = element.getNodeName();
                boolean specialCase = props.isUseCdataForScriptAndStyle() && ("script".equalsIgnoreCase(nodeName) || "style".equalsIgnoreCase(nodeName));
                if (escapeXml && !specialCase) {
                    content = escapeXml(content, props, true);
                }
                element.appendChild(specialCase ? document.createCDATASection(content) : document.createTextNode(content));
            } else if (item instanceof TagNode) {
                TagNode subTagNode = (TagNode) item;
                Element subelement = document.createElement(subTagNode.getName());
                ;
                setAttributes(subTagNode, subelement);
                // recursively create subnodes
                createSubnodes(document, subelement, subTagNode.getChildren());
                element.appendChild(subelement);
            } else if (item instanceof List) {
                List sublist = (List) item;
                createSubnodes(document, element, sublist);
            }
        }
    }
}
Also used : CommentToken(org.htmlcleaner.CommentToken) Comment(org.w3c.dom.Comment) ContentToken(org.htmlcleaner.ContentToken) Element(org.w3c.dom.Element) Iterator(java.util.Iterator) List(java.util.List) TagNode(org.htmlcleaner.TagNode)

Aggregations

Comment (org.w3c.dom.Comment)22 Node (org.w3c.dom.Node)14 NodeList (org.w3c.dom.NodeList)13 Element (org.w3c.dom.Element)9 EntityReference (org.w3c.dom.EntityReference)8 CharacterData (org.w3c.dom.CharacterData)7 Document (org.w3c.dom.Document)7 DocumentType (org.w3c.dom.DocumentType)2 ProcessingInstruction (org.w3c.dom.ProcessingInstruction)2 Text (org.w3c.dom.Text)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 Enumeration (java.util.Enumeration)1 InvalidPropertiesFormatException (java.util.InvalidPropertiesFormatException)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1