Example usage for org.dom4j Element add

List of usage examples for org.dom4j Element add

Introduction

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

Prototype

void add(Namespace namespace);

Source Link

Document

Adds the given Namespace to this element.

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 w w  w . jav a 2  s  .  co m*/
private static 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.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  w  w  w .  ja  v  a2  s .  c  o  m
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.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  ww  w. ja v a 2s  .  c o m*/
 */
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.
 *//*from   w w  w.ja  v a  2 s.  com*/
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.
 *///from w w  w.  j  a  va2  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() {
        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 ww  w .j  a va  2 s .  co  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  w w. j av  a 2 s.c om*/
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

/**
 * Removes a element by pulling up its children into the parent node.
 *//*  www . j  a v a2s  .  c  om*/
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.
 *///from w  w  w. j  av  a  2  s  .c o  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);
        }
    }
}

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

License:Apache License

/**
 * Creates new elements for TU and TUV with lowercase names by
 * constructing new elements and inserting them in place of the
 * old ones./*from w ww  .  j a  v a 2s .co m*/
 */
private Element lowercaseElements(Element p_tu) {
    Element tu = m_factory.createElement("tu");
    tu.setAttributes(p_tu.attributes());

    List tuvs = p_tu.selectNodes("//Tuv");

    for (int i = 0, max = tuvs.size(); i < max; i++) {
        Element tuv = (Element) tuvs.get(i);
        // Detach the TUV or else TUVs will accumulate on
        // subsequent TUs (sic!).
        tuv.detach();

        Element newTuv = m_factory.createElement("tuv");
        newTuv.setAttributes(tuv.attributes());
        newTuv.setContent(tuv.content());

        tu.add(newTuv);
    }

    return tu;
}