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.cladonia.xml.XMLFormatter.java

License:Mozilla Public License

protected void writePreservedNode(Node node) throws IOException {
    if (DEBUG)/*from  w  w w.j av  a  2 s  .  c  o  m*/
        System.out.println("XMLFormatter.writeMixedNode( " + node + ")");
    int nodeType = node.getNodeType();
    switch (nodeType) {
    case Node.ELEMENT_NODE:
        writePreservedElement((Element) node);
        break;
    case Node.ATTRIBUTE_NODE:
        writeAttribute((Attribute) node);
        break;
    case Node.TEXT_NODE:
        writeString(node.getText());
        //write((Text) node);
        break;
    case Node.CDATA_SECTION_NODE:
        writeCDATA(node.getText());
        break;
    case Node.ENTITY_REFERENCE_NODE:
        writeEntity((Entity) node);
        break;
    case Node.PROCESSING_INSTRUCTION_NODE:
        writeProcessingInstruction((ProcessingInstruction) node);
        break;
    case Node.COMMENT_NODE:
        writeComment(node.getText());
        break;
    case Node.DOCUMENT_NODE:
        write((Document) node);
        break;
    case Node.DOCUMENT_TYPE_NODE:
        writeDocType((DocumentType) node);
        break;
    case Node.NAMESPACE_NODE:
        // Will be output with attributes
        //write((Namespace) node);
        break;
    default:
        throw new IOException("Invalid node type: " + node);
    }
}

From source file:com.devoteam.srit.xmlloader.core.utils.XMLElementTextOnlyParser.java

License:Open Source License

public List<Element> replace(Element element, ParameterPool variables) throws Exception {
    List<Element> result = new LinkedList();
    result.add(element.createCopy());/* www.  j  ava 2  s.  c  o  m*/
    element = result.get(0);

    Iterator nodesIterator = element.nodeIterator();
    HashMap<Node, Node> nodesToReplace = new HashMap<Node, Node>();

    boolean alreadyNext = false;

    while (nodesIterator.hasNext()) {
        Node node = null;
        if (!alreadyNext) {
            node = (Node) nodesIterator.next();
        }

        Node lastTextNode = null;
        String lastTextNodeText = "";
        alreadyNext = false;

        //
        // We put all successive TEXT Nodes into one node ( there is some fragmentation i don't understand )
        //
        while (null != node && node.getNodeType() == Node.TEXT_NODE) {
            alreadyNext = true;
            lastTextNode = (Text) node;
            lastTextNodeText += lastTextNode.getText();

            // this node will be deleted later ... if not overwritten in this hashmap
            nodesToReplace.put(lastTextNode, null);
            if (nodesIterator.hasNext()) {
                node = (Node) nodesIterator.next();
            } else {
                node = null;
            }
        }

        //
        // We process normally the CDATA Nodes
        // 
        if (null != node && node.getNodeType() == Node.CDATA_SECTION_NODE) {
            lastTextNode = (Node) node;
            lastTextNodeText = lastTextNode.getText();
        }

        //
        // We do nothing for the other type Nodes
        // 
        if (null == lastTextNode) {
            continue;
        }

        lastTextNode.setText(lastTextNodeText);

        //
        // Now that we have only one, complete, TEXT node or one CDATA node to proceed
        //
        Node textNode = (Node) lastTextNode;

        String text = textNode.getText();
        String out = "";

        int endOfLine;
        String line;

        //
        // Transform all \r\n, in \n
        //
        //text = Utils.replaceNoRegex(text, "\r", "");

        while (text.length() > 0) {
            //
            // Read a line
            //
            endOfLine = text.indexOf("\n");

            if (endOfLine == -1) {
                line = text;
                text = "";
            } else {
                line = text.substring(0, endOfLine + 1);
                text = text.substring(endOfLine + 1);
            }

            //
            // Replace line if it contains at least a variable
            //
            if (Parameter.containsParameter(line)) {
                List<String> results = variables.parse(line);

                for (String s : results) {
                    out += s;
                }
            } else {
                out += line;
            }
        }

        //
        // Set new text into new AVP
        //
        Text newTextNode = new DefaultText(out);
        nodesToReplace.put(textNode, newTextNode);
    }

    for (Node key : nodesToReplace.keySet()) {
        DefaultElementInterface.replaceNode((DefaultElement) element, key, nodesToReplace.get(key));
    }

    if (result.size() != 1) {
        throw new ExecutionException("Size of result for XMLElementTextOnlyParser should be 1");
    }

    return result;
}

From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java

License:Apache License

/**
 * Returns the XML representation like Element.asXML() but without the
 * top-level tag.//from   w  w  w.j a  v a 2  s  .  c o  m
 */
@SuppressWarnings("unchecked")
private static String getInnerXml(Element p_node) {
    StringBuffer result = new StringBuffer();
    List<Node> content = p_node.content();

    for (Node node : content) {
        if (node.getNodeType() == Node.TEXT_NODE) {
            result.append(EditUtil.encodeXmlEntities(node.getText()));
        } else {
            StringWriter out = new StringWriter();
            result.append(out.toString());
        }
    }

    return result.toString();
}

