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.Configurable;
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.logger.AbstractLogEnabled;
023: import org.apache.avalon.framework.thread.ThreadSafe;
024:
025: import java.util.HashMap;
026: import java.util.Iterator;
027: import java.util.Map;
028: import java.util.SortedSet;
029: import java.util.TreeSet;
030:
031: /**
032: * Set a number of constants. To override the values with input from
033: * another module, combine this one with the ChainMetaModule and an
034: * arbitrary number of other modules.
035: *
036: * <pre>
037: * <values>
038: * <skin>myskin</skin>
039: * <base>baseurl</base>
040: * ...
041: * </values>
042: * </pre>
043: *
044: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
045: * @version $Id: DefaultsModule.java 433543 2006-08-22 06:22:54Z crossley $
046: */
047: public class DefaultsModule extends AbstractLogEnabled implements
048: InputModule, Configurable, ThreadSafe {
049:
050: private Map constants = null;
051:
052: public void configure(Configuration config)
053: throws ConfigurationException {
054:
055: this .constants = new HashMap();
056: Configuration[] consts = config.getChild("values")
057: .getChildren();
058: for (int i = 0; i < consts.length; i++) {
059: this .constants.put(consts[i].getName(), consts[i]
060: .getValue(""));
061: }
062: }
063:
064: public Object[] getAttributeValues(String name,
065: Configuration modeConf, Map objectModel)
066: throws ConfigurationException {
067:
068: String parameter = name;
069: Configuration mConf = null;
070: if (modeConf != null) {
071: mConf = modeConf.getChild("values");
072: }
073:
074: Object[] values = new Object[1];
075: values[0] = (mConf != null ? mConf.getChild(parameter)
076: .getValue((String) this .constants.get(parameter))
077: : this .constants.get(parameter));
078: return values;
079: }
080:
081: public Iterator getAttributeNames(Configuration modeConf,
082: Map objectModel) throws ConfigurationException {
083:
084: SortedSet matchset = new TreeSet(this .constants.keySet());
085: if (modeConf != null) {
086: Configuration[] consts = modeConf.getChild("values")
087: .getChildren();
088: for (int i = 0; i < consts.length; i++)
089: matchset.add(consts[i].getName());
090: }
091: return matchset.iterator();
092: }
093:
094: public Object getAttribute(String name, Configuration modeConf,
095: Map objectModel) throws ConfigurationException {
096:
097: Object[] values = this .getAttributeValues(name, modeConf,
098: objectModel);
099: return values[0];
100: }
101:
102: }
|