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

/**
 * Gets the entry's concept ID.//from w w  w .j av  a  2  s .c  o  m
 * @return 0 if the entry has no ID yet, else a positive number.
 */
static public long getConceptId(Entry p_entry) throws TermbaseException {
    Document dom = p_entry.getDom();
    Element root = dom.getRootElement();
    Element concept = root.element("concept");

    if (concept == null || concept.getText().length() == 0) {
        return 0;
    } else {
        return Long.parseLong(concept.getText());
    }
}

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

License:Apache License

/**
 * Get TBX files entry id/*  w w w  .  j  a  v a2s  .  c o m*/
 * @param entry
 * @return
 * @throws TermbaseException
 */
public static String getTbxTermEntryId(Entry entry) throws TermbaseException {
    Document dom = entry.getDom();
    Element root = dom.getRootElement();

    if (root.attribute("id") == null || root.attribute("id").getText().length() == 0) {
        return null;
    } else {
        return root.attribute("id").getText();
    }
}

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

License:Apache License

/**
 * Inserts <transacGrp><transac type="origination">user</transac>
 * <date></date></transacGrp> into an entry.  This is for
 * Termbase.addEntry() where the creation time is NOW.
 *
 * For imported entries that may have their own timestamp, use a
 * batch import function./* www .  j  a  v  a  2  s  .co m*/
 */
static public void setCreationTimeStamp(Entry p_entry, SessionInfo p_session) throws TermbaseException {
    String timestamp = UTC.valueOf(p_session.getTimestamp());
    String username = p_session.getUserName();

    Document dom = p_entry.getDom();
    Element root = dom.getRootElement();

    Element transac = (Element) root.selectSingleNode("/conceptGrp/transacGrp/transac[@type='origination']");
    Element conceptGrp, transacGrp, date;

    if (transac != null) {
        // Timestamp sneaked in, overwrite.
        transac.setText(username);

        transacGrp = transac.getParent();
        date = transacGrp.element("date");
        if (date == null) {
            transacGrp.addElement("date").addText(timestamp);
        } else {
            date.setText(timestamp);
        }
    } else {
        // No origination timestamp, create one.
        transacGrp = root.addElement("transacGrp");
        transac = transacGrp.addElement("transac").addAttribute("type", "origination").addText(username);
        date = transacGrp.addElement("date").addText(timestamp);
    }

    // Man this was a cinch. Me love dom4j.

    // let entry know its dom is dirty
    p_entry.setDom(dom);
}

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

License:Apache License

/**
 * Inserts <transacGrp><transac type="modification">user</transac>
 * <date></date></transacGrp> into an entry.  This is for
 * Termbase.updateEntry() where the creation time is NOW.
 */// w ww.j a v  a 2 s. co m
static public void setModificationTimeStamp(Entry p_entry, SessionInfo p_session) throws TermbaseException {
    String timestamp = UTC.valueOf(p_session.getTimestamp());
    String username = p_session.getUserName();

    Document dom = p_entry.getDom();
    Element root = dom.getRootElement();

    Element transac = (Element) root.selectSingleNode("/conceptGrp/transacGrp/transac[@type='modification']");
    Element conceptGrp, transacGrp, date;

    if (transac != null) {
        // Timestamp exists, overwrite.
        transac.setText(username);

        transacGrp = transac.getParent();
        date = transacGrp.element("date");
        if (date == null) {
            transacGrp.addElement("date").addText(timestamp);
        } else {
            date.setText(timestamp);
        }
    } else {
        // No modification timestamp, create one.
        transacGrp = root.addElement("transacGrp");
        transac = transacGrp.addElement("transac").addAttribute("type", "modification").addText(username);
        transacGrp.addElement("date").addText(timestamp);
    }

    // let entry know its dom is dirty
    p_entry.setDom(dom);
}

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

License:Apache License

/** Returns the list of <languageGrp> nodes in the entry. */
static public List getLanguageGrps(Entry p_entry) throws TermbaseException {
    Document dom = p_entry.getDom();
    Element root = dom.getRootElement();

    return root.selectNodes("//languageGrp");
}

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

License:Apache License

/** Returns the list of <term> nodes in the entry. */
static public List getTerms(Entry p_entry) throws TermbaseException {
    Document dom = p_entry.getDom();
    Element root = dom.getRootElement();

    return root.selectNodes("//term");
}

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

License:Apache License

/**
 * Finds the preferred term in the given language. The preferred
 * term is the one that has a usage='preferred' field. If no term
 * is qualified as preferred, the first term is returned.
 *
 * @return preferred term as string if found, else null.
 *//*  w  w w  .jav a  2  s. c om*/
static public String getPreferredTbxTerm(Entry p_entry, String p_language) throws TermbaseException {
    Document dom = p_entry.getDom();
    Element root = dom.getRootElement();
    String result = null;

    List langSets = root.elements("langSet");
    if (langSets.size() == 0) {
        return null;
    }

    for (int i = 0; i < langSets.size(); i++) {
        Element langSet = (Element) langSets.get(i);
        if (EntryUtils.getLanguageName(langSet.attribute("lang").getText()).equals(p_language)) {
            if (langSet.elements("ntig").size() != 0) {
                result = langSet.element("ntig").element("termGrp").element("term").getText();
            }
            if (langSet.elements("tig").size() != 0) {
                result = langSet.element("tig").element("term").getText();
            }

            break;
        }
    }
    return result;
}

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

License:Apache License

