1 17 package org.apache.servicemix.expression; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 22 import org.jaxen.FunctionContext; 23 import org.jaxen.JaxenException; 24 import org.jaxen.NamespaceContext; 25 import org.jaxen.XPath; 26 import org.jaxen.dom.DOMXPath; 27 import org.springframework.beans.factory.InitializingBean; 28 import org.w3c.dom.Node ; 29 import org.xml.sax.SAXException ; 30 31 import javax.jbi.messaging.MessageExchange; 32 import javax.jbi.messaging.MessagingException; 33 import javax.jbi.messaging.NormalizedMessage; 34 import javax.xml.parsers.ParserConfigurationException ; 35 import javax.xml.transform.TransformerException ; 36 37 import java.io.IOException ; 38 39 40 45 public class JaxenXPathExpression implements Expression, InitializingBean { 46 private static final transient Log log = LogFactory.getLog(JaxenXPathExpression.class); 47 48 private String xpath; 49 private SourceTransformer transformer = new SourceTransformer(); 50 private JaxenVariableContext variableContext = new JaxenVariableContext(); 51 private XPath xpathObject; 52 private NamespaceContext namespaceContext; 53 private FunctionContext functionContext; 54 55 public JaxenXPathExpression() { 56 } 57 58 62 public JaxenXPathExpression(String xpath) throws Exception { 63 this.xpath = xpath; 64 afterPropertiesSet(); 65 } 66 67 public void afterPropertiesSet() throws Exception { 68 if (xpathObject == null) { 69 if (xpath == null) { 70 throw new IllegalArgumentException ("You must specify the xpath property"); 71 } 72 xpathObject = createXPath(xpath); 73 xpathObject.setVariableContext(variableContext); 74 if (namespaceContext != null) { 75 xpathObject.setNamespaceContext(namespaceContext); 76 } 77 if (functionContext != null) { 78 xpathObject.setFunctionContext(functionContext); 79 } 80 } 81 } 82 83 public Object evaluate(MessageExchange exchange, NormalizedMessage message) throws MessagingException { 84 try { 85 Object object = getXMLNode(exchange, message); 86 if (object == null) { 87 return null; 88 } 89 synchronized (this) { 90 variableContext.setExchange(exchange); 91 variableContext.setMessage(message); 92 return evaluateXPath(object); 93 } 94 } 95 catch (TransformerException e) { 96 throw new MessagingException(e); 97 } 98 catch (JaxenException e) { 99 throw new MessagingException(e); 100 } 101 catch (ParserConfigurationException e) { 102 throw new MessagingException(e); 103 } 104 catch (IOException e) { 105 throw new MessagingException(e); 106 } 107 catch (SAXException e) { 108 throw new MessagingException(e); 109 } 110 } 111 112 public boolean matches(MessageExchange exchange, NormalizedMessage message) throws MessagingException { 113 try { 114 Object object = getXMLNode(exchange, message); 115 if (object == null) { 116 return false; 117 } 118 synchronized (this) { 119 variableContext.setExchange(exchange); 120 variableContext.setMessage(message); 121 return evaluateXPathAsBoolean(object); 122 } 123 } 124 catch (TransformerException e) { 125 throw new MessagingException(e); 126 } 127 catch (JaxenException e) { 128 throw new MessagingException(e); 129 } 130 catch (ParserConfigurationException e) { 131 throw new MessagingException(e); 132 } 133 catch (IOException e) { 134 throw new MessagingException(e); 135 } 136 catch (SAXException e) { 137 throw new MessagingException(e); 138 } 139 } 140 141 public XPath getXpathObject() { 144 return xpathObject; 145 } 146 147 public void setXpathObject(XPath xpathObject) { 148 this.xpathObject = xpathObject; 149 } 150 151 public String getXpath() { 152 return xpath; 153 } 154 155 public void setXpath(String xpath) { 156 this.xpath = xpath; 157 } 158 159 public SourceTransformer getTransformer() { 160 return transformer; 161 } 162 163 public void setTransformer(SourceTransformer transformer) { 164 this.transformer = transformer; 165 } 166 167 public JaxenVariableContext getVariableContext() { 168 return variableContext; 169 } 170 171 public void setVariableContext(JaxenVariableContext variableContext) { 172 this.variableContext = variableContext; 173 } 174 175 public NamespaceContext getNamespaceContext() { 176 return namespaceContext; 177 } 178 179 public void setNamespaceContext(NamespaceContext namespaceContext) { 180 this.namespaceContext = namespaceContext; 181 } 182 183 public FunctionContext getFunctionContext() { 184 return functionContext; 185 } 186 187 public void setFunctionContext(FunctionContext functionContext) { 188 this.functionContext = functionContext; 189 } 190 191 protected XPath createXPath(String xpath) throws JaxenException { 194 return new DOMXPath(xpath); 195 } 196 197 protected Object evaluateXPath(Object object) throws JaxenException { 198 return xpathObject.evaluate(object); 199 } 200 201 protected boolean evaluateXPathAsBoolean(Object object) throws JaxenException { 202 return xpathObject.booleanValueOf(object); 203 } 204 205 206 protected Object getXMLNode(MessageExchange exchange, NormalizedMessage message) throws TransformerException , MessagingException, ParserConfigurationException , IOException , SAXException { 207 Node node = null; 208 if (message != null) { 209 node = transformer.toDOMNode(message); 210 } 211 else { 212 log.warn("Null message for exchange: " + exchange); 213 } 214 if (node == null) { 215 node = transformer.createDocument(); 217 } 218 return node; 219 } 220 } 221 | Popular Tags |