List of usage examples for org.dom4j.tree DefaultAttribute DefaultAttribute
public DefaultAttribute(String name, String value)
Attribute with the specified local name and value. From source file:com.globalsight.everest.edit.offline.OfflineEditManagerLocal.java
License:Apache License
/** * Only when the source file is XLF, need re-wrap the off-line down-loaded * XLIFF file.// ww w . j av a 2 s . c o m */ private void reWrapXliff(Document doc, HashSet<Long> jobIds) { Element root = doc.getRootElement(); Element bodyElement = root.element(XliffConstants.FILE).element(XliffConstants.BODY); // Find TU elements that are from XLF source file. List<Element> xlfTuElements = new ArrayList<Element>(); for (Iterator i = bodyElement.elementIterator(XliffConstants.TRANS_UNIT); i.hasNext();) { Element foo = (org.dom4j.Element) i.next(); if (isSrcFileXlf(foo, jobIds)) { xlfTuElements.add(foo); } } if (xlfTuElements.size() == 0) return; // StringBuffer xliffString = new StringBuffer(); xliffString.append("<?xml version=\"1.0\"?>"); xliffString.append("<xliff version=\"1.2\">"); xliffString.append("<file>"); xliffString.append("<body>"); Attribute stateAttr = null; ArrayList<String> trgStates = new ArrayList<String>(); for (Element foo : xlfTuElements) { Element sourceElement = foo.element(XliffConstants.SOURCE); String sourceContent = sourceElement.asXML(); sourceContent = sourceContent.replaceFirst("<" + XliffConstants.SOURCE + "[^>]*>", ""); sourceContent = sourceContent.replace("</" + XliffConstants.SOURCE + ">", ""); Element targetElement = foo.element(XliffConstants.TARGET); String targetContent = targetElement.asXML(); targetContent = targetContent.replaceFirst("<" + XliffConstants.TARGET + "[^>]*>", ""); targetContent = targetContent.replace("</" + XliffConstants.TARGET + ">", ""); xliffString.append("<trans-unit>"); xliffString.append("<source>").append(sourceContent).append("</source>"); xliffString.append("<target>").append(targetContent).append("</target>"); xliffString.append("</trans-unit>"); // Store the state attributes in sequence stateAttr = targetElement.attribute(XliffConstants.STATE); if (stateAttr == null) { trgStates.add(""); } else { trgStates.add(stateAttr.getValue()); } } xliffString.append("</body>"); xliffString.append("</file>"); xliffString.append("</xliff>"); // Extract it to get re-wrapped segments. DiplomatAPI api = new DiplomatAPI(); api.setEncoding("UTF-8"); api.setLocale(new Locale("en_US")); api.setInputFormat("xlf"); api.setSentenceSegmentation(false); api.setSegmenterPreserveWhitespace(true); api.setSourceString(xliffString.toString()); ArrayList<String> sourceArray = new ArrayList<String>(); ArrayList<String> targetArray = new ArrayList<String>(); try { api.extract(); Output output = api.getOutput(); for (Iterator it = output.documentElementIterator(); it.hasNext();) { DocumentElement element = (DocumentElement) it.next(); if (element instanceof TranslatableElement) { TranslatableElement trans = (TranslatableElement) element; SegmentNode src = (SegmentNode) (trans.getSegments().get(0)); if (trans.getXliffPartByName().equals("source")) { sourceArray.add("<source>" + replaceEntity(src.getSegment()) + "</source>"); } else if (trans.getXliffPartByName().equals("target")) { targetArray.add("<target>" + replaceEntity(src.getSegment()) + "</target>"); } } } } catch (Exception e) { if (s_category.isDebugEnabled()) { s_category.error(e.getMessage(), e); } } // Replace source/target elements int index = 0; if (sourceArray != null && targetArray != null) { for (Iterator i = bodyElement.elementIterator(XliffConstants.TRANS_UNIT); i.hasNext();) { if (index >= sourceArray.size()) { break; } Element foo = (org.dom4j.Element) i.next(); if (!isSrcFileXlf(foo, jobIds)) { continue; } TuImpl tu = getTu(foo, jobIds); GxmlElement srcGxmlElement = tu.getSourceTuv().getGxmlElement(); String newSrc = "<segment>" + GxmlUtil.stripRootTag(sourceArray.get(index)) + "</segment>"; GxmlElement trgGxmlElement = SegmentUtil2.getGxmlElement(newSrc); newSrc = SegmentUtil2.adjustSegmentAttributeValues(srcGxmlElement, trgGxmlElement, "xlf"); newSrc = "<source>" + GxmlUtil.stripRootTag(newSrc) + "</source>"; Element sourceElement = foo.element(XliffConstants.SOURCE); Element newSourceElement = getDom(newSrc).getRootElement(); foo.remove(sourceElement); foo.add(newSourceElement); String newTrg = "<segment>" + GxmlUtil.stripRootTag(targetArray.get(index)) + "</segment>"; trgGxmlElement = SegmentUtil2.getGxmlElement(newTrg); newTrg = SegmentUtil2.adjustSegmentAttributeValues(srcGxmlElement, trgGxmlElement, "xlf"); newTrg = "<target>" + GxmlUtil.stripRootTag(newTrg) + "</target>"; Element newTargetElement = getDom(newTrg).getRootElement(); Element targetElement = foo.element(XliffConstants.TARGET); foo.remove(targetElement); // If target has "state" attribute, it should be preserved. try { if (!"".equals(trgStates.get(index))) { newTargetElement.add(new DefaultAttribute(XliffConstants.STATE, trgStates.get(index))); } } catch (Exception ignore) { } foo.add(newTargetElement); index++; } } }
From source file:com.globalsight.everest.tm.exporter.TmxChecker.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" })
public String fixSegment(String segment) {
Document dom = getDom(segment);
Element root = dom.getRootElement();
Iterator ite = dtdMap.entrySet().iterator();
while (ite.hasNext()) {
Map.Entry entry = (Map.Entry) ite.next();
String key = (String) entry.getKey();
ArrayList array = (ArrayList) entry.getValue();
String nodeName = "//" + key;
List nodes = root.selectNodes(nodeName);
for (int x = 0; x < nodes.size(); x++) {
Element node = (Element) nodes.get(x);
Attribute internalAttr = node.attribute("internal");
Attribute typeAttr = node.attribute("type");
ArrayList list = new ArrayList();
list.addAll(node.attributes());
resetXAttribute(key, list);//from w w w . j a v a2 s . co m
for (int y = 0; y < list.size(); y++) {
Attribute temp = (Attribute) list.get(y);
String name = temp.getName();
if (!array.contains(name)) {
node.remove(temp);
}
}
// GBS-3537 & GBS-3691
if (internalAttr != null && "yes".equalsIgnoreCase(internalAttr.getValue())) {
String exportedType = "x-internal";
if (typeAttr != null) {
String type = typeAttr.getValue();
if (StringUtil.isNotEmpty(type)) {
exportedType += "-" + type.trim().toLowerCase();
}
typeAttr.setValue(exportedType);
} else {
node.add(new DefaultAttribute("type", exportedType));
}
}
}
}
return root.asXML();
}
From source file:com.globalsight.everest.tm.exporter.TmxChecker.java
License:Apache License
/** * When export TM, "internal='yes' type='style'" will be merged to * "type='x-internal-style'"; When import back, need revert back. * * @param segment/*w w w . j a v a2 s. c om*/ * @return */ public String revertInternalTag(String segment) { Document dom = getDom(segment); Element root = dom.getRootElement(); for (String tag : dtdMap.keySet()) { String nodeName = "//" + tag; List nodes = root.selectNodes(nodeName); for (int x = 0; x < nodes.size(); x++) { Element node = (Element) nodes.get(x); if (node.attribute("type") != null && node.attribute("type").getValue() != null) { Attribute typeAttr = node.attribute("type"); String type = typeAttr.getValue(); if ("x-internal".equalsIgnoreCase(type)) { node.remove(typeAttr); node.add(new DefaultAttribute("internal", "yes")); } else if (type.startsWith("x-internal")) { String realTypeValue = type.substring("x-internal".length() + 1); typeAttr.setValue(realTypeValue); node.add(new DefaultAttribute("internal", "yes")); } } } } return root.asXML(); }
From source file:com.mor.blogengine.model.BlogCategory.java
License:Open Source License
/** * a-like as {@link #toString() }/*from w w w. j a v a 2 s . c o m*/ * * @return an XML representation of element */ @Override public DefaultElement toElement() { // QName lElementDecl = new QName("Category", mNamespace); DefaultElement lReturnElement = new DefaultElement("Category"); // Attribute= a=new Attribute("", mCatName) lReturnElement.add(new DefaultAttribute("ID", getEntityID())); lReturnElement.add(new DefaultAttribute("name", getCatName())); lReturnElement.add(new DefaultAttribute("description", getDescription())); return lReturnElement; }
From source file:com.mor.blogengine.model.BlogComment.java
License:Open Source License
@Override public DefaultElement toElement() { // this is element Declaration in complete form. // QName lCommentTextDecl = new QName("CommentText", mNamespace); // QName lReturnelementdecl = new QName("Comment", mNamespace); DefaultElement lReturnElement = null; DefaultElement lCommentText = new DefaultElement("CommentText"); lCommentText.addText(mCommentText);/* w w w .ja va 2s . c om*/ lReturnElement = new DefaultElement("Comment"); // lReturnElement.add(mNamespace); lReturnElement.add(new DefaultAttribute("entryID", getEntryID())); lReturnElement.add(new DefaultAttribute("ID", getEntityID())); lReturnElement.add(new DefaultAttribute("date", mDate)); lReturnElement.add(new DefaultAttribute("author", mAuthor)); lReturnElement.add(new DefaultAttribute("webPage", mWebPage)); lReturnElement.add(lCommentText); return lReturnElement; }
From source file:com.mor.blogengine.model.BlogEntry.java
License:Open Source License
/** * * @return XML node representation of entry *//* w w w . j a v a2s . com*/ @Override public DefaultElement toElement() { // this is element Declaration in complete form. // QName lElementDecl = new QName("Entry", mNamespace); DefaultElement lReturnElement = new DefaultElement("Entry"); lReturnElement.add(new DefaultAttribute("date", getDate())); lReturnElement.add(new DefaultAttribute("categoryID", getCatID())); lReturnElement.add(new DefaultAttribute("allowComments", getAllowComments())); lReturnElement.add(new DefaultAttribute("ID", getEntityID())); // this is element Declaration in complete form. // QName lEntryTextDecl = new QName("Text", mNamespace); DefaultElement lEntryText = new DefaultElement("Text"); lEntryText.addText(mTexte); lReturnElement.add(lEntryText); // QName lResumeDecl = new QName("Resume", mNamespace); DefaultElement lEntryResume = new DefaultElement("Resume"); lEntryResume.addText(mResume); lReturnElement.add(lEntryResume); return lReturnElement; }
From source file:com.mor.blogengine.xpath.SearchEngine.java
License:Open Source License
List<DefaultElement> getEntriesForCategory(String pCatID)
throws NoMatchesFoundException, MissingPropertyException, IncorrectPropertyValueException {
trace("Building XPath search Query to get entries for a category");
List<String> lNodes = new ArrayList<>();
lNodes.add("Entry");
DefaultAttribute lAttribute = new DefaultAttribute("categoryID", pCatID);
List<DefaultAttribute> lAttList = new ArrayList<>();
lAttList.add(lAttribute);//from www. ja va 2s. c om
String exp = new XPathExpressionBuilder("Blog", lNodes, lAttList, mXpathVersion).compileExpression();
return Collections.unmodifiableList(configurator.findContent(exp));
}
From source file:com.mor.blogengine.xpath.SearchEngine.java
License:Open Source License
List<DefaultElement> getEntriesforDate(String pDate) throws NoMatchesFoundException, InvalidXPathException, MissingPropertyException, IncorrectPropertyValueException { trace("Building XPath search Query to get entries for a date"); List<String> lNodesList = new ArrayList<>(); List<DefaultAttribute> lAttList = new ArrayList<>(); lNodesList.add("Entry"); lAttList.add(new DefaultAttribute("date", pDate)); String exp = new XPathExpressionBuilder("Blog", lNodesList, lAttList, mXpathVersion).compileExpression(); return Collections.unmodifiableList(configurator.findContent(exp)); }
From source file:com.mor.blogengine.xpath.SearchEngine.java
License:Open Source License
/** * * @param pSearchedElement//w w w. j a v a 2s .c o m * @return * @param pSearchedElementName * @param id */ List<DefaultElement> getSingleElement(String pSearchedElementName, String id) throws NoMatchesFoundException, MissingPropertyException, IncorrectPropertyValueException { trace("Building XPath search Query to get a single element"); List<String> lNodes = new ArrayList<>(); lNodes.add(pSearchedElementName); DefaultAttribute lAttribute = new DefaultAttribute("ID", id); List<DefaultAttribute> lAttList = new ArrayList<>(); lAttList.add(lAttribute); String exp = new XPathExpressionBuilder("Blog", lNodes, lAttList, mXpathVersion).compileExpression(); return Collections.unmodifiableList(configurator.findContent(exp)); }
From source file:com.mor.blogengine.xpath.SearchEngine.java
License:Open Source License
List<DefaultElement> getComentsForEntry(String ID)
throws NoMatchesFoundException, MissingPropertyException, IncorrectPropertyValueException {
trace("Building XPath search Query to get comment for an entry");
List<String> lNodes = new ArrayList<>();
lNodes.add("Entry");
lNodes.add("Comment");
DefaultAttribute lAttribute = new DefaultAttribute("entryID", ID);
List<DefaultAttribute> lAttList = new ArrayList<>();
lAttList.add(lAttribute);// www . java 2s . c o m
String exp = new XPathExpressionBuilder("Blog", lNodes, lAttList, mXpathVersion).compileExpression();
trace(exp);
return Collections.unmodifiableList(configurator.findContent(exp));
}