/**
 * Finds the preferred term in the given language. The preferred
 * term is the one that has a usage='preferred' field. If no term
 * is qualified as preferred, the first term is returned.
 *
 * @return preferred term as string if found, else null.
 *//*  ww  w  . j  a  v a  2 s  . co  m*/
static public String getPreferredTerm(Entry p_entry, String p_language) throws TermbaseException {
    Document dom = p_entry.getDom();
    Element root = dom.getRootElement();

    List termGrps = root.selectNodes("//languageGrp[./language/@name='" + p_language + "']/termGrp");

    if (termGrps.size() == 0) {
        return null;
    }

    for (int i = 0, max = termGrps.size(); i < max; i++) {
        Element termGrp = (Element) termGrps.get(i);
        Node usage = termGrp.selectSingleNode("descripGrp/descrip[@type='usage']");

        if (usage != null && "preferred".equals(usage.getText())) {
            return termGrp.element("term").getText();
        }
    }

    return ((Element) termGrps.get(0)).element("term").getText();
}

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

License:Apache License

/**
 * <p>Checks that an TBX entry is consistent with the termbase
 * definition (at least one term per language, languages defined
 * in termbase, required fields present, etc).</p>
 *
 * <p>For now, check that we have at least one term. General entry
 * structure validation should have been done by the XML Parser
 * that parsed the entry into DOM - using the EntryStructure
 * schema. We also remove empty fields and groups.</p>
 *
 * TO BE COMPLETED//from   w  w  w.  java 2  s  .co  m
 */
public static void normalizeTbxEntry(Entry p_entry, Definition p_definition) throws TermbaseException {
    try {
        boolean ok = false;
        // Remove empty fields and then check if there's at least
        // one term in the entry.
        pruneEntry(p_entry);

        Document dom = p_entry.getDom();
        Element root = dom.getRootElement();

        //termEntry-level
        for (Iterator it = root.elementIterator(); it.hasNext();) {
            Element tnode = (Element) it.next();

            if (tnode.getName().equals("langSet")) {
                //langSet-level
                for (Iterator it2 = tnode.elementIterator(); it2.hasNext();) {
                    Element lnode = (Element) it2.next();

                    if (lnode.getName().equals("ntig")) {
                        //ntig-level
                        for (Iterator it3 = lnode.elementIterator(); it3.hasNext();) {
                            Element ntignode = (Element) it3.next();

                            if (ntignode.getName().equals("termGrp")) {
                                //term-node
                                for (Iterator it4 = ntignode.elementIterator(); it4.hasNext();) {
                                    Element termnode = (Element) it4.next();

                                    if (termnode.getName().equals("term")) {
                                        String text = termnode.getText();
                                        if (text != null && text.length() > 0) {
                                            ok = true;
                                            break;
                                        }
                                    }
                                }
                            }
                            if (ok)
                                break;
                        }
                        if (!ok)
                            invalidEntry("no terms defined");
                    }
                    if (lnode.getName().equals("tig")) {
                        //tig-level
                        for (Iterator it3 = lnode.elementIterator(); it3.hasNext();) {
                            Element tignode = (Element) it3.next();

                            if (tignode.getName().equals("term")) {
                                String text = tignode.getText();
                                if (text != null && text.length() > 0) {
                                    ok = true;
                                    break;
                                }
                            }
                        }
                    }
                    if (ok)
                        break;
                }
            }
            if (ok)
                break;
        }
        if (!ok) {
            invalidEntry("no languages defined");
        }
    } catch (TermbaseException e) {
        throw e;
    } catch (Exception e) {
        invalidEntry(e.getMessage());
    }
}

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

License:Apache License

/**
 * <p>Checks that an entry is consistent with the termbase
 * definition (at least one term per language, languages defined
 * in termbase, required fields present, etc).</p>
 *
 * <p>For now, check that we have at least one term. General entry
 * structure validation should have been done by the XML Parser
 * that parsed the entry into DOM - using the EntryStructure
 * schema. We also remove empty fields and groups.</p>
 *
 * TO BE COMPLETED//  w  w w  . ja  v a2 s.  co m
 */
static public void normalizeEntry(Entry p_entry, Definition p_definition) throws TermbaseException {
    try {
        boolean ok = false;

        // Remove empty fields and then check if there's at least
        // one term in the entry.
        pruneEntry(p_entry);

        Document dom = p_entry.getDom();
        Element root = dom.getRootElement();

        // CONCEPT-LEVEL
        for (Iterator it = root.elementIterator(); it.hasNext();) {
            Element cnode = (Element) it.next();

            if (cnode.getName().equals("languageGrp")) {
                // LANGUAGE-LEVEL
                for (Iterator it2 = cnode.elementIterator(); it2.hasNext();) {
                    Element lnode = (Element) it2.next();

                    if (lnode.getName().equals("termGrp")) {
                        // TERM-LEVEL
                        for (Iterator it3 = lnode.elementIterator(); it3.hasNext();) {
                            Element tnode = (Element) it3.next();

                            if (tnode.getName().equals("term")) {
                                String text = tnode.getText();
                                if (text != null && text.length() > 0) {
                                    ok = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (ok)
                        break;
                }

                if (!ok) {
                    invalidEntry("no terms defined");
                }
            }

            if (ok)
                break;
        }

        if (!ok) {
            invalidEntry("no languages defined");
        }
    } catch (TermbaseException e) {
        throw e;
    } catch (Exception e) {
        invalidEntry(e.getMessage());
    }
}