Example usage for org.dom4j Node COMMENT_NODE

List of usage examples for org.dom4j Node COMMENT_NODE

Introduction

In this page you can find the example usage for org.dom4j Node COMMENT_NODE.

Prototype

short COMMENT_NODE

To view the source code for org.dom4j Node COMMENT_NODE.

Click Source Link

Document

Matches Comments nodes

Usage

From source file:com.nokia.ant.Database.java

License:Open Source License

private void processMacro(Element macroNode, Element outProjectNode, String antFile)
        throws IOException, DocumentException {
    String macroName = macroNode.attributeValue("name");
    log("Processing macro: " + macroName, Project.MSG_DEBUG);

    Element outmacroNode = outProjectNode.addElement("macro");
    addTextElement(outmacroNode, "name", macroNode.attributeValue("name"));
    addTextElement(outmacroNode, "description", macroNode.attributeValue("description"));

    // Add location
    // Project project = getProject();
    // Macro antmacro = (Macro) project.getTargets().get(macroName);
    // System.out.println(project.getMacroDefinitions());
    // System.out.println(macroName);
    // MacroInstance antmacro = (MacroInstance)
    // project.getMacroDefinitions().get("http://www.nokia.com/helium:" +
    // macroName);

    // Add the location with just the file path for now and a dummy line
    // number.//from   w ww .jav a  2  s .  c o  m
    // TODO - Later we should find the line number from the XML input.
    addTextElement(outmacroNode, "location", antFile + ":1:");

    List<Node> statements = macroNode.selectNodes("//scriptdef[@name='" + macroName
            + "']/attribute | //macrodef[@name='" + macroName + "']/attribute");
    String usage = "";
    for (Node statement : statements) {
        String defaultval = statement.valueOf("@default");
        if (defaultval.equals(""))
            defaultval = "value";
        else
            defaultval = "<i>" + defaultval + "</i>";
        usage = usage + " " + statement.valueOf("@name") + "=\"" + defaultval + "\"";
    }

    String macroElements = "";
    statements = macroNode.selectNodes(
            "//scriptdef[@name='" + macroName + "']/element | //macrodef[@name='" + macroName + "']/element");
    for (Node statement : statements) {
        macroElements = "&lt;" + statement.valueOf("@name") + "/&gt;\n" + macroElements;
    }
    if (macroElements.equals(""))
        addTextElement(outmacroNode, "usage", "&lt;hlm:" + macroName + " " + usage + "/&gt;");
    else
        addTextElement(outmacroNode, "usage", "&lt;hlm:" + macroName + " " + usage + "&gt;\n" + macroElements
                + "&lt;/hlm:" + macroName + "&gt;");

    // Add dependencies
    // Enumeration dependencies = antmacro.getDependencies();
    // while (dependencies.hasMoreElements())
    // {
    // String dependency = (String) dependencies.nextElement();
    // Element dependencyElement = addTextElement(outmacroNode,
    // "dependency", dependency);
    // dependencyElement.addAttribute("type","direct");
    // }

    callAntTargetVisitor(macroNode, outmacroNode, outProjectNode);

    // Add documentation
    // Get comment element before the macro element to extract macro doc
    List children = macroNode.selectNodes("preceding-sibling::node()");
    if (children.size() > 0) {
        // Scan past the text nodes, which are most likely whitespace
        int index = children.size() - 1;
        Node child = (Node) children.get(index);
        while (index > 0 && child.getNodeType() == Node.TEXT_NODE) {
            index--;
            child = (Node) children.get(index);
        }

        // Check if there is a comment node
        String commentText = null;
        if (child.getNodeType() == Node.COMMENT_NODE) {
            Comment macroComment = (Comment) child;
            commentText = macroComment.getStringValue().trim();
            log(macroName + " comment: " + commentText, Project.MSG_DEBUG);
        } else {
            log("Macro has no comment: " + macroName, Project.MSG_WARN);
        }

        insertDocumentation(outmacroNode, commentText);

        Node previousNode = (Node) children.get(children.size() - 1);
    }

    // Get names of all properties used in this macro
    ArrayList properties = new ArrayList();
    Visitor visitor = new AntPropertyVisitor(properties);
    macroNode.accept(visitor);
    for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
        String property = (String) iterator.next();
        addTextElement(outmacroNode, "propertyDependency", property);
    }
}

