Example usage for org.dom4j Element content

List of usage examples for org.dom4j Element content

Introduction

In this page you can find the example usage for org.dom4j Element content.

Prototype

List<Node> content();

Source Link

Document

Returns the content nodes of this branch as a backed List so that the content of this branch may be modified directly using the List interface.

Usage

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

License:Apache License

/**
 * Removes the given <sub> element from the segment. <sub> is special since
 * it does not only surround embedded tags but also text, which must be
 * pulled out of the <sub> and added to the parent tag.
 *//*from  www.  j  av a 2 s.  c  om*/
private static void replaceNbsp(Element p_element) {
    Element parent = p_element.getParent();
    int index = parent.indexOf(p_element);

    // We copy the current content, clear out the parent, and then
    // re-add the old content, inserting the <sub>'s textual
    // content instead of the <sub> (this clears any embedded TMX
    // tags in the subflow).

    ArrayList newContent = new ArrayList();
    List content = parent.content();

    for (int i = content.size() - 1; i >= 0; --i) {
        Node node = (Node) content.get(i);

        newContent.add(node.detach());
    }

    Collections.reverse(newContent);
    parent.clearContent();

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

        if (i == index) {
            parent.addText("\u00A0");
        } else {
            parent.add(node);
        }
    }
}

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  w  w.  ja  v  a  2 s . c  om*/
 */
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.importer.TmxReaderThread.java

License:Apache License

/**
 * Removes the given TMX 1.4 <sub> element from the segment. <sub>
 * is special since it does not only surround embedded tags but
 * also text, which must be pulled out of the <sub> and added to
 * the parent tag./*from w  w w  .  j  a  va 2s  .com*/
 */
private void removeSubElement(Element p_element) {
    Element parent = p_element.getParent();
    int index = parent.indexOf(p_element);

    // We copy the current content, clear out the parent, and then
    // re-add the old content, inserting the <sub>'s textual
    // content instead of the <sub> (this clears any embedded TMX
    // tags in the subflow).

    ArrayList newContent = new ArrayList();
    List content = parent.content();

    for (int i = content.size() - 1; i >= 0; --i) {
        Node node = (Node) content.get(i);

        newContent.add(node.detach());
    }

    Collections.reverse(newContent);
    parent.clearContent();

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

        if (i == index) {
            parent.addText(p_element.getText());
        } else {
            parent.add(node);
        }
    }
}

From source file:com.globalsight.everest.tm.util.trados.TradosFmSgmlTmxToGxml.java

License:Apache License

/**
 * Main method to call, returns the new filename of the result.
 *//*  w  w  w . jav  a  2  s. c  o  m*/
