KickJava   Java API By Example, From Geeks To Geeks.

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


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.jaxen.dom.XPath;
16
17
18 import java.util.List JavaDoc;
19
20 /**
21  * This class defines the implementation of the {@link XPathProcessor}
22  * component.
23  *
24  * To configure it, add the following lines in the
25  * <file>cocoon.xconf</file> file:
26  *
27  * <pre>
28  * &lt;xpath-processor class="org.apache.cocoon.components.xpath.JaxenProcessorImpl"&gt;
29  * &lt;/xpath-processor&gt;
30  * </pre>
31  *
32  * @author <a HREF="mailto:dims@yahoo.com">Davanum Srinivas</a>
33  * @version CVS $Revision: 1.5 $ $Date: 2002/01/08 07:57:40 $ $Author: cziegeler $
34  */

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

48     public Node JavaDoc selectSingleNode(Node JavaDoc contextNode, String JavaDoc str)
49     {
50         try {
51             XPath path = new XPath(str);
52             return (Node JavaDoc)path.selectSingleNode((Object JavaDoc)contextNode);
53         } catch (Exception JavaDoc e){
54             // ignore it
55
}
56         return null;
57     }
58
59       /**
60        * Use an XPath string to select a nodelist.
61        * XPath namespace prefixes are resolved from the contextNode.
62        *
63        * @param contextNode The node to start searching from.
64        * @param str A valid XPath string.
65        * @return A NodeList, should never be null.
66        */

67     public NodeList JavaDoc selectNodeList(Node JavaDoc contextNode, String JavaDoc str)
68     {
69         try {
70             XPath path = new XPath(str);
71             List JavaDoc list = path.selectNodes((Object JavaDoc)contextNode);
72             return new NodeListEx(list);
73         } catch (Exception JavaDoc e){
74             // ignore it
75
}
76         return new NodeListEx();
77     }
78
79     class NodeListEx implements NodeList JavaDoc{
80         List JavaDoc list = null;
81         NodeListEx(){
82         }
83         NodeListEx(List JavaDoc l){
84             list = l;
85         }
86         public Node JavaDoc item(int index) {
87             if(list==null)
88                 return null;
89             return (Node JavaDoc)list.get(index);
90         }
91         public int getLength(){
92             if(list==null)
93                 return 0;
94             return list.size();
95         }
96     }
97 }
98
Popular Tags