Example usage for org.dom4j Element attributeValue

List of usage examples for org.dom4j Element attributeValue

Introduction

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

Prototype

String attributeValue(QName qName);

Source Link

Document

This returns the attribute value for the attribute with the given fully qualified name or null if there is no such attribute or the empty string if the attribute value is empty.

Usage

From source file:com.globalsight.everest.projecthandler.ProjectTmTuvT.java

License:Apache License

public void reconvertFromTmx(Element p_root, ProjectTmTuT tu) throws Exception {
    // language of the TUV "EN-US", case insensitive
    String lang = p_root.attributeValue(Tmx.LANG);

    String locale = ImportUtil.normalizeLocale(lang);
    LocaleManagerLocal manager = new LocaleManagerLocal();
    setLocale(manager.getLocaleByString(locale));

    // Creation user - always set to a known value
    String user = p_root.attributeValue(Tmx.CREATIONID);
    if (user == null) {
        user = p_root.getParent().attributeValue(Tmx.CREATIONID);
    }/*from w w w.j  a v a  2  s.  c om*/
    setCreationUser(user != null ? user : Tmx.DEFAULT_USER);

    // Modification user - only set if known
    user = p_root.attributeValue(Tmx.CHANGEID);
    if (user == null) {
        user = p_root.getParent().attributeValue(Tmx.CHANGEID);
    }
    if (user != null) {
        setModifyUser(user);
    }

    Date now = new Date();
    Date date;

    String ts = p_root.attributeValue(Tmx.CREATIONDATE);
    if (ts == null) {
        ts = p_root.getParent().attributeValue(Tmx.CREATIONDATE);
    }

    if (ts != null) {
        date = UTC.parseNoSeparators(ts);
        if (date == null) {
            date = UTC.parse(ts);
        }

        setCreationDate(date);
    } else {
        setCreationDate(now);
    }

    ts = p_root.attributeValue(Tmx.CHANGEDATE);
    if (ts == null) {
        ts = p_root.getParent().attributeValue(Tmx.CHANGEDATE);
    }

    if (ts != null) {
        date = UTC.parseNoSeparators(ts);
        if (date == null) {
            date = UTC.parse(ts);
        }

        setModifyDate(date);
    }

    StringBuffer segment = new StringBuffer();
    segment.append("<segment>");
    segment.append(getSegmentValue(p_root));
    segment.append("</segment>");
    setSegmentString(segment.toString());
    setSid(tu.getSid());
}

From source file:com.globalsight.everest.segmentationhelper.XmlLoader.java

License:Apache License

