KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.apache.avalon.framework.configuration.Configuration;
19 import org.apache.avalon.framework.configuration.ConfigurationException;
20
21 import org.apache.commons.jxpath.JXPathBeanInfo;
22 import org.apache.commons.jxpath.JXPathContext;
23 import org.apache.commons.jxpath.JXPathIntrospector;
24
25 import java.util.Iterator JavaDoc;
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28 import java.util.Map JavaDoc;
29
30 /**
31  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
32  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
33  * @version $Id: JXPathHelper.java 160292 2005-04-06 13:54:33Z vgritsenko $
34  */

35 public class JXPathHelper {
36
37     private JXPathHelper() {
38         // no instances allowed
39
}
40
41     /**
42      * Configure component. Preprocess list of packages, functions
43      * and namespaces to add to the JXPath context later.
44      *
45      * This method used in both AbstractJXPathModule and JXPathMetaModule
46      * to configure JXPath.
47      *
48      * @param config a <code>Configuration</code> value
49      * @exception ConfigurationException if an error occurs
50      */

51     public static JXPathHelperConfiguration setup(Configuration config)
52     throws ConfigurationException {
53
54         return new JXPathHelperConfiguration(config);
55     }
56
57
58     /**
59      * Actually add global functions and packages as well as those
60      * listed in the configuration object.
61      *
62      * @param context a <code>JXPathContext</code> value
63      * @param conf a <code>Configuration</code> value holding local
64      * packages and functions.
65      */

66     private static void setup(JXPathHelperConfiguration setup, JXPathContext context, Configuration conf)
67     throws ConfigurationException {
68
69         // Create local config (if necessary)
70
JXPathHelperConfiguration local = conf == null ? setup : new JXPathHelperConfiguration(setup, conf);
71
72         // Setup context with local config
73
context.setLenient(setup.isLenient());
74         context.setFunctions(local.getLibrary());
75         if (local.getNamespaces() != null) {
76             for (Iterator JavaDoc i = local.getNamespaces().entrySet().iterator(); i.hasNext();) {
77                 final Map.Entry JavaDoc entry = (Map.Entry JavaDoc) i.next();
78                 context.registerNamespace((String JavaDoc) entry.getKey(), (String JavaDoc) entry.getValue());
79             }
80         }
81     }
82
83     public static Object JavaDoc getAttribute(String JavaDoc name,
84                                       Configuration modeConf,
85                                       JXPathHelperConfiguration setup,
86                                       Object JavaDoc contextObj)
87     throws ConfigurationException {
88
89         if (contextObj == null) {
90             return null;
91         }
92
93         try {
94             JXPathContext jxContext = JXPathContext.newContext(contextObj);
95             setup(setup, jxContext, modeConf);
96
97             Object JavaDoc obj = jxContext.getValue(name);
98             return obj;
99         } catch (Exception JavaDoc e) {
100             throw new ConfigurationException("Module does not support <" + name + ">" + "attribute.", e);
101         }
102     }
103
104     public static Object JavaDoc[] getAttributeValues(String JavaDoc name,
105                                               Configuration modeConf,
106                                               JXPathHelperConfiguration setup,
107                                               Object JavaDoc contextObj)
108     throws ConfigurationException {
109
110         if (contextObj == null) {
111             return null;
112         }
113
114         try {
115             JXPathContext jxContext = JXPathContext.newContext(contextObj);
116             setup(setup, jxContext, modeConf);
117
118             List JavaDoc values = null;
119             Iterator JavaDoc i = jxContext.iterate(name);
120             if (i.hasNext()) {
121                 values = new LinkedList JavaDoc();
122             }
123             while (i.hasNext()) {
124                 values.add(i.next());
125             }
126             Object JavaDoc[] obj = null;
127             if (values != null) {
128                 obj = values.toArray();
129                 if (obj.length == 0) {
130                     obj = null;
131                 }
132             }
133             return obj;
134         } catch (Exception JavaDoc e) {
135             throw new ConfigurationException("Module does not support <" + name + ">" + "attribute.", e);
136         }
137     }
138
139
140     public static Iterator JavaDoc getAttributeNames(JXPathHelperConfiguration setup, Object JavaDoc contextObj)
141     throws ConfigurationException {
142
143         if (contextObj == null) {
144             return null;
145         }
146
147         try {
148             JXPathBeanInfo info = JXPathIntrospector.getBeanInfo(contextObj.getClass());
149             java.beans.PropertyDescriptor JavaDoc[] properties = info.getPropertyDescriptors();
150
151             List JavaDoc names = new LinkedList JavaDoc();
152             for (int i = 0; i < properties.length; i++) {
153                 names.add(properties[i].getName());
154             }
155
156             return names.listIterator();
157         } catch (Exception JavaDoc e) {
158             throw new ConfigurationException("Error retrieving attribute names for class: " + contextObj.getClass(), e);
159         }
160     }
161 }
162
Popular Tags