1 17 package org.apache.servicemix.components.mps; 18 19 import javax.jbi.JBIException; 20 import javax.jbi.messaging.NormalizedMessage; 21 22 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 23 import org.w3c.dom.Document ; 24 25 import org.apache.xpath.CachedXPathAPI; 26 import org.apache.xpath.objects.XObject; 27 28 33 public class XPathContentMessagePropertyValue implements PropertyValue { 34 35 public final static String XML_ELEMENT_NAME = "xpath-expression"; 36 37 private String xpath; 38 39 public XPathContentMessagePropertyValue(String xpath) { 40 this.xpath = xpath; 41 } 42 43 47 public String getPropertyValue(NormalizedMessage msg) throws JBIException { 48 String resultValue = null; 49 if (msg.getContent() != null) { 50 CachedXPathAPI xpathApi = new CachedXPathAPI(); 51 try { 52 Document doc = new SourceTransformer().toDOMDocument(msg); 53 XObject result = xpathApi.eval(doc,xpath); 54 55 resultValue = result.toString(); 56 if ("".equals(resultValue)) { 57 resultValue = null; 58 } 59 } catch (Exception e) { 60 throw new JBIException("Could not get value from message via xpath " + xpath, e); 61 } 62 } 63 return resultValue; 64 65 } 66 67 } 68 | Popular Tags |