Example usage for org.dom4j Element addAttribute

List of usage examples for org.dom4j Element addAttribute

Introduction

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

Prototype

Element addAttribute(QName qName, String value);

Source Link

Document

Adds the attribute value of the given fully qualified name.

Usage

From source file:com.globalsight.cxe.adapter.msoffice.ExcelRepairer.java

License:Apache License

private void repairExcelSharedStrings() throws Exception {
    File f = new File(path + "/xl/sharedStrings.xml");
    if (!f.exists())
        return;/* w  w w  . ja v a 2  s .  c  o  m*/

    String content = FileUtil.readFile(f, "utf-8");

    XmlParser parser = new XmlParser();
    org.dom4j.Document document = parser.parseXml(content);
    Element element = document.getRootElement();
    List<Element> rs = getElementByName(element, "r");
    for (Element r : rs) {
        @SuppressWarnings("rawtypes")
        List els = r.content();

        StringBuffer sb = new StringBuffer();
        Element wt = null;
        List<DefaultText> texts = new ArrayList<DefaultText>();

        for (Object el : els) {
            if (el instanceof DefaultText) {
                DefaultText text = (DefaultText) el;
                String s = text.getStringValue();
                if ("\n".equals(s))
                    continue;

                texts.add(text);
                sb.append(text.getStringValue());
            } else if (el instanceof Element) {
                Element elm = (Element) el;
                if ("t".equals(elm.getName())) {
                    wt = elm;
                    sb.append(elm.getStringValue());
                }
            }
        }

        if (wt == null) {
            wt = r.addElement("t");
            wt.addAttribute("xml:space", "preserve");
        }

        if (sb.length() == 0)
            sb.append(" ");

        wt.clearContent();
        wt.addText(sb.toString());

        for (DefaultText text : texts) {
            r.remove(text);
        }
    }

    Writer fileWriter = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
    XMLWriter xmlWriter = new XMLWriter(fileWriter);
    xmlWriter.write(document);
    xmlWriter.close();
}

From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java

License:Apache License

private static void forWrInWr(Element element) {
    @SuppressWarnings("unchecked")
    List<Node> ts = element.selectNodes("//w:r/w:r");

    for (Node t : ts) {
        Element wr = t.getParent();

        if (wr == null)
            continue;

        List<?> els = wr.content();

        StringBuffer sb = new StringBuffer();
        Element wt = null;
        List<Element> wrs = new ArrayList<Element>();

        for (Object el : els) {
            if (el instanceof Element) {
                Element elm = (Element) el;
                if ("t".equals(elm.getName())) {
                    wt = elm;//from   w  w w  . ja va2 s  .c o m
                    sb.append(elm.getStringValue());
                } else if ("r".equals(elm.getName())) {
                    sb.append(elm.getStringValue());
                    wrs.add(elm);
                }
            }
        }

        if (wt == null) {
            wt = wr.addElement("w:t");
            wt.addAttribute("xml:space", "preserve");
        }

        wt.setText(sb.toString());

        for (Element w : wrs) {
            wr.remove(w);
        }
    }
}

From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java

License:Apache License

private static void forTextInWr(Element element) {
    @SuppressWarnings("unchecked")
    List<Node> ts = element.selectNodes("//w:r/text()");

    for (Node t : ts) {
        if (t.getText().matches("[\n\r]*")) {
            continue;
        }// ww  w . j  a  v  a 2s.  c om

        Element wr = t.getParent();

        if (wr == null) {
            continue;
        }

        List<?> els = wr.content();

        StringBuffer sb = new StringBuffer();
        Element wt = null;
        List<DefaultText> texts = new ArrayList<DefaultText>();

        for (Object el : els) {
            if (el instanceof DefaultText) {
                DefaultText text = (DefaultText) el;
                texts.add(text);
                sb.append(text.getStringValue());
            } else if (el instanceof Element) {
                Element elm = (Element) el;
                if ("t".equals(elm.getName())) {
                    wt = elm;
                    sb.append(elm.getStringValue());
                }
            }
        }

        if (wt == null) {
            wt = wr.addElement("w:t");
            wt.addAttribute("xml:space", "preserve");
        }

        wt.setText(sb.toString());

        for (DefaultText text : texts) {
            wr.remove(text);
        }
    }
}

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./*  w w  w .j  a v  a  2 s.c o 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  a  va 2  s . co 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.exporter.TmxWriter.java

License:Apache License

/**
 * Updates a single "x" attribute in all other TUVs with a new value.
 */// w ww.  j  a va2  s.  c o m
private void fixAttributeX(String p_name, String p_oldX, String p_newX, ArrayList p_roots) {
    for (int i = 0, max = p_roots.size(); i < max; i++) {
        Element root = (Element) p_roots.get(i);

        Element elem = (Element) root.selectSingleNode("//" + p_name + "[@x='" + p_oldX + "']");

        if (elem == null) {
            continue;
        }

        elem.addAttribute("x", p_newX);
    }
}

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

License:Apache License

/**
 * Normalizes the spelling of the "lang" elements.
 *//* w  ww .jav  a 2s  .co  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);
    }
}

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;// w  ww. j a  v a  2 s. com

    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.ttx.TtxToTmx.java

License:Apache License

/**
 * Lowercases relevant TMX attributes like &lt;tuv lang&gt;.
 *//*from w ww  .  j a v  a  2 s .  co m*/
private void lowercaseAttributes(Element p_tu) {
    // TUVs have been lowercased already (need `.')
    List tuvs = p_tu.selectNodes(".//tuv");

    // Rename uppercase <tuv Lang> to lowercase lang.
    for (int i = 0, max = tuvs.size(); i < max; i++) {
        Element tuv = (Element) tuvs.get(i);

        Attribute attr = tuv.attribute(Ttx.LANG);
        tuv.remove(attr);

        tuv.addAttribute("xml:lang", attr.getValue());
    }
}

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

License:Apache License

/**
 * Assigns word counts to all sub-flows in the segment.
 * /*from  w w  w. j  av a2 s.co  m*/
 * @ return true if has sub.
 */
private boolean countSubs(Element p_element) {
    int words;
    ArrayList elems = new ArrayList();

    findSubElements(elems, p_element);

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

        if (!isSkipElement(sub)) {
            String subLocType = sub.attributeValue(DiplomatNames.Attribute.LOCTYPE);
            if (subLocType == null || subLocType.equals(DiplomatNames.Element.TRANSLATABLE)) {
                words = countWords(sub);
            } else {
                // Localizables count as 1 token or 0, depending on
                // the configuration (Diplomat.properties).
                words = m_localizableCount;
            }

            // Sub-flow word counts contribute to overall word count.
            m_totalWordCount += words;
            sub.addAttribute(DiplomatNames.Attribute.WORDCOUNT, String.valueOf(words));
        } else {
            // Currently, this only affect the JavaScrpt embedded in the
            // HTML
            // Attribute.
            sub.addAttribute(DiplomatNames.Attribute.WORDCOUNT, "0");
        }
    }

    return elems.size() > 0;
}