Example usage for org.dom4j Element addElement

List of usage examples for org.dom4j Element addElement

Introduction

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

Prototype

Element addElement(String name);

Source Link

Document

Adds a new Element node with the given name to this branch and returns a reference to the new node.

Usage

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.//from   w w  w. java2  s.c  om
 */
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.
 *//*from  w  w w  . j a  v  a 2 s.c  o 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.exporter.MtfWriter.java

License:Apache License

/**
 * Converts a GlobalSight concept group to a MultiTerm iX concept
 * group. Differences://from   w  w  w .  j a va  2 s  .c om
 *
 *  - 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.importer.CsvReaderThread.java

License:Apache License

private Element buildTermGrp(String p_value) {
    Element grp = m_factory.createElement("termGrp");
    Element node = grp.addElement("term").addText(p_value);
    return grp;//from  ww  w  .  ja  v a  2 s  .c o m
}

From source file:com.globalsight.terminology.importer.CsvReaderThread.java

License:Apache License

private Element buildDescripGrp(String p_value, String p_type) {
    Element grp = m_factory.createElement("descripGrp");
    Element node = grp.addElement("descrip").addText(p_value);
    node.addAttribute("type", p_type);
    return grp;//from  ww w  .  ja  v  a  2  s .  co  m
}

From source file:com.globalsight.terminology.importer.CsvReaderThread.java

License:Apache License

private Element buildSourceGrp(String p_value) {
    Element grp = m_factory.createElement("sourceGrp");
    Element node = grp.addElement("source").addText(p_value);
    return grp;/*from ww w .  j  av a 2  s.  c  om*/
}

From source file:com.globalsight.terminology.importer.CsvReaderThread.java

License:Apache License

private Element buildLanguageGrp(String p_language) throws TermbaseException {
    String locale = m_termbase.getLocaleByLanguage(p_language);

    Element grp = m_factory.createElement("languageGrp");
    Element node = grp.addElement("language");
    node.addAttribute("name", p_language);
    node.addAttribute("locale", locale);
    return grp;//from  ww  w.  j a  v a  2  s . c o  m
}

From source file:com.globalsight.terminology.importer.MtfReaderThread.java

License:Apache License

/**
 * Converts a MultiTerm MTF concept group to a GlobalSight concept
 * group. Differences://from ww w  .  ja  va  2  s .  c om
 *
 *  - <system type="entryClass|status"> -->
 *    <descrip type="entryClass|status">
 *
 *  - <language type="English" lang="EN" /> -->
 *    <language name="English" locale="en_US" />
 *
 *  - <descripGrp><descrip type="note"> -->
 *    <noteGrp><note>
 *
 *  - <descripGrp><descrip type="source"> -->
 *    <sourceGrp><source>
 *
 *  - descripGrp is recursive, must map to noteGrps or delete.
 *
 *  - remove empty descripGrp <descrip type="Graphic"/>
 */
private Element convertMtf(Element p_elem) {
    List nodes;
    Node node;
    Element elem;
    Iterator it;
    ListIterator lit;

    // fix <system>
    nodes = p_elem.elements("system");
    for (it = nodes.iterator(); it.hasNext();) {
        elem = (Element) it.next();
        p_elem.remove(elem);
        p_elem.addElement("descrip").addAttribute("type", elem.attributeValue("type")).addText(elem.getText());
    }

    // fix Graphic; we cannot handle them, so remove them
    nodes = p_elem.selectNodes("descripGrp[descrip/@type='Graphic']");
    for (it = nodes.iterator(); it.hasNext();) {
        elem = (Element) it.next();
        p_elem.remove(elem);
    }

    // convert <descripGrp><descrip type="note"> to noteGrp
    nodes = p_elem.selectNodes(".//descripGrp[descrip/@type='note']");
    for (it = nodes.iterator(); it.hasNext();) {
        elem = (Element) it.next();

        convertToNoteGrp(elem);
    }

    // convert <descripGrp><descrip type="source"> to sourceGrp
    nodes = p_elem.selectNodes(".//descripGrp[descrip/@type='source']");
    for (it = nodes.iterator(); it.hasNext();) {
        elem = (Element) it.next();

        convertToSourceGrp(elem);
    }

    // Convert recursive descripGrps to noteGrps if possible.
    convertRecursiveDescripGrps(p_elem, ".//conceptGrp/descripGrp");
    convertRecursiveDescripGrps(p_elem, ".//languageGrp/descripGrp");
    convertRecursiveDescripGrps(p_elem, ".//termGrp/descripGrp");

    // Remove the recursive descripGrps that are left over.
    // (In case there are doubly recursive descrips and stuff.)
    removeRecursiveDescripGrps(p_elem);

    // fix <language>
    nodes = p_elem.selectNodes("languageGrp/language");

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

        Attribute nameAttr = elem.attribute("type");
        Attribute langAttr = elem.attribute("lang");

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

        // locales in entries consist of 2 letter codes.
        langLocale = langLocale.substring(0, 2).toLowerCase();

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

        elem.addAttribute("name", langName);
        elem.addAttribute("locale", langLocale);
    }

    return p_elem;
}

From source file:com.gote.pojo.Game.java

License:Apache License

/**
 * Transform Game to XML// ww w  .  j  a  va2 s  . c  om
 * 
 * @param pRoot "Games" element
 * @see TournamentGOTEUtil
 */
public void toXML(Element pRoot) {
    // Create game element
    Element game = pRoot.addElement(TournamentGOTEUtil.TAG_GAME);
    // Add its attributes
    game.addAttribute(TournamentGOTEUtil.ATT_GAME_WHITEPLAYER, getWhite().getPseudo());
    game.addAttribute(TournamentGOTEUtil.ATT_GAME_BLACKPLAYER, getBlack().getPseudo());
    game.addAttribute(TournamentGOTEUtil.ATT_GAME_URL, getGameUrl());
    game.addAttribute(TournamentGOTEUtil.ATT_GAME_KOMI, getKomi());
    game.addAttribute(TournamentGOTEUtil.ATT_GAME_HANDICAP, getHandicap());
    game.addAttribute(TournamentGOTEUtil.ATT_GAME_RESULT, getResult());
}

From source file:com.gote.pojo.Player.java

License:Apache License

/**
 * Transform player to XML/*from ww  w.  j  a v a 2s  . co m*/
 * 
 * @param pRoot "Players" element
 * @see TournamentGOTEUtil
 */
public void toXML(Element pRoot) {
    // Create player element
    Element player = pRoot.addElement(TournamentGOTEUtil.TAG_PLAYER);
    // Add its attributes
    player.addAttribute(TournamentGOTEUtil.ATT_PLAYER_PSEUDO, getPseudo());
    player.addAttribute(TournamentGOTEUtil.ATT_PLAYER_RANK, getRank());
    player.addAttribute(TournamentGOTEUtil.ATT_PLAYER_FIRSTNAME, getFirstname());
}