KickJava   Java API By Example, From Geeks To Geeks.

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


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.dom.DOMSource JavaDoc;
20 import javax.xml.transform.TransformerFactory JavaDoc;
21
22 import org.apache.avalon.framework.component.Component;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24
25 import com.icl.saxon.Context;
26 import com.icl.saxon.TransformerFactoryImpl;
27 import com.icl.saxon.tinytree.TinyBuilder;
28 import com.icl.saxon.expr.Expression;
29 import com.icl.saxon.expr.StandaloneContext;
30 import com.icl.saxon.expr.Value;
31 import com.icl.saxon.expr.XPathException;
32 import com.icl.saxon.expr.NodeSetValue;
33 import com.icl.saxon.om.DocumentInfo;
34 import com.icl.saxon.om.NamePool;
35 import com.icl.saxon.om.NodeInfo;
36 import com.icl.saxon.om.NodeEnumeration;
37 import org.w3c.dom.Node JavaDoc;
38 import org.w3c.dom.NodeList JavaDoc;
39
40 /**
41  * This class defines the implementation of the {@link XPathProcessor}
42  * component. This implementation depends on Saxon 6.X XSLT processor.
43  * This implementation was tested with Saxon 6.5.2 release.
44  *
45  * To configure it, add the following lines in the
46  * <file>cocoon.xconf</file> file:
47  *
48  * <pre>
49  * &lt;xpath-processor class="org.apache.cocoon.components.xpath.Saxon6ProcessorImpl"&gt;
50  * &lt;/xpath-processor&gt;
51  * </pre>
52  *
53  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
54  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:15 $ $Author: cziegeler $
55  */

56 public class Saxon6ProcessorImpl
57         extends AbstractProcessorImpl
58         implements XPathProcessor, Component, ThreadSafe
59 {
60     private static final TransformerFactory JavaDoc factory = new TransformerFactoryImpl();
61
62     /**
63      * Evaluate XPath expression within a context.
64      *
65      * @param contextNode The context node.
66      * @param str A valid XPath string.
67      * @param resolver a PrefixResolver, used for resolving namespace prefixes
68      * @return expression result as boolean.
69      */

70     public boolean evaluateAsBoolean(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
71     {
72         try
73         {
74             Value value = evaluate(contextNode, str, resolver);
75             if (value == null)
76             {
77                 return false;
78             }
79
80             return value.asBoolean();
81         }
82         catch (final Exception JavaDoc e)
83         {
84             if (getLogger().isDebugEnabled()) {
85                 getLogger().debug("Failed to evaluate '" + str + "'", e);
86             }
87
88             // ignore it
89
return false;
90         }
91     }
92
93     /**
94      * Evaluate XPath expression within a context.
95      *
96      * @param contextNode The context node.
97      * @param str A valid XPath string.
98      * @param resolver a PrefixResolver, used for resolving namespace prefixes
99      * @return expression result as number.
100      */

101     public Number JavaDoc evaluateAsNumber(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
102     {
103         try
104         {
105             Value value = evaluate(contextNode, str, resolver);
106             if (value == null)
107             {
108                 return null;
109             }
110
111             return new Double JavaDoc(value.asNumber());
112         }
113         catch (final Exception JavaDoc e)
114         {
115             if (getLogger().isDebugEnabled()) {
116                 getLogger().debug("Failed to evaluate '" + str + "'", e);
117             }
118
119             // ignore it
120
return null;
121         }
122     }
123
124     /**
125      * Evaluate XPath expression within a context.
126      *
127      * @param contextNode The context node.
128      * @param str A valid XPath string.
129      * @param resolver a PrefixResolver, used for resolving namespace prefixes
130      * @return expression result as string.
131      */

132     public String JavaDoc evaluateAsString(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver) {
133         try
134         {
135             Value value = evaluate(contextNode, str, resolver);
136             if (value == null)
137             {
138                 return null;
139             }
140
141             return value.asString();
142         }
143         catch (final Exception JavaDoc e)
144         {
145             if (getLogger().isDebugEnabled()) {
146                 getLogger().debug("Failed to evaluate '" + str + "'", e);
147             }
148
149             // ignore it
150
return null;
151         }
152     }
153
154     public Node JavaDoc selectSingleNode(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
155     {
156         try
157         {
158             Value value = evaluate(contextNode, str, resolver);
159             if (value == null || value.getDataType() != Value.NODESET)
160             {
161                 return null;
162             }
163
164             return (Node JavaDoc)((NodeSetValue)value).getFirst();
165         }
166         catch (final Exception JavaDoc e)
167         {
168             if (getLogger().isDebugEnabled()) {
169                 getLogger().debug("Failed to evaluate '" + str + "'", e);
170             }
171
172             // ignore it
173
return null;
174         }
175     }
176
177     public NodeList JavaDoc selectNodeList(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
178     {
179         try
180         {
181             Value value = evaluate(contextNode, str, resolver);
182             if (value.getDataType() != Value.NODESET)
183             {
184                 return null;
185             }
186
187             NodeSetValue nodeset = (NodeSetValue)value;
188             NodeEnumeration enumeration = nodeset.enumerate();
189             Node JavaDoc[] nodes = new Node JavaDoc[nodeset.getCount()];
190             for (int i = 0; i < nodes.length; i++)
191             {
192                 nodes[i] = (Node JavaDoc)enumeration.nextElement();
193             }
194
195             return new NodeListImpl(nodes);
196         } catch (final Exception JavaDoc e) {
197             if (getLogger().isDebugEnabled()) {
198                 getLogger().debug("Failed to evaluate '" + str + "'", e);
199             }
200
201             // ignore it
202
return null;
203         }
204     }
205
206     private Value evaluate(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver)
207     {
208         try
209         {
210             if (!(contextNode instanceof NodeInfo))
211             {
212                 getLogger().debug("Input tree is not SAXON TinyTree, converting");
213
214                 DOMSource JavaDoc source = new DOMSource JavaDoc(contextNode);
215                 TinyBuilder result = new TinyBuilder();
216                 factory.newTransformer().transform(source, result);
217                 contextNode = (Node JavaDoc)result.getCurrentDocument();
218             }
219
220             DocumentInfo doc = ((NodeInfo)contextNode).getDocumentRoot();
221             NamePool pool = doc.getNamePool();
222             if (pool == null)
223             {
224                 pool = NamePool.getDefaultNamePool();
225                 doc.setNamePool(pool);
226             }
227             Expression expression = Expression.make(str, new Saxon6Context(pool, resolver));
228
229             Context context = new Context();
230             context.setContextNode((NodeInfo)contextNode);
231             context.setPosition(1);
232             context.setLast(1);
233
234             return expression.evaluate(context);
235         }
236         catch (final Exception JavaDoc e)
237         {
238             if (getLogger().isDebugEnabled()) {
239                 getLogger().debug("Failed to evaluate '" + str + "'", e);
240             }
241
242             // ignore it
243
return null;
244         }
245     }
246
247     private class Saxon6Context extends StandaloneContext
248     {
249         private final PrefixResolver resolver;
250
251         public Saxon6Context(NamePool namePool, PrefixResolver resolver)
252         {
253             super(namePool);
254             this.resolver = resolver;
255         }
256
257         public String JavaDoc getURIForPrefix(String JavaDoc prefix) throws XPathException
258         {
259             return resolver.prefixToNamespace(prefix);
260         }
261     }
262 }
263
Popular Tags