List of usage examples for org.dom4j DocumentHelper parseText
public static Document parseText(String text) throws DocumentException
parseText
parses the given text as an XML document and returns the newly created Document.
From source file:org.mule.module.xml.functional.Dom4jXPathExpressionWithNamespaceTestCase.java
License:Open Source License
protected Document documentFor(String name) throws Exception { return DocumentHelper.parseText(MessageFormat.format(MESSAGE, name)); }
From source file:org.mule.module.xml.transformer.JXPathExtractor.java
License:Open Source License
/** * Evaluate the expression in the context of the given object and returns the * result. If the given object is a string, it assumes it is an valid xml and * parses it before evaluating the xpath expression. *//* w w w . j a v a 2s . c o m*/ @Override public Object doTransform(Object src, String encoding) throws TransformerException { try { Object result = null; if (src instanceof String) { Document doc = DocumentHelper.parseText((String) src); XPath xpath = doc.createXPath(expression); if (namespaces != null) { xpath.setNamespaceURIs(namespaces); } // This is the way we always did it before, so keep doing it that way // as xpath.evaluate() will return non-string results (like Doubles) // for some scenarios. if (outputType == null && singleResult) { return xpath.valueOf(doc); } // TODO handle non-list cases, see //http://www.dom4j.org/apidocs/org/dom4j/XPath.html#evaluate(java.lang.Object) Object obj = xpath.evaluate(doc); if (obj instanceof List) { for (int i = 0; i < ((List) obj).size(); i++) { final Node node = (Node) ((List) obj).get(i); result = add(result, node); if (singleResult) { break; } } } else { result = add(result, obj); } } else { JXPathContext context = JXPathContext.newContext(src); result = context.getValue(expression); } return result; } catch (Exception e) { throw new TransformerException(this, e); } }
From source file:org.mule.module.xml.util.XMLTestUtils.java
License:Open Source License
public static Document toDom4jDocument(String resource) throws IOException, DocumentException { String xml = toString(resource); return DocumentHelper.parseText(xml); }
From source file:org.mule.transformers.xml.DomXmlTransformerEncodingByteArrayTestCase.java
License:Open Source License
@Override public boolean compareResults(Object expected, Object result) { try {// w w w .ja v a 2 s.c o m // This is only used during roundtrip test, so it will always be byte[] instances if (expected instanceof byte[]) { org.dom4j.Document dom4jDoc = null; dom4jDoc = DocumentHelper.parseText(new String((byte[]) expected, "UTF-8")); expected = new DOMWriter().write(dom4jDoc); dom4jDoc = DocumentHelper.parseText(new String((byte[]) result, "UTF-8")); result = new DOMWriter().write(dom4jDoc); } } catch (Exception ex) { // ignored. } return super.compareResults(expected, result); }
From source file:org.mule.transformers.xml.DomXmlTransformerEncodingTestCase.java
License:Open Source License
@Override protected void doSetUp() throws Exception { org.dom4j.Document dom4jDoc = DocumentHelper.parseText( IOUtils.toString(IOUtils.getResourceAsStream("cdcatalog-utf-8.xml", getClass()), "UTF-8")); srcData = new DOMWriter().write(dom4jDoc); resultData = IOUtils.toString(IOUtils.getResourceAsStream("cdcatalog-us-ascii.xml", getClass()), "US-ASCII"); }
From source file:org.mule.transformers.xml.XmlToOutputHandlerByteArrayTestCase.java
License:Open Source License
@Override public boolean compareResults(Object expected, Object result) { if (result instanceof OutputHandler) { OutputHandler handler = (OutputHandler) result; ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {//from ww w . ja v a 2s . co m handler.write(null, bos); org.dom4j.Document dom4jDoc = null; dom4jDoc = DocumentHelper.parseText((String) expected); expected = new DOMWriter().write(dom4jDoc); dom4jDoc = DocumentHelper.parseText(new String(bos.toByteArray(), "UTF-8")); result = new DOMWriter().write(dom4jDoc); } catch (Exception e) { fail(); } } return super.compareResults(expected, result); }
From source file:org.mustangproject.ZUGFeRD.ZUGFeRD2PullProvider.java
License:Open Source License
@Override public byte[] getXML() { byte[] res = zugferdData; StringWriter sw = new StringWriter(); Document document = null;/* w ww. java 2s . c o m*/ try { document = DocumentHelper.parseText(new String(zugferdData)); } catch (DocumentException e1) { Logger.getLogger(ZUGFeRD2PullProvider.class.getName()).log(Level.SEVERE, null, e1); } try { OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(sw, format); writer.write(document); res = sw.toString().getBytes("UTF-8"); } catch (IOException e) { Logger.getLogger(ZUGFeRD2PullProvider.class.getName()).log(Level.SEVERE, null, e); } return res; }
From source file:org.neuclear.id.SignedNamedDocument.java
License:Open Source License
/** * This parses the document from the encoded version and returns it to you. * Note it parses it on every call, so you might want to * @return/*from w w w .j a va 2 s . c o m*/ * @throws DocumentException */ public Document getDocument() throws DocumentException { return DocumentHelper.parseText(getEncoded()); }
From source file:org.nuxeo.ecm.platform.publisher.remoting.marshaling.DefaultDocumentLocationMarshaler.java
License:Open Source License
public DocumentLocation unMarshalDocumentLocation(String data) throws PublishingMarshalingException { DocumentLocation docLoc;/*from w w w .j a v a2 s .c o m*/ try { Document document = DocumentHelper.parseText(data); org.dom4j.Element rootElem = document.getRootElement(); String repoName = rootElem.attribute("repository").getValue(); String refStr = rootElem.attribute("ref").getValue(); DocumentRef ref = null; if (refStr.startsWith("/")) { ref = new PathRef(refStr); } else { ref = new IdRef(refStr); } if (rootElem.attributeValue("originalServer") != null) { docLoc = new ExtendedDocumentLocation(rootElem.attributeValue("originalServer"), repoName, ref); } else { docLoc = new DocumentLocationImpl(repoName, ref); } } catch (DocumentException e) { throw new PublishingMarshalingException("Unable to unmarshal Piublication Node", e); } return docLoc; }
From source file:org.nuxeo.ecm.platform.publisher.remoting.marshaling.DefaultMarshaler.java
License:Open Source License
protected List<Object> unMarshallParameters(String data, CoreSession session) throws PublishingMarshalingException { List<Object> params = new ArrayList<Object>(); Document document;/*from w ww. ja v a 2s .co m*/ try { document = DocumentHelper.parseText(data); org.dom4j.Element rootElem = document.getRootElement(); for (Iterator i = rootElem.elementIterator(parameterTag); i.hasNext();) { org.dom4j.Element param = (org.dom4j.Element) i.next(); if (param.elements().size() > 0) { String xmlParam = ((org.dom4j.Element) param.elements().get(0)).asXML(); params.add(unMarshalSingleObject(xmlParam, session)); } else { String value = param.getText(); if ("null".equals(value)) { value = null; } params.add(value); } } } catch (DocumentException e) { throw new PublishingMarshalingException("Error during unmarshaling of parameters", e); } return params; }