public String convertToGxml(String p_url) throws Exception {
    final String baseName = getBaseName(p_url);
    final String extension = getExtension(p_url);

    info("Converting TMX file to GXML: `" + p_url + "'");
    startOutputFile(baseName);

    m_entryCount = 0;

    // Reading from a file, need to use Xerces.
    SAXReader reader = new SAXReader();
    reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser");
    reader.setEntityResolver(DtdResolver.getInstance());
    reader.setValidation(true);

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx", new ElementHandler() {
        final public void onStart(ElementPath path) {
            Element element = path.getCurrent();

            m_version = element.attributeValue("version");
        }

        final public void onEnd(ElementPath path) {
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/header", new ElementHandler() {
        final public void onStart(ElementPath path) {
        }

        final public void onEnd(ElementPath path) {
            Element element = path.getCurrent();

            setOldHeader(element);
            createNewHeader();

            // prune the current element to reduce memory
            element.detach();

            element = null;
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/body/tu", new ElementHandler() {
        final public void onStart(ElementPath path) {
            ++m_entryCount;
            m_tuError = false;
        }

        final public void onEnd(ElementPath path) {
            Element element = path.getCurrent();

            if (m_tuError) {
                m_errorCount++;
            } else {
                writeEntry(element.asXML());
            }

            // prune the current element to reduce memory
            element.detach();

            element = null;

            if (m_entryCount % 1000 == 0) {
                debug("Entry " + m_entryCount);
            }
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/body/tu/tuv/seg", new ElementHandler() {
        final public void onStart(ElementPath path) {
        }

        final public void onEnd(ElementPath path) {
            Element element = path.getCurrent();

            try {
                String gxml = handleTuv(element);
                Document doc = parse("<root>" + gxml + "</root>");

                // Remove old content of seg
                List content = element.content();
                for (int i = content.size() - 1; i >= 0; --i) {
                    ((Node) content.get(i)).detach();
                }

                // Add new GXML content (backwards)
                content = doc.getRootElement().content();
                Collections.reverse(content);
                for (int i = content.size() - 1; i >= 0; --i) {
                    Node node = (Node) content.get(i);
                    element.add(node.detach());
                }
            } catch (Throwable ex) {
                m_tuError = true;
            }
        }
    });

    Document document = reader.read(p_url);

    closeOutputFile();

    info("Processed " + m_entryCount + " TUs " + "into file `" + m_filename + "', " + m_errorCount
            + " errors.");

    return m_filename;
}

From source file:com.globalsight.everest.tm.util.trados.TradosFmTmxToGxml.java

License:Apache License

/**
 * Main method to call, returns the new filename of the result.
 *///  ww  w  .  jav a2s .c o  m
public String convertToGxml(String p_url) throws Exception {
    final String baseName = getBaseName(p_url);
    final String extension = getExtension(p_url);

    info("Converting TMX file to GXML: `" + p_url + "'");
    startOutputFile(baseName);

    m_entryCount = 0;

    // Reading from a file, need to use Xerces.
    SAXReader reader = new SAXReader();
    reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser");
    reader.setEntityResolver(DtdResolver.getInstance());
    reader.setValidation(true);

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx", new ElementHandler() {
        public void onStart(ElementPath path) {
            Element element = path.getCurrent();

            m_version = element.attributeValue("version");
        }

        public void onEnd(ElementPath path) {
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/header", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element element = path.getCurrent();

            setOldHeader(element);
            createNewHeader();

            // prune the current element to reduce memory
            element.detach();

            element = null;
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/body/tu", new ElementHandler() {
        public void onStart(ElementPath path) {
            ++m_entryCount;
            m_tuError = false;
        }

        public void onEnd(ElementPath path) {
            Element element = path.getCurrent();

            if (m_tuError) {
                m_errorCount++;
            } else {
                writeEntry(element.asXML());
            }

            // prune the current element to reduce memory
            element.detach();

            element = null;

            if (m_entryCount % 1000 == 0) {
                debug("Entry " + m_entryCount);
            }
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/body/tu/tuv/seg", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element element = path.getCurrent();

            try {
                String gxml = handleTuv(element);
                Document doc = parse("<root>" + gxml + "</root>");

                // Remove old content of seg
                List content = element.content();
                for (int i = content.size() - 1; i >= 0; --i) {
                    ((Node) content.get(i)).detach();
                }

                // Add new GXML content (backwards)
                content = doc.getRootElement().content();
                Collections.reverse(content);
                for (int i = content.size() - 1; i >= 0; --i) {
                    Node node = (Node) content.get(i);
                    element.add(node.detach());
                }
            } catch (Throwable ex) {
                m_tuError = true;
            }
        }
    });

    Document document = reader.read(p_url);

    closeOutputFile();

    info("Processed " + m_entryCount + " TUs " + "into file `" + m_filename + "', " + m_errorCount
            + " errors.");

    return m_filename;
}

From source file:com.globalsight.everest.tm.util.trados.TradosHtmlTmxToGxml.java

License:Apache License

/**
 * Main method to call, returns the new filename of the result.
 *///from w w w .  j  a v a 2s .c o  m
