KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > transformation > helpers > VariableConfiguration


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.transformation.helpers;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import org.apache.avalon.framework.configuration.Configuration;
22 import org.apache.avalon.framework.configuration.ConfigurationException;
23 import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
24 import org.apache.avalon.framework.parameters.Parameters;
25 import org.xml.sax.SAXException JavaDoc;
26
27 /**
28  * An Avalon <code>Configuration</code> factory that allows {variables} to be
29  * replaced with values from a lookup table.
30  *
31  * @author <a HREF="jefft@apache.org">Jeff Turner</a>
32  * @version CVS $Id: VariableConfiguration.java 30932 2004-07-29 17:35:38Z vgritsenko $
33  */

34 public class VariableConfiguration {
35     public static final String JavaDoc UNSET_VAR="unset";
36     private Configuration conf;
37     private Map JavaDoc vars = new HashMap JavaDoc();
38
39     /** Constructor.
40      * @param conf Template Configuration with {variables} to marking where
41      * values should be interpolated. May be <code>null</code>.
42      */

43     public VariableConfiguration(Configuration conf) {
44         this.conf = conf;
45     }
46
47     /** Add a name-value pair.
48      */

49     public void addVariable(String JavaDoc name, String JavaDoc value) {
50         vars.put(name, value);
51     }
52
53     /** Add a set of name-value pairs.
54      */

55     public void addVariables(Parameters params) {
56         String JavaDoc[] names = params.getNames();
57         for (int i=0; i<names.length; i++) {
58             String JavaDoc paramVal = params.getParameter(names[i], null);
59             if (paramVal != null) {
60                 vars.put(names[i], paramVal);
61             }
62         }
63     }
64
65     /**
66      * Get a generated Configuration with interpolated variable values.
67      * @return The Configuration passed in the constructor, with {variable}
68      * tokens in attributes and element bodies replaced with values (if
69      * specified), or <code>null</code>.
70      */

71     public Configuration getConfiguration() throws SAXException JavaDoc, ConfigurationException {
72
73         if (this.conf == null) return null;
74         InterpolatingConfigurationHandler handler = new InterpolatingConfigurationHandler(this.vars, this.conf.getLocation());
75         DefaultConfigurationSerializer ser = new DefaultConfigurationSerializer();
76         ser.serialize(handler, this.conf);
77         return handler.getConfiguration();
78     }
79
80 }
81
Popular Tags