List of usage examples for org.dom4j Element elements
List<Element> elements();
From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java
License:Apache License
@SuppressWarnings("unchecked") private static void forHyperlinkInWr(Element element) { List<Node> ts = element.selectNodes("//w:r/w:hyperlink/w:r"); for (Node t : ts) { Element hyperlink = t.getParent(); if (hyperlink == null) continue; Element wr = hyperlink.getParent(); if (wr == null) continue; Element wrParent = wr.getParent(); if (wrParent == null) continue; boolean beforeWt = false; List<?> els = wr.content(); for (Object el : els) { if (el instanceof Element) { Element elm = (Element) el; if ("t".equals(elm.getName())) { beforeWt = false;/* w w w . j av a 2 s .co m*/ break; } else if (hyperlink.equals(elm)) { beforeWt = true; break; } } } wr.remove(hyperlink); @SuppressWarnings("rawtypes") List es = wrParent.elements(); int index = es.indexOf(wr); index = beforeWt ? index : index + 1; hyperlink.setParent(wrParent); es.add(index, hyperlink); } }
From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java
License:Apache License
private static void forNodesInWt(Element element) { @SuppressWarnings("unchecked") List<Element> wts = element.selectNodes("//w:t"); for (Element wt : wts) { @SuppressWarnings("unchecked") List<Element> es = wt.elements(); if (!wt.isTextOnly()) { String text = wt.getStringValue(); for (Element e : es) { wt.remove(e);//from w w w . j a v a2 s.c o m } wt.setText(text); } } }
From source file:com.globalsight.cxe.adapter.msoffice.WordRepairer.java
License:Apache License
private static void forWtNotInWr(Element element) { @SuppressWarnings("unchecked") List<Element> wts = element.selectNodes("//w:t"); for (Element wt : wts) { Element parent = wt.getParent(); if (parent == null || "r".equals(parent.getName())) continue; @SuppressWarnings("unchecked") List<Element> es = parent.elements(); int wtIndex = -1; for (Element e : es) { wtIndex++;/*from www .j a va2 s .co m*/ if (wt.equals(e)) { break; } } for (int i = 1; i < es.size(); i++) { int prefix = wtIndex - i; int suffix = wtIndex + i; if (prefix < 0 && suffix > es.size() - 1) { break; } if (prefix > -1) { Element prefixElement = es.get(prefix); if ("r".equals(prefixElement.getName())) { @SuppressWarnings("unchecked") List<Element> preWts = prefixElement.elements("t"); if (preWts.size() > 0) { String text = wt.getStringValue(); Element preWt = preWts.get(preWts.size() - 1); preWt.setText(preWt.getStringValue() + text); parent.remove(wt); break; } } } if (suffix < es.size()) { Element sufixElement = es.get(prefix); if ("r".equals(sufixElement.getName())) { @SuppressWarnings("unchecked") List<Element> sufWts = sufixElement.elements("t"); if (sufWts.size() > 0) { String text = wt.getStringValue(); Element sufWt = sufWts.get(0); sufWt.setText(text + sufWt.getStringValue()); parent.remove(wt); break; } } } } } }
From source file:com.globalsight.everest.edit.offline.OfflineEditManagerLocal.java
License:Apache License
/** * Adds all comments to database. The path is trans-unit/note. * //from w w w . ja va 2s .c o m * @param doc * The document of the xliff file. * @param p_user * The user who uploaded the xliff file. */ private void addComment(Document doc, User p_user, HashSet<Long> jobIds) { XmlEntities entity = new XmlEntities(); Element root = doc.getRootElement(); Element file = root.element(XliffConstants.FILE); String target = file.attributeValue("target-language"); target = target.replace("-", "_"); org.dom4j.Element bodyElement = file.element(XliffConstants.BODY); for (Iterator i = bodyElement.elementIterator(XliffConstants.TRANS_UNIT); i.hasNext();) { Element foo = (Element) i.next(); // For GBS-3643: if resname="SID", do not add note value as comment. String resName = foo.attributeValue("resname"); if ("SID".equalsIgnoreCase(resName)) { continue; } for (Iterator notes = foo.elementIterator(XliffConstants.NOTE); notes.hasNext();) { Element note = (Element) notes.next(); List elements = note.elements(); String msg = m_resource.getString("msg_note_format_error"); if (elements == null || note.content().size() == 0) { continue; } for (Object obj : note.content()) { if (!(obj instanceof DefaultText)) { s_category.error(msg); s_category.error("Error note: " + note.asXML()); throw new IllegalArgumentException(msg); } } String textContent = note.getText(); if (textContent.startsWith("Match Type:")) { continue; } textContent = entity.decodeString(textContent, null); String tuId = foo.attributeValue("id"); try { // As we can not get to know the job ID only by the tuId, we // have to loop jobIds until we can get the TU object. long jobId = -1; TuImpl tu = null; for (long id : jobIds) { tu = SegmentTuUtil.getTuById(Long.parseLong(tuId), id); jobId = id; if (tu != null) { break; } } for (Object ob : tu.getTuvs(true, jobId)) { TuvImpl tuv = (TuvImpl) ob; TargetPage tPage = tuv.getTargetPage(jobId); if (tuv.getGlobalSightLocale().toString().equalsIgnoreCase(target)) { String title = String.valueOf(tu.getId()); String priority = "Medium"; String status = "open"; String category = "Type01"; IssueImpl issue = tuv.getComment(); if (issue == null) { String key = CommentHelper.makeLogicalKey(tPage.getId(), tu.getId(), tuv.getId(), 0); issue = new IssueImpl(Issue.TYPE_SEGMENT, tuv.getId(), title, priority, status, category, p_user.getUserId(), textContent, key); issue.setShare(false); issue.setOverwrite(false); } else { issue.setTitle(title); issue.setPriority(priority); issue.setStatus(status); issue.setCategory(category); issue.addHistory(p_user.getUserId(), textContent); issue.setShare(false); issue.setOverwrite(false); } HibernateUtil.saveOrUpdate(issue); break; } } } catch (Exception e) { s_category.error("Failed to add comments", e); } } } }
From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java
License:Apache License
/** * Remove the embed tags to consist with the TM. EX: The input might * contains: <bpt type="bold" x="1"><b></bpt>, Just remove * "<b>"./*from w ww. j a v a 2 s . c om*/ * * @param root * the root element. */ private static void removeEmbedTags(Element root) { List elems = root.elements(); for (int i = 0; i < elems.size(); i++) { Object obj = elems.get(i); if (obj instanceof org.dom4j.Element) { org.dom4j.Element element = (org.dom4j.Element) obj; element.clearContent(); } } }
From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java
License:Apache License
private static void removeUncompliantAttributes(Element root, String[] attributes) { List elements = root.elements(); Iterator it = elements.iterator(); while (it.hasNext()) { Element element = (Element) it.next(); for (int i = 0; i < attributes.length; i++) { Attribute attribute = element.attribute(attributes[i]); if (attribute != null) { element.remove(element.attribute(attributes[i])); }/*w w w . j ava 2 s . com*/ } } }
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;//ww w .jav a 2 s.c om 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.terminology.EntryUtils.java
License:Apache License
/** * <p>Recursively prunes empty fields and groups from the given entry. * The entry is destructively modified.</p> * * <p>A depth-first traversal first removes empty leaf nodes, and * then groups that are empty or not fully filled.</p> * * <p>Example: a <descripGrp> must contain at least one <descrip> * child. A <languageGrp> must contain at least one <language> * and one <termGrp> child (2 children minimum).</p> * * <p>As of 6.2, non-relevant whitespace nodes are also removed.</p> * <p>As of 6.3, admissible empty HTML tags are not pruned: IMG, HR, BR.</p> *///from w w w .ja v a2s. c o m static private boolean pruneEmptyFields(Element p_node) { boolean dirty = false; if (!p_node.hasContent()) { return dirty; } // Cannot iterate child elements with node.elementIterator() // because that doesn't implement the remove() method. for (Iterator it = p_node.content().iterator(); it.hasNext();) { Node temp = (Node) it.next(); // Only work on child elements. if (temp.getNodeType() != Node.ELEMENT_NODE) { continue; } Element node = (Element) temp; // Depth-first recursion. dirty |= pruneEmptyFields(node); // Sat Jan 15 02:17:38 2005 CvdL Need to allow empty HTML tags. String name = node.getName().toLowerCase(); if (name.equals("language") || name.equals("img") || name.equals("hr") || name.equals("br")) { continue; } // Leaf nodes if (node.isTextOnly()) { String value = node.getText(); if (value == null || value.trim().length() == 0) { // prune empty leaf nodes it.remove(); dirty = true; } } else { // Group nodes int childCount = node.elements().size(); if (childCount == 0 || (node.getName().equals("languageGrp") && childCount < 2)) { // prune empty groups it.remove(); dirty = true; } } } return dirty; }
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 .j a v 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
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 va2 s . com*/ } else { lit.remove(); } } Element parent = p_elem.getParent(); parent.content().set(parent.indexOf(p_elem), noteGrp); }