Example usage for org.dom4j Element attribute

List of usage examples for org.dom4j Element attribute

Introduction

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

Prototype

Attribute attribute(QName qName);

Source Link

Document

DOCUMENT ME!

Usage

From source file:com.globalsight.everest.tm.util.Ttx.java

License:Apache License

private void init(Element p_element) {
    Element elem;
    Attribute attr;//ww w.  ja v a2s .  co  m
    List nodes;
    Date date;

    elem = (Element) p_element.selectSingleNode("//ToolSettings");

    attr = elem.attribute(CREATIONDATE);
    if (attr == null) {
        date = null;
    } else {
        date = UTC.parseNoSeparators(attr.getValue());
        if (date == null) {
            date = UTC.parse(attr.getValue());
        }
    }
    m_creationdate = date;

    m_creationtool = elem.attributeValue(CREATIONTOOL);
    m_creationtoolversion = elem.attributeValue(CREATIONTOOLVERSION);

    elem = (Element) p_element.selectSingleNode("//UserSettings");

    m_datatype = elem.attributeValue(DATATYPE);
    m_o_encoding = elem.attributeValue(O_ENCODING);
    m_settingsname = elem.attributeValue(SETTINGSNAME);
    m_settingspath = elem.attributeValue(SETTINGSPATH);
    m_sourcelanguage = elem.attributeValue(SOURCELANGUAGE);
    m_targetlanguage = elem.attributeValue(TARGETLANGUAGE);
    m_targetdefaultfont = elem.attributeValue(TARGETDEFAULTFONT);
    m_sourcedocumentpath = elem.attributeValue(SOURCEDOCUMENTPATH);
}

From source file:com.globalsight.ling.docproc.DiplomatWordCounter.java

License:Apache License

private boolean isSkipElement(Element element) {
    Attribute attribute = element.attribute("isSkip");
    if (attribute != null) {
        return "true".equals(attribute.getValue());
    }//from ww  w.  j a v a 2 s.c  o m
    return false;
}

From source file:com.globalsight.ling.docproc.worldserver.WsSkeletonDispose.java

License:Apache License

@Override
public String dealSkeleton(String skeleton, String localizedBy) {
    boolean localizedByUser = false;
    boolean localizedByMT = false;
    boolean localizedByLocalTM = false;

    if (localizedBy != null && localizedBy.equals(PageTemplate.byUser)) {
        localizedByUser = true;/*w ww.  j  a  v  a  2  s  .co m*/
    } else if (localizedBy != null && localizedBy.equals(PageTemplate.byMT)) {
        localizedByMT = true;
    } else if (localizedBy != null && localizedBy.equals(PageTemplate.byLocalTM)) {
        localizedByLocalTM = true;
    }

    int begin = skeleton.indexOf("<" + IWS_SEGMENT_DATA);
    int end = skeleton.indexOf("</" + IWS_SEGMENT_DATA) + ("</" + IWS_SEGMENT_DATA + ">").length();

    String iwsStr = skeleton.substring(begin, end);
    iwsStr = "<segmentdata " + "xmlns:iws=\"http://www.idiominc.com/ws/asset\">" + iwsStr + "</segmentdata>";
    Document dom = getDom(iwsStr);
    Element root = dom.getRootElement();
    List iwsStatusList = root.selectNodes("//iws:status");

    for (int x = 0; x < iwsStatusList.size(); x++) {
        Element status = (Element) iwsStatusList.get(x);

        if (localizedByUser || localizedByLocalTM || localizedByMT) {
            /*
             * if (status.attribute("match-quality") != null)
             * status.remove(status.attribute("match-quality"));
             * 
             * if (status.attribute("source_content") != null)
             * status.remove(status.attribute("source_content"));
             * 
             * if (status.attribute("target_content") != null)
             * status.remove(status.attribute("target_content"));
             */
            List<Attribute> attrList = new ArrayList();
            attrList.addAll(status.attributes());

            for (int i = 0; i < attrList.size(); i++) {
                String name = attrList.get(i).getName();
                if (!name.equals(IWS_TRANSLATION_STATUS) && !name.equals(IWS_TRANSLATION_TYPE)
                        && !name.equals(IWS_SOURCE_CONTENT)) {
                    status.remove(attrList.get(i));
                }
            }
        }

        if (status.attribute(IWS_TRANSLATION_STATUS) != null) {
            if (localizedByUser) {
                status.attribute(IWS_TRANSLATION_STATUS).setValue("finished");
            } else if (localizedByLocalTM) {
                status.attribute(IWS_TRANSLATION_STATUS).setValue("pending");
            }
        } else {
            if (localizedByUser) {
                status.addAttribute(IWS_TRANSLATION_STATUS, "finished");
            } else if (localizedByLocalTM) {
                status.addAttribute(IWS_TRANSLATION_STATUS, "pending");
            }
        }

        if (status.attribute(IWS_TRANSLATION_TYPE) != null) {
            if (localizedByMT) {
                status.attribute(IWS_TRANSLATION_TYPE).setValue("machine_translation_mt");
            } else if (localizedByUser || localizedByLocalTM) {
                status.attribute(IWS_TRANSLATION_TYPE).setValue("manual_translation");
            }
        } else {
            if (localizedBy != null) {
                if (localizedByMT) {
                    status.addAttribute(IWS_TRANSLATION_TYPE, "machine_translation_mt");
                } else if (localizedByUser || localizedByLocalTM) {
                    status.addAttribute(IWS_TRANSLATION_TYPE, "manual_translation");
                }
            }
        }
    }

    iwsStr = dom.selectSingleNode("//iws:segment-metadata").asXML();
    String str = "xmlns:iws=\"http://www.idiominc.com/ws/asset\"";
    iwsStr = iwsStr.replace(str, "");
    skeleton = skeleton.substring(0, begin) + iwsStr + skeleton.substring(end);

    return skeleton;
}

