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.tm.importer.TmxReaderThread.java

License:Apache License

/**
 * Filters the source and target TUVs to import.
 *//*from w  w w.  j ava2s.com*/
private void filterTu(Element p_tu) {
    String srcLang = p_tu.attributeValue(Tmx.SRCLANG);
    if (srcLang == null) {
        srcLang = m_defaultSrcLang;
    }

    // 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);

        String tuvLang = elem.attributeValue(Tmx.LANG);

        // Is this the TU's source TUV?
        if (tuvLang.equalsIgnoreCase(srcLang)) {
            continue;
        }

        // Treat as target TUV, should it be imported?
        if (m_importAllTargets || m_targetLocales.contains(tuvLang)) {
            continue;
        }

        // Nope, remove.
        elem.detach();
    }
}

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

License:Apache License

/**
 * Validates a TU by checking it contains a TUV in a source
 * language that should be imported. Also checks if there are more
 * than 2 TUVs./*from   w ww.  j  av a  2 s .  co m*/
 */
private void validateTu(Element p_tu) throws Exception {
    boolean b_found = false;

    String tuvLang = null;
    String srcLang = p_tu.attributeValue(Tmx.SRCLANG);
    if (srcLang == null) {
        srcLang = m_defaultSrcLang;
    }

    // can't use xpath here because xml:lang won't be matched
    List nodes = p_tu.selectNodes("./tuv");

    if (nodes.size() < 2) {
        throw new Exception("TU contains less than 2 TUVs (after filtering), ignoring");
    }

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

        tuvLang = elem.attributeValue(Tmx.LANG);
        if (tuvLang.equalsIgnoreCase(srcLang)) {
            b_found = true;
            break;
        }
    }

    if (!b_found) {
        throw new Exception("TU is missing TUV in source language " + srcLang);
    }

    if (!m_importAllSources && !tuvLang.equalsIgnoreCase(m_sourceLocale)) {
        throw new Exception("TU has no source TUV in " + m_sourceLocale + ", ignoring");
    }
}

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

License:Apache License

/**
 * Converts a DOM TU to a GS SegmentTmTu, thereby converting any TMX
 * format specialities as best as possible.
 */// ww w  . j  ava  2s  . c o  m
private SegmentTmTu createTu(Element p_root) throws Exception {
    SegmentTmTu result = new SegmentTmTu();

    // Optional TU attributes:

    // Original TU id, if known
    String id = p_root.attributeValue(Tmx.TUID);
    if (id != null && id.length() > 0) {
        try {
            long lid = Long.parseLong(id);
            result.setId(lid);
        } catch (Throwable ignore) {
            // <TU tuid> can be an alphanumeric token.
            // If it is not a simple number, we ignore it.
        }
    }

    // Datatype of the TU (html, javascript etc)
    String format = p_root.attributeValue(Tmx.DATATYPE);
    if (format == null || format.length() == 0) {
        format = m_tmx.getDatatype();
    }
    result.setFormat(format);

    // Locale of Source TUV (use default from header)
    String lang = p_root.attributeValue(Tmx.SRCLANG);

    if (lang == null || lang.length() == 0) {
        lang = m_defaultSrcLang;
    }

    try {
        String locale = ImportUtil.normalizeLocale(lang);
        result.setSourceLocale(ImportUtil.getLocaleByName(locale));
    } catch (Throwable ex) {
        CATEGORY.warn("invalid locale " + lang);

        throw new Exception("cannot handle locale " + lang);
    }

    // TODO: other optional attributes
    String usageCount = p_root.attributeValue(Tmx.USAGECOUNT);
    String usageDate = p_root.attributeValue(Tmx.LASTUSAGEDATE);
    //String tool = p_root.attributeValue(Tmx.CREATIONTOOL);
    //String toolversion = p_root.attributeValue(Tmx.CREATIONTOOLVERSION);
    // used in createTuv()
    //String creationDate = p_root.attributeValue(Tmx.CREATIONDATE);
    //String creationUser = p_root.attributeValue(Tmx.CREATIONID);
    //String changeDate = p_root.attributeValue(Tmx.CHANGEDATE);
    //String changeUser = p_root.attributeValue(Tmx.CHANGEID);

    // GlobalSight-defined properties:

    // Segment type (text, css-color, etc)
    String segmentType = "text";

    Node node = p_root.selectSingleNode(".//prop[@type = '" + Tmx.PROP_SEGMENTTYPE + "']");

    if (node != null) {
        segmentType = node.getText();
    }
    result.setType(segmentType);

    //Read SID
    node = p_root.selectSingleNode(".//prop[@type= '" + Tmx.PROP_TM_UDA_SID + "']");
    if (node != null) {
        result.setSID(node.getText());
    }

    // TU type (T or L)
    boolean isTranslatable = true;
    node = p_root.selectSingleNode(".//prop[@type = '" + Tmx.PROP_TUTYPE + "']");

    if (node != null) {
        isTranslatable = node.getText().equals(Tmx.VAL_TU_TRANSLATABLE);
    }

    if (isTranslatable) {
        result.setTranslatable();
    } else {
        result.setLocalizable();
    }

    // prop with Att::
    List propNodes = p_root.elements("prop");
    for (int i = 0; i < propNodes.size(); i++) {
        Element elem = (Element) propNodes.get(i);
        ProjectTmTuTProp prop = createProp(result, elem);

        if (prop != null)
            result.addProp(prop);
    }

    // TUVs
    List nodes = p_root.elements("tuv");
    for (int i = 0; i < nodes.size(); i++) {
        Element elem = (Element) nodes.get(i);

        SegmentTmTuv tuv = createTuv(result, elem);

        result.addTuv(tuv);
    }

    if (com.globalsight.everest.tm.importer.ImportOptions.TYPE_TMX_WORLD_SERVER
            .equals(m_options.getFileType())) {
        result.setFromWorldServer(true);
    }

    return result;
}

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

