private Object getXmlValueText(List nodes) { if (nodes == null || nodes.isEmpty()) { return null; } Object node = nodes.get(0); if (node instanceof DefaultText) { return StringUtils.trim(((DefaultText) node).getText()); } if (node instanceof DefaultAttribute) { return StringUtils.trim(((DefaultAttribute) node).getText()); } if (node instanceof DefaultElement) { return StringUtils.trim(((DefaultElement) node).getText()); } throw new IllegalArgumentException("unsupported node type ["+node+"]."); }
/** * Selects text nodes as list of strings * * @param xpath xpath expression, should end with text() function, example: "//name/text()" * @return list of found strings matching expression, never null. If nothing matches, list will be empty. */ private List<String> selectStrings(String xpath) { Object res = doc.selectObject(xpath); if(!(res instanceof List)){ throw new IllegalArgumentException("xpath expression must correspond to a list"); } List defaultTextList = (List) res; if(defaultTextList.size() > 0 && !(defaultTextList.get(0) instanceof DefaultText)){ throw new IllegalArgumentException("xpath expression must correspond to a list of text nodes, i.e. end with: text()"); } List<String> stringsList = new ArrayList<String>(); for (Object textNode : defaultTextList) { stringsList.add(((DefaultText)textNode).getText()); } return stringsList; }
public static void streamResult(PipelineContext context, XMLReceiver xmlReceiver, Object result, LocationData locationData) throws SAXException { String strVal = null; if (result instanceof Element || result instanceof Document) { // If element or document, serialize it to content handler final Element element = result instanceof Element ? (Element) result : ((Document) result).getRootElement(); final String systemId = Dom4jUtils.makeSystemId(element); // TODO: Should probably use Dom4jUtils.createDocumentCopyParentNamespaces() or equivalent to handle namespaces better // -> could maybe simply get the list of namespaces in scope on both sides, and output start/endPrefixMapping() final DOMGenerator domGenerator = new DOMGenerator (element, "xpath result doc", DOMGenerator.ZeroValidity, systemId); final ProcessorOutput domOutput = domGenerator.createOutput(OUTPUT_DATA); domOutput.read(context, new EmbeddedDocumentXMLReceiver(xmlReceiver)); } else if (result instanceof NodeInfo) { final NodeInfo nodeInfo = (NodeInfo) result; TransformerUtils.writeTinyTree(nodeInfo, new EmbeddedDocumentXMLReceiver(xmlReceiver)); } else if (result instanceof DefaultProcessingInstruction) { DefaultProcessingInstruction processingInstruction = (DefaultProcessingInstruction) result; xmlReceiver.processingInstruction(processingInstruction.getTarget(), processingInstruction.getText()); } else if (result instanceof DefaultText) { strVal = ((DefaultText) result).getText(); } else if (result instanceof String) { strVal = (String) result; } else if (result instanceof Long) { strVal = Long.toString((Long) result); } else if (result instanceof Double) { final double d = ((Double) result).doubleValue(); strVal = XMLUtils.removeScientificNotation(d); } else if (result instanceof Boolean) { strVal = (result.toString()); } else if (result instanceof StringBuilder) { strVal = result.toString(); } else { String message = "Unsupported type returned by XPath expression: " + (result == null ? "null" : result.getClass().getName()); throw new ValidationException(message, locationData); } // Send string representation of simple type to content handler if (strVal != null) { final char[] ch = strVal.toCharArray(); final int len = strVal.length(); xmlReceiver.characters( ch, 0, len ); } }