KickJava   Java API By Example, From Geeks To Geeks.

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


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.ClassFunctions;
22 import org.apache.commons.jxpath.FunctionLibrary;
23 import org.apache.commons.jxpath.PackageFunctions;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Map JavaDoc;
27
28 /**
29  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
30  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
31  * @version $Id: JXPathHelperConfiguration.java 160292 2005-04-06 13:54:33Z vgritsenko $
32  */

33 public class JXPathHelperConfiguration {
34
35     /**
36      * Contains all globally registered extension classes and
37      * packages. Thus the lookup and loading of globally registered
38      * extensions is done only once.
39      */

40     private FunctionLibrary library;
41
42     /**
43      * Set lenient mode for jxpath
44      * (i.e. throw an exception on unsupported attributes)?
45      * Defaults to true.
46      */

47     private boolean lenient;
48
49     /**
50      * Contains all registered namespace prefixes.
51      */

52     private Map JavaDoc namespaces;
53
54
55     /**
56      * Create empty jxpath configuration
57      */

58     public JXPathHelperConfiguration() {
59         this.lenient = true;
60     }
61
62     /**
63      * Create root jxpath configuration
64      */

65     public JXPathHelperConfiguration(Configuration config)
66     throws ConfigurationException {
67         this.lenient = config.getChild("lenient").getValueAsBoolean(true);
68         this.library = new FunctionLibrary();
69         setup(config);
70
71         // the following is necessary to be able to use methods on objects without
72
// explicitely registering extension functions (see PackageFunctions javadoc)
73
this.library.addFunctions(new PackageFunctions("", null));
74     }
75
76     /**
77      * Create child jxpath configuration
78      */

79     public JXPathHelperConfiguration(JXPathHelperConfiguration global, Configuration config)
80     throws ConfigurationException {
81         this.lenient = global.lenient;
82         this.library = new FunctionLibrary();
83         this.library.addFunctions(global.getLibrary());
84         if (global.getNamespaces() != null) {
85             this.namespaces = new HashMap JavaDoc(global.getNamespaces());
86         }
87         setup(config);
88     }
89
90
91     public boolean isLenient() {
92         return this.lenient;
93     }
94
95     public FunctionLibrary getLibrary() {
96         return this.library;
97     }
98
99     public Map JavaDoc getNamespaces() {
100         return this.namespaces;
101     }
102
103
104     private void setup(Configuration config)
105     throws ConfigurationException {
106         getFunctions(config);
107         getPackages(config);
108         getNamespaces(config);
109     }
110
111     /**
112      * Register all extension functions listed in the configuration
113      * through <code>&lt;function name="fully.qualified.Class"
114      * prefix="prefix"/&gt;</code> in the given FunctionLibrary.
115      *
116      * @param conf a <code>Configuration</code> value
117      */

118     private void getFunctions(Configuration conf) {
119
120         Configuration[] children = conf.getChildren("function");
121         int i = children.length;
122         while (i-- > 0) {
123             String JavaDoc clazzName = children[i].getAttribute("name", null);
124             String JavaDoc prefix = children[i].getAttribute("prefix", null);
125             if (clazzName != null && prefix != null) {
126                 try {
127                     Class JavaDoc clazz = Class.forName(clazzName);
128                     this.library.addFunctions(new ClassFunctions(clazz, prefix));
129                 } catch (ClassNotFoundException JavaDoc cnf) {
130                     // ignore
131
}
132             }
133         }
134     }
135
136     /**
137      * Register all extension packages listed in the configuration
138      * through <code>&lt;package name="fully.qualified.package"
139      * prefix="prefix"/&gt;</code> in the given FunctionLibrary.
140      *
141      * @param conf a <code>Configuration</code> value
142      */

143     private void getPackages(Configuration conf) {
144
145         Configuration[] children = conf.getChildren("package");
146         int i = children.length;
147         while (i-- > 0) {
148             String JavaDoc packageName = children[i].getAttribute("name", null);
149             String JavaDoc prefix = children[i].getAttribute("prefix", null);
150             if (packageName != null && prefix != null) {
151                 this.library.addFunctions(new PackageFunctions(packageName, prefix));
152             }
153         }
154     }
155
156     /**
157      * Register all namespaces listed in the configuration
158      * through <code>&lt;namespace uri="uri:foo"
159      * prefix="bar"/&gt;</code> in the configuration.
160      *
161      * @param conf a <code>Configuration</code> value
162      */

163     private void getNamespaces(Configuration conf)
164     throws ConfigurationException {
165
166         Configuration[] children = conf.getChildren("namespace");
167         int i = children.length;
168         if (i > 0) {
169             this.namespaces = new HashMap JavaDoc(i + 2);
170         }
171         while (i-- > 0) {
172             String JavaDoc uri = children[i].getAttribute("uri");
173             String JavaDoc prefix = children[i].getAttribute("prefix");
174             if (uri != null && prefix != null) {
175                 this.namespaces.put(prefix, uri);
176             }
177         }
178     }
179 }
180
Popular Tags