License:Apache License

/**
 * Converts a DOM TUV to a GS SegmentTmTuv, thereby converting any
 * TMX format specialities as best as possible.
 *
 * Note: if the attributes in one TUV are incorrect and can not be
 * repaired so we cannot create correct GXML (which depends on
 * correct i/x attributes and type), we need to process all TUVs
 * together; if one encounters an error, all TUVs should be
 * imported without tags as Level 1./*from  w w w . ja v  a 2s  .  c o m*/
 *
 * @param p_root the TUV node in the DOM structure.
 */
private SegmentTmTuv createTuv(SegmentTmTu p_tu, Element p_root) throws Exception {
    SegmentTmTuv result = new SegmentTmTuv();
    result.setOrgSegment(p_root.asXML());

    // need to set backpointer to tuv, or SegmentTmTuv.equals() fails.
    result.setTu(p_tu);

    // language of the TUV "EN-US", case insensitive
    String lang = p_root.attributeValue(Tmx.LANG);

    try {
        String locale = ImportUtil.normalizeLocale(lang);
        result.setLocale(ImportUtil.getLocaleByName(locale));
    } catch (Throwable ex) {
        throw new Exception("unknown locale " + lang + ",you can create it in system then retry.");
    }

    // 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);
    }

    result.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) {
        result.setModifyUser(user);
    }

    // Timestamps (should be expressed using java.util.Date).
    // In TMX, timestamps use the short form: yyyymmddThhmmssZ,
    // so prepare for both short and long form.
    Date now = new Date();
    Date date;

    // Creation date - always set to a known value
    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);
        }
        result.setCreationDate(new Timestamp(date.getTime()));
    } else {
        result.setCreationDate(new Timestamp(now.getTime()));
    }

    // Modification date - only set if known (note: currently
    // AbstractTmTuv sets the modification date to 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);
        }
        result.setModifyDate(new Timestamp(date.getTime()));
    } else {
        // If no "changedate", set it same as "creationdate".
        result.setModifyDate(result.getCreationDate());
    }

    ts = p_root.attributeValue(Tmx.LASTUSAGEDATE);
    if (ts == null) {
        ts = p_root.getParent().attributeValue(Tmx.LASTUSAGEDATE);
    }
    if (ts != null) {
        date = UTC.parseNoSeparators(ts);
        if (date == null) {
            date = UTC.parse(ts);
        }
        result.setLastUsageDate(new Timestamp(date.getTime()));
    }

    List tuvPropNodes = p_root.elements("prop");
    for (int i = 0; i < tuvPropNodes.size(); i++) {
        Element elem = (Element) tuvPropNodes.get(i);
        String type = elem.attributeValue("type");
        String value = elem.getText();
        if (Tmx.PROP_PREVIOUS_HASH.equalsIgnoreCase(type)) {
            result.setPreviousHash(Long.parseLong(value));
        } else if (Tmx.PROP_NEXT_HASH.equalsIgnoreCase(type)) {
            result.setNextHash(Long.parseLong(value));
        } else if (Tmx.PROP_JOB_ID.equalsIgnoreCase(type)) {
            result.setJobId(Long.parseLong(value));
        } else if (Tmx.PROP_JOB_NAME.equalsIgnoreCase(type)) {
            result.setJobName(value);
        } else if (Tmx.PROP_CREATION_PROJECT.equalsIgnoreCase(type)) {
            result.setUpdatedProject(value);
        }
    }
    // Segment text: need to produce root elements <translatable>
    // and <localizable> depending on TU type.
    StringBuffer segment = new StringBuffer();

    if (p_tu.isTranslatable()) {
        segment.append("<segment>");
    } else {
        segment.append("<localizable>");
    }

    segment.append(getSegmentValue(p_root));

    if (p_tu.isTranslatable()) {
        segment.append("</segment>");
    } else {
        segment.append("</localizable>");
    }

    result.setSid(p_tu.getSID());
    //End of Added
    result.setSegment(segment.toString());

    return result;
}

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

