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:
018: package org.apache.cocoon.components.modules.input;
019:
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Vector;
023:
024: import org.apache.avalon.framework.activity.Disposable;
025: import org.apache.avalon.framework.configuration.Configurable;
026: import org.apache.avalon.framework.configuration.Configuration;
027: import org.apache.avalon.framework.configuration.ConfigurationException;
028: import org.apache.avalon.framework.logger.AbstractLogEnabled;
029:
030: import org.apache.cocoon.util.HashMap;
031:
032: /**
033: * AbstractInputModule gives you the infrastructure for easily
034: * deploying more InputModules. In order to get at the Logger, use
035: * getLogger().
036: *
037: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
038: * @version CVS $Id: AbstractInputModule.java 433543 2006-08-22 06:22:54Z crossley $
039: */
040: public abstract class AbstractInputModule extends AbstractLogEnabled
041: implements InputModule, Configurable, Disposable {
042:
043: /**
044: * For those modules that access only one attribute, have a
045: * fixed collection we can return an iterator for.
046: */
047: final static Vector returnNames;
048: static {
049: Vector tmp = new Vector();
050: tmp.add("attribute");
051: returnNames = tmp;
052: }
053:
054: /**
055: * Stores (global) configuration parameters as <code>key</code> /
056: * <code>value</code> pairs.
057: */
058: protected HashMap settings = null;
059:
060: /**
061: * Configures the database access helper.
062: *
063: * Takes all elements nested in component declaration and stores
064: * them as key-value pairs in <code>settings</code>. Nested
065: * configuration option are not catered for. This way global
066: * configuration options can be used.
067: *
068: * For nested configurations override this function.
069: * */
070: public void configure(Configuration conf)
071: throws ConfigurationException {
072: Configuration[] parameters = conf.getChildren();
073: this .settings = new HashMap(parameters.length);
074: for (int i = 0; i < parameters.length; i++) {
075: String key = parameters[i].getName();
076: String val = parameters[i].getValue("");
077: this .settings.put(key, val);
078: }
079: }
080:
081: /**
082: * dispose
083: */
084: public void dispose() {
085: // Purposely empty so that we don't need to implement it in every
086: // class.
087: }
088:
089: //
090: // you need to implement at least one of the following two methods
091: // since the ones below have a cyclic dependency!
092: //
093:
094: /* (non-Javadoc)
095: * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
096: */
097: public Object getAttribute(String name, Configuration modeConf,
098: Map objectModel) throws ConfigurationException {
099: Object[] result = this .getAttributeValues(name, modeConf,
100: objectModel);
101: return (result == null ? null : result[0]);
102: }
103:
104: /* (non-Javadoc)
105: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
106: */
107: public Object[] getAttributeValues(String name,
108: Configuration modeConf, Map objectModel)
109: throws ConfigurationException {
110: Object result = this .getAttribute(name, modeConf, objectModel);
111: return (result == null ? null : new Object[] { result });
112: }
113:
114: /* (non-Javadoc)
115: * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration, java.util.Map)
116: */
117: public Iterator getAttributeNames(Configuration modeConf,
118: Map objectModel) throws ConfigurationException {
119: return AbstractInputModule.returnNames.iterator();
120: }
121: }
|