From source file:com.nokia.ant.Database.java

License:Open Source License

private void processTarget(Element targetNode, Element outProjectNode) throws IOException, DocumentException {
    String targetName = targetNode.attributeValue("name");
    log("Processing target: " + targetName, Project.MSG_DEBUG);

    // Add documentation
    // Get comment element before the target element to extract target doc
    String commentText = "";
    List children = targetNode.selectNodes("preceding-sibling::node()");
    if (children.size() > 0) {
        // Scan past the text nodes, which are most likely whitespace
        int index = children.size() - 1;
        Node child = (Node) children.get(index);
        while (index > 0 && child.getNodeType() == Node.TEXT_NODE) {
            index--;/* www.j a  v a 2 s . co m*/
            child = (Node) children.get(index);
        }

        // Check if there is a comment node
        if (child.getNodeType() == Node.COMMENT_NODE) {
            Comment targetComment = (Comment) child;
            commentText = targetComment.getStringValue().trim();

            log(targetName + " comment: " + commentText, Project.MSG_DEBUG);
        } else {
            log("Target has no comment: " + targetName, Project.MSG_WARN);
        }

        Node previousNode = (Node) children.get(children.size() - 1);
    }

    if (!commentText.contains("Private:")) {
        Element outTargetNode = outProjectNode.addElement("target");

        addTextElement(outTargetNode, "name", targetNode.attributeValue("name"));
        addTextElement(outTargetNode, "ifDependency", targetNode.attributeValue("if"));
        addTextElement(outTargetNode, "unlessDependency", targetNode.attributeValue("unless"));
        addTextElement(outTargetNode, "description", targetNode.attributeValue("description"));
        addTextElement(outTargetNode, "tasks", String.valueOf(targetNode.elements().size()));

        // Add location
        Project project = getProject();
        Target antTarget = (Target) project.getTargets().get(targetName);

        if (antTarget == null)
            return;

        addTextElement(outTargetNode, "location", antTarget.getLocation().toString());

        // Add dependencies
        Enumeration dependencies = antTarget.getDependencies();
        while (dependencies.hasMoreElements()) {
            String dependency = (String) dependencies.nextElement();
            Element dependencyElement = addTextElement(outTargetNode, "dependency", dependency);
            dependencyElement.addAttribute("type", "direct");
        }

        callAntTargetVisitor(targetNode, outTargetNode, outProjectNode);

        // Process the comment text as MediaWiki syntax and convert to HTML
        insertDocumentation(outTargetNode, commentText);

        // Get names of all properties used in this target
        ArrayList properties = new ArrayList();
        Visitor visitor = new AntPropertyVisitor(properties);
        targetNode.accept(visitor);
        for (Iterator iterator = properties.iterator(); iterator.hasNext();) {
            String property = (String) iterator.next();
            addTextElement(outTargetNode, "propertyDependency", property);
        }

        // Add the raw XML content of the element
        String targetXml = targetNode.asXML();
        // Replace the CDATA end notation to avoid nested CDATA sections
        targetXml = targetXml.replace("]]>", "] ]>");

        addTextElement(outTargetNode, "source", targetXml, true);
    }
}

From source file:com.nokia.helium.ant.data.AntObjectMeta.java

License:Open Source License