public static SegmentationRule parseSegmentationRule(String p_xmlText) throws Exception {
    Document doc = parseWithSAX(p_xmlText);
    Element root = doc.getRootElement();
    SegmentationRule segmentationRule = new SegmentationRule();
    SrxHeader header = new SrxHeader();
    HashMap<String, String> formathandle = new HashMap<String, String>();
    HashMap<String, ArrayList<Rule>> langrules = new HashMap<String, ArrayList<Rule>>();
    ArrayList<LanguageMap> langruleMap = new ArrayList<LanguageMap>();

    segmentationRule.setRootName(root.getQualifiedName());
    // Now it only supports SRX2.0.
    segmentationRule.setVersion(root.attributeValue("version"));

    Element headerElement = root.element("header");
    Element bodyElement = root.element("body");
    Element maprulesElement = bodyElement.element("maprules");
    // to get SRX header informations
    if (headerElement != null) {
        // option segmentsubflows
        String segsub = headerElement.attributeValue("segmentsubflows");
        if (segsub == null) {
            // Take default value.
            header.isSegmentsubflows(true);
        } else {//from w  ww. ja  v  a 2 s.  c  o  m
            header.isSegmentsubflows(!segsub.equalsIgnoreCase("no"));
        }

        // option cascade
        String cascade = headerElement.attributeValue("cascade");
        if (cascade == null) {
            header.isCascade(false);
        } else {
            header.isCascade(!cascade.equalsIgnoreCase("no"));
        }

        // elements : formathandle
        List formatList = headerElement.elements("formathandle");
        Iterator formatIter = formatList != null ? formatList.iterator() : (new ArrayList()).iterator();
        // If the header does not contain formathandle elements
        // we use the default values
        if (!formatIter.hasNext()) {
            formathandle.put("start", "no");
            formathandle.put("end", "yes");
            formathandle.put("isolated", "no");
        }
        // If the header contains formathandle elements
        // we use the values specified by formathandle elements
        else {
            while (formatIter.hasNext()) {
                Element formatElement = (Element) formatIter.next();
                String type = formatElement.attributeValue("type");
                String include = formatElement.attributeValue("include");
                formathandle.put(type, include);
            }
        }
        header.setFormatHandle(formathandle);

        // okpsrx extension
        Element okpOptions = headerElement.element("options");
        Element okpSample = headerElement.element("sample");
        Element okpRangeRule = headerElement.element("rangeRule");

        if (okpOptions != null) {
            String oneSegmentIncludesAll = okpOptions.attributeValue("oneSegmentIncludesAll");
            String trimLeadingWs = okpOptions.attributeValue("trimLeadingWhitespaces");
            String trimTrailingWs = okpOptions.attributeValue("trimTrailingWhitespaces");

            header.setOneSegmentIncludesAll("yes".equalsIgnoreCase(oneSegmentIncludesAll));
            header.setTrimLeadingWhitespaces("yes".equalsIgnoreCase(trimLeadingWs));
            header.setTrimTrailingWhitespaces("yes".equalsIgnoreCase(trimTrailingWs));
        }

        if (okpSample != null) {
            String language = okpSample.attributeValue("language");
            String useMappedRules = okpSample.attributeValue("useMappedRules");
            String sample = okpSample.getText();

            header.setSample(sample);
            header.setSampleLanguage(language);
            header.setUseMappedRulesForSample(!"no".equalsIgnoreCase(useMappedRules));
        }

        if (okpRangeRule != null) {
            String rangeRule = okpRangeRule.getText();
            header.setRangeRule(rangeRule);
        }
    }

    if (bodyElement != null) {
        Element languagerulesElement = bodyElement.element("languagerules");
        if (languagerulesElement != null) {
            Iterator languageruleIter = languagerulesElement.elementIterator();
            while (languageruleIter.hasNext()) {
                Element languageruleElement = (Element) languageruleIter.next();
                String languageName = languageruleElement.attributeValue("languagerulename");
                Iterator ruleIter = languageruleElement.elementIterator();
                ArrayList<Rule> rules = new ArrayList<Rule>();
                while (ruleIter.hasNext()) {
                    Element ruleSub = (Element) ruleIter.next();
                    String breakvalue = ruleSub.attributeValue("break");
                    String beforebreak = ruleSub.elementText("beforebreak");
                    String afterbreak = ruleSub.elementText("afterbreak");
                    Rule rule = new Rule();
                    if (breakvalue == null) {
                        // Take default value.
                        rule.isBreak(true);
                    } else {
                        if (breakvalue.equalsIgnoreCase("no")) {
                            rule.isBreak(false);
                        } else {
                            rule.isBreak(true);
                        }
                    }

                    // System.out.println(rule.getBreak());
                    rule.setAfterBreak(afterbreak);
                    rule.setBeforeBreak(beforebreak);
                    rules.add(rule);
                }
                langrules.put(languageName, rules);
            }
        } // end languageruleElement
    } // end bodyElement

    if (maprulesElement != null) {
        Iterator languagemapIter = maprulesElement.elementIterator();
        while (languagemapIter.hasNext()) {
            Element languagemapElement = (Element) languagemapIter.next();
            String languagepattern = languagemapElement.attributeValue("languagepattern");
            String languagerulename = languagemapElement.attributeValue("languagerulename");
            LanguageMap langMap = new LanguageMap();
            langMap.setLanguagePattern(languagepattern);
            langMap.setLanguageruleName(languagerulename);
            langruleMap.add(langMap);
        }
    }

    segmentationRule.setHeader(header);
    segmentationRule.setLanguageMap(langruleMap);
    segmentationRule.setRules(langrules);
    return segmentationRule;
}

From source file:com.globalsight.everest.tm.exporter.TmxWriter.java

License:Apache License

