KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > excalibur > xml > xpath > XPathProcessorImpl


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

17 package org.apache.excalibur.xml.xpath;
18
19 import javax.xml.transform.TransformerException JavaDoc;
20
21 import org.apache.avalon.framework.component.Component;
22 import org.apache.avalon.framework.configuration.Configurable;
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.thread.ThreadSafe;
26 import org.apache.xpath.XPathAPI;
27 import org.apache.xpath.objects.XObject;
28
29 import org.w3c.dom.Node JavaDoc;
30 import org.w3c.dom.NodeList JavaDoc;
31
32 /**
33  * This class defines the implementation of the {@link XPathProcessor}
34  * component.
35  *
36  * To configure it, add the following lines in the
37  * <file>cocoon.xconf</file> file:
38  *
39  * <pre>
40  * &lt;xpath-processor class="org.apache.cocoon.components.xpath.XPathProcessorImpl"&gt;
41  * &lt;/xpath-processor&gt;
42  * </pre>
43  *
44  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
45  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:15 $ $Author: cziegeler $
46  */

47 public final class XPathProcessorImpl
48         extends AbstractProcessorImpl
49         implements XPathProcessor, Configurable, Component, ThreadSafe
50 {
51     private String JavaDoc m_baseURI;
52
53     public void configure( Configuration configuration ) throws ConfigurationException
54     {
55         super.configure(configuration);
56         final Configuration namespaceMappings = configuration.getChild( "namespace-mappings", true );
57         m_baseURI = namespaceMappings.getAttribute( "base-uri", null );
58     }
59
60     /**
61      * Evaluate XPath expression within a context.
62      *
63      * @param contextNode The context node.
64      * @param str A valid XPath string.
65      * @param resolver a PrefixResolver, used for resolving namespace prefixes
66      * @return expression result as boolean.
67      */

68     public boolean evaluateAsBoolean(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
69     {
70         try
71         {
72             final XObject result = XPathAPI.eval( contextNode, str, new XalanResolver(resolver) );
73             return result.bool();
74         }
75         catch( final TransformerException JavaDoc e )
76         {
77             if (getLogger().isDebugEnabled()) {
78                 getLogger().debug("Failed to evaluate '" + str + "'", e);
79             }
80
81             // ignore it
82
return false;
83         }
84     }
85
86     /**
87      * Evaluate XPath expression within a context.
88      *
89      * @param contextNode The context node.
90      * @param str A valid XPath string.
91      * @param resolver a PrefixResolver, used for resolving namespace prefixes
92      * @return expression result as number.
93      */

94     public Number JavaDoc evaluateAsNumber(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
95     {
96         try
97         {
98             final XObject result = XPathAPI.eval( contextNode, str, new XalanResolver(resolver) );
99             return new Double JavaDoc( result.num() );
100         }
101         catch( final TransformerException JavaDoc e )
102         {
103             if (getLogger().isDebugEnabled()) {
104                 getLogger().debug("Failed to evaluate '" + str + "'", e);
105             }
106
107             // ignore it
108
return null;
109         }
110     }
111
112     /**
113      * Evaluate XPath expression within a context.
114      *
115      * @param contextNode The context node.
116      * @param str A valid XPath string.
117      * @param resolver a PrefixResolver, used for resolving namespace prefixes
118      * @return expression result as string.
119      */

120     public String JavaDoc evaluateAsString(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
121     {
122         try
123         {
124             final XObject result = XPathAPI.eval( contextNode, str, new XalanResolver(resolver) );
125             return result.str();
126         }
127         catch( final TransformerException JavaDoc e )
128         {
129             if (getLogger().isDebugEnabled()) {
130                 getLogger().debug("Failed to evaluate '" + str + "'", e);
131             }
132
133             // ignore it
134
return null;
135         }
136     }
137
138     /**
139      * Use an XPath string to select a single node.
140      *
141      * @param contextNode The node to start searching from.
142      * @param str A valid XPath string.
143      * @param resolver a PrefixResolver, used for resolving namespace prefixes
144      * @return The first node found that matches the XPath, or null.
145      */

146     public Node JavaDoc selectSingleNode(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
147     {
148         try
149         {
150             final XObject result = XPathAPI.eval( contextNode, str, new XalanResolver(resolver) );
151             return result.nodeset().nextNode();
152         }
153         catch( final TransformerException JavaDoc e )
154         {
155             if (getLogger().isDebugEnabled()) {
156                 getLogger().debug("Failed to evaluate '" + str + "'", e);
157             }
158
159             // ignore it
160
return null;
161         }
162     }
163
164     /**
165      * Use an XPath string to select a nodelist.
166      *
167      * @param contextNode The node to start searching from.
168      * @param str A valid XPath string.
169      * @param resolver a PrefixResolver, used for resolving namespace prefixes
170      * @return A List, should never be null.
171      */

172     public NodeList JavaDoc selectNodeList(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
173     {
174         try
175         {
176             final XObject result = XPathAPI.eval( contextNode, str, new XalanResolver(resolver) );
177             return result.nodelist();
178         }
179         catch( final TransformerException JavaDoc e )
180         {
181             if (getLogger().isDebugEnabled()) {
182                 getLogger().debug("Failed to evaluate '" + str + "'", e);
183             }
184
185             // ignore it
186
return new EmptyNodeList();
187         }
188     }
189
190     /**
191      * A Xalan-specific wrapper for the PrefixResolver.
192      */

193     private final class XalanResolver implements org.apache.xml.utils.PrefixResolver {
194         private final PrefixResolver resolver;
195
196         public XalanResolver(PrefixResolver resolver) {
197             this.resolver = resolver;
198         }
199
200         public String JavaDoc getNamespaceForPrefix(String JavaDoc prefix)
201         {
202             return resolver.prefixToNamespace(prefix);
203         }
204
205         public String JavaDoc getNamespaceForPrefix(String JavaDoc prefix, Node JavaDoc context)
206         {
207             return resolver.prefixToNamespace(prefix);
208         }
209
210         public String JavaDoc getBaseIdentifier()
211         {
212             return m_baseURI;
213         }
214
215         public boolean handlesNullPrefixes()
216         {
217             return false;
218         }
219     }
220 }
221
Popular Tags