KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > AbstractJXPathModule


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.modules.input;
17
18 import java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.configuration.Configuration;
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23
24 /**
25  * JXPathModule allows to access properties of any object in generic
26  * way. JXPath provides APIs for the traversal of graphs of
27  * JavaBeans, DOM and other types of objects using the XPath
28  * syntax.
29  *
30  * <p><strong>Note:</strong> JXPathMetaModule is based on this class
31  * and duplicates the code since multiple inheritance is not possible.
32  * Please keep both classes in sync.</p>
33  *
34  * <h3>Configuration</h3>
35  * <table>
36  * <tr>
37  * <td><code>&lt;lenient&gt;false&lt;/lenient&gt;</code></td>
38  * <td>When set to true, non-existing attributes return null; when set to false,
39  * an exception is thrown. Default is true.</td>
40  * </tr>
41  * <tr>
42  * <td><code>&lt;parameter&gt;foo&lt;/parameter&gt;</td>
43  * <td>When set overrides attribute name passed to module.</td>
44  * </tr>
45  * <tr>
46  * <td><code>&lt;function name="java.lang.String" prefix="str"/&gt;</td>
47  * <td>Imports the class "String" as extension class to the JXPathContext using
48  * the prefix "str". Thus "str:length(xpath)" would apply the method "length" to
49  * the string object obtained from the xpath expression. Please note that the class
50  * needs to be fully qualified.</td>
51  * </tr>
52  * <tr>
53  * <td><code>&lt;package name="java.util" prefix="util"/&gt;</td>
54  * <td>Imports all classes in the package "java.util" as extension classes to the
55  * JXPathContext using the prefix "util". Thus "util:Date.new()" would create a
56  * new java.util.Date object.</td>
57  * </tr>
58  * <tr>
59  * <td><code>&lt;namespace uri="uri:foo" prefix="bar"/&gt;</td>
60  * <td>Registers the namespace identified by URI <code>uri:foo</code>
61  * with the JXPathContext using the prefix <code>bar</code>. Thus
62  * expressions can query XML with nodes in this namespace using
63  * registered prefix.</td>
64  * </tr>
65  * </table>
66  *
67  * @author <a HREF="mailto:kpiroumian@apache.org">Konstantin Piroumian</a>
68  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
69  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
70  * @version $Id: AbstractJXPathModule.java 160292 2005-04-06 13:54:33Z vgritsenko $
71  */

72 public abstract class AbstractJXPathModule extends AbstractInputModule {
73
74     /**
75      * Contains all globally registered extension classes and
76      * packages. Thus the lookup and loading of globally registered
77      * extensions is done only once.
78      */

79     protected JXPathHelperConfiguration configuration;
80
81     /**
82      * Overrides attribute name
83      */

84     protected String JavaDoc parameter;
85
86     /**
87      * Configure component. Preprocess list of packages and functions
88      * to add to JXPath context later.
89      *
90      * @param config a <code>Configuration</code> value
91      * @exception ConfigurationException if an error occurs
92      */

93     public void configure(Configuration config) throws ConfigurationException {
94
95         this.configuration = JXPathHelper.setup(config);
96     }
97
98
99     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
100     throws ConfigurationException {
101
102         Object JavaDoc contextObj = getContextObject(modeConf, objectModel);
103         if (modeConf != null) {
104             name = modeConf.getChild("parameter").getValue(this.parameter != null ? this.parameter : name);
105         }
106         return JXPathHelper.getAttribute(name, modeConf, this.configuration, contextObj);
107     }
108
109
110     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
111     throws ConfigurationException {
112
113         Object JavaDoc contextObj = getContextObject(modeConf, objectModel);
114         return JXPathHelper.getAttributeNames(this.configuration, contextObj);
115     }
116
117
118     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
119     throws ConfigurationException {
120
121         Object JavaDoc contextObj = getContextObject(modeConf, objectModel);
122         if (modeConf != null) {
123             name = modeConf.getChild("parameter").getValue(this.parameter != null ? this.parameter : name);
124         }
125         return JXPathHelper.getAttributeValues(name, modeConf, this.configuration, contextObj);
126     }
127
128
129     /**
130      * Returns the object which should be used as JXPath context.
131      * Descendants should override this method to return a specific object
132      * that is requried by the implementing class.
133      * Examples are: request, session and application context objects.
134      */

135     protected abstract Object JavaDoc getContextObject(Configuration modeConf,
136                                                Map JavaDoc objectModel)
137     throws ConfigurationException;
138 }
139
Popular Tags