public String convertToGxml(String p_url) throws Exception {
    final String baseName = getBaseName(p_url);
    final String extension = getExtension(p_url);

    info("Converting TMX file to GXML: `" + p_url + "'");

    startOutputFile(baseName);

    m_entryCount = 0;

    // Reading from a file, need to use Xerces.
    SAXReader reader = new SAXReader();
    reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser");
    reader.setEntityResolver(DtdResolver.getInstance());
    reader.setValidation(true);

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx", new ElementHandler() {
        public void onStart(ElementPath path) {
            Element element = path.getCurrent();

            m_version = element.attributeValue("version");
        }

        public void onEnd(ElementPath path) {
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/header", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element element = path.getCurrent();
            setOldHeader(element);
            createNewHeader();

            // prune the current element to reduce memory
            element.detach();

            element = null;
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/body/tu", new ElementHandler() {
        public void onStart(ElementPath path) {
            ++m_entryCount;
            m_tuError = false;
        }

        public void onEnd(ElementPath path) {
            Element element = path.getCurrent();

            if (m_tuError) {
                m_errorCount++;
            } else {
                writeEntry(element.asXML());
            }

            // prune the current element to reduce memory
            element.detach();

            element = null;

            if (m_entryCount % 1000 == 0) {
                debug("Entry " + m_entryCount);
            }
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/body/tu/tuv/seg", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element element = path.getCurrent();

            try {
                element = removeUtElements(element);

                String gxml = handleTuv(element.getText());
                Document doc = parse("<root>" + gxml + "</root>");

                // Remove old content of seg
                List content = element.content();
                for (int i = content.size() - 1; i >= 0; --i) {
                    ((Node) content.get(i)).detach();
                }

                // Add new GXML content (backwards)
                content = doc.getRootElement().content();
                Collections.reverse(content);
                for (int i = content.size() - 1; i >= 0; --i) {
                    Node node = (Node) content.get(i);
                    element.add(node.detach());
                }
            } catch (Throwable ex) {
                m_tuError = true;
            }
        }
    });

    Document document = reader.read(p_url);

    closeOutputFile();

    info("Processed " + m_entryCount + " TUs into file `" + m_filename + "', " + m_errorCount + " errors.");

    return m_filename;
}

From source file:com.globalsight.everest.tm.util.trados.TradosTmxToRtf.java

License:Apache License

/**
 * Removes the given TMX 1.4 <ut> element from the segment. <ut> is special
 * since it does not surround embedded tags but text, witch must be pulled
 * out of the <ut> and added to the parent segment.
 *///from  w  ww .  j a va 2 s .co m
private void removeUtElement(Element p_element) {
    Element parent = p_element.getParent();
    int index = parent.indexOf(p_element);

    // We copy the current content, clear out the parent, and then
    // re-add the old content, inserting the <ut>'s content
    // instead of the <ut>.

    ArrayList newContent = new ArrayList();
    List content = parent.content();

    for (int i = content.size() - 1; i >= 0; --i) {
        Node node = (Node) content.get(i);

        newContent.add(node.detach());
    }

    Collections.reverse(newContent);
    parent.clearContent();

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

        if (i == index) {
            parent.appendContent(p_element);
        } else {
            parent.add(node);
        }
    }
}

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./*from ww  w  . j  a va  2  s.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.everest.tm.util.ttx.TtxClean.java

License:Apache License

/**
 * Removes a element by pulling up its children into the parent node.
 *///from   w w  w . ja  v a  2s  .  co m
private void removeElement(Element p_element) {
    Element parent = p_element.getParent();
    int index = parent.indexOf(p_element);

    // We copy the current content, clear out the parent, and then
    // re-add the old content, inserting the <ut>'s content
    // instead of the <ut>.

    ArrayList newContent = new ArrayList();
    List content = parent.content();

    for (int i = content.size() - 1; i >= 0; --i) {
        Node node = (Node) content.get(i);

        newContent.add(node.detach());
    }

    Collections.reverse(newContent);
    parent.clearContent();

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

        if (i == index) {
            parent.appendContent(p_element);
        } else {
            parent.add(node);
        }
    }
}

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

License:Apache License

/**
 * Removes a TU element by pulling up the content of the TUV in
 * the given locale into the parent node.
 *///  ww  w . j a v a2 s .co m
private void removeTuElement(Element p_element, String p_locale) {
    // The source or target language TUV to replace the TU with.
    Element tuv = (Element) p_element.selectSingleNode("//Tuv[@Lang='" + p_locale + "']");

    Element parent = p_element.getParent();
    int index = parent.indexOf(p_element);

    // We copy the current content, clear out the parent, and then
    // re-add the tuv content.

    ArrayList newContent = new ArrayList();
    List content = parent.content();

    for (int i = content.size() - 1; i >= 0; --i) {
        Node node = (Node) content.get(i);

        newContent.add(node.detach());
    }

    Collections.reverse(newContent);
    parent.clearContent();

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

        if (i == index) {
            parent.appendContent(tuv);
        } else {
            parent.add(node);
        }
    }
}