KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************************
2  * Copyright (C) The Apache Software Foundation. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * This software is published under the terms of the Apache Software License *
5  * version 1.1, a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 package org.apache.avalon.excalibur.xml.xpath;
9
10 import org.apache.avalon.framework.thread.ThreadSafe;
11 import org.apache.avalon.framework.component.Component;
12 import org.apache.avalon.framework.logger.AbstractLoggable;
13 import org.w3c.dom.Node JavaDoc;
14 import org.w3c.dom.NodeList JavaDoc;
15 import org.apache.xpath.XPathAPI;
16
17 /**
18  * This class defines the implementation of the {@link XPathProcessor}
19  * component.
20  *
21  * To configure it, add the following lines in the
22  * <file>cocoon.xconf</file> file:
23  *
24  * <pre>
25  * &lt;xpath-processor class="org.apache.cocoon.components.xpath.XPathProcessorImpl"&gt;
26  * &lt;/xpath-processor&gt;
27  * </pre>
28  *
29  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
30  * @version CVS $Revision: 1.5 $ $Date: 2002/01/08 07:57:40 $ $Author: cziegeler $
31  */

32 public class XPathProcessorImpl
33   extends AbstractLoggable
34   implements XPathProcessor, ThreadSafe
35 {
36     /**
37      * Use an XPath string to select a single node. XPath namespace
38      * prefixes are resolved from the context node, which may not
39      * be what you want (see the next method).
40      *
41      * @param contextNode The node to start searching from.
42      * @param str A valid XPath string.
43      * @return The first node found that matches the XPath, or null.
44      */

45     public Node JavaDoc selectSingleNode(Node JavaDoc contextNode, String JavaDoc str)
46     {
47         try {
48             return XPathAPI.selectSingleNode(contextNode, str);
49         } catch (javax.xml.transform.TransformerException JavaDoc e){
50             return null;
51         }
52     }
53
54       /**
55        * Use an XPath string to select a nodelist.
56        * XPath namespace prefixes are resolved from the contextNode.
57        *
58        * @param contextNode The node to start searching from.
59        * @param str A valid XPath string.
60        * @return A NodeList, should never be null.
61        */

62     public NodeList JavaDoc selectNodeList(Node JavaDoc contextNode, String JavaDoc str)
63     {
64       try {
65           return XPathAPI.selectNodeList(contextNode, str);
66       } catch (javax.xml.transform.TransformerException JavaDoc e){
67           return new NodeList JavaDoc(){
68               public Node JavaDoc item(int index) { return null;}
69               public int getLength(){return 0;}
70           };
71       }
72     }
73 }
74
Popular Tags