private static void findNbspElements(ArrayList p_result, Element p_element) {
    // Depth-first traversal: add embedded <ph x-nbspace> to the list first.
    for (int i = 0, max = p_element.nodeCount(); i < max; i++) {
        Node child = (Node) p_element.node(i);

        if (child instanceof Element) {
            findNbspElements(p_result, (Element) child);
        }/*from   w  w w . j  av  a 2s . c  om*/
    }

    if (p_element.getName().equals("ph")) {
        String attr = p_element.attributeValue("type");

        if (attr != null && attr.equals("x-nbspace")) {
            p_result.add(p_element);
        }
    }
}

From source file:com.globalsight.everest.tm.exporter.TmxWriter.java

License:Apache License

/**
 * Finds elements bearing an "x" element. These are bpt (required) and it,
 * ph, hi (optional)./*from ww w.  ja v a2 s.  co  m*/
 */
private void findElementsWithX(ArrayList p_result, Element p_element) {
    // Prefix-traversal
    if (p_element.attributeValue("x") != null) {
        p_result.add(p_element);
    }

    for (int i = 0, max = p_element.nodeCount(); i < max; i++) {
        Node child = (Node) p_element.node(i);

        if (child instanceof Element) {
            findElementsWithX(p_result, (Element) child);
        }
    }
}

From source file:com.globalsight.everest.tm.exporter.TmxWriter.java

License:Apache License

private static void injectStandardFormattingCodes(Element p_root, Element p_element) {
    // Depth-first traversal: add embedded <sub> to the list first.
    for (int i = 0, max = p_element.nodeCount(); i < max; i++) {
        Node child = (Node) p_element.node(i);

        if (child instanceof Element) {
            injectStandardFormattingCodes(p_root, (Element) child);
        }// w ww.  ja v a  2 s.co m
    }

    String tagName = p_element.getName();
    String typeAttr = p_element.attributeValue("type");
    String iAttr = p_element.attributeValue("i");
    String posAttr = p_element.attributeValue("pos");

    if (tagName.equals("bpt") && typeAttr != null && iAttr != null) {
        Element ept = (Element) p_root.selectSingleNode("//ept[@i='" + iAttr + "']");

        if (typeAttr.equals("bold")) {
            p_element.addText("<B>");
            ept.addText("</B>");
        } else if (typeAttr.equals("italic")) {
            p_element.addText("<I>");
            ept.addText("</I>");
        } else if (typeAttr.equals("ulined")) {
            p_element.addText("<U>");
            ept.addText("</U>");
        }
    } else if (tagName.equals("it") && typeAttr != null && posAttr != null) {
        if (typeAttr.equals("bold")) {
            if (posAttr.equals("begin")) {
                p_element.addText("<B>");
            } else {
                p_element.addText("</B>");
            }
        } else if (typeAttr.equals("italic")) {
            if (posAttr.equals("begin")) {
                p_element.addText("<I>");
            } else {
                p_element.addText("</I>");
            }
        } else if (typeAttr.equals("ulined")) {
            if (posAttr.equals("begin")) {
                p_element.addText("<U>");
            } else {
                p_element.addText("</U>");
            }
        }
    }
}

From source file:com.globalsight.everest.tm.exporter.TmxWriter.java

License:Apache License

/**
 * Finds all "x" and "i" attributes in the source TUV that need to be fixed
 * in the other TUVs./*from w w w. j  a  v  a 2 s. co  m*/
 */
private void fixAttributeIX(Element p_root, ArrayList p_roots) {
    // First use the same "i" across source and target tuvs.
    List bpts = p_root.selectNodes("//bpt");

    for (int i = 0, max = bpts.size(); i < max; i++) {
        Element bpt = (Element) bpts.get(i);

        String xAttr = bpt.attributeValue("x");
        String iAttr = bpt.attributeValue("i");

        // Be prepared for data errors where "x" is missing.
        // Don't crash here because of it. Fix it elsewhere.
        if (xAttr != null && iAttr != null) {
            fixAttributeI(xAttr, iAttr, p_roots);
        }
    }

    // Then renumber all "x" starting at one. Gaaah.
    ArrayList elems = new ArrayList();
    findElementsWithX(elems, p_root);

    for (int num = 1, i = 0, max = elems.size(); i < max; i++, num++) {
        Element elem = (Element) elems.get(i);

        String name = elem.getName();
        String oldX = elem.attributeValue("x");
        String newX = String.valueOf(num);

        // Renumber in this TUV.
        elem.addAttribute("x", newX);

        // Renumber in all others TUV.
        fixAttributeX(name, oldX, newX, p_roots);
    }
}

