List of usage examples for org.dom4j Element add
void add(Namespace namespace);
Namespace
to this element. From source file:com.glaf.core.xml.XmlBuilder.java
License:Apache License
@SuppressWarnings({ "unchecked", "rawtypes" }) protected void processForEachNode(Element element, Map<String, DatasetModel> dataSetMap, Map<String, Object> myDataMap, String systemName, Map<String, Object> params) { //LOG.debug("---------------------processForEachNode-----------------------"); Element parent = element.getParent(); String dsId = element.attributeValue("DataSetId"); if (StringUtils.isNotEmpty(dsId)) { if (myDataMap != null && !myDataMap.isEmpty()) { params.putAll(myDataMap);/* ww w. j a v a 2 s . c o m*/ } DatasetModel dsm = dataSetMap.get(dsId); String sql = dsm.getSql(); String queryId = dsm.getQueryId(); List<Map<String, Object>> rows = null; if (StringUtils.isNotEmpty(queryId)) { Environment.setCurrentSystemName(systemName); List<Object> list = entityService.getList(queryId, params); if (list != null && !list.isEmpty()) { rows = new ArrayList<Map<String, Object>>(); for (Object object : list) { if (object instanceof Map) { Map dataMap = (Map) object; rows.add(dataMap); } else { try { Map dataMap = BeanUtils.describe(object); rows.add(dataMap); } catch (Exception e) { } } } } } else if (StringUtils.isNotEmpty(sql)) { LOG.debug("sql:" + sql); LOG.debug("params:" + params); rows = queryHelper.getResultList(systemName, sql, params); // ITablePageService tablePageService = // ContextFactory.getBean("tablePageService"); // rows = queryHelper.getResultList(systemName, sql, params); // Environment.setCurrentSystemName(systemName); // rows = tablePageService.getListData(sql, params); LOG.debug("#1 rows:" + rows.size()); } if (rows != null && !rows.isEmpty()) { int sortNo = 0; List<?> elements = element.elements(); if (elements != null && elements.size() == 1) { Element elem = (Element) elements.get(0); LOG.debug("name:" + elem.getName()); for (Map<String, Object> dataMap : rows) { sortNo = sortNo + 1; dataMap.put("sortNo", sortNo); Element e = elem.createCopy(); if (dsm.getControllers() != null && !dsm.getControllers().isEmpty()) { List<FieldController> controllers = dsm.getControllers(); for (FieldController c : controllers) { Class<?> x = ClassUtils.classForName(c.getProvider()); FieldConverter fp = (FieldConverter) ReflectUtils.newInstance(x); fp.convert(c.getFromName(), c.getToName(), dataMap); } } if (e.isTextOnly()) { String value = e.getStringValue(); if (StringUtils.contains(value, "#{") && StringUtils.contains(value, "}")) { String text = QueryUtils.replaceBlankParas(value, dataMap); e.setText(text); } } List<?> attrs = e.attributes(); Iterator<?> iter = attrs.iterator(); while (iter.hasNext()) { Attribute attr = (Attribute) iter.next(); String value = attr.getValue(); if (StringUtils.contains(value, "#{") && StringUtils.contains(value, "}")) { String text = QueryUtils.replaceBlankParas(value, dataMap); attr.setValue(text); } } e.setParent(null); parent.add(e); } } } } parent.remove(element); }
From source file:com.glaf.core.xml.XmlBuilder.java
License:Apache License
@SuppressWarnings({ "rawtypes", "unchecked" }) protected void processNode(Element element, Map<String, DatasetModel> dataSetMap, String systemName, Map<String, Object> params) { //LOG.debug("---------------------processNode-----------------------"); String dsId = element.attributeValue("DataSetId"); if (StringUtils.isNotEmpty(dsId) && dataSetMap.get(dsId) != null) { //LOG.debug(element.getName() + " ds->" + dsId); element.remove(element.attribute("DataSetId")); Element parent = element.getParent(); DatasetModel dsm = dataSetMap.get(dsId); String sql = dsm.getSql(); String queryId = dsm.getQueryId(); boolean single = dsm.isSingle(); boolean foreachPerRow = dsm.isForeachPerRow(); List<Map<String, Object>> rows = null; List<String> splits = dsm.getSplitList(); if (foreachPerRow) { LOG.debug("sql:" + sql); LOG.debug("params:" + params); rows = queryHelper.getResultList(systemName, sql, params); // ITablePageService tablePageService = // ContextFactory.getBean("tablePageService"); // rows = queryHelper.getResultList(systemName, sql, params); // Environment.setCurrentSystemName(systemName); // rows = tablePageService.getListData(sql, params); LOG.debug("#2 rows:" + rows.size()); } else {/*from w ww .ja v a 2 s . c o m*/ rows = ResultThreadLocal.getResultList(dsId); if (rows == null) { if (StringUtils.isNotEmpty(queryId)) { Environment.setCurrentSystemName(systemName); List<Object> list = entityService.getList(queryId, params); if (list != null && !list.isEmpty()) { rows = new ArrayList<Map<String, Object>>(); for (Object object : list) { if (object instanceof Map) { Map dataMap = (Map) object; rows.add(dataMap); } else { try { Map dataMap = BeanUtils.describe(object); rows.add(dataMap); } catch (Exception e) { } } } } } else if (StringUtils.isNotEmpty(sql)) { LOG.debug("sql:" + sql); LOG.debug("params:" + params); rows = queryHelper.getResultList(systemName, sql, params); // ITablePageService tablePageService = // ContextFactory.getBean("tablePageService"); // rows = queryHelper.getResultList(systemName, sql, // params); // Environment.setCurrentSystemName(systemName); // rows = tablePageService.getListData(sql, params); LOG.debug("#3 rows:" + rows.size()); } ResultThreadLocal.setResultList(dsId, rows); } } if (rows != null && !rows.isEmpty()) { if (single) { Map<String, Object> dataMap = rows.get(0); if (dsm.getControllers() != null && !dsm.getControllers().isEmpty()) { List<FieldController> controllers = dsm.getControllers(); for (FieldController c : controllers) { Class<?> x = ClassUtils.classForName(c.getProvider()); FieldConverter fp = (FieldConverter) ReflectUtils.newInstance(x); fp.convert(c.getFromName(), c.getToName(), dataMap); } } this.processTextNode(element, dataSetMap, dataMap, systemName, params); } else { int sortNo = 0; for (Map<String, Object> dataMap : rows) { if (splits != null && !splits.isEmpty()) { int splitCnt = 0; Iterator<String> it = splits.iterator(); while (it.hasNext()) { String str = it.next(); if (str != null && ObjectUtils.equals(dataMap.get(str), params.get(str))) { splitCnt = splitCnt + 1; } else if (str != null && ObjectUtils.equals(dataMap.get(str.toLowerCase()), params.get(str.toLowerCase()))) { splitCnt = splitCnt + 1; } } /** * ??? */ if (splitCnt != splits.size()) { // LOG.debug("dsId="+dsId); // LOG.debug("params:"+params ); // LOG.debug(dataMap + " ???"); continue; } } sortNo = sortNo + 1; dataMap.put("sortNo", sortNo); if (dsm.getControllers() != null && !dsm.getControllers().isEmpty()) { List<FieldController> controllers = dsm.getControllers(); for (FieldController c : controllers) { Class<?> x = ClassUtils.classForName(c.getProvider()); FieldConverter fp = (FieldConverter) ReflectUtils.newInstance(x); fp.convert(c.getFromName(), c.getToName(), dataMap); } } Element e = element.createCopy(); this.processTextNode(e, dataSetMap, dataMap, systemName, params); parent.add(e); } parent.remove(element); } } else { parent.remove(element); } } else { this.processTextNode(element, dataSetMap, null, systemName, params); } }
From source file:com.glaf.jbpm.export.MxJbpmProcessExporter.java
License:Apache License
public void addElement(Document doc, Map<String, Object> context) { Element root = doc.getRootElement(); GraphSession graphSession = null;//from w ww. j ava2 s .c o m JbpmContext jbpmContext = null; try { jbpmContext = ProcessContainer.getContainer().createJbpmContext(); graphSession = jbpmContext.getGraphSession(); List<ProcessDefinition> processDefinitions = graphSession.findLatestProcessDefinitions(); if (processDefinitions != null && processDefinitions.size() > 0) { Element element = root.addElement("processes"); Iterator<ProcessDefinition> iterator = processDefinitions.iterator(); while (iterator.hasNext()) { ProcessDefinition pd = iterator.next(); Element elem = element.addElement("process"); elem.addAttribute("name", pd.getName()); elem.addAttribute("description", pd.getDescription()); } } } catch (Exception ex) { if (LogUtils.isDebug()) { ex.printStackTrace(); } } finally { Context.close(jbpmContext); } Element jbpmCfgTemplate = root.addElement("jbpm-cfg-template"); String configPath = SystemProperties.getConfigRootPath(); String path = configPath + "/conf/jbpm/template"; java.io.File directory = new java.io.File(path); String[] filelist = directory.list(); InputStream inputStream = null; SAXReader xmlReader = new SAXReader(); for (int i = 0; i < filelist.length; i++) { String filename = directory.getAbsolutePath() + "/" + filelist[i]; File file = new File(filename); if (file.isFile() && file.getName().endsWith(".xml")) { try { inputStream = new FileInputStream(file); Document doc2x = xmlReader.read(inputStream); Element root2x = doc2x.getRootElement(); List<?> elements = root2x.elements("action-definition"); Iterator<?> iterator = elements.iterator(); while (iterator.hasNext()) { Element elem = (Element) iterator.next(); elem.setParent(jbpmCfgTemplate); elem.setDocument(doc); jbpmCfgTemplate.add(elem); } inputStream.close(); inputStream = null; } catch (Exception ex) { throw new RuntimeException(ex); } } } }
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./*from ww w . j a v a 2s .co 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.edit.offline.page.TmxUtil.java
License:Apache License
/** * Removes the given <sub> element from the segment. <sub> is special since * it does not only surround embedded tags but also text, which must be * pulled out of the <sub> and added to the parent tag. *//*from w w w . j av a 2 s . c o m*/ public static void replaceNbsp(Element p_element) { Element parent = p_element.getParent(); int index = parent.indexOf(p_element); // We copy the current content, clear out the parent, and then // re-add the old content, inserting the <sub>'s textual // content instead of the <sub> (this clears any embedded TMX // tags in the subflow). ArrayList newContent = new ArrayList(); List content = parent.content(); for (int i = content.size() - 1; i >= 0; --i) { Node node = (Node) content.get(i); newContent.add(node.detach()); } Collections.reverse(newContent); parent.clearContent(); for (int i = 0, max = newContent.size(); i < max; ++i) { Node node = (Node) newContent.get(i); if (i == index) { parent.addText("\u00A0"); } else { parent.add(node); } } }
From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java
License:Apache License
/** * Removes the given <sub> element from the segment. <sub> is special since * it does not only surround embedded tags but also text, which must be * pulled out of the <sub> and added to the parent tag. *//*from w ww . j a v a2 s . com*/ public static void removeSubElement(Element p_element) { Element parent = p_element.getParent(); int index = parent.indexOf(p_element); // We copy the current content, clear out the parent, and then // re-add the old content, inserting the <sub>'s textual // content instead of the <sub> (this clears any embedded TMX // tags in the subflow). ArrayList newContent = new ArrayList(); List content = parent.content(); for (int i = content.size() - 1; i >= 0; --i) { Node node = (Node) content.get(i); newContent.add(node.detach()); } Collections.reverse(newContent); parent.clearContent(); for (int i = 0, max = newContent.size(); i < max; ++i) { Node node = (Node) newContent.get(i); if (i == index) { parent.addText(p_element.getText()); } else { parent.add(node); } } }
From source file:com.globalsight.everest.edit.offline.page.TmxUtil.java
License:Apache License
/** * Removes the given TMX 1.4 <hi> element from the segment. <hi> is special * since it does not surround embedded tags but text, which must be pulled * out of the <hi> and added to the parent segment. */// w w w.j a va2s . c o m private static void removeHiElement(Element p_element) { Element parent = p_element.getParent(); int index = parent.indexOf(p_element); // We copy the current content, clear out the parent, and then // re-add the old content, inserting the <hi>'s content // instead of the <hi>. ArrayList newContent = new ArrayList(); List content = parent.content(); for (int i = content.size() - 1; i >= 0; --i) { Node node = (Node) content.get(i); newContent.add(node.detach()); } Collections.reverse(newContent); parent.clearContent(); for (int i = 0, max = newContent.size(); i < max; ++i) { Node node = (Node) newContent.get(i); if (i == index) { parent.appendContent(p_element); } else { parent.add(node); } } }
From source file:com.globalsight.everest.projecthandler.ProjectTmTuvT.java
License:Apache License
/** * Removes the given TMX 1.4 <hi> element from the segment. <hi> is special * since it does not surround embedded tags but text, which must be pulled * out of the <hi> and added to the parent segment. */// w ww . j av a 2s .com private void removeHiElement(Element p_element) { Element parent = p_element.getParent(); int index = parent.indexOf(p_element); // We copy the current content, clear out the parent, and then // re-add the old content, inserting the <hi>'s content // instead of the <hi>. ArrayList newContent = new ArrayList(); List content = parent.content(); for (int i = content.size() - 1; i >= 0; --i) { Node node = (Node) content.get(i); newContent.add(node.detach()); } Collections.reverse(newContent); parent.clearContent(); for (int i = 0, max = newContent.size(); i < max; ++i) { Node node = (Node) newContent.get(i); if (i == index) { parent.appendContent(p_element); } else { parent.add(node); } } }
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);//w ww .jav a2s . c o 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. ja v a 2 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(); }