KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > jelly > expression > xpath > XPathExpression


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

16 package org.apache.commons.jelly.expression.xpath;
17
18 import java.util.Hashtable JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.commons.jelly.JellyContext;
23 import org.apache.commons.jelly.expression.Expression;
24 import org.apache.commons.jelly.expression.ExpressionSupport;
25 import org.apache.commons.jelly.impl.TagScript;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.jaxen.SimpleNamespaceContext;
29 import org.jaxen.VariableContext;
30 import org.jaxen.XPath;
31 import org.jaxen.JaxenException;
32 import org.jaxen.dom4j.Dom4jXPath;
33
34 /** An expression which returns an XPath object.
35   *
36   * @author <a HREF="mailto:jstrachan@apache.org">James Strachan</a>
37   * @version $Revision: 155420 $
38   */

39 public class XPathExpression extends ExpressionSupport implements VariableContext {
40
41     /** The Log to which logging calls will be made. */
42     private Log log = LogFactory.getLog(XPathExpression.class);
43
44     private String JavaDoc text;
45     private Expression xpathExpr;
46     private JellyContext context;
47     private Map JavaDoc uris;
48
49     public XPathExpression() {
50     }
51
52     public XPathExpression(String JavaDoc text,
53                            Expression xpathExpr,
54                            TagScript tagScript) {
55         this.text = text;
56         this.xpathExpr = xpathExpr;
57
58         Map JavaDoc namespaceContext = tagScript.getNamespaceContext();
59
60         this.uris = createUriMap(namespaceContext);
61     }
62
63     public String JavaDoc toString() {
64         return getExpressionText();
65     }
66
67     // Expression interface
68
//-------------------------------------------------------------------------
69
public String JavaDoc getExpressionText() {
70         return this.text;
71     }
72
73     public Object JavaDoc evaluate(JellyContext context) {
74         this.context = context;
75
76         try
77         {
78             XPath xpath = new Dom4jXPath( this.xpathExpr.evaluateAsString( context ) );
79
80             xpath.setVariableContext(this);
81
82             if (log.isDebugEnabled()) {
83                 log.debug( "Setting the namespace context to be: " + uris );
84             }
85
86             xpath.setNamespaceContext( new SimpleNamespaceContext( this.uris ) );
87
88             return xpath;
89         }
90         catch (JaxenException e)
91         {
92             log.error( "Error constructing xpath",
93                        e );
94         }
95
96         return null;
97     }
98
99     // VariableContext interface
100
//-------------------------------------------------------------------------
101
public Object JavaDoc getVariableValue(
102         String JavaDoc namespaceURI,
103         String JavaDoc prefix,
104         String JavaDoc localName) {
105
106         Object JavaDoc value = context.getVariable(localName);
107
108         //log.debug( "Looking up XPath variable of name: " + localName + " value is: " + value );
109

110         return value;
111     }
112
113     // Implementation methods
114
//-------------------------------------------------------------------------
115

116     /**
117      * Factory method to create a synchronized Map of non-null and non-blank
118      * namespace prefixes to namespace URIs
119      */

120     protected Map JavaDoc createUriMap(Map JavaDoc namespaceContext) {
121         // now lets clone the Map but ignoring default or null prefixes
122
Map JavaDoc uris = new Hashtable JavaDoc(namespaceContext.size());
123         for (Iterator JavaDoc iter = namespaceContext.entrySet().iterator(); iter.hasNext(); ) {
124             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) iter.next();
125             String JavaDoc prefix = (String JavaDoc) entry.getKey();
126             if (prefix != null && prefix.length() != 0) {
127                 uris.put(prefix, entry.getValue());
128             }
129         }
130         return uris;
131     }
132 }
133
Popular Tags