From source file:com.globalsight.reports.util.ReportHandlerFactory.java

License:Apache License

/**
 * Reads in the Report handler mapping from an XML file, parses it
 * and populates this data structure//from w  w  w  .j  a  v  a 2 s.  c om
 *
 * @param reportConfigXML the name of the XML file that
 * holds the report mapping.
 * @return <code>true</code> if successful, <code>false</code>
 * otherwise.
 */
public static boolean createReportHandlerMap(String reportConfigXML) {
    boolean retVal = false;
    SAXReader reader = new SAXReader();
    Document document = null;

    try {
        InputStream is = ReportHandlerFactory.class.getResourceAsStream(reportConfigXML);
        document = reader.read(new InputSource(is));

        // parse XML file in order to get className counterpart to pagename
        List<?> moduleList = document.selectNodes(Constants.REPORTMODULE_NODE_XPATH);
        for (Iterator<?> iter = moduleList.iterator(); iter.hasNext();) {
            Element element = (Element) iter.next();
            Attribute attribute = element.attribute(Constants.REPORTNAME_ATTRIBUTE);
            Element elementNode = element.element(Constants.REPORTHANDLER_NODE);

            Attribute attributeNode = elementNode.attribute(Constants.CLASS_ATTRIBUTE);

            if (CATEGORY.isDebugEnabled()) {
                CATEGORY.debug("CreateReportHandlerMap key: " + attribute.getValue() + " value: "
                        + attributeNode.getValue());
            }

            reportHandlerMap.put(attribute.getValue(), attributeNode.getValue());
        }

        // parse XML file in order to get target Url counterpart to pagename 
        List<?> urlList = document.selectNodes(Constants.REPORTURL_NODE_XPATH);
        for (Iterator<?> iterUrl = urlList.iterator(); iterUrl.hasNext();) {
            Element urlElement = (Element) iterUrl.next();
            Attribute jspNameAttribute = urlElement.attribute(Constants.JSPNAME_ATTRIBUTE);
            Attribute urlAttribute = urlElement.attribute(Constants.TARGETURL_ATTRIBUTE);
            reportTargetUrlMap.put(jspNameAttribute.getValue(), urlAttribute.getValue());
        }

        if (instance == null) {
            instance = new ReportHandlerFactory();
            retVal = true;
        }

    } catch (DocumentException e) {
        CATEGORY.error("Cannot read the ReportConfig.xml", e);
    }

    return retVal;
}

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

License:Apache License

/**
 * Get TBX files entry id//from   ww w  . j a v a2s.  c om
 * @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

/**
 * 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.
 *///from  w  ww.ja  v  a  2  s .co  m
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

private static void mergeLangSet(Element p_one, Element p_two, Element node, NodeComparator p_comp) {
    String languageName = node.attribute("lang").getText();

    List langSets = p_one.elements("langSet");
    List<Element> langSetsWithEqualLanguageName = new ArrayList<Element>();
    for (int i = 0; i < langSets.size(); i++) {
        Element tmp = (Element) langSets.get(i);
        if (tmp.attribute("lang").getText().equals(languageName)) {
            langSetsWithEqualLanguageName.add(tmp);
        }//from  w w  w .  j  a v a  2s . c  om
    }
    if (langSetsWithEqualLanguageName.size() == 0) {
        p_one.add(node);
        return;
    }
    mergeTbxNtigsOrTigs(langSetsWithEqualLanguageName, langSetsWithEqualLanguageName.get(0), node, p_comp);
}

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

License:Apache License

/**
 * Converts a GlobalSight concept group to a MultiTerm iX concept
 * group. Differences:/*w ww.jav a  2 s.  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.importer.MtfReaderThread.java

License:Apache License

/**
 * Converts a MultiTerm MTF concept group to a GlobalSight concept
 * group. Differences:/* w ww . j  a v a  2 s . c  o  m*/
 *
 *  - <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.globalsight.terminology.importer.MtfReaderThread.java

License:Apache License

private void convertToNoteGrp(Element p_elem) {
    Element noteGrp = p_elem.createCopy("noteGrp");

    // noteGrps contain no other fields, remove all child
    // nodes, remembering the <descrip type="note"> itself
    for (ListIterator lit = noteGrp.elements().listIterator(); lit.hasNext();) {
        Element child = (Element) lit.next();

        if (child.getName().equals("descrip")) {
            Element note = child.createCopy("note");
            note.remove(note.attribute("type"));

            lit.set(note);//from   w w w.  j a  v  a  2 s  .c  o m
        } else {
            lit.remove();
        }
    }

    Element parent = p_elem.getParent();
    parent.content().set(parent.indexOf(p_elem), noteGrp);
}