KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.Iterator JavaDoc;
19 import java.util.Map JavaDoc;
20 import java.util.TreeMap JavaDoc;
21
22 import org.apache.avalon.framework.component.ComponentException;
23 import org.apache.avalon.framework.component.ComponentManager;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.thread.ThreadSafe;
27
28 /**
29  *
30  * <h2>Configuration</h2>
31  * <table><tbody>
32  * <tr><th>input-module</th>
33  * <td>Configuration and name of input module used for the selection.</td>
34  * <td>req</td>
35  * <td>String</td><td><code>null</code></td>
36  * </tr>
37  * <tr><th>when</th>
38  * <td>Selection case, condition in test attribute, input module name
39  * in name attribute. Optional configuration as nested content.</td>
40  * <td>req</td><td>String</td><td><code>null</code></td>
41  * </tr>
42  * <tr><th>otherwise</th>
43  * <td>Default selection case. If not present and no case matches, <code>null</code>
44  * is returned.</td>
45  * <td></td><td>String</td><td><code>null</code></td>
46  * </tr>
47  * </tbody></table>
48  *
49  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
50  * @version CVS $Id: SelectMetaInputModule.java 37203 2004-08-30 14:19:06Z cziegeler $
51  */

52 public class SelectMetaInputModule extends AbstractMetaModule implements ThreadSafe {
53
54     private Map JavaDoc whenTest = null;
55     private ModuleHolder expression = null;
56     private ModuleHolder otherwise = null;
57     private String JavaDoc parameter = null;
58
59     public SelectMetaInputModule() {
60         super();
61         this.defaultInput = null; // not needed
62
}
63         
64     /* (non-Javadoc)
65      * @see org.apache.avalon.framework.configuration.Configurable#configure(Configuration)
66      */

67     public void configure(Configuration config) throws ConfigurationException {
68
69         Configuration[] expr = config.getChildren("input-module");
70         if (expr == null || expr.length != 1) {
71             throw new ConfigurationException("Need to have exactly one input-module element.");
72         }
73         this.parameter = config.getChild("parameter").getValue();
74         Configuration[] whens = config.getChildren("when");
75         Configuration[] others = config.getChildren("otherwise");
76         if ((whens == null && others == null)
77             || ((whens == null || whens.length == 0) && (others == null || others.length == 0))) {
78             throw new ConfigurationException("Need to have at least one when or otherwise element.");
79         }
80         if (others != null && others.length > 1) {
81             throw new ConfigurationException("Need to have at most one otherwise element.");
82         }
83         this.whenTest = new TreeMap JavaDoc();
84         for (int i = 0; i < expr.length; i++) {
85             String JavaDoc name = expr[i].getAttribute("name");
86             this.expression = new ModuleHolder(name, expr[i], null);
87         }
88
89         if (others != null) {
90             for (int i = 0; i < others.length; i++) {
91                 String JavaDoc name = others[i].getAttribute("name");
92                 this.otherwise = new ModuleHolder(name, others[i], null);
93             }
94         }
95
96         if (whens != null) {
97             for (int i = 0; i < whens.length; i++) {
98                 String JavaDoc name = whens[i].getAttribute("name");
99                 this.whenTest.put(
100                     whens[i].getAttribute("test"),
101                     new ModuleHolder(name, whens[i], null));
102             }
103         }
104     }
105
106     /* (non-Javadoc)
107      * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(String, Configuration, Map)
108      */

109     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
110     throws ConfigurationException {
111         Object JavaDoc result = this.getAttribute(name, modeConf, objectModel, false);
112         return result;
113     }
114
115     public Object JavaDoc[] getAttributeValues(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
116     throws ConfigurationException {
117         Object JavaDoc result = this.getAttribute(name, modeConf, objectModel, true);
118         return (result != null ? (Object JavaDoc[]) result : null );
119     }
120     
121     private Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel, boolean getValues)
122     throws ConfigurationException {
123         if (!this.initialized) {
124             this.lazy_initialize();
125         }
126         ModuleHolder expression = this.expression;
127         ModuleHolder otherwise = this.otherwise;
128         ModuleHolder module = null;
129         String JavaDoc parameter = this.parameter;
130         boolean needRelease = false;
131         boolean dynamicConfig = (modeConf != null && modeConf.getChildren().length > 0);
132
133         if (dynamicConfig) {
134             // clear all configured values so that they
135
// don't get mixed up
136
expression = null;
137             otherwise = null;
138             needRelease = true;
139
140             Configuration[] expr = modeConf.getChildren("input-module");
141             Configuration[] other = modeConf.getChildren("otherwise");
142             if (expr != null && expr.length == 1) {
143                 expression = new ModuleHolder(expr[0].getAttribute("name"), expr[0]);
144             }
145             if (other != null && other.length == 1) {
146                 otherwise = new ModuleHolder(other[0].getAttribute("name"), other[0]);
147             }
148             parameter = modeConf.getChild("parameter").getValue();
149         }
150
151         String JavaDoc value =
152             (String JavaDoc) this.getValue(parameter, objectModel, expression.input, expression.name, expression.config);
153         if (needRelease) {
154             this.releaseModule(expression.input);
155         }
156         if (this.getLogger().isDebugEnabled()) {
157             this.getLogger().debug(
158                 (dynamicConfig ? "(dyn)" : "(static)")
159                     + " select ("
160                     + value
161                     + ") from "
162                     + expression.name
163                     + ":"
164                     + parameter);
165         }
166
167         if (dynamicConfig && value != null) {
168             Configuration[] whens = modeConf.getChildren("when");
169             if (whens != null && whens.length > 0) {
170                 int i = 0;
171                 boolean found = false;
172                 while (!found && i < whens.length) {
173                     if (whens[i].getAttribute("test").equals(value)) {
174                         found = true;
175                         break;
176                     }
177                     i++;
178                 }
179                 if (found) {
180                     module = new ModuleHolder(whens[i].getAttribute("name"), whens[i]);
181                 }
182             }
183         } else if (value != null) {
184             module = (ModuleHolder) this.whenTest.get(value);
185         }
186         if (module != null) {
187             if (this.getLogger().isDebugEnabled()) {
188                 this.getLogger().debug("found matching when : "+module.name);
189             }
190         } else {
191             module = otherwise;
192             if (this.getLogger().isDebugEnabled()) {
193                 this.getLogger().debug("using otherwise : "+module.name);
194             }
195         }
196
197         Object JavaDoc result;
198         if (getValues){
199             result = (module == null ? null : this.getValues(name, objectModel, module));
200         } else {
201             result = (module == null ? null : this.getValue(name, objectModel, module));
202         }
203         
204         if (needRelease && module != null) {
205             this.releaseModule(module.input);
206         }
207         if (this.getLogger().isDebugEnabled()) {
208             this.getLogger().debug("Obtained value : "+result);
209         }
210         return result;
211     }
212
213     /* (non-Javadoc)
214      * @see org.apache.avalon.framework.component.Composable#compose(ComponentManager)
215      */

216     public void compose(ComponentManager manager) throws ComponentException {
217         super.compose(manager);
218     }
219
220     /* (non-Javadoc)
221      * @see org.apache.avalon.framework.activity.Disposable#dispose()
222      */

223     public void dispose() {
224         this.releaseModule(this.expression.input);
225         this.expression = null;
226
227         if (this.otherwise != null) {
228             this.releaseModule(this.otherwise.input);
229             this.otherwise = null;
230         }
231
232         for (Iterator JavaDoc i = this.whenTest.values().iterator(); i.hasNext();) {
233             ModuleHolder holder = (ModuleHolder) i.next();
234             this.releaseModule(holder.input);
235         }
236         this.whenTest = null;
237
238         super.dispose();
239     }
240
241     /* (non-Javadoc)
242      * @see org.apache.cocoon.components.modules.input.AbstractMetaModule#lazy_initialize()
243      */

244     public synchronized void lazy_initialize() {
245         if (this.initialized) {
246             return;
247         }
248
249         super.lazy_initialize();
250         
251         if (this.expression != null) {
252             this.expression.input = this.obtainModule(this.expression.name);
253         }
254         if (this.otherwise != null){
255             this.otherwise.input = this.obtainModule(this.otherwise.name);
256         }
257         if (this.whenTest != null){
258             for (Iterator JavaDoc i = this.whenTest.values().iterator(); i.hasNext(); ){
259                 ModuleHolder moduleHolder = (ModuleHolder) i.next();
260                 moduleHolder.input = this.obtainModule(moduleHolder.name);
261             }
262         }
263     }
264 }
265
Popular Tags