001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.modules.input;
018:
019: import org.apache.avalon.framework.configuration.Configuration;
020: import org.apache.avalon.framework.configuration.ConfigurationException;
021:
022: import org.apache.commons.jxpath.ClassFunctions;
023: import org.apache.commons.jxpath.FunctionLibrary;
024: import org.apache.commons.jxpath.PackageFunctions;
025:
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: /**
030: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
031: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
032: * @version $Id: JXPathHelperConfiguration.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class JXPathHelperConfiguration {
035:
036: /**
037: * Contains all globally registered extension classes and
038: * packages. Thus the lookup and loading of globally registered
039: * extensions is done only once.
040: */
041: private FunctionLibrary library;
042:
043: /**
044: * Set lenient mode for jxpath
045: * (i.e. throw an exception on unsupported attributes)?
046: * Defaults to true.
047: */
048: private boolean lenient;
049:
050: /**
051: * Contains all registered namespace prefixes.
052: */
053: private Map namespaces;
054:
055: /**
056: * Create empty jxpath configuration
057: */
058: public JXPathHelperConfiguration() {
059: this .lenient = true;
060: }
061:
062: /**
063: * Create root jxpath configuration
064: */
065: public JXPathHelperConfiguration(Configuration config)
066: throws ConfigurationException {
067: this .lenient = config.getChild("lenient").getValueAsBoolean(
068: true);
069: this .library = new FunctionLibrary();
070: setup(config);
071:
072: // the following is necessary to be able to use methods on objects without
073: // explicitely registering extension functions (see PackageFunctions javadoc)
074: this .library.addFunctions(new PackageFunctions("", null));
075: }
076:
077: /**
078: * Create child jxpath configuration
079: */
080: public JXPathHelperConfiguration(JXPathHelperConfiguration global,
081: Configuration config) throws ConfigurationException {
082: this .lenient = global.lenient;
083: this .library = new FunctionLibrary();
084: this .library.addFunctions(global.getLibrary());
085: if (global.getNamespaces() != null) {
086: this .namespaces = new HashMap(global.getNamespaces());
087: }
088: setup(config);
089: }
090:
091: public boolean isLenient() {
092: return this .lenient;
093: }
094:
095: public FunctionLibrary getLibrary() {
096: return this .library;
097: }
098:
099: public Map getNamespaces() {
100: return this .namespaces;
101: }
102:
103: private void setup(Configuration config)
104: throws ConfigurationException {
105: getFunctions(config);
106: getPackages(config);
107: getNamespaces(config);
108: }
109:
110: /**
111: * Register all extension functions listed in the configuration
112: * through <code><function name="fully.qualified.Class"
113: * prefix="prefix"/></code> in the given FunctionLibrary.
114: *
115: * @param conf a <code>Configuration</code> value
116: */
117: private void getFunctions(Configuration conf) {
118:
119: Configuration[] children = conf.getChildren("function");
120: int i = children.length;
121: while (i-- > 0) {
122: String clazzName = children[i].getAttribute("name", null);
123: String prefix = children[i].getAttribute("prefix", null);
124: if (clazzName != null && prefix != null) {
125: try {
126: Class clazz = Class.forName(clazzName);
127: this .library.addFunctions(new ClassFunctions(clazz,
128: prefix));
129: } catch (ClassNotFoundException cnf) {
130: // ignore
131: }
132: }
133: }
134: }
135:
136: /**
137: * Register all extension packages listed in the configuration
138: * through <code><package name="fully.qualified.package"
139: * prefix="prefix"/></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 packageName = children[i].getAttribute("name", null);
149: String prefix = children[i].getAttribute("prefix", null);
150: if (packageName != null && prefix != null) {
151: this .library.addFunctions(new PackageFunctions(
152: packageName, prefix));
153: }
154: }
155: }
156:
157: /**
158: * Register all namespaces listed in the configuration
159: * through <code><namespace uri="uri:foo"
160: * prefix="bar"/></code> in the configuration.
161: *
162: * @param conf a <code>Configuration</code> value
163: */
164: private void getNamespaces(Configuration conf)
165: throws ConfigurationException {
166:
167: Configuration[] children = conf.getChildren("namespace");
168: int i = children.length;
169: if (i > 0) {
170: this .namespaces = new HashMap(i + 2);
171: }
172: while (i-- > 0) {
173: String uri = children[i].getAttribute("uri");
174: String prefix = children[i].getAttribute("prefix");
175: if (uri != null && prefix != null) {
176: this.namespaces.put(prefix, uri);
177: }
178: }
179: }
180: }
|