KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
20
21 import org.apache.avalon.framework.configuration.Configurable;
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.logger.AbstractLogEnabled;
25
26 import org.w3c.dom.Node JavaDoc;
27 import org.w3c.dom.NodeList JavaDoc;
28
29 /**
30  * This class defines base class for the implementations of the
31  * {@link XPathProcessor} component.
32  * Provides implementation of the {@link PrefixResolver} and common
33  * implementation of five selectXXX methods.
34  *
35  * @author <a HREF="mailto:dev@avalon.apache.org">Avalon Development Team</a>
36  * @version CVS $Revision: 1.4 $ $Date: 2004/02/28 11:47:15 $ $Author: cziegeler $
37  */

38 public abstract class AbstractProcessorImpl
39         extends AbstractLogEnabled
40         implements XPathProcessor, Configurable, PrefixResolver
41 {
42     private final HashMap JavaDoc m_mappings = new HashMap JavaDoc();
43
44     public void configure( Configuration configuration ) throws ConfigurationException
45     {
46         final Configuration namespaceMappings = configuration.getChild( "namespace-mappings", true );
47         final Configuration[] namespaces = namespaceMappings.getChildren( "namespace" );
48         for( int i = 0; i < namespaces.length; i++ )
49         {
50             final String JavaDoc prefix = namespaces[ i ].getAttribute( "prefix" );
51             final String JavaDoc uri = namespaces[ i ].getAttribute( "uri" );
52             m_mappings.put( prefix, uri );
53         }
54     }
55
56     /**
57      * Use an XPath string to select a single node. XPath namespace
58      * prefixes are resolved from the context node, which may not
59      * be what you want (see the next method).
60      *
61      * @param contextNode The node to start searching from.
62      * @param str A valid XPath string.
63      * @return The first node found that matches the XPath, or null.
64      */

65     public Node JavaDoc selectSingleNode( final Node JavaDoc contextNode,
66                                   final String JavaDoc str )
67     {
68         return selectSingleNode(contextNode, str, this);
69     }
70
71     /**
72      * Use an XPath string to select a nodelist.
73      * XPath namespace prefixes are resolved from the contextNode.
74      *
75      * @param contextNode The node to start searching from.
76      * @param str A valid XPath string.
77      * @return A NodeList, should never be null.
78      */

79     public NodeList JavaDoc selectNodeList( final Node JavaDoc contextNode,
80                                     final String JavaDoc str )
81     {
82         return selectNodeList(contextNode, str, this);
83     }
84
85     /**
86      * Evaluate XPath expression within a context.
87      *
88      * @param contextNode The context node.
89      * @param str A valid XPath string.
90      * @return expression result as boolean.
91      */

92     public boolean evaluateAsBoolean( Node JavaDoc contextNode, String JavaDoc str )
93     {
94         return evaluateAsBoolean(contextNode, str, this);
95     }
96
97     /**
98      * Evaluate XPath expression within a context.
99      *
100      * @param contextNode The context node.
101      * @param str A valid XPath string.
102      * @return expression result as number.
103      */

104     public Number JavaDoc evaluateAsNumber( Node JavaDoc contextNode, String JavaDoc str )
105     {
106         return evaluateAsNumber(contextNode, str, this);
107     }
108
109     /**
110      * Evaluate XPath expression within a context.
111      *
112      * @param contextNode The context node.
113      * @param str A valid XPath string.
114      * @return expression result as string.
115      */

116     public String JavaDoc evaluateAsString( Node JavaDoc contextNode, String JavaDoc str )
117     {
118         return evaluateAsString(contextNode, str, this);
119     }
120
121     /**
122      * Evaluate XPath expression within a context.
123      *
124      * @param contextNode The context node.
125      * @param str A valid XPath string.
126      * @param resolver a PrefixResolver, used for resolving namespace prefixes
127      * @return expression result as boolean.
128      */

129     public abstract boolean evaluateAsBoolean(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver);
130
131     /**
132      * Evaluate XPath expression within a context.
133      *
134      * @param contextNode The context node.
135      * @param str A valid XPath string.
136      * @param resolver a PrefixResolver, used for resolving namespace prefixes
137      * @return expression result as number.
138      */

139     public abstract Number JavaDoc evaluateAsNumber(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver);
140
141     /**
142      * Evaluate XPath expression within a context.
143      *
144      * @param contextNode The context node.
145      * @param str A valid XPath string.
146      * @param resolver a PrefixResolver, used for resolving namespace prefixes
147      * @return expression result as string.
148      */

149     public abstract String JavaDoc evaluateAsString(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver);
150
151     /**
152      * Use an XPath string to select a single node.
153      *
154      * @param contextNode The node to start searching from.
155      * @param str A valid XPath string.
156      * @param resolver a PrefixResolver, used for resolving namespace prefixes
157      * @return The first node found that matches the XPath, or null.
158      */

159     public abstract Node JavaDoc selectSingleNode(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver);
160
161     /**
162      * Use an XPath string to select a nodelist.
163      *
164      * @param contextNode The node to start searching from.
165      * @param str A valid XPath string.
166      * @param resolver a PrefixResolver, used for resolving namespace prefixes
167      * @return A List, should never be null.
168      */

169     public abstract NodeList JavaDoc selectNodeList(Node JavaDoc contextNode, String JavaDoc str, PrefixResolver resolver);
170
171     public String JavaDoc prefixToNamespace(String JavaDoc prefix)
172     {
173         return (String JavaDoc)m_mappings.get( prefix );
174     }
175 }
176
Popular Tags