KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
20
21 import org.apache.avalon.framework.component.Component;
22 import org.apache.avalon.framework.thread.ThreadSafe;
23
24 import org.jaxen.NamespaceContext;
25 import org.jaxen.dom.DOMXPath;
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28
29 /**
30  * This class defines the implementation of the {@link XPathProcessor}
31  * component.
32  *
33  * To configure it, add the following lines in the
34  * <file>cocoon.xconf</file> file:
35  *
36  * <pre>
37  * &lt;xpath-processor class="org.apache.cocoon.components.xpath.JaxenProcessorImpl"&gt;
38  * &lt;/xpath-processor&gt;
39  * </pre>
40  *
41  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
42  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:15 $ $Author: cziegeler $
43  */

44 public final class JaxenProcessorImpl
45         extends AbstractProcessorImpl
46         implements XPathProcessor, Component, ThreadSafe
47 {
48     /**
49      * Evaluate XPath expression within a context.
50      *
51      * @param contextNode The context node.
52      * @param str A valid XPath string.
53      * @param resolver a PrefixResolver, used for resolving namespace prefixes
54      * @return expression result as boolean.
55      */

56     public boolean evaluateAsBoolean(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
57     {
58         try
59         {
60             final DOMXPath path = new DOMXPath( str );
61             path.setNamespaceContext( new JaxenResolver(resolver) );
62             return path.booleanValueOf( contextNode );
63         }
64         catch( final Exception JavaDoc e )
65         {
66             if (getLogger().isDebugEnabled()) {
67                 getLogger().debug("Failed to evaluate '" + str + "'", e);
68             }
69
70             // ignore it
71
return false;
72         }
73     }
74
75     /**
76      * Evaluate XPath expression within a context.
77      *
78      * @param contextNode The context node.
79      * @param str A valid XPath string.
80      * @param resolver a PrefixResolver, used for resolving namespace prefixes
81      * @return expression result as number.
82      */

83     public Number JavaDoc evaluateAsNumber(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
84     {
85         try
86         {
87             final DOMXPath path = new DOMXPath( str );
88             path.setNamespaceContext( new JaxenResolver(resolver) );
89             return path.numberValueOf( contextNode );
90         }
91         catch( final Exception JavaDoc e )
92         {
93             if (getLogger().isDebugEnabled()) {
94                 getLogger().debug("Failed to evaluate '" + str + "'", e);
95             }
96
97             // ignore it
98
return null;
99         }
100     }
101
102     /**
103      * Evaluate XPath expression within a context.
104      *
105      * @param contextNode The context node.
106      * @param str A valid XPath string.
107      * @param resolver a PrefixResolver, used for resolving namespace prefixes
108      * @return expression result as string.
109      */

110     public String JavaDoc evaluateAsString(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
111     {
112         try
113         {
114             final DOMXPath path = new DOMXPath( str );
115             path.setNamespaceContext( new JaxenResolver(resolver) );
116             return path.stringValueOf( contextNode );
117         }
118         catch( final Exception JavaDoc e )
119         {
120             if (getLogger().isDebugEnabled()) {
121                 getLogger().debug("Failed to evaluate '" + str + "'", e);
122             }
123
124             // ignore it
125
return null;
126         }
127     }
128
129     /**
130      * Use an XPath string to select a single node.
131      *
132      * @param contextNode The node to start searching from.
133      * @param str A valid XPath string.
134      * @param resolver a PrefixResolver, used for resolving namespace prefixes
135      * @return The first node found that matches the XPath, or null.
136      */

137     public Node JavaDoc selectSingleNode(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
138     {
139         try
140         {
141             final DOMXPath path = new DOMXPath( str );
142             path.setNamespaceContext( new JaxenResolver(resolver) );
143             return (Node JavaDoc)path.selectSingleNode( contextNode );
144         }
145         catch( final Exception JavaDoc e )
146         {
147             if (getLogger().isDebugEnabled()) {
148                 getLogger().debug("Failed to evaluate '" + str + "'", e);
149             }
150
151             // ignore it
152
return null;
153         }
154     }
155
156     /**
157      * Use an XPath string to select a nodelist.
158      *
159      * @param contextNode The node to start searching from.
160      * @param str A valid XPath string.
161      * @param resolver a PrefixResolver, used for resolving namespace prefixes
162      * @return A List, should never be null.
163      */

164     public NodeList JavaDoc selectNodeList(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
165     {
166         try
167         {
168             final DOMXPath path = new DOMXPath( str );
169             path.setNamespaceContext( new JaxenResolver(resolver) );
170             final List JavaDoc list = path.selectNodes( contextNode );
171             return new SimpleNodeList( list );
172         }
173         catch( final Exception JavaDoc e )
174         {
175             if (getLogger().isDebugEnabled()) {
176                 getLogger().debug("Failed to evaluate '" + str + "'", e);
177             }
178
179             // ignore it
180
return new EmptyNodeList();
181         }
182     }
183
184     /**
185      * A Jaxen-specific wrapper for the PrefixResolver.
186      */

187     private static class JaxenResolver implements NamespaceContext
188     {
189         private final PrefixResolver resolver;
190
191         public JaxenResolver(PrefixResolver resolver)
192         {
193             this.resolver = resolver;
194         }
195
196         public String JavaDoc translateNamespacePrefixToUri(String JavaDoc prefix)
197         {
198             return resolver.prefixToNamespace(prefix);
199         }
200     }
201 }
202
Popular Tags