@SuppressWarnings("unchecked")
private Comment getCommentNode() {
    Node commentNode = null;/* www.  j  ava2 s.  co  m*/
    if (node.getNodeType() == Node.COMMENT_NODE) {
        commentNode = node;
    } else {
        List<Node> children = node.selectNodes("preceding-sibling::node()");
        if (children.size() > 0) {
            // Scan past the text nodess, which are most likely whitespace
            int index = children.size() - 1;
            Node child = children.get(index);
            while (index > 0 && child.getNodeType() == Node.TEXT_NODE) {
                index--;
                child = children.get(index);
            }

            // Check if there is a comment node
            if (child.getNodeType() == Node.COMMENT_NODE) {
                commentNode = child;
                log("Node has comment: " + node.getStringValue(), Project.MSG_DEBUG);
            } else {
                log("Node has no comment: " + node.toString(), Project.MSG_WARN);
            }
        }
    }
    return (Comment) commentNode;
}

From source file:com.webslingerz.jpt.PageTemplateImpl.java

License:Open Source License

private void defaultContent(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler,
        Interpreter beanShell, Stack<Map<String, Slot>> slotStack)
        throws SAXException, PageTemplateException, IOException {
    // Use default template content
    for (Iterator i = element.nodeIterator(); i.hasNext();) {
        Node node = (Node) i.next();
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            processElement((Element) node, contentHandler, lexicalHandler, beanShell, slotStack);
            break;

        case Node.TEXT_NODE:
            char[] text = Expression.evaluateText(node.getText().toString(), beanShell).toCharArray();
            contentHandler.characters(text, 0, text.length);
            break;

        case Node.COMMENT_NODE:
            char[] comment = node.getText().toCharArray();
            lexicalHandler.comment(comment, 0, comment.length);
            break;

        case Node.CDATA_SECTION_NODE:
            lexicalHandler.startCDATA();
            char[] cdata = node.getText().toCharArray();
            contentHandler.characters(cdata, 0, cdata.length);
            lexicalHandler.endCDATA();/*from  ww  w  .ja v a 2  s.c  om*/
            break;

        case Node.NAMESPACE_NODE:
            Namespace declared = (Namespace) node;
            // System.err.println( "Declared namespace: " +
            // declared.getPrefix() + ":" + declared.getURI() );
            namespaces.put(declared.getPrefix(), declared.getURI());
            // if ( declared.getURI().equals( TAL_NAMESPACE_URI ) ) {
            // this.talNamespacePrefix = declared.getPrefix();
            // }
            // else if (declared.getURI().equals( METAL_NAMESPACE_URI ) ) {
            // this.metalNamespacePrefix = declared.getPrefix();
            // }
            break;

        case Node.ATTRIBUTE_NODE:
            // Already handled
            break;

        case Node.DOCUMENT_TYPE_NODE:
        case Node.ENTITY_REFERENCE_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
        default:
            // System.err.println( "WARNING: Node type not supported: " +
            // node.getNodeTypeName() );
        }
    }
}

From source file:freemarker.ext.xml._Dom4jNavigator.java

License:Apache License

String getType(Object node) {
    switch (((Node) node).getNodeType()) {
    case Node.ATTRIBUTE_NODE: {
        return "attribute";
    }//  w  w  w .  j a  va  2s.c  o  m
    case Node.CDATA_SECTION_NODE: {
        return "cdata";
    }
    case Node.COMMENT_NODE: {
        return "comment";
    }
    case Node.DOCUMENT_NODE: {
        return "document";
    }
    case Node.DOCUMENT_TYPE_NODE: {
        return "documentType";
    }
    case Node.ELEMENT_NODE: {
        return "element";
    }
    case Node.ENTITY_REFERENCE_NODE: {
        return "entityReference";
    }
    case Node.NAMESPACE_NODE: {
        return "namespace";
    }
    case Node.PROCESSING_INSTRUCTION_NODE: {
        return "processingInstruction";
    }
    case Node.TEXT_NODE: {
        return "text";
    }
    }
    return "unknown";
}

From source file:io.mashin.oep.hpdl.XMLReadUtils.java

License:Open Source License

