KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.Set JavaDoc;
27
28 /** Meta module that obtains values from an other module and by
29  * replacing the requested attribute name with another name. This is
30  * done first through a replacement table and may additionally prepend
31  * or append a string. Replacement works in both ways, it is applied
32  * to the returned attribute names as well.
33  *
34  * <p>Example configuration:<pre>
35  * &lt;prefix&gt;cocoon.&lt;/prefix&gt;
36  * &lt;suffix&gt;.attr&lt;/suffix&gt;
37  * &lt;mapping in="foo" out="bar"/&gt;
38  * &lt;mapping in="yuk" out="yeeha"/&gt;
39  *</pre>
40  *
41  * Will map a parameter "foo" to the real one named
42  * "cocoon.bar.attr". If parameters "coocoon.yeeha.attr" and
43  * "shopping.cart" exist, the iterator will return
44  * "yeeha". "shopping.cart" does not contain the pre-/ suffix and thus
45  * is dropped.</p>
46  *
47  * <p>Similarily, rm-prefix and rm-suffix will be removed from the
48  * attribute name.</p>
49  *
50  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
51  * @version CVS $Id: SimpleMappingMetaModule.java 124685 2005-01-08 22:20:56Z antonio $
52  */

53 public class SimpleMappingMetaModule extends AbstractMetaModule implements ThreadSafe {
54
55     String JavaDoc prefix = null;
56     String JavaDoc suffix = null;
57     String JavaDoc rmPrefix = null;
58     String JavaDoc rmSuffix = null;
59     Mapping mapping = null;
60
61     protected static class Mapping {
62         Map JavaDoc toMap = null;
63         Map JavaDoc fromMap = null;
64
65         public Mapping() {
66         }
67
68         public Mapping(Map JavaDoc to, Map JavaDoc from) {
69             this.toMap = to;
70             this.fromMap = from;
71         }
72
73         public Mapping(Configuration config) throws ConfigurationException {
74             Configuration[] mappings = config.getChildren("mapping");
75             if (mappings!=null) {
76                 if (this.toMap == null) this.toMap = new HashMap JavaDoc();
77                 if (this.fromMap == null) this.fromMap = new HashMap JavaDoc();
78                 for (int i=0; i < mappings.length; i++){
79                     String JavaDoc in = mappings[i].getAttribute("in",null);
80                     String JavaDoc out = mappings[i].getAttribute("out",null);
81                     if (in != null && out != null) {
82                         this.toMap.put(in,out);
83                         this.fromMap.put(out,in);
84                     }
85                 }
86             }
87         }
88
89         private String JavaDoc mapIt(Map JavaDoc map, String JavaDoc param) {
90             Object JavaDoc newParam = param;
91             if (map != null) {
92                 newParam = map.get(param);
93                 if (!map.containsKey(param) || newParam == null)
94                     newParam = param;
95             }
96             return (String JavaDoc) newParam;
97         }
98
99         public String JavaDoc mapFrom(String JavaDoc param) {
100             return this.mapIt(this.fromMap, param);
101         }
102
103         public String JavaDoc mapTo(String JavaDoc param) {
104             return this.mapIt(this.toMap, param);
105         }
106     }
107
108
109     public void configure(Configuration config) throws ConfigurationException {
110
111         // It seems that even if there is no config, we'll get an empty
112
// input-module element here, so it will never be null (JT)
113
this.inputConf = config.getChild("input-module");
114         this.defaultInput = this.inputConf.getAttribute("name", this.defaultInput);
115         this.prefix = config.getChild("prefix").getValue(null);
116         this.suffix = config.getChild("suffix").getValue(null);
117         this.rmPrefix = config.getChild("rm-prefix").getValue(null);
118         this.rmSuffix = config.getChild("rm-suffix").getValue(null);
119         this.mapping = new Mapping(config);
120     }
121
122
123     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
124         throws ConfigurationException {
125
126         if (!this.initialized) {
127             this.lazy_initialize();
128         }
129         if (this.defaultInput == null) {
130             if (getLogger().isWarnEnabled())
131                 getLogger().warn("No input module given. FAILING");
132             return null;
133         }
134
135         Configuration inputConfig = null;
136         String JavaDoc inputName=null;
137         Mapping mapping = this.mapping;
138         String JavaDoc prefix = this.prefix;
139         String JavaDoc suffix = this.suffix;
140         String JavaDoc rmPrefix = this.rmPrefix;
141         String JavaDoc rmSuffix = this.rmSuffix;
142
143         if (modeConf!=null && modeConf.getChildren().length > 0) {
144             inputName = modeConf.getChild("input-module").getAttribute("name",null);
145             if (inputName != null) {
146                 inputConfig = modeConf.getChild("input-module");
147             }
148             mapping = new Mapping(modeConf);
149             prefix = modeConf.getChild("prefix").getValue(null);
150             suffix = modeConf.getChild("suffix").getValue(null);
151             rmPrefix = modeConf.getChild("rm-prefix").getValue(null);
152             rmSuffix = modeConf.getChild("rm-suffix").getValue(null);
153         }
154         
155         // remove rm-prefix and rm-suffix
156
if (rmPrefix != null && name.startsWith(rmPrefix)) {
157             name = name.substring(rmPrefix.length());
158         }
159         if (rmSuffix != null && name.endsWith(rmSuffix)) {
160             name = name.substring(0,name.length() - rmSuffix.length());
161         }
162         // map
163
String JavaDoc param = mapping.mapTo(name);
164         // add prefix and suffix
165
if (prefix != null) param = prefix + param;
166         if (suffix != null) param = param + suffix;
167         if (getLogger().isDebugEnabled())
168             getLogger().debug("mapping ['"+name+"'] to ['"+param+"']");
169
170         Object JavaDoc res = getValue(param, objectModel,
171                               this.input, this.defaultInput, this.inputConf,
172                               null, inputName, inputConfig);
173         
174         if (getLogger().isDebugEnabled())
175             getLogger().debug("getting for real attribute ['"+param+"'] value: "+res);
176
177         return res;
178     }
179
180
181
182
183
184     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
185         throws ConfigurationException {
186
187         if (!this.initialized) {
188             this.lazy_initialize();
189         }
190         if (this.defaultInput == null) {
191             if (getLogger().isWarnEnabled())
192                 getLogger().warn("No input module given. FAILING");
193             return null;
194         }
195
196         Configuration inputConfig = null;
197         String JavaDoc inputName=null;
198         Mapping mapping = this.mapping;
199         String JavaDoc prefix = this.prefix;
200         String JavaDoc suffix = this.suffix;
201         String JavaDoc rmPrefix = this.rmPrefix;
202         String JavaDoc rmSuffix = this.rmSuffix;
203
204         if (modeConf!=null && modeConf.getChildren().length > 0) {
205             inputName = modeConf.getChild("input-module").getAttribute("name",null);
206             if (inputName != null) {
207                 inputConfig = modeConf.getChild("input-module");
208             }
209             mapping = new Mapping(modeConf);
210             prefix = modeConf.getChild("prefix").getValue(null);
211             suffix = modeConf.getChild("suffix").getValue(null);
212             rmPrefix = modeConf.getChild("rm-prefix").getValue(null);
213             rmSuffix = modeConf.getChild("rm-suffix").getValue(null);
214         }
215         
216         // remove rm-prefix and rm-suffix
217
if (rmPrefix != null && name.startsWith(rmPrefix)) {
218             name = name.substring(rmPrefix.length());
219         }
220         if (rmSuffix != null && name.endsWith(rmSuffix)) {
221             name = name.substring(0,name.length() - rmSuffix.length());
222         }
223         // map
224
String JavaDoc param = mapping.mapTo(name);
225         // add prefix and suffix
226
if (prefix != null) param = prefix + param;
227         if (suffix != null) param = param + suffix;
228         if (getLogger().isDebugEnabled())
229             getLogger().debug("mapping ['"+name+"'] to ['"+param+"']");
230
231         Object JavaDoc[] res = getValues(param, objectModel,
232                                  this.input, this.defaultInput, this.inputConf,
233                                  null, inputName, inputConfig);
234         if (getLogger().isDebugEnabled())
235             getLogger().debug("getting for real attribute ['"+param+"'] value: "+res);
236
237         return res;
238     }
239
240
241
242     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel )
243         throws ConfigurationException {
244
245         if (!this.initialized) {
246             this.lazy_initialize();
247         }
248         if (this.defaultInput == null) {
249             if (getLogger().isWarnEnabled())
250                 getLogger().warn("No input module given. FAILING");
251             return null;
252         }
253
254         Configuration inputConfig = null;
255         String JavaDoc inputName=null;
256         Mapping mapping = this.mapping;
257         String JavaDoc prefix = this.prefix;
258         String JavaDoc suffix = this.suffix;
259         String JavaDoc rmPrefix = this.rmPrefix;
260         String JavaDoc rmSuffix = this.rmSuffix;
261         if (modeConf!=null && modeConf.getChildren().length > 0) {
262             inputName = modeConf.getChild("input-module").getAttribute("name",null);
263             if (inputName != null) {
264                 inputConfig = modeConf.getChild("input-module");
265             }
266             mapping = new Mapping(modeConf);
267             prefix = modeConf.getChild("prefix").getValue(null);
268             suffix = modeConf.getChild("suffix").getValue(null);
269             rmPrefix = modeConf.getChild("rm-prefix").getValue(null);
270             rmSuffix = modeConf.getChild("rm-suffix").getValue(null);
271         }
272         
273         Iterator JavaDoc names = getNames(objectModel,
274                                   this.input, this.defaultInput, this.inputConf,
275                                   null, inputName, inputConfig);
276
277         Set JavaDoc set = new HashSet JavaDoc();
278         while (names.hasNext()) {
279             String JavaDoc param = (String JavaDoc) names.next();
280             if (getLogger().isDebugEnabled())
281                 getLogger().debug("reverse mapping starts with ['"+param+"']");
282             if (prefix != null)
283                 if (param.startsWith(prefix))
284                     param = param.substring(prefix.length());
285                 else
286                     continue; // prefix is set but parameter does not start with it.
287

288             //if (getLogger().isDebugEnabled())
289
// getLogger().debug("reverse mapping after remove prefix ['"+param+"']");
290

291             if (suffix != null)
292                 if (param.endsWith(suffix))
293                     param = param.substring(0,param.length() - suffix.length());
294                 else
295                     continue; // suffix is set but parameter does not end with it.
296

297             //if (getLogger().isDebugEnabled())
298
// getLogger().debug("reverse mapping after remove suffix ['"+param+"']");
299

300             if (param.length() < 1)
301                 continue; // nothing left
302

303             String JavaDoc newName = mapping.mapFrom(param);
304
305             if (rmPrefix != null) newName = rmPrefix + newName;
306             if (rmSuffix != null) newName = newName + rmSuffix;
307
308             if (getLogger().isDebugEnabled())
309                 getLogger().debug("reverse mapping results in ['"+newName+"']");
310
311             set.add(newName);
312         }
313
314         return set.iterator();
315
316     }
317
318 }
319
Popular Tags