KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 import org.apache.avalon.framework.configuration.Configurable;
20 import org.apache.avalon.framework.configuration.Configuration;
21 import org.apache.avalon.framework.configuration.ConfigurationException;
22 import org.apache.avalon.framework.logger.AbstractLogEnabled;
23 import org.apache.avalon.framework.thread.ThreadSafe;
24
25 import java.util.HashMap JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.SortedSet JavaDoc;
29 import java.util.TreeSet JavaDoc;
30
31 /**
32  * Set a number of constants. To override the values with input from
33  * another module, combine this one with the ChainMetaModule and an
34  * arbitrary number of other modules.
35  *
36  * <values>
37  * <skin>myskin</skin>
38  * <base>baseurl</base>
39  * ...
40  * </values>
41  *
42  * @author <a HREF="mailto:haul@apache.org">Christian Haul</a>
43  * @version CVS $Id: DefaultsModule.java 30932 2004-07-29 17:35:38Z vgritsenko $
44  */

45 public class DefaultsModule extends AbstractLogEnabled
46     implements InputModule, Configurable, ThreadSafe {
47
48     private Map JavaDoc constants = null;
49     
50     public void configure(Configuration config) throws ConfigurationException {
51
52         this.constants = new HashMap JavaDoc();
53         Configuration[] consts = config.getChild("values").getChildren();
54         for (int i=0; i<consts.length; i++) {
55             this.constants.put(consts[i].getName(), consts[i].getValue(""));
56         }
57     }
58
59
60     public Object JavaDoc[] getAttributeValues( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
61         throws ConfigurationException {
62
63         String JavaDoc parameter=name;
64         Configuration mConf = null;
65         if (modeConf!=null) {
66             mConf = modeConf.getChild("values");
67         }
68
69         Object JavaDoc[] values = new Object JavaDoc[1];
70         values[0] = (mConf!=null? mConf.getChild(parameter).getValue((String JavaDoc) this.constants.get(parameter))
71                      : this.constants.get(parameter));
72         return values;
73     }
74
75
76     public Iterator JavaDoc getAttributeNames( Configuration modeConf, Map JavaDoc objectModel )
77         throws ConfigurationException {
78
79         SortedSet JavaDoc matchset = new TreeSet JavaDoc(this.constants.keySet());
80         if (modeConf!=null) {
81             Configuration[] consts = modeConf.getChild("values").getChildren();
82             for (int i=0; i<consts.length; i++)
83                 matchset.add(consts[i].getName());
84         }
85         return matchset.iterator();
86      }
87
88
89     public Object JavaDoc getAttribute( String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel )
90         throws ConfigurationException {
91
92         Object JavaDoc[] values = this.getAttributeValues(name,modeConf,objectModel);
93         return values[0];
94     }
95
96 }
97
Popular Tags