List of usage examples for org.dom4j Element isTextOnly
boolean isTextOnly();
Element
has text only content. From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java
License:Open Source License
/** * ?XPATH ?text "/path/@seq" ? "/path/" ? * //www . j a v a 2s . c om * @param element * Element * @param path * String * @return String * @throws BaseException */ public static String getElementContext(Element element, String path) throws BaseException { if (element == null || path == null) { return null; } Object o = element.selectSingleNode(path); if (o == null) { // return null; } if (o instanceof Element) { // 1? Element Element e = (Element) o; if (e.isTextOnly()) { // text only return e.getText(); } else { // element return generateXMLStringBuffer(e, "").toString(); } } else if (o instanceof Attribute) { // 2? Attribute return ((Attribute) o).getValue(); } else { // 3? Other node return generateXMLStringBuffer(o, "").toString(); } }
From source file:com.blocks.framework.utils.date.XMLDom4jUtils.java
License:Open Source License
/** * ?XPATH ?text "/path/@seq" ? "/path/" ? * /*from w w w.j av a 2 s. c om*/ * @param element * Element * @param path * String * @return String * @throws BaseException */ public static String[] getElementContextArray(Element element, String path) throws BaseException { if (element == null || path == null) { return null; } List nodes = element.selectNodes(path); String[] eleContext = new String[nodes.size()]; Iterator iter = nodes.iterator(); int length = 0; Object o = null; Element e = null; while (iter.hasNext()) { o = (Object) iter.next(); if (o instanceof Element) { // 1? Element e = (Element) o; if (e.isTextOnly()) { // text only eleContext[length] = e.getText(); length++; } else { // element eleContext[length] = generateXMLStringBuffer(e, "").toString(); length++; } } else if (o instanceof Attribute) { // 2? Attribute eleContext[length] = ((Attribute) o).getValue(); length++; } else { // 3? Other node eleContext[length] = generateXMLStringBuffer(o, "").toString(); length++; } } return eleContext; }
From source file:com.github.ipaas.ifw.util.XmlUtil.java
License:Apache License
/** * , dom4j Element???map//from ww w . j av a 2s. c o m * * @param container * -- ?dom4j Elementmap * @param elem * -- dom4j Element * @return -- map */ private static Map generateMap(Map container, Element elem) { String name = elem.getName(); Object obj = container.get(name); // ???? if (null != obj) { if (!List.class.isInstance(obj)) { // ??,?List(List) List<Object> newBean = new LinkedList<Object>(); newBean.add(obj); container.put(name, newBean); generateMap(container, elem); } else { // ??,List(?elem?List) List<Object> bean = (List<Object>) obj; if (elem.isTextOnly()) { bean.add(elem.getStringValue()); } else { List<Element> subs = elem.elements(); Map nodes = new LinkedHashMap(); bean.add(nodes); for (Element item : subs) { generateMap(nodes, item); } } } return container; } // ????? if (elem.isTextOnly()) { // ?xml,? container.put(name, elem.getStringValue()); } else { List<Element> subs = elem.elements(); Map nodes = new LinkedHashMap(); container.put(name, nodes); for (Element item : subs) { generateMap(nodes, item); } } return container; }
From source file:com.glaf.core.xml.XmlBuilder.java
License:Apache License
protected void processTextNode(Element element, Map<String, DatasetModel> dataSetMap, Map<String, Object> dataMap, String systemName, Map<String, Object> params) { //LOG.debug("---------------------processTextNode-----------------------"); if (StringUtils.equals(element.getName(), "foreach")) { this.processForEachNode(element, dataSetMap, dataMap, systemName, params); return;/*from ww w . jav a2 s . c o m*/ } String dsId = element.attributeValue("DataSetId"); if (StringUtils.isNotEmpty(dsId)) { if (dataMap != null && !dataMap.isEmpty()) { params.putAll(dataMap); } this.processNode(element, dataSetMap, systemName, params); return; } if (element.isTextOnly()) { String value = element.getStringValue(); if (StringUtils.contains(value, "#{") && StringUtils.contains(value, "}")) { String text = QueryUtils.replaceBlankParas(value, dataMap); element.setText(text); } } List<?> attrs = element.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); } } List<?> elements = element.elements(); Iterator<?> iterator = elements.iterator(); while (iterator.hasNext()) { Element elem = (Element) iterator.next(); if (StringUtils.equals(elem.getName(), "foreach")) { this.processForEachNode(elem, dataSetMap, dataMap, systemName, params); } else { String dsId2 = elem.attributeValue("DataSetId"); if (StringUtils.isNotEmpty(dsId2)) { if (dataMap != null && !dataMap.isEmpty()) { params.putAll(dataMap); } this.processNode(elem, dataSetMap, systemName, params); } else { this.processTextNode(elem, dataSetMap, dataMap, systemName, params); } } } }
From source file:com.glaf.jbpm.util.CustomFieldInstantiator.java
License:Apache License
public static Object getValue(Class<?> type, Element propertyElement) { Object value = null;//from w w w .j a va2 s. c om if (type == String.class) { value = propertyElement.getText(); } else if ((type == Integer.class) || (type == int.class)) { value = Integer.parseInt(propertyElement.getTextTrim()); } else if ((type == Long.class) || (type == long.class)) { value = Long.parseLong(propertyElement.getTextTrim()); } else if ((type == Float.class) || (type == float.class)) { value = new Float(propertyElement.getTextTrim()); } else if ((type == Double.class) || (type == double.class)) { value = Double.parseDouble(propertyElement.getTextTrim()); } else if ((type == Boolean.class) || (type == boolean.class)) { value = Boolean.valueOf(propertyElement.getTextTrim()); } else if ((type == Character.class) || (type == char.class)) { value = Character.valueOf(propertyElement.getTextTrim().charAt(0)); } else if ((type == Short.class) || (type == short.class)) { value = Short.valueOf(propertyElement.getTextTrim()); } else if ((type == Byte.class) || (type == byte.class)) { value = Byte.valueOf(propertyElement.getTextTrim()); } else if (type.isAssignableFrom(java.util.Date.class)) { value = DateUtils.toDate(propertyElement.getTextTrim()); } else if (type.isAssignableFrom(List.class)) { value = getCollectionValue(propertyElement, new java.util.ArrayList<Object>()); } else if (type.isAssignableFrom(Set.class)) { value = getCollectionValue(propertyElement, new LinkedHashSet<Object>()); } else if (type.isAssignableFrom(Collection.class)) { value = getCollectionValue(propertyElement, new java.util.ArrayList<Object>()); } else if (type.isAssignableFrom(Map.class)) { value = getMapValue(propertyElement, new LinkedHashMap<Object, Object>()); } else if (type == Element.class) { value = propertyElement; } else { try { Constructor<?> constructor = type.getConstructor(new Class[] { String.class }); if ((propertyElement.isTextOnly()) && (constructor != null)) { value = constructor.newInstance(new Object[] { propertyElement.getTextTrim() }); } } catch (Exception ex) { logger.error("couldn't parse the bean property value '" + propertyElement.asXML() + "' to a '" + type.getName() + "'"); throw new RuntimeException(ex); } } return value; }
From source file:com.globalsight.cxe.adapter.msoffice.ExcelRepairer.java
License:Apache License
private void repairWt() throws Exception { List<File> fs = getExcelRepairFiles(); for (File f : fs) { String content = FileUtil.readFile(f, "utf-8"); XmlParser parser = new XmlParser(); org.dom4j.Document document = parser.parseXml(content); Element element = document.getRootElement(); @SuppressWarnings("unchecked") List<Element> wts = element.selectNodes("//t"); for (Element wt : wts) { if (wt == null) continue; @SuppressWarnings("unchecked") List<Element> es = wt.elements(); if (!wt.isTextOnly()) { String text = wt.getStringValue(); for (Element e : es) { wt.remove(e);// w ww. j a v a2 s . c om } wt.setText(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 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 ww . j av a 2 s. co m } wt.setText(text); } } }
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 ww . j av a2 s . 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.heren.turtle.gear.meta.AbstractService.java
License:Open Source License
protected Map<String, Object> getMessage(String message) throws DocumentException { Document document = DocumentHelper.parseText(message); Element rootElement = document.getRootElement(); List elements = rootElement.elements(); Map<String, Object> result = new HashMap<String, Object>(); for (Iterator it = elements.iterator(); it.hasNext();) { Element subElement = (Element) it.next(); if (subElement.isTextOnly()) { result.put(subElement.getName(), subElement.getTextTrim()); } else {// ww w . j a va 2s .co m List<String> subList = new ArrayList<String>(); List subEle = subElement.elements(); for (Iterator iterator = subEle.iterator(); iterator.hasNext();) { Element itemElements = (Element) iterator.next(); subList.add(itemElements.getTextTrim()); } result.put(subElement.getName(), subList); } } return result; }
From source file:com.heren.turtle.server.utils.XmlUtils.java
License:Open Source License
/** * delete the label of xml that is not use * * @param message// w ww . j av a 2 s.c om * @return */ public static Map<String, Object> getMessage(String message) throws DocumentException { message = replaceWrongPart(message); Document document = DocumentHelper.parseText(message); Element rootElement = document.getRootElement(); Element request = rootElement.element("request"); List elements = request.elements(); Map<String, Object> result = new HashMap<>(); for (Object element : elements) { Element subElement = (Element) element; if (subElement.isTextOnly()) { result.put(subElement.getName(), subElement.getTextTrim()); } else { List<String> subList = new ArrayList<>(); List subEle = subElement.elements(); for (Object aSubEle : subEle) { Element itemElements = (Element) aSubEle; subList.add(itemElements.getTextTrim()); } result.put(subElement.getName(), subList); } } return result; }