From source file:com.globalsight.everest.tm.exporter.TmxWriter.java

License:Apache License

/**
 * Fixes a single "i" attribute in all other TUVs based on the "x".
 *///from  w w  w .j  av  a 2  s  .  c  o m
private void fixAttributeI(String p_x, String p_i, ArrayList p_roots) {
    for (int i = 0, max = p_roots.size(); i < max; i++) {
        Element root = (Element) p_roots.get(i);

        Element bpt = (Element) root.selectSingleNode("//bpt[@x='" + p_x + "']");

        if (bpt == null) {
            continue;
        }

        String curI = bpt.attributeValue("i");
        Element ept = (Element) root.selectSingleNode("//ept[@i='" + curI + "']");

        bpt.addAttribute("i", p_i);
        if (ept != null) {
            ept.addAttribute("i", p_i);
        }
    }
}

From source file:com.globalsight.everest.tm.importer.TmxReader.java

License:Apache License

/**
 * Reads an XML file and checks its correctness by validating
 * against the TMX DTD. If there's any error in the file, an
 * exception is thrown./*from  ww w.j  a v  a2s  .c o m*/
 *
 * As a side effect, this method builds a list of source and
 * target locales found in the file, including the declared source
 * locale from the header.
 */
private void analyzeXml(String p_url) throws Exception {
    if (m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_RTF || m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_HTML
            || m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_FM || m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_FM_SGML
            || m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_IL
            || m_tmxLevel == ImportUtil.TMX_LEVEL_TRADOS_XPTAG) {
        // Convert the Trados codes to native System4 codes by
        // converting the file to RTF, saving it as HTML and
        // extracting the resulting TUVs.

        CATEGORY.info("Converting Trados TMX to native TMX: " + p_url);

        p_url = convertTradosTmx(p_url, m_tmxLevel);

        // Now we have a new file that contains native content.
        m_options.setFileName(p_url);
        m_options.setFileType(com.globalsight.everest.tm.importer.ImportOptions.TYPE_XML);

        m_tmxLevel = ImportUtil.TMX_LEVEL_NATIVE;
    }

    CATEGORY.debug("Analyzing document: " + p_url);

    // Reset list of locales found in the file.
    m_sourceLocales = new HashSet();
    m_targetLocales = new HashSet();

    SAXReader reader = new SAXReader();
    reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser");

    // Read the DTD and validate.
    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_tmxVersion = element.attributeValue(Tmx.VERSION);
        }

        public void onEnd(ElementPath path) {
        }
    });

    reader.addHandler("/tmx/header", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element element = path.getCurrent();

            m_header = new Tmx(element);
            m_header.setTmxVersion(m_tmxVersion);

            element.detach();
        }
    });

    reader.addHandler("/tmx/body/tu", new ElementHandler() {
        public void onStart(ElementPath path) {
            ++m_entryCount;
        }

        public void onEnd(ElementPath path) {
            Element element = path.getCurrent();

            // Record optional source language declared on TU.
            String srclang = element.attributeValue(Tmx.SRCLANG);
            if (srclang != null) {
                m_sourceLocales.add(ImportUtil.normalizeLocale(srclang));
            }

            // Find target languages
            HashSet langs = new HashSet();
            List tuvs = element.selectNodes("./tuv");

            for (int i = 0, max = tuvs.size(); i < max; i++) {
                Element tuv = (Element) tuvs.get(i);

                String lang = tuv.attributeValue(Tmx.LANG);

                // Collect TUV locales
                langs.add(ImportUtil.normalizeLocale(lang));
            }

            langs.remove(srclang);
            m_targetLocales.addAll(langs);

            // prune the current element to reduce memory
            element.detach();
        }
    });

    Document document = reader.read(p_url);

    // Add declared source language from header.
    String sourceLocale = ImportUtil.normalizeLocale(m_header.getSourceLang());

    m_sourceLocales.add(sourceLocale);
}

