Example usage for org.dom4j Document getRootElement

List of usage examples for org.dom4j Document getRootElement

Introduction

In this page you can find the example usage for org.dom4j Document getRootElement.

Prototype

Element getRootElement();

Source Link

Document

Returns the root Element for this document.

Usage

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

License:Apache License

static public void pruneEntry(Entry p_entry) throws TermbaseException {
    try {/*w  w w  .  j  ava  2 s  . com*/
        Document dom = p_entry.getDom();
        Element root = dom.getRootElement();

        boolean dirty = removeInsignificantWhitespace(root);
        dirty |= pruneEmptyFields(root);

        if (dirty) {
            // let entry know its dom is dirty
            p_entry.setDom(dom);
        }
    } catch (Exception e) {
        invalidEntry(e.getMessage());
    }
}

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

License:Apache License

/**
 * <p>Merges the information in entry2 into entry1.</p>
 *
 * @return the modified version of entry1.
 *//* ww  w . jav  a 2s.co  m*/
public static Entry mergeTbxEntries(Entry p_one, Entry p_two) throws TermbaseException {
    Entry result = new Entry();

    pruneEntry(p_one);
    pruneEntry(p_two);

    Document dom1 = p_one.getDom();
    Element root1 = dom1.getRootElement();

    Document dom2 = p_two.getDom();
    Element root2 = dom2.getRootElement();

    NodeComparator comp = new NodeComparator();
    mergeTbxInnerGroups(root1, root2, comp);

    // let entry 1 know its dom is dirty
    result.setDom(root1.getDocument());

    return result;
}

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

License:Apache License

/**
 * <p>Merges the information in entry2 into entry1.</p>
 *
 * @return the modified version of entry1.
 *///w w w.j ava 2  s . c  om
static public Entry mergeEntries(Entry p_one, Entry p_two) throws TermbaseException {
    // Remove empty fields and normalize whitespace.
    pruneEntry(p_one);
    pruneEntry(p_two);

    Document dom1 = p_one.getDom();
    Element root1 = dom1.getRootElement();

    Document dom2 = p_two.getDom();
    Element root2 = dom2.getRootElement();

    NodeComparator comp = new NodeComparator();
    mergeInnerGroups(root1, root2, comp);

    // let entry 1 know its dom is dirty
    p_one.setDom(dom1);

    return p_one;
}

From source file:com.globalsight.terminology.exporter.ExportUtil.java

License:Apache License

/**
 * Removes system fields from a termbase entry. System fields are:
 * <ul>//  w  w w  .j ava  2  s  .  c  o  m
 * <li>all transactional timestamps (create, modify)
 * </ul>
 */
static public Document removeSystemFields(Document p_dom) {
    Element root = p_dom.getRootElement();

    removeNodes(root, "//transacGrp");

    return p_dom;
}

From source file:com.globalsight.terminology.exporter.MtfWriter.java

License:Apache License

/**
 * Converts a GlobalSight concept group to a MultiTerm iX concept
 * group. Differences://from  w  ww . java2s. c o  m
 *
 *  - concept level <descrip type="entryClass|status"> -->
 *    <system type="entryClass|status">
 *
 *  - <language name="English" locale="en_US" /> -->
 *    <language type="English" lang="EN" />
 *
 *  - <noteGrp><note> -->
 *    <descripGrp><descrip type="note"></descripGrp>
 *
 *  - <note> -->  (should not be produced but could be in old data)
 *    <descripGrp><descrip type="note"></descripGrp>
 *
 *  - <sourceGrp><source></sourceGrp> -->
 *    <descripGrp><descrip type="source"></descripGrp>
 *
 *  - descripGrp is not recursive
 */
