Example usage for org.dom4j Node TEXT_NODE

List of usage examples for org.dom4j Node TEXT_NODE

Introduction

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

Prototype

short TEXT_NODE

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

Click Source Link

Document

Matches elements nodes

Usage

From source file:com.globalsight.terminology.EntryUtils.java

License:Apache License

/**
 * Returns the XML representation like Element.asXML() but without
 * the top-level tag./*  www.  j  av a  2s .co m*/
 */
static public String getInnerXml(Element p_node) {
    StringBuffer result = new StringBuffer();

    List content = p_node.content();

    for (int i = 0, max = content.size(); i < max; i++) {
        Node node = (Node) content.get(i);

        // Work around a specific behaviour of DOM4J text nodes:
        // The text node asXML() returns the plain Unicode string,
        // so we need to encode entities manually.
        if (node.getNodeType() == Node.TEXT_NODE) {
            result.append(EditUtil.encodeXmlEntities(node.getText()));
        } else {
            // Element nodes write their text nodes correctly.
            result.append(node.asXML());
        }
    }

    return result.toString();
}

From source file:com.globalsight.terminology.EntryUtils.java

License:Apache License

/**
 * Returns the HTML representation of an element's text. This is
 * like getInnerXml() but doesn't encode apostrophes.
 *///from   w  ww  . j a  v a  2 s. c o m
static public String getInnerHtml(Element p_node) {
    StringBuffer result = new StringBuffer();

    List content = p_node.content();

    for (int i = 0, max = content.size(); i < max; i++) {
        Node node = (Node) content.get(i);

        // Work around a specific behaviour of DOM4J text nodes:
        // The text node asXML() returns the plain Unicode string,
        // so we need to encode entities manually.
        if (node.getNodeType() == Node.TEXT_NODE) {
            result.append(EditUtil.encodeHtmlEntities(node.getText()));
        } else {
            // Element nodes write their text nodes correctly.
            result.append(node.asXML());
        }
    }

    return result.toString();
}

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

License:Open Source License

/**
* If target has comment that says it is private them print warning
* 
*///  www . j a  va  2  s . co m
public void checkIfTargetPrivate(Target target, Project project) {
    Element targetElement = findTargetElement(target, project);
    if (targetElement != null) {
        List children = targetElement.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);
            }

            if (commentText != null) {
                if (commentText.contains("Private:")) {
                    output.add(
                            "Warning: " + target.getName() + " is private and should only be called by helium");
                }
                if (commentText.contains("<deprecated>")) {
                    output.add("Warning: " + target.getName() + "\n" + commentText);
                }
            }
        }
    }
}

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  w  w .j a v 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--;//  ww  w  .j a  v a  2  s.  c  o 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;//from w  w  w. ja va2 s. c  om
    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.smartwork.im.utils.XMLWriter.java

License:Open Source License

public void characters(char[] ch, int start, int length) throws SAXException {
    if (ch == null || ch.length == 0 || length <= 0) {
        return;// w  w  w. j  a va2 s.co m
    }

    try {
        /*
         * we can't use the writeString method here because it's possible
         * we don't receive all characters at once and calling writeString
         * would cause unwanted spaces to be added in between these chunks
         * of character arrays.
         */
        String string = new String(ch, start, length);

        if (escapeText) {
            string = escapeElementEntities(string);
        }

        if (format.isTrimText()) {
            if ((lastOutputNodeType == Node.TEXT_NODE) && !charactersAdded) {
                writer.write(" ");
            } else if (charactersAdded && Character.isWhitespace(lastChar)) {
                writer.write(lastChar);
            }

            String delim = "";
            StringTokenizer tokens = new StringTokenizer(string);
            while (tokens.hasMoreTokens()) {
                writer.write(delim);
                writer.write(tokens.nextToken());
                delim = " ";
            }
        } else {
            writer.write(string);
        }

        charactersAdded = true;
        lastChar = ch[start + length - 1];
        lastOutputNodeType = Node.TEXT_NODE;

        super.characters(ch, start, length);
    } catch (IOException e) {
        handleException(e);
    }
}

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 o m
            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:darks.orm.core.config.sqlmap.DMLConfigReader.java

License:Apache License

private void parseSqlTag(AbstractTag parent, Element el, String namesp) throws Exception {
    AbstractTag prevTag = null;//from ww w.  j  a  va 2 s.  com
    List<Node> list = el.content();
    Iterator<Node> it = list.iterator();
    while (it.hasNext()) {
        Node node = it.next();
        switch (node.getNodeType()) {
        case Node.ELEMENT_NODE:
            Element childEl = (Element) node;
            prevTag = parseElementTag(parent, node, childEl, namesp, prevTag);
            break;
        case Node.CDATA_SECTION_NODE:
        case Node.TEXT_NODE:
            String text = node.getText().replaceAll("\n|\t", " ").trim();
            if (!"".equals(text)) {
                TextTag tag = new TextTag(text, prevTag);
                parent.addChild(tag);
                prevTag = tag;
            }
            break;
        }
    }
}

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";
    }/*from  ww w . ja v  a 2s . c om*/
    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";
}