public static HashMap<String, Point> graphicalInfoFrom(Document document) {
    HashMap<String, Point> graphicalInfoMap = new HashMap<String, Point>();
    try {// www . j a v a 2  s.  c  o m
        SAXReader reader = new SAXReader();
        Pattern p = Pattern.compile("\\s*<workflow>.*</workflow>\\s*", Pattern.DOTALL);

        @SuppressWarnings("unchecked")
        Iterator<Node> iter = document.nodeIterator();
        while (iter.hasNext()) {

            Node xmlNode = iter.next();
            if (xmlNode.getNodeType() == Node.COMMENT_NODE) {

                String graphicalInfo = xmlNode.getText();
                if (p.matcher(graphicalInfo).find()) {

                    Element graphicalElement = reader.read(new StringReader(graphicalInfo)).getRootElement();
                    @SuppressWarnings("unchecked")
                    Iterator<Node> gIter = graphicalElement.nodeIterator();

                    while (gIter.hasNext()) {
                        Node gNode = gIter.next();
                        if (gNode.getName() != null && gNode.getName().equals("node")) {
                            graphicalInfoMap.put(gNode.valueOf("@name"),
                                    new Point(Integer.parseInt(gNode.valueOf("@x")),
                                            Integer.parseInt(gNode.valueOf("@y"))));
                        }
                    }
                    break;

                }

            }

        }
    } catch (DocumentException ex) {
        ex.printStackTrace();
    }
    return graphicalInfoMap;
}

From source file:org.hudsonci.xpath.impl.Dom2Dom.java

License:Open Source License

private org.w3c.dom.Node createChild(Node child, org.w3c.dom.Node wparent) {
    int type = child.getNodeType();

    // Collapse multiple consecutive text nodes to a single text node
    // with trimmed value.
    if (type != Node.TEXT_NODE)
        endText(wparent);/*from  ww  w .  j a va  2  s  . com*/

    Name name;
    org.w3c.dom.Node node = null;

    switch (type) {
    case Node.ATTRIBUTE_NODE:
        break;
    case Node.CDATA_SECTION_NODE:
        CDATA cd = (CDATA) child;
        wparent.appendChild(node = wdoc.createCDATASection(cd.getText()));
        break;
    case Node.COMMENT_NODE:
        Comment co = (Comment) child;
        wparent.appendChild(node = wdoc.createComment(co.getText()));
        break;
    case Node.DOCUMENT_TYPE_NODE:
        DocumentType dt = (DocumentType) child;
        wparent.appendChild(new XDocumentType(dt, wparent));
        break;
    case Node.ELEMENT_NODE:
        Element el = (Element) child;
        name = new Name(el);
        org.w3c.dom.Element e = name.namespaceURI == null ? wdoc.createElement(name.qualifiedName)
                : wdoc.createElementNS(name.namespaceURI, name.qualifiedName);
        wparent.appendChild(e);
        node = currentElement = e;

        for (int i = 0, n = el.attributeCount(); i < n; i++) {
            Attribute at = el.attribute(i);
            name = new Name(at);
            if (name.namespaceURI == null)
                e.setAttribute(name.qualifiedName, at.getValue());
            else
                e.setAttributeNS(name.namespaceURI, name.qualifiedName, at.getValue());
        }
        return e;
    case Node.ENTITY_REFERENCE_NODE:
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        ProcessingInstruction p = (ProcessingInstruction) child;
        wparent.appendChild(node = wdoc.createProcessingInstruction(p.getTarget(), p.getText()));
        break;
    case Node.TEXT_NODE:
        textBuilder.append(child.getText());
        lastText = (Text) child;
        break;
    case Node.NAMESPACE_NODE:
        Namespace ns = (Namespace) child;
        name = new Name(ns);
        currentElement.setAttribute(name.qualifiedName, ns.getURI());
        break;
    default:
        throw new IllegalStateException("Unknown node type");
    }
    if (node != null)
        reverseMap.put(node, child);
    return null;
}

From source file:org.talend.repository.ui.wizards.exportjob.scriptsmanager.JobJavaScriptsManager.java

License:Open Source License

