KickJava   Java API By Example, From Geeks To Geeks.

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


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.Configurable;
22 import org.apache.avalon.framework.configuration.Configuration;
23 import org.apache.avalon.framework.configuration.ConfigurationException;
24 import org.apache.avalon.framework.thread.ThreadSafe;
25
26 /**
27  * JXPathModule allows to access properties of any object in generic
28  * way. JXPath provides APIs for the traversal of graphs of
29  * JavaBeans, DOM and other types of objects using the XPath
30  * syntax.
31  *
32  * <p><strong>Note:</strong> This is based on the AbstractJXPathModule
33  * and duplicates the code since multiple inheritance is not possible.
34  * Please keep both classes in sync.</p>
35  *
36  * <p>Configuration example:</p>
37  * <table>
38  * <tr>
39  * <td><code>&lt;lenient&gt;false&lt;/lenient&gt;</td>
40  * <td>When set to true, non-existing attributes return null, when set to false,
41  * an exception is thrown. Default is true.</td>
42  * </tr>
43  * <tr>
44  * <td><code>&lt;parameter&gt;false&lt;/parameter&gt;</td>
45  * <td>Attribute name to be used instead of passed attribute name.</td>
46  * </tr>
47  * <tr>
48  * <td><code>&lt;from-parameter&gt;false&lt;/from-parameter&gt;</td>
49  * <td>Attribute name to pass to configured input module</td>
50  * </tr>
51  * <tr>
52  * <td><code>&lt;input-module name="request-attr"/&gt;</td>
53  * <td>Uses the "request-attr" input module to obtain a value and
54  * applies the given JXPath expression to it.</td>
55  * </tr>
56  * <tr>
57  * <td><code>&lt;function name="java.lang.String" prefix="str"/&gt;</td>
58  * <td>Imports the class "String" as extension class to the JXPathContext using
59  * the prefix "str". Thus "str:length(xpath)" would apply the method "length" to
60  * the string object obtained from the xpath expression. Please note that the class
61  * needs to be fully qualified.</td>
62  * </tr>
63  * <tr>
64  * <td><code>&lt;package name="java.util" prefix="util"/&gt;</td>
65  * <td>Imports all classes in the package "java.util" as extension classes to the
66  * JXPathContext using the prefix "util". Thus "util:Date.new()" would create a
67  * new java.util.Date object.</td>
68  * </tr>
69  * <tr>
70  * <td><code>&lt;namespace uri="uri:foo" prefix="bar"/&gt;</td>
71  * <td>Registers the namespace identified by URI <code>uri:foo</code>
72  * with the JXPathContext using the prefix <code>bar</code>. Thus
73  * expressions can query XML with nodes in this namespace using
74  * registered prefix.</td>
75  * </tr>
76  * </table>
77  *
78  * <p>In addition, it accepts the attributes "parameter" to override
79  * the attribute name and "from-parameter" to pass as attribute name
80  * to the configured input module.</p>
81  *
82  * @author <a HREF="mailto:kpiroumian@apache.org">Konstantin Piroumian</a>
83  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
84  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
85  * @version $Id: JXPathMetaModule.java 160292 2005-04-06 13:54:33Z vgritsenko $
86  */

87 public class JXPathMetaModule extends AbstractMetaModule
88                               implements Configurable, ThreadSafe {
89
90     /**
91      * Contains all globally registered extension classes and
92      * packages. Thus the lookup and loading of globally registered
93      * extensions is done only once.
94      */

95     protected JXPathHelperConfiguration configuration;
96
97     /**
98      * Overrides attribute name
99      */

100     protected String JavaDoc parameter = "";
101
102
103     public JXPathMetaModule() {
104         // this value has a default in the super class
105
super.defaultInput = "request-attr";
106     }
107
108
109     /**
110      * Configure component. Preprocess list of packages and functions
111      * to add to JXPath context later.
112      *
113      * @param config a <code>Configuration</code> value
114      * @exception ConfigurationException if an error occurs
115      */

116     public void configure(Configuration config) throws ConfigurationException {
117
118         this.inputConf = config.getChild("input-module");
119         this.defaultInput = this.inputConf.getAttribute("name",this.defaultInput);
120         this.parameter = config.getChild("parameter").getValue(this.parameter);
121
122         this.configuration = JXPathHelper.setup(config);
123     }
124
125
126     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
127     throws ConfigurationException {
128
129         Object JavaDoc contextObj = getContextObject(modeConf, objectModel);
130         if (modeConf != null) {
131             name = modeConf.getChild("parameter").getValue(!this.parameter.equals("") ? this.parameter : name);
132         }
133         return JXPathHelper.getAttribute(name, modeConf, this.configuration, contextObj);
134     }
135
136
137     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
138     throws ConfigurationException {
139
140         Object JavaDoc contextObj = getContextObject(modeConf, objectModel);
141         return JXPathHelper.getAttributeNames(this.configuration, contextObj);
142     }
143
144
145     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
146     throws ConfigurationException {
147
148         Object JavaDoc contextObj = getContextObject(modeConf, objectModel);
149         if (modeConf != null) {
150             name = modeConf.getChild("parameter").getValue(!this.parameter.equals("") ? this.parameter : name);
151         }
152         return JXPathHelper.getAttributeValues(name, modeConf, this.configuration, contextObj);
153     }
154
155
156     /**
157      * Looks up object from configured InputModule.
158      *
159      * @param modeConf a <code>Configuration</code> value
160      * @param objectModel a <code>Map</code> value
161      * @return an <code>Object</code> value
162      */

163     protected Object JavaDoc getContextObject(Configuration modeConf, Map JavaDoc objectModel)
164     throws ConfigurationException {
165
166         if (!this.initialized) {
167             lazy_initialize();
168         }
169
170         Configuration mConf = null;
171         String JavaDoc inputName = null;
172         String JavaDoc parameter = this.parameter;
173         if (modeConf != null) {
174             mConf = modeConf.getChild("input-module");
175             inputName = mConf.getAttribute("name", null);
176             parameter = modeConf.getChild("from-parameter").getValue(parameter);
177         }
178
179         if (getLogger().isDebugEnabled()) {
180             getLogger().debug("modeConf is " + modeConf + " this.inputConf is " + this.inputConf
181                               + " mConf is " + mConf + " this.input is " + this.input
182                               + " this.defaultInput is " + this.defaultInput
183                               + " inputName is " + inputName + " parameter is " + parameter);
184         }
185
186         Object JavaDoc obj = getValue(parameter, objectModel,
187                               this.input, this.defaultInput, this.inputConf,
188                               null, inputName, mConf);
189
190         if (getLogger().isDebugEnabled()) {
191             getLogger().debug("returning an " + (obj == null ? "null" : obj.getClass().getName()) +
192                               " as " + obj);
193         }
194
195         return obj;
196     }
197 }
198
Popular Tags