private Document convertToMtf(Document p_elem) {
    List nodes;
    Node node;
    Element root = p_elem.getRootElement();
    Element elem;
    Iterator it;
    ListIterator lit;

    if (false && CATEGORY.isDebugEnabled()) {
        CATEGORY.debug("gt2mtf init: " + p_elem.asXML());
    }

    // rewrite <descrip type=entryClass> (only one on concept level)
    nodes = root.selectNodes("descrip[@type='entryClass']");
    for (it = nodes.iterator(); it.hasNext();) {
        elem = (Element) it.next();
        Element parent = elem.getParent();
        parent.remove(elem);
        parent.addElement("system").addAttribute("type", "entryClass").addText(elem.getText());
    }

    // rewrite <descrip type=status> (?? used in MTF?)
    nodes = root.selectNodes("descrip[@type='status']");
    for (it = nodes.iterator(); it.hasNext();) {
        elem = (Element) it.next();
        Element parent = elem.getParent();
        parent.remove(elem);
        parent.addElement("system").addAttribute("type", "status").addText(elem.getText());
    }

    // rewrite <noteGrp>
    while (true) {
        // refresh the node list, we're rewriting the structure
        node = root.selectSingleNode("//noteGrp");
        if (node == null) {
            break;
        }

        elem = (Element) node;

        Element parent = elem.getParent();
        parent.remove(elem);

        Element newNote = parent.addElement("descripGrp");
        Element note = null;

        // copy all child nodes but remember the <note>
        for (lit = elem.elements().listIterator(); lit.hasNext();) {
            Element child = (Element) lit.next();

            if (child.getName().equals("note")) {
                note = child;
            } else {
                lit.remove();
                newNote.add(child);
            }
        }

        // create new <descrip type="note"> with note's value
        newNote.addElement("descrip").addAttribute("type", "note").addText(note.getText());
    }

    // rewrite single <note>, if any are left in the entry
    while (true) {
        // refresh the node list, we're rewriting the structure
        node = root.selectSingleNode("//note");
        if (node == null) {
            break;
        }

        Element note = (Element) node;

        Element parent = note.getParent();
        parent.remove(note);

        Element newNote = parent.addElement("descripGrp");
        newNote.addElement("descrip").addAttribute("type", "note").addText(note.getText());
    }

    // rewrite <sourceGrp>
    while (true) {
        // refresh the node list, we're rewriting the structure
        node = root.selectSingleNode("//sourceGrp");
        if (node == null) {
            break;
        }

        elem = (Element) node;

        Element parent = elem.getParent();
        parent.remove(elem);

        Element newSource = parent.addElement("descripGrp");
        Element source = null;

        // copy all child nodes but remember the <source>
        for (lit = elem.elements().listIterator(); lit.hasNext();) {
            Element child = (Element) lit.next();

            if (child.getName().equals("source")) {
                source = child;
            } else {
                lit.remove();
                newSource.add(child);
            }
        }

        // create new <descrip type="source"> with source's value
        newSource.addElement("descrip").addAttribute("type", "source").addText(source.getText());
    }

    // rewrite <language>
    nodes = root.selectNodes("//languageGrp/language");

    for (it = nodes.iterator(); it.hasNext();) {
        elem = (Element) it.next();

        Attribute nameAttr = elem.attribute("name");
        Attribute langAttr = elem.attribute("locale");

        String langName = nameAttr.getValue();
        String langLocale = langAttr.getValue();

        // locales in MTF consist of 2 letter codes (uppercase).
        langLocale = langLocale.substring(0, 2).toUpperCase();

        elem.remove(nameAttr);
        elem.remove(langAttr);

        elem.addAttribute("type", langName);
        elem.addAttribute("lang", langLocale);
    }

    if (false && CATEGORY.isDebugEnabled()) {
        CATEGORY.debug("gt2mtf done: " + p_elem.asXML());
    }

    return p_elem;
}

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

License:Apache License

/**
 * Reads and validates a ImportOptions XML string.
 *
 * Xml Format:// w w w  .j a  v a2  s  . c om
 */