From source file:com.globalsight.everest.tm.importer.TmxReaderThread.java

License:Apache License

public void run() {
    try {//  w w w . j  av  a 2  s  . c  o  m
        SAXReader reader = new SAXReader();
        reader.setXMLReaderClassName("org.apache.xerces.parsers.SAXParser");

        // Read the DTD and validate.
        reader.setEntityResolver(DtdResolver.getInstance());
        reader.setValidation(true);

        reader.addHandler("/tmx", new ElementHandler() {
            public void onStart(ElementPath path) {
                Element element = path.getCurrent();

                m_tmxVersion = element.attributeValue(Tmx.VERSION);
            }

            public void onEnd(ElementPath path) {
            }
        });

        reader.addHandler("/tmx/header", new ElementHandler() {
            public void onStart(ElementPath path) {
            }

            public void onEnd(ElementPath path) {
                Element element = path.getCurrent();
                element.detach();

                m_tmx = new Tmx(element);
                m_tmx.setTmxVersion(m_tmxVersion);

                m_defaultSrcLang = ImportUtil.normalizeLocale(m_tmx.getSourceLang());
            }
        });

        // enable pruning to call me back as each Element is complete
        reader.addHandler("/tmx/body/tu", new ElementHandler() {
            public void onStart(ElementPath path) {
                m_count++;
            }

            public void onEnd(ElementPath path) {
                Element element = path.getCurrent();
                element.detach();

                m_result = m_results.hireResult();

                try {
                    // Normalize spelling of locales.
                    normalizeTu(element);
                    // Filter out targets not to be imported.
                    filterTu(element);
                    // Validate we have source and target.
                    validateTu(element);

                    // Create TU objects
                    SegmentTmTu tu = createTu(element);

                    if (CATEGORY.isDebugEnabled()) {
                        CATEGORY.debug(tu.toDebugString(true));
                    }

                    m_result.setResultObject(tu);
                } catch (Throwable ex) {
                    String msg = "Entry " + m_count + ": " + ex.getMessage();

                    m_result.setError(msg);

                    if (CATEGORY.isDebugEnabled()) {
                        CATEGORY.debug(msg, ex);
                    } else {
                        CATEGORY.warn(msg);
                    }
                }

                boolean done = m_results.put(m_result);
                m_result = null;

                // Stop reading the TMX file.
                if (done) {
                    throw new ThreadDeath();
                }
            }
        });

        String url = m_options.getFileName();

        Document document = reader.read(url);
    } catch (ThreadDeath ignore) {
        CATEGORY.info("ReaderThread: interrupted.");
    } catch (Throwable ignore) {
        // Should never happen, and I don't know how to handle
        // this case other than passing the exception in
        // m_results, which I won't do for now.
        CATEGORY.error("unexpected error", ignore);
    } finally {
        if (m_result != null) {
            m_results.fireResult(m_result);
        }

        m_results.producerDone();
        m_results = null;

        CATEGORY.debug("ReaderThread: done.");
    }
}

From source file:com.globalsight.everest.tm.importer.TmxReaderThread.java

License:Apache License

/**
 * Normalizes the spelling of the "lang" elements.
 *//* ww w.ja v  a2 s  .c  o  m*/
private void normalizeTu(Element p_tu) throws Exception {
    // Header default source lang normalized when header is read.
    // Locales read from m_options were normalized by TmxReader.

    String lang = p_tu.attributeValue(Tmx.SRCLANG);
    if (lang != null) {
        lang = ImportUtil.normalizeLocale(lang);
        p_tu.addAttribute(Tmx.SRCLANG, lang);
    }

    // can't use xpath here because xml:lang won't be matched
    List nodes = p_tu.selectNodes("./tuv");
    for (int i = 0, max = nodes.size(); i < max; i++) {
        Element elem = (Element) nodes.get(i);

        lang = elem.attributeValue(Tmx.LANG);
        lang = ImportUtil.normalizeLocale(lang);

        elem.addAttribute(Tmx.LANG, lang);
    }
}