Example usage for org.dom4j Node getNodeType

List of usage examples for org.dom4j Node getNodeType

Introduction

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

Prototype

short getNodeType();

Source Link

Document

Returns the code according to the type of node.

Usage

From source file:com.globalsight.ling.docproc.DiplomatWordCounter.java

License:Apache License

/**
 * Returns the string value of an element with tags representing whitespace
 * replaced by either whitespace or nbsps.
 *//*  w  w  w.jav  a  2  s  . c om*/
static public String getTextWithWhite(Element p_node, boolean... bs) {
    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);

        if (node.getNodeType() == Node.TEXT_NODE && bs.length == 0) {
            boolean isInternalText = isInternalText(content, i);
            if (!isInternalText) {
                result.append(node.getText());
            } else {
                // add space around internal text
                result.append(" ").append(node.getText()).append(" ");
            }
        } else if (node.getNodeType() == Node.ELEMENT_NODE) {
            Element elem = (Element) node;
            String type = elem.attributeValue("type");
            int childNodes = elem.content().size();
            // For word counting, always treat TMX whitespace tags
            // as white.
            if (Text.isTmxWhitespaceNode(type) || Text.isTmxMsoWhitespaceNode(type)) {
                result.append(" ");
            } else {
                if (childNodes > 0) {
                    boolean isExtract = false;
                    for (int j = 0; j < childNodes; j++) {
                        if (((Node) elem.content().get(j)).getNodeType() == Node.ELEMENT_NODE) {
                            String s = ((Element) elem.content().get(j)).attributeValue("isTranslate");
                            String innerTextNodeIndex = ((Element) elem.content().get(j))
                                    .attributeValue("innerTextNodeIndex");
                            if (s != null && Boolean.parseBoolean(s)) {
                                isExtract = true;
                                // getTextWithWhite((Element)elem.content().get(j),
                                // true);
                                // ((Element)elem.content().get(j)).
                                // result.append(getTranslateInnerXml((Element)
                                // elem.content().get(j)));
                            } else {
                                isExtract = false;
                            }

                        } else if (((Node) elem.content().get(j)).getNodeType() == Node.TEXT_NODE
                                && isExtract) {
                            result.append(((Node) elem.content().get(j)).getText());
                        }
                    }
                }
            }
        } else {
            System.err.println("Please fix the word counter: " + node);
        }
    }

    return result.toString();
}

From source file:com.globalsight.ling.docproc.DiplomatWordCounter.java

License:Apache License

private static boolean isInternalText(List content, int i) {
    if (i == 0 || i + 1 >= content.size()) {
        return false;
    }//from w ww.  j  a va2 s. c o m

    Node prenode = (Node) content.get(i - 1);
    Node nextnode = (Node) content.get(i + 1);

    if (prenode.getNodeType() != Node.ELEMENT_NODE || nextnode.getNodeType() != Node.ELEMENT_NODE) {
        return false;
    }

    Element preElem = (Element) prenode;
    Element nextElem = (Element) nextnode;

    String preelemName = preElem.getName();
    String nextelemName = nextElem.getName();
    String isInternal = preElem.attributeValue("internal");

    if ("bpt".equalsIgnoreCase(preelemName) && "ept".equalsIgnoreCase(nextelemName)
            && "yes".equalsIgnoreCase(isInternal)) {
        return true;
    } else {
        return false;
    }
}

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

License:Apache License

/**
 * Returns the XML representation like Element.asXML() but without
 * the top-level tag./*from ww  w .  j a va  2  s.  c o 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 w w  .j a v  a  2  s. co 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.globalsight.terminology.EntryUtils.java

License:Apache License

/**
 * <p>Removes insignificant whitespace between elements in groups.
 * Whitespace inside non-Grps, i.e. the data elements is
 * significant and is preserved.</p>
 *
 * This method is needed for comparing nodes.
 *//*from   ww w.  j a  va2 s.  c o m*/
static private boolean removeInsignificantWhitespace(Element p_node) {
    boolean dirty = false;

    boolean isGrp = p_node.getName().endsWith("Grp");

    for (Iterator it = p_node.content().iterator(); it.hasNext();) {
        Node temp = (Node) it.next();

        if (temp.getNodeType() != Node.ELEMENT_NODE) {
            if (isGrp) {
                it.remove();
                dirty = true;
            }

            continue;
        }

        Element node = (Element) temp;

        // Depth-first recursion.
        dirty |= removeInsignificantWhitespace(node);
    }

    return dirty;
}

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

License:Apache License

/**
 * <p>Recursively prunes empty fields and groups from the given entry.
 * The entry is destructively modified.</p>
 *
 * <p>A depth-first traversal first removes empty leaf nodes, and
 * then groups that are empty or not fully filled.</p>
 *
 * <p>Example: a <descripGrp> must contain at least one <descrip>
 * child.  A <languageGrp> must contain at least one <language>
 * and one <termGrp> child (2 children minimum).</p>
 *
 * <p>As of 6.2, non-relevant whitespace nodes are also removed.</p>
 * <p>As of 6.3, admissible empty HTML tags are not pruned: IMG, HR, BR.</p>
 *//*  ww  w .  j  av a 2s.  c  o  m*/
static private boolean pruneEmptyFields(Element p_node) {
    boolean dirty = false;

    if (!p_node.hasContent()) {
        return dirty;
    }

    // Cannot iterate child elements with node.elementIterator()
    // because that doesn't implement the remove() method.
    for (Iterator it = p_node.content().iterator(); it.hasNext();) {
        Node temp = (Node) it.next();

        // Only work on child elements.
        if (temp.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }

        Element node = (Element) temp;

        // Depth-first recursion.
        dirty |= pruneEmptyFields(node);

        // Sat Jan 15 02:17:38 2005 CvdL Need to allow empty HTML tags.
        String name = node.getName().toLowerCase();
        if (name.equals("language") || name.equals("img") || name.equals("hr") || name.equals("br")) {
            continue;
        }

        // Leaf nodes
        if (node.isTextOnly()) {
            String value = node.getText();
            if (value == null || value.trim().length() == 0) {
                // prune empty leaf nodes
                it.remove();
                dirty = true;
            }
        } else {
            // Group nodes
            int childCount = node.elements().size();
            if (childCount == 0 || (node.getName().equals("languageGrp") && childCount < 2)) {
                // prune empty groups
                it.remove();
                dirty = true;
            }
        }
    }

    return dirty;
}

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

License:Open Source License

/**
* If target has comment that says it is private them print warning
* 
*///from  w ww. ja v a 2 s .  com
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 .  ja v  a2s.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--;/*from  w  ww. jav  a  2 s.c om*/
            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 ww  w  .  j a va 2 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;
}