KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2004 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 import org.apache.avalon.framework.thread.ThreadSafe;
21
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * Meta module that obtains an Object from another module, assumes
27  * that this Object implements the java.util.Map interface, and gives
28  * access to the map contents. Possible use is to propagate data from
29  * flow through request attributes to database actions.
30  * The same can be achieved by using the {@link JXPathMetaModule}.
31  *
32  * <p>Configuration: "input-module", "object", "parameter"</p>
33  *
34  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
35  * @version CVS $Id: MapMetaModule.java 30932 2004-07-29 17:35:38Z vgritsenko $
36  */

37 public class MapMetaModule extends AbstractMetaModule implements ThreadSafe {
38
39     protected String JavaDoc objectName = null;
40     protected String JavaDoc parameter = null;
41
42
43     public void configure(Configuration config) throws ConfigurationException {
44
45         this.inputConf = config.getChild("input-module");
46         this.defaultInput = this.inputConf.getAttribute("name", this.defaultInput);
47         this.objectName = this.inputConf.getAttribute("object",this.objectName);
48         this.parameter = this.inputConf.getAttribute("parameter",this.parameter);
49
50         // preferred
51
this.objectName = config.getChild("object").getValue(this.objectName);
52         this.parameter = config.getChild("parameter").getValue(this.parameter);
53     }
54
55
56     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
57         throws ConfigurationException {
58
59         if (!this.initialized) {
60             this.lazy_initialize();
61         }
62         if (this.defaultInput == null) {
63             if (getLogger().isWarnEnabled())
64                 getLogger().warn("No input module given. FAILING");
65             return null;
66         }
67
68         // obtain correct configuration objects
69
// default vs dynamic
70
String JavaDoc inputName=null;
71         String JavaDoc objectName = this.objectName;
72         String JavaDoc parameter = this.parameter;
73         if (modeConf != null) {
74             inputName = modeConf.getChild("input-module").getAttribute("name",null);
75             objectName = modeConf.getAttribute("object",objectName);
76             parameter = modeConf.getAttribute("parameter",parameter);
77
78             // preferred
79
objectName = modeConf.getChild("object").getValue(objectName);
80             parameter = modeConf.getChild("parameter").getValue(parameter);
81         }
82         parameter = (parameter != null? parameter : name);
83
84         Object JavaDoc value = getValue(objectName, objectModel,
85                                 this.input, this.defaultInput, this.inputConf,
86                                 null, inputName, modeConf);
87
88         value = (value!=null? ((Map JavaDoc) value).get(parameter) : null);
89
90         return value;
91     }
92
93
94
95
96     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel )
97         throws ConfigurationException {
98
99          if (!this.initialized) {
100              this.lazy_initialize();
101         }
102         if (this.defaultInput == null) {
103             if (getLogger().isWarnEnabled())
104                 getLogger().warn("No input module given. FAILING");
105             return null;
106         }
107
108         // obtain correct configuration objects
109
// default vs dynamic
110
Configuration inputConfig = null;
111         String JavaDoc inputName=null;
112         String JavaDoc objectName = this.objectName;
113         if (modeConf!=null) {
114             inputName = modeConf.getChild("input-module").getAttribute("name",null);
115             objectName = modeConf.getAttribute("object",this.objectName);
116
117             // preferred
118
objectName = modeConf.getChild("object").getValue(objectName);
119             if (inputName != null) {
120                 inputConfig = modeConf.getChild("input-module");
121             }
122         }
123
124         Iterator JavaDoc keys = ((Map JavaDoc) getValue(objectName, objectModel,
125                                         this.input, this.defaultInput, this.inputConf,
126                                         null, inputName, inputConfig)).keySet().iterator();
127
128         return keys;
129    }
130
131
132
133
134     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
135         throws ConfigurationException {
136
137         Object JavaDoc[] values = new Object JavaDoc[1];
138         values[0] = this.getAttribute(name, modeConf, objectModel);
139         return (values[0]!=null?values:null);
140     }
141
142 }
143
Popular Tags