private void init(String p_options) throws TermbaseException {
    XmlParser parser = null;
    Document dom;

    try {
        parser = XmlParser.hire();
        dom = parser.parseXml(p_options);
    } finally {
        XmlParser.fire(parser);
    }

    try {
        Element root = dom.getRootElement();

        Element elem = (Element) root.selectSingleNode("/importOptions/fileOptions");

        m_fileOptions.m_name = elem.elementText("fileName");
        m_fileOptions.m_type = elem.elementText("fileType");
        m_fileOptions.m_encoding = elem.elementText("fileEncoding");
        m_fileOptions.m_separator = elem.elementText("separator");
        m_fileOptions.m_ignoreHeader = elem.elementText("ignoreHeader");
        m_fileOptions.m_entryCount = elem.elementText("entryCount");
        m_fileOptions.m_status = elem.elementText("status");
        m_fileOptions.m_errorMessage = elem.elementText("errorMessage");

        List columns = root.selectNodes("/importOptions/columnOptions/column");
        for (int i = 0; i < columns.size(); ++i) {
            Element col = (Element) columns.get(i);
            ColumnDescriptor desc = new ColumnDescriptor();

            desc.m_position = Integer.parseInt(col.attributeValue("id"));
            desc.m_name = col.elementText("name");
            desc.m_example = col.elementText("example");
            desc.m_type = col.elementText("type");
            desc.m_termLanguage = col.elementText("termLanguage");
            desc.m_encoding = col.elementText("encoding");
            desc.m_associatedColumn = col.elementText("associatedColumn");

            m_columns.add(desc);
        }

        elem = (Element) root.selectSingleNode("/importOptions/syncOptions");

        m_syncOptions.m_syncMode = elem.elementText("syncMode");
        m_syncOptions.m_syncLanguage = elem.elementText("syncLanguage");
        m_syncOptions.m_syncAction = elem.elementText("syncAction");
    } catch (Exception e) {
        // cast exception and throw
        error(e.getMessage(), e);
    }
}

From source file:com.globalsight.terminology.indexer.Writer.java

License:Apache License

/**
 * Adds text fields in a single XML fragment to a fulltext index.
 *
 * @param p_object an IndexObject containing an XML string
 * representing an entry fragment./*from www. j  av  a 2  s.  co m*/
 * @see Entry
 */
public void indexXml(Object p_object, ArrayList p_texts) throws IOException {
    IndexObject object = (IndexObject) p_object;

    long cid = object.m_cid;
    long tid = object.m_tid;
    String xml = object.m_text;

    if (xml == null || xml.length() == 0) {
        return;
    }

    try {
        Document dom = parseFragment(xml);
        Element root = dom.getRootElement();

        p_texts.clear();
        getIndexableText(root, p_texts);

        for (int i = 0, max = p_texts.size(); i < max; i++) {
            String text = (String) p_texts.get(i);

            m_index.batchAddDocument(cid, tid, text);
        }
    } catch (IOException ex) {
        throw ex;
    } catch (Throwable ex) {
        throw new IOException(ex.getMessage());
    }
}

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

License:Apache License

private void init(String p_xml) throws TermbaseException {
    XmlParser parser = null;//ww  w . j  av  a 2  s . c  o  m
    Document dom;

    try {
        parser = XmlParser.hire();
        dom = parser.parseXml(p_xml);
    } finally {
        XmlParser.fire(parser);
    }

    try {
        Element root = dom.getRootElement();
        Node lock = root.selectSingleNode("/lock");

        String tbid = lock.valueOf("termbase");
        String cid = lock.valueOf("conceptid");
        String who = lock.valueOf("who");
        String email = lock.valueOf("email");
        String when = lock.valueOf("when");
        String cookie = lock.valueOf("cookie");

        if (tbid == null || cid == null || who == null || email == null || when == null || cookie == null) {
            error("null field in XML", null);
        }

        setTermbase(Long.parseLong(tbid));
        setConceptId(Long.parseLong(cid));
        setUser(who);
        setEmail(email);
        setDate(UTC.parse(when));
        setCookie(cookie);
    } catch (TermbaseException e) {
        throw e;
    } catch (Exception e) {
        // cast exception and throw
        error(e.getMessage(), e);
    }
}

From source file:com.globalsight.terminology.searchreplace.TbMaintance.java

License:Apache License

protected void searchField(ArrayList list, String xml, long conceptId, long levelId) {
    xml = "<root>" + xml + "</root>";

    if (!quickMatch(xml)) {
        return;// w  w  w. ja v a2s  .  c o m
    }

    Document dom = parseXml(xml);
    searchNode(dom.getRootElement(), list, conceptId, levelId);
}

From source file:com.globalsight.terminology.searchreplace.TbMaintance.java

License:Apache License

protected String replaceField(String xml, String oldFieldText, String replaceText) {
    xml = "<root>" + xml + "</root>";
    Document dom = parseXml(xml);
    doNodeVistor(dom.getRootElement(), oldFieldText, replaceText);
    List children = dom.getRootElement().elements();
    String newXml = new String();

    for (int i = 0, max = children.size(); i < max; i++) {
        Element child = (Element) children.get(i);
        newXml = newXml + child.asXML();
    }//from   w  w  w  . j a  v a  2s  .com

    return newXml;
}