License:Apache License

private ProjectTmTuTProp createProp(SegmentTmTu p_tu, Element p_root) throws Exception {
    ProjectTmTuTProp result = null;//  w  w w. j  av  a2s .c o  m
    String type = p_root.attributeValue("type");

    if (type != null && type.startsWith(ProjectTmTuTProp.TYPE_ATT_PREFIX)) {
        result = new ProjectTmTuTProp();
        result.setPropType(type);
        result.setPropValue(p_root.getText());
    }

    // When a TM is imported: for each TUV: for each TUV attribute defined on the TM: 
    // if the attribute exists in the imported TMX file, then the attribute is created 
    // and the value is set from the imported TM. 
    if (result != null && m_database instanceof ProjectTM) {
        String attName = result.getAttributeName();
        ProjectTM ptm = (ProjectTM) m_database;
        List<String> tmAttNames = ptm.getAllTMAttributenames();

        // ignore the tu attributes if not defined in TM.
        if (!tmAttNames.contains(attName)) {
            result = null;
        }
    }

    return result;
}

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

License:Apache License

private Element validateSegment(Element p_tuv, Element p_seg, IntHolder p_x_count) throws Exception {
    String attr;/*from  w  w w  .ja  v  a2s .  c o  m*/

    List elems = p_seg.elements();

    for (Iterator it = elems.iterator(); it.hasNext();) {
        Element elem = (Element) it.next();
        String name = elem.getName();

        if (name.equals("bpt")) {
            attr = elem.attributeValue("x"); // mandatory only in 1.4
            if (attr == null || attr.length() == 0) {
                elem.addAttribute("x", String.valueOf(p_x_count.inc()));
            }

            attr = elem.attributeValue("i"); // mandatory
            if (attr == null || attr.length() == 0) {
                throw new Exception("A <bpt> tag is lacking the mandatory i attribute.");
            }

            attr = elem.attributeValue("type");
            if (attr == null || attr.length() == 0) {
                //elem.addAttribute("type", DEFAULT_TYPE);
            }
        } else if (name.equals("ept")) {
            attr = elem.attributeValue("i"); // mandatory
            if (attr == null || attr.length() == 0) {
                throw new Exception("A <ept> tag is lacking the mandatory i attribute.");
            }
        } else if (name.equals("it")) {
            attr = elem.attributeValue("x"); // mandatory only in 1.4
            if (attr == null || attr.length() == 0) {
                elem.addAttribute("x", String.valueOf(p_x_count.inc()));
            }

            attr = elem.attributeValue("pos"); // mandatory
            if (attr == null || attr.length() == 0) {
                throw new Exception("A <it> tag is lacking the mandatory pos attribute.");
            }

            attr = elem.attributeValue("type");
            if (attr == null || attr.length() == 0) {
                elem.addAttribute("type", DEFAULT_TYPE);
            }
        } else if (name.equals("ph")) {
            attr = elem.attributeValue("x"); // mandatory only in 1.4
            if (attr == null || attr.length() == 0) {
                elem.addAttribute("x", String.valueOf(p_x_count.inc()));
            }

            attr = elem.attributeValue("type");
            if (attr == null || attr.length() == 0) {
                elem.addAttribute("type", DEFAULT_TYPE);
            }

            // GXML doesn't care about assoc, just preserve it.
            // attr = elem.attributeValue("assoc");
        } else if (name.equals("ut")) {
            // TMX level 2 does not allow UT. We can either remove
            // it, or look inside and guess what it may be.
            it.remove();
            continue;
        }

        // Recurse into any subs.
        validateSubs(p_tuv, elem, p_x_count);
    }

    return p_seg;
}

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

License:Apache License

private void init(Element p_element) {
    Attribute attr;/*from  www. java  2  s .c  o  m*/
    List nodes;
    Date date;

    // mandatory

    m_creationtoolversion = p_element.attributeValue(CREATIONTOOLVERSION);
    m_creationtool = p_element.attributeValue(CREATIONTOOL);
    m_segtype = p_element.attributeValue(SEGTYPE);
    m_o_tmf = p_element.attributeValue(O_TMF);
    m_adminlang = p_element.attributeValue(ADMINLANG);
    m_srclang = p_element.attributeValue(SRCLANG);
    m_datatype = p_element.attributeValue(DATATYPE);

    // optional

    m_o_encoding = p_element.attributeValue(O_ENCODING);
    m_creationid = p_element.attributeValue(CREATIONID);
    m_changeid = p_element.attributeValue(CHANGEID);

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

    attr = p_element.attribute(CHANGEDATE);
    if (attr == null) {
        date = null;
    } else {
        date = UTC.parseNoSeparators(attr.getValue());
        if (date == null) {
            date = UTC.parse(attr.getValue());
        }
    }
    m_changedate = date;

    // elements

    nodes = p_element.selectNodes("note");
    for (int i = 0; i < nodes.size(); i++) {
        Element node = (Element) nodes.get(i);

        m_notes.add(new Note(node));
    }

    nodes = p_element.selectNodes("prop");
    for (int i = 0; i < nodes.size(); i++) {
        Element node = (Element) nodes.get(i);

        m_props.add(new Prop(node));
    }

    // TODO: UDE
}

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

