1 17 package org.apache.servicemix.components.drools.dsl; 18 19 import org.apache.servicemix.expression.JaxenXPathExpression; 20 import org.drools.rule.Rule; 21 import org.drools.smf.ConditionFactory; 22 import org.drools.smf.Configuration; 23 import org.drools.smf.FactoryException; 24 import org.drools.spi.Condition; 25 import org.drools.spi.RuleBaseContext; 26 import org.jaxen.NamespaceContext; 27 import org.jaxen.SimpleNamespaceContext; 28 29 34 public class JaxenConditionFactory implements ConditionFactory { 35 36 public Condition[] newCondition(Rule rule, RuleBaseContext ruleBaseContext, Configuration configuration) throws FactoryException { 37 String text = configuration.getText(); 38 if (text == null) { 39 throw new FactoryException("No XPath provided!"); 40 } 41 try { 42 JaxenXPathExpression expression = new JaxenXPathExpression(text); 43 expression.setNamespaceContext(createNamespaceContext(configuration)); 44 return new Condition[]{ new JaxenCondition(rule, expression) }; 45 } 46 catch (Exception e) { 47 throw new FactoryException(e); 48 } 49 } 50 51 protected NamespaceContext createNamespaceContext(Configuration configuration) { 52 SimpleNamespaceContext answer = new SimpleNamespaceContext(); 53 String [] names = configuration.getAttributeNames(); 54 for (int i = 0; i < names.length; i++) { 55 String name = names[i]; 56 if (name.equals("xmlns")) { 57 answer.addNamespace("", configuration.getAttribute(name)); 58 } 59 else { 60 if (name.startsWith("xmlns:")) { 61 String prefix = name.substring(6); 62 String uri = configuration.getAttribute(name); 63 answer.addNamespace(prefix, uri); 64 } 65 } 66 } 67 return answer; 68 } 69 } 70 | Popular Tags |