KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > drools > dsl > JaxenConditionFactory


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 /**
30  * A condition which uses Jaxen based XPath expressions
31  *
32  * @version $Revision: 426415 $
33  */

34 public class JaxenConditionFactory implements ConditionFactory {
35
36     public Condition[] newCondition(Rule rule, RuleBaseContext ruleBaseContext, Configuration configuration) throws FactoryException {
37         String JavaDoc 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 JavaDoc e) {
47             throw new FactoryException(e);
48         }
49     }
50
51     protected NamespaceContext createNamespaceContext(Configuration configuration) {
52         SimpleNamespaceContext answer = new SimpleNamespaceContext();
53         String JavaDoc[] names = configuration.getAttributeNames();
54         for (int i = 0; i < names.length; i++) {
55             String JavaDoc name = names[i];
56             if (name.equals("xmlns")) {
57                 answer.addNamespace("", configuration.getAttribute(name));
58             }
59             else {
60                 if (name.startsWith("xmlns:")) {
61                     String JavaDoc prefix = name.substring(6);
62                     String JavaDoc uri = configuration.getAttribute(name);
63                     answer.addNamespace(prefix, uri);
64                 }
65             }
66         }
67         return answer;
68     }
69 }
70
Popular Tags