From source file:com.globalsight.everest.edit.offline.ttx.TTXParser.java

License:Apache License

/**
 * Parse main contents/*  w ww.  j  av  a  2s . c  o  m*/
 * 
 * @param p_element
 */
private void domNodehandler(Node p_node, boolean isSource) {
    // public static final short ANY_NODE 0
    // public static final short ATTRIBUTE_NODE 2
    // public static final short CDATA_SECTION_NODE 4
    // public static final short COMMENT_NODE 8
    // public static final short DOCUMENT_NODE 9
    // public static final short DOCUMENT_TYPE_NODE 10
    // public static final short ELEMENT_NODE 1
    // public static final short ENTITY_REFERENCE_NODE 5
    // public static final short MAX_NODE_TYPE 14
    // public static final short NAMESPACE_NODE 13
    // public static final short PROCESSING_INSTRUCTION_NODE 7
    // public static final short TEXT_NODE 3
    // public static final short UNKNOWN_NODE 14
    if (p_node == null) {
        return;
    }

    switch (p_node.getNodeType()) {
    case Node.ELEMENT_NODE:
        elementNodeProcessor(p_node, isSource);

        break;
    case Node.TEXT_NODE:
        String nodeValue = p_node.getStringValue();
        if (nodeValue.startsWith("#")) {
            nodeValue = nodeValue.replaceFirst("#", OfflineConstants.PONUD_SIGN);
        }
        if (isParsingTTXForGS) {
            boolean isInTargetTuv = isInTargetTuv(p_node);
            if (nodeValue != null && isInTargetTuv) {
                results.append(nodeValue);
            } else if (nodeValue != null && isLockedSegment(p_node)) {
                results.append(AmbassadorDwUpConstants.SEGMENT_MATCH_TYPE_KEY).append(" ")
                        .append("DO NOT TRANSLATE OR MODIFY (Locked).").append(TTXConstants.NEW_LINE);
                results.append(nodeValue);
            }
        } else {
            results.append(nodeValue);
        }
        break;
    default:

        return;
    }
}

From source file:com.globalsight.everest.projecthandler.ProjectTmTuvT.java

License:Apache License

/**
 * Returns the XML representation like Element.asXML() but without the
 * top-level tag.//from   w  w  w  .j  av  a2  s. co  m
 */
@SuppressWarnings("unchecked")
private String getInnerXml(Element p_node) {
    StringBuffer result = new StringBuffer();
    List<Node> content = p_node.content();

    for (Node node : content) {
        if (node.getNodeType() == Node.TEXT_NODE) {
            result.append(EditUtil.encodeXmlEntities(node.getText()));
        } else {
            StringWriter out = new StringWriter();
            result.append(out.toString());
        }
    }

    return result.toString();
}

From source file:com.globalsight.everest.tm.exporter.TmxWriter.java

License:Apache License

/**
 * Returns the XML representation like Element.asXML() but without the
 * top-level tag./*from  w w  w.j  a va 2  s . c om*/
 */
private static String getInnerXml(Element p_node, OutputFormat outputFormat) {
    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 {
            // Note: DOM4J's node.asXML() constructs the same 2 objects.
            StringWriter out = new StringWriter();
            XMLWriter writer = new XMLWriter(out, outputFormat);

            try {
                writer.write(node);
            } catch (IOException ignore) {
            }

            result.append(out.toString());
        }
    }

    return result.toString();
}

From source file:com.globalsight.everest.tm.importer.ImportUtil.java

License:Apache License

/**
 * Returns the XML representation like Element.asXML() but without the
 * top-level tag.//from w  ww  .ja  va2  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(encodeXmlEntities(node.getText()));
        } else {
            // Element nodes write their text nodes correctly.
            result.append(node.asXML());
        }
    }

    return result.toString();
}

From source file:com.globalsight.everest.tm.util.ttx.TtxClean.java

License:Apache License

/**
 * Returns the inner text like Element.getText() but for all
 * embedded text nodes.//  w  w  w.j a v  a 2s . c  om
 */
static public String getInnerText(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);

        if (node.getNodeType() == Node.TEXT_NODE) {
            result.append(node.getText());
        } else if (node.getNodeType() == Node.ELEMENT_NODE) {
            result.append(getInnerText((Element) node));
        }
    }

    return result.toString();
}

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

License:Apache License

static public String getTranslateInnerXml(Element p_node) {
    StringBuilder result = new StringBuilder();
    List content = p_node.content();
    for (int i = 0; i < content.size(); i++) {
        Node node = (Node) content.get(i);
        if (node.getNodeType() == Node.TEXT_NODE) {
            result.append(encodeXmlEntities(node.getText()));
        }/*from  w ww . ja va 2s  .c  o  m*/
    }
    return result.toString();
}

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.j  a  v  a 2s . c o m
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();
}