1 10 11 package org.mule.routing.filters.xml; 12 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 import org.apache.commons.jxpath.AbstractFactory; 17 import org.apache.commons.jxpath.JXPathContext; 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import org.dom4j.Document; 21 import org.dom4j.DocumentException; 22 import org.dom4j.DocumentHelper; 23 import org.dom4j.XPath; 24 import org.mule.umo.UMOFilter; 25 import org.mule.umo.UMOMessage; 26 import org.mule.util.StringMessageUtils; 27 28 32 public class JXPathFilter implements UMOFilter 33 { 34 35 protected transient Log logger = LogFactory.getLog(getClass()); 36 37 private String expression; 38 private String expectedValue; 39 private Map namespaces = null; 40 private Map contextProperties = null; 41 private AbstractFactory factory; 42 private boolean lenient = true; 43 44 public JXPathFilter() 45 { 46 super(); 47 } 48 49 public JXPathFilter(String expression) 50 { 51 this.expression = expression; 52 } 53 54 public JXPathFilter(String expression, String expectedValue) 55 { 56 this.expression = expression; 57 this.expectedValue = expectedValue; 58 } 59 60 public boolean accept(UMOMessage obj) 61 { 62 return accept(obj.getPayload()); 63 } 64 65 private boolean accept(Object obj) 66 { 67 if (obj == null) 68 { 69 logger.warn("Applying JXPathFilter to null object."); 70 return false; 71 } 72 if (expression == null) 73 { 74 logger.warn("Expression for JXPathFilter is not set."); 75 return false; 76 } 77 if (expectedValue == null) 78 { 79 if (expression.endsWith("= null") || expression.endsWith("=null")) 81 { 82 expectedValue = "null"; 83 expression = expression.substring(0, expression.lastIndexOf("=")); 84 } 85 else 86 { 87 if (logger.isInfoEnabled()) 88 { 89 logger.info("Expected value for JXPathFilter is not set, using 'true' by default"); 90 } 91 expectedValue = Boolean.TRUE.toString(); 92 } 93 } 94 95 Object xpathResult = null; 96 boolean accept = false; 97 98 if (obj instanceof Document) 100 { 101 if (namespaces == null) 102 { 103 xpathResult = ((Document)obj).valueOf(expression); 105 } 106 else 107 { 108 XPath xpath = DocumentHelper.createXPath(expression); 110 xpath.setNamespaceURIs(namespaces); 111 xpathResult = xpath.valueOf(obj); 112 } 113 114 } 115 else if (obj instanceof String ) 117 { 118 try 119 { 120 return accept(DocumentHelper.parseText((String )obj)); 121 } 122 catch (DocumentException e) 123 { 124 logger.warn("JXPathFilter unable to parse XML document: " + e.getMessage(), e); 125 if (logger.isDebugEnabled()) 126 logger.debug("XML = " + StringMessageUtils.truncate((String )obj, 200, false)); 127 return false; 128 } 129 } 130 else 132 { 133 if (logger.isDebugEnabled()) 134 { 135 logger.debug("Passing object of type " + obj.getClass().getName() + " to JXPathContext"); 136 } 137 JXPathContext context = JXPathContext.newContext(obj); 138 initialise(context); 139 xpathResult = context.getValue(expression); 140 } 141 142 if (logger.isDebugEnabled()) 143 { 144 logger.debug("JXPathFilter Expression result = '" + xpathResult + "' - Expected value = '" 145 + expectedValue + "'"); 146 } 147 if (xpathResult != null) 149 { 150 accept = xpathResult.toString().equals(expectedValue); 151 } 152 else 153 { 154 if (expectedValue.equals("null")) 156 { 157 accept = true; 158 } 159 else 161 { 162 logger.warn("JXPathFilter expression evaluates to null: " + expression); 163 } 164 } 165 166 if (logger.isDebugEnabled()) 167 { 168 logger.debug("JXPathFilter accept object : " + accept); 169 } 170 171 return accept; 172 } 173 174 180 protected void initialise(JXPathContext context) 181 { 182 Map.Entry entry = null; 183 if (namespaces != null) 184 { 185 if (logger.isDebugEnabled()) 186 { 187 logger.debug("Initializing JXPathContext with namespaces: " + namespaces); 188 } 189 190 for (Iterator iterator = namespaces.entrySet().iterator(); iterator.hasNext();) 191 { 192 entry = (Map.Entry )iterator.next(); 193 context.registerNamespace(entry.getKey().toString(), entry.getValue().toString()); 194 } 195 } 196 197 if (contextProperties != null) 198 { 199 if (logger.isDebugEnabled()) 200 { 201 logger.debug("Initializing JXPathContext with properties: " + contextProperties); 202 } 203 204 for (Iterator iterator = contextProperties.entrySet().iterator(); iterator.hasNext();) 205 { 206 entry = (Map.Entry )iterator.next(); 207 context.setValue(entry.getKey().toString(), entry.getValue()); 208 } 209 } 210 211 if (factory != null) 212 { 213 context.setFactory(factory); 214 } 215 216 context.setLenient(lenient); 217 } 218 219 222 public String getExpression() 223 { 224 return expression; 225 } 226 227 230 public void setExpression(String expression) 231 { 232 this.expression = expression; 233 } 234 235 238 public String getExpectedValue() 239 { 240 return expectedValue; 241 } 242 243 246 public void setExpectedValue(String expectedValue) 247 { 248 this.expectedValue = expectedValue; 249 } 250 251 255 public String getValue() 256 { 257 return getExpectedValue(); 258 } 259 260 265 public void setValue(String value) 266 { 267 setExpectedValue(value); 268 } 269 270 public Map getNamespaces() 271 { 272 return namespaces; 273 } 274 275 public void setNamespaces(Map namespaces) 276 { 277 this.namespaces = namespaces; 278 } 279 280 public Map getContextProperties() 281 { 282 return contextProperties; 283 } 284 285 public void setContextProperties(Map contextProperties) 286 { 287 this.contextProperties = contextProperties; 288 } 289 290 public AbstractFactory getFactory() 291 { 292 return factory; 293 } 294 295 public void setFactory(AbstractFactory factory) 296 { 297 this.factory = factory; 298 } 299 300 public boolean isLenient() 301 { 302 return lenient; 303 } 304 305 public void setLenient(boolean lenient) 306 { 307 this.lenient = lenient; 308 } 309 } 310 | Popular Tags |