KickJava   Java API By Example, From Geeks To Geeks.

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


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.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.component.Component;
23 import org.apache.avalon.framework.configuration.Configurable;
24 import org.apache.avalon.framework.configuration.Configuration;
25 import org.apache.avalon.framework.configuration.ConfigurationException;
26 import org.apache.avalon.framework.logger.AbstractLogEnabled;
27 import org.apache.avalon.framework.thread.ThreadSafe;
28 import org.apache.cocoon.components.ChainedConfiguration;
29 import org.apache.cocoon.components.SitemapConfigurable;
30 import org.apache.cocoon.components.SitemapConfigurationHolder;
31
32 /**
33  * This "component" is a trick to get global variables on a per
34  * sitemap base
35  *
36  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
37  * @version CVS $Id: SitemapVariableHolder.java 37203 2004-08-30 14:19:06Z cziegeler $
38  */

39 public final class SitemapVariableHolder
40     extends AbstractLogEnabled
41     implements Component, Configurable, SitemapConfigurable, ThreadSafe
42 {
43  
44     public static final String JavaDoc ROLE = SitemapVariableHolder.class.getName();
45     
46     /**
47      * Stores (global) configuration parameters as <code>key</code> /
48      * <code>value</code> pairs from the component configuration
49      */

50     private Map JavaDoc globalValues;
51
52     /** Manager for sitemap/sub sitemap configuration */
53     private SitemapConfigurationHolder holder;
54
55     /**
56      * Configures the database access helper.
57      *
58      * Takes all elements nested in component declaration and stores
59      * them as key-value pairs in <code>settings</code>. Nested
60      * configuration option are not catered for. This way global
61      * configuration options can be used.
62      *
63      * For nested configurations override this function.
64      * */

65     public void configure(Configuration conf)
66     throws ConfigurationException {
67         final Configuration[] parameters = conf.getChildren();
68         this.globalValues = new HashMap JavaDoc(parameters.length);
69         for (int i = 0; i < parameters.length; i++) {
70             final String JavaDoc key = parameters[i].getName();
71             final String JavaDoc value = parameters[i].getValue();
72             this.globalValues.put(key, value);
73         }
74     }
75
76     /**
77      * Set the <code>Configuration</code> from a sitemap
78      */

79     public void configure(SitemapConfigurationHolder holder) {
80         this.holder = holder;
81     }
82
83     /**
84      * Get a value
85      */

86     public Object JavaDoc get(String JavaDoc key) {
87         return this.getValues().get(key);
88     }
89     
90     /**
91      * Get keys
92      */

93     public Iterator JavaDoc getKeys() {
94         return this.getValues().keySet().iterator();
95     }
96     
97     protected Map JavaDoc getValues() {
98         Map JavaDoc values = (Map JavaDoc)this.holder.getPreparedConfiguration();
99         if ( null == values ) {
100             values = new HashMap JavaDoc(this.globalValues);
101             ChainedConfiguration conf = this.holder.getConfiguration();
102             if ( conf != null ) {
103                 this.prepare(conf, values);
104                 this.holder.setPreparedConfiguration(conf, values);
105             }
106         }
107         return values;
108     }
109     
110     protected void prepare(ChainedConfiguration conf, Map JavaDoc values) {
111         ChainedConfiguration parent = conf.getParent();
112         if ( null != parent) {
113             this.prepare(parent, values);
114         }
115         final Configuration[] parameters = conf.getChildren();
116         final int len = parameters.length;
117         for ( int i = 0; i < len; i++) {
118             final String JavaDoc key = parameters[i].getName();
119             final String JavaDoc value = parameters[i].getValue("");
120             if ( key != null && value != null) {
121                 values.put(key, value);
122             }
123         }
124     }
125 }
126
Popular Tags