1 17 package org.apache.servicemix.expression; 18 19 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 20 import org.springframework.beans.factory.InitializingBean; 21 import org.xml.sax.SAXException ; 22 23 import javax.jbi.messaging.MessageExchange; 24 import javax.jbi.messaging.MessagingException; 25 import javax.jbi.messaging.NormalizedMessage; 26 import javax.xml.namespace.NamespaceContext ; 27 import javax.xml.parsers.ParserConfigurationException ; 28 import javax.xml.transform.TransformerException ; 29 import javax.xml.xpath.XPath ; 30 import javax.xml.xpath.XPathException ; 31 import javax.xml.xpath.XPathExpression ; 32 import javax.xml.xpath.XPathExpressionException ; 33 import javax.xml.xpath.XPathFactory ; 34 import javax.xml.xpath.XPathFunctionResolver ; 35 36 import java.io.IOException ; 37 38 43 public class JAXPXPathExpression implements Expression, InitializingBean { 44 private String xpath; 45 private SourceTransformer transformer = new SourceTransformer(); 46 private MessageVariableResolver variableResolver = new MessageVariableResolver(); 47 private XPathExpression xPathExpression; 48 private XPathFunctionResolver functionResolver; 49 private NamespaceContext namespaceContext; 50 private XPathFactory factory; 51 52 public JAXPXPathExpression() { 53 } 54 55 58 public JAXPXPathExpression(String xpath) throws Exception { 59 this.xpath = xpath; 60 } 61 62 65 public void afterPropertiesSet() throws XPathExpressionException { 66 if (xPathExpression == null) { 67 if (xpath == null) { 68 throw new IllegalArgumentException ("You must specify the xpath property"); 69 } 70 71 if (factory == null) { 72 factory = XPathFactory.newInstance(); 73 } 74 XPath xpathObject = factory.newXPath(); 75 xpathObject.setXPathVariableResolver(variableResolver); 76 if (functionResolver != null) { 77 xpathObject.setXPathFunctionResolver(functionResolver); 78 } 79 if (namespaceContext != null) { 80 xpathObject.setNamespaceContext(namespaceContext); 81 } 82 xPathExpression = xpathObject.compile(xpath); 83 } 84 } 85 86 90 public Object evaluate(MessageExchange exchange, NormalizedMessage message) throws MessagingException { 91 try { 92 afterPropertiesSet(); 93 Object object = getXMLNode(exchange, message); 94 synchronized (this) { 95 variableResolver.setExchange(exchange); 96 variableResolver.setMessage(message); 97 return evaluateXPath(object); 98 } 99 } 100 catch (TransformerException e) { 101 throw new MessagingException(e); 102 } 103 catch (XPathExpressionException e) { 104 throw new MessagingException(e); 105 } 106 catch (ParserConfigurationException e) { 107 throw new MessagingException(e); 108 } 109 catch (IOException e) { 110 throw new MessagingException(e); 111 } 112 catch (SAXException e) { 113 throw new MessagingException(e); 114 } 115 } 116 117 public String getXPath() { 120 return xpath; 121 } 122 123 public void setXPath(String xpath) { 124 this.xpath = xpath; 125 } 126 127 public SourceTransformer getTransformer() { 128 return transformer; 129 } 130 131 public void setTransformer(SourceTransformer transformer) { 132 this.transformer = transformer; 133 } 134 135 public MessageVariableResolver getVariableResolver() { 136 return variableResolver; 137 } 138 139 public void setVariableResolver(MessageVariableResolver variableResolver) { 140 this.variableResolver = variableResolver; 141 } 142 143 public XPathFactory getFactory() { 144 return factory; 145 } 146 147 public void setFactory(XPathFactory factory) { 148 this.factory = factory; 149 } 150 151 public XPathFunctionResolver getFunctionResolver() { 152 return functionResolver; 153 } 154 155 public void setFunctionResolver(XPathFunctionResolver functionResolver) { 156 this.functionResolver = functionResolver; 157 } 158 159 public NamespaceContext getNamespaceContext() { 160 return namespaceContext; 161 } 162 163 public void setNamespaceContext(NamespaceContext namespaceContext) { 164 this.namespaceContext = namespaceContext; 165 } 166 167 protected Object evaluateXPath(Object object) throws XPathExpressionException { 170 return xPathExpression.evaluate(object); 171 } 172 173 protected XPathExpression getXPathExpression() { 174 return xPathExpression; 175 } 176 177 protected Object getXMLNode(MessageExchange exchange, NormalizedMessage message) throws TransformerException , MessagingException, ParserConfigurationException , IOException , SAXException { 178 return transformer.toDOMNode(message); 179 } 180 } 181 | Popular Tags |