License:Apache License

public void analyze(String p_url) throws Exception {
    m_tuCount = 0;//from  ww  w  . ja  v  a  2 s.c om
    m_tuvCount = 0;
    m_localeCount = 0;
    m_locales = new HashSet();
    m_tmxVersion = "";
    m_tmx = null;

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

    reader.setEntityResolver(DtdResolver.getInstance());
    reader.setValidation(true);

    log("Analyzing document: " + p_url);

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

            m_tmxVersion = element.attributeValue("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);
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/body/tu", new ElementHandler() {
        public void onStart(ElementPath path) {
            ++m_tuCount;

            if (m_tuCount % 1000 == 0) {
                log("TU " + m_tuCount);
            }
        }

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

            List tuvs = element.selectNodes("//tuv");

            m_tuvCount += tuvs.size();

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

                String locale = tuv.attributeValue("lang");
                m_locales.add(locale);
            }

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

            element = null;
        }
    });

    Document document = reader.read(p_url);

    m_localeCount = m_locales.size();

    log("File: " + p_url);
    log("TMX version: " + m_tmxVersion);
    log("Total TUs: " + m_tuCount);
    log("Total TUVs: " + m_tuvCount);
    log("Total Locales: " + m_localeCount);

    for (Iterator it = m_locales.iterator(); it.hasNext();) {
        String locale = (String) it.next();

        log(locale);
    }

    // all done
}

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

License:Apache License

public void split(String p_url) throws Exception {
    final String baseName = getBaseName(p_url);
    final String extension = getExtension(p_url);

    m_entryCount = 0;/* w ww .j  av a2s. c o  m*/

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

    reader.setEntityResolver(DtdResolver.getInstance());
    reader.setValidation(true);

    log("Splitting document `" + p_url + "'");

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx", new ElementHandler() {
        public void onStart(ElementPath path) {
            Element element = path.getCurrent();

            m_version = element.attributeValue("version");
        }

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

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/header", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

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

            m_header = element;

            try {
                startFiles(baseName, extension);
            } catch (Exception ex) {
                log(ex.toString());
                System.exit(1);
            }

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

            element = null;
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/body/tu", new ElementHandler() {
        public void onStart(ElementPath path) {
            ++m_entryCount;
        }

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

            if (containsTags(element)) {
                writeTagsEntry(element.asXML());

                m_tagsCount++;
            } else {
                writeTextEntry(element.asXML());

                m_textCount++;
            }

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

            element = null;
        }
    });

    Document document = reader.read(p_url);

    closeFiles();

    log("Processed " + m_entryCount + " TUs, " + m_textCount + " level 1 (text), " + m_tagsCount
            + " level 2 (tags)");

    // all done
}

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

License:Apache License

public void split(String p_url, String p_numEntries) throws Exception {
    final int maxEntries = Integer.parseInt(p_numEntries);
    final String baseName = getBaseName(p_url);
    final String extension = getExtension(p_url);

    m_entryCount = 0;//www  .j  av a2  s .  co  m

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

    reader.setEntityResolver(DtdResolver.getInstance());
    reader.setValidation(true);

    log("Splitting document `" + p_url + "'");

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx", new ElementHandler() {
        public void onStart(ElementPath path) {
            Element element = path.getCurrent();

            m_version = element.attributeValue("version");
        }

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

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/header", new ElementHandler() {
        public void onStart(ElementPath path) {
        }

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

            m_header = element;

            try {
                startFile(baseName, extension);
            } catch (Exception ex) {
                log(ex.toString());
                System.exit(1);
            }

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

            element = null;
        }
    });

    // enable element complete notifications to conserve memory
    reader.addHandler("/tmx/body/tu", new ElementHandler() {
        public void onStart(ElementPath path) {
            ++m_entryCount;

            if (m_entryCount % maxEntries == 0) {
                try {
                    closeFile();
                    startFile(baseName, extension);
                } catch (Exception ex) {
                    log(ex.toString());
                    System.exit(1);
                }
            }
        }

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

            writeEntry(element.asXML());

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

            element = null;
        }
    });

    Document document = reader.read(p_url);

    closeFile();

    // all done
}