protected void removeComments(Element ele) {
    // remove comments.
    Iterator commentIterator = ele.nodeIterator();
    while (commentIterator.hasNext()) {
        Object commentObj = commentIterator.next();
        if (commentObj instanceof Comment) {
            Comment comment = (Comment) commentObj;
            if (comment.getNodeType() == Node.COMMENT_NODE) {
                commentIterator.remove();
            }/*from   w  w w.ja  v  a 2 s  . c  o  m*/
        }
    }
}

From source file:org.zenonpagetemplates.onePhaseImpl.PageTemplateImpl.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
private void defaultContent(Element element, ContentHandler contentHandler, LexicalHandler lexicalHandler,
        EvaluationHelper evaluationHelper, Stack<Map<String, Slot>> slotStack)
        throws SAXException, PageTemplateException, IOException, EvaluationException {
    // Use default template content
    for (Iterator<Node> i = element.nodeIterator(); i.hasNext();) {
        Node node = i.next();// w  ww  .  ja v  a 2 s  .  c  o m
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            processElement((Element) node, contentHandler, lexicalHandler, evaluationHelper, slotStack);
            break;

        case Node.TEXT_NODE:
            char[] text = node.getText().toCharArray();
            contentHandler.characters(text, 0, text.length);
            break;

        case Node.COMMENT_NODE:
            char[] comment = node.getText().toCharArray();
            lexicalHandler.comment(comment, 0, comment.length);
            break;

        case Node.CDATA_SECTION_NODE:
            lexicalHandler.startCDATA();
            char[] cdata = node.getText().toCharArray();
            contentHandler.characters(cdata, 0, cdata.length);
            lexicalHandler.endCDATA();
            break;

        case Node.NAMESPACE_NODE:
            Namespace declared = (Namespace) node;
            //System.err.println( "Declared namespace: " + declared.getPrefix() + ":" + declared.getURI() );
            this.namespaces.put(declared.getPrefix(), declared.getURI());
            //if ( declared.getURI().equals( TAL_NAMESPACE_URI ) ) {
            //    this.talNamespacePrefix = declared.getPrefix();
            //} 
            //else if (declared.getURI().equals( METAL_NAMESPACE_URI ) ) {
            //    this.metalNamespacePrefix = declared.getPrefix();
            //}
            break;

        case Node.ATTRIBUTE_NODE:
            // Already handled
            break;

        case Node.DOCUMENT_TYPE_NODE:
        case Node.ENTITY_REFERENCE_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
        default:
            //System.err.println( "WARNING: Node type not supported: " + node.getNodeTypeName() );       
        }
    }
}

From source file:org.zenonpagetemplates.twoPhasesImpl.ZPTDocumentFactory.java

License:Open Source License

@SuppressWarnings({ "unchecked" })
static private void mapContent(Element element, ZPTElement zptElement, ZPTDocument zptDocument,
        Stack<Map<String, Slot>> slotStack) throws SAXException, PageTemplateException, IOException {

    // Use default template content
    for (Iterator<Node> i = element.nodeIterator(); i.hasNext();) {
        Node node = i.next();// www  . ja  v a2 s  .c  o  m
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            zptElement.addContent(getNewZPTElement((Element) node, zptDocument, slotStack));
            break;

        case Node.TEXT_NODE:
            zptElement.addContent(new TextNode(node.getText()));
            break;

        case Node.CDATA_SECTION_NODE:
            zptElement.addContent(new CDATANode(node.getText()));
            break;

        case Node.NAMESPACE_NODE: // Already handled
            /*
             Namespace declared = (Namespace)node;
             if (zptDocument.isNamespaceToDeclare(declared)){
                zptDocument.addNamespace(declared);
             } else {
                zptElement.addNamespaceStaticAttribute(declared);
             }
             break;*/

        case Node.ATTRIBUTE_NODE: // Already handled
        case Node.COMMENT_NODE: // Remove all comments
        case Node.DOCUMENT_TYPE_NODE:
        case Node.ENTITY_REFERENCE_NODE:
        case Node.PROCESSING_INSTRUCTION_NODE:
        default:
            // Nothing to do
        }
    }
}