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: import org.apache.avalon.framework.thread.ThreadSafe;
022:
023: import java.util.Iterator;
024: import java.util.Map;
025:
026: /**
027: * Meta module that obtains an Object from another module, assumes
028: * that this Object implements the java.util.Map interface, and gives
029: * access to the map contents. Possible use is to propagate data from
030: * flow through request attributes to database actions.
031: * The same can be achieved by using the {@link JXPathMetaModule}.
032: *
033: * <p>Configuration: "input-module", "object", "parameter"</p>
034: *
035: * @author <a href="mailto:haul@apache.org">Christian Haul</a>
036: * @version $Id: MapMetaModule.java 433543 2006-08-22 06:22:54Z crossley $
037: */
038: public class MapMetaModule extends AbstractMetaModule implements
039: ThreadSafe {
040:
041: protected String objectName = null;
042: protected String parameter = null;
043:
044: public void configure(Configuration config)
045: throws ConfigurationException {
046:
047: this .inputConf = config.getChild("input-module");
048: this .defaultInput = this .inputConf.getAttribute("name",
049: this .defaultInput);
050: this .objectName = this .inputConf.getAttribute("object",
051: this .objectName);
052: this .parameter = this .inputConf.getAttribute("parameter",
053: this .parameter);
054:
055: // preferred
056: this .objectName = config.getChild("object").getValue(
057: this .objectName);
058: this .parameter = config.getChild("parameter").getValue(
059: this .parameter);
060: }
061:
062: public Object getAttribute(String name, Configuration modeConf,
063: Map objectModel) throws ConfigurationException {
064:
065: if (!this .initialized) {
066: this .lazy_initialize();
067: }
068: if (this .defaultInput == null) {
069: if (getLogger().isWarnEnabled())
070: getLogger().warn("No input module given. FAILING");
071: return null;
072: }
073:
074: // obtain correct configuration objects
075: // default vs dynamic
076: String inputName = null;
077: String objectName = this .objectName;
078: String parameter = this .parameter;
079: if (modeConf != null) {
080: inputName = modeConf.getChild("input-module").getAttribute(
081: "name", null);
082: objectName = modeConf.getAttribute("object", objectName);
083: parameter = modeConf.getAttribute("parameter", parameter);
084:
085: // preferred
086: objectName = modeConf.getChild("object").getValue(
087: objectName);
088: parameter = modeConf.getChild("parameter").getValue(
089: parameter);
090: }
091: parameter = (parameter != null ? parameter : name);
092:
093: Object value = getValue(objectName, objectModel, this .input,
094: this .defaultInput, this .inputConf, null, inputName,
095: modeConf);
096:
097: value = (value != null ? ((Map) value).get(parameter) : null);
098:
099: return value;
100: }
101:
102: public Iterator getAttributeNames(Configuration modeConf,
103: Map objectModel) throws ConfigurationException {
104:
105: if (!this .initialized) {
106: this .lazy_initialize();
107: }
108: if (this .defaultInput == null) {
109: if (getLogger().isWarnEnabled())
110: getLogger().warn("No input module given. FAILING");
111: return null;
112: }
113:
114: // obtain correct configuration objects
115: // default vs dynamic
116: Configuration inputConfig = null;
117: String inputName = null;
118: String objectName = this .objectName;
119: if (modeConf != null) {
120: inputName = modeConf.getChild("input-module").getAttribute(
121: "name", null);
122: objectName = modeConf.getAttribute("object",
123: this .objectName);
124:
125: // preferred
126: objectName = modeConf.getChild("object").getValue(
127: objectName);
128: if (inputName != null) {
129: inputConfig = modeConf.getChild("input-module");
130: }
131: }
132:
133: Iterator keys = ((Map) getValue(objectName, objectModel,
134: this .input, this .defaultInput, this .inputConf, null,
135: inputName, inputConfig)).keySet().iterator();
136:
137: return keys;
138: }
139:
140: public Object[] getAttributeValues(String name,
141: Configuration modeConf, Map objectModel)
142: throws ConfigurationException {
143:
144: Object[] values = new Object[1];
145: values[0] = this .getAttribute(name, modeConf, objectModel);
146: return (values[0] != null ? values : null);
147: }
148:
149: }
|