01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.transformation.helpers;
18:
19: import java.util.HashMap;
20: import java.util.Map;
21:
22: import org.apache.avalon.framework.configuration.Configuration;
23: import org.apache.avalon.framework.configuration.ConfigurationException;
24: import org.apache.avalon.framework.configuration.DefaultConfigurationSerializer;
25: import org.apache.avalon.framework.parameters.Parameters;
26: import org.xml.sax.SAXException;
27:
28: /**
29: * An Avalon <code>Configuration</code> factory that allows {variables} to be
30: * replaced with values from a lookup table.
31: *
32: * @author <a href="jefft@apache.org">Jeff Turner</a>
33: * @version CVS $Id: VariableConfiguration.java 433543 2006-08-22 06:22:54Z crossley $
34: */
35: public class VariableConfiguration {
36: public static final String UNSET_VAR = "unset";
37: private Configuration conf;
38: private Map vars = new HashMap();
39:
40: /** Constructor.
41: * @param conf Template Configuration with {variables} to marking where
42: * values should be interpolated. May be <code>null</code>.
43: */
44: public VariableConfiguration(Configuration conf) {
45: this .conf = conf;
46: }
47:
48: /** Add a name-value pair.
49: */
50: public void addVariable(String name, String value) {
51: vars.put(name, value);
52: }
53:
54: /** Add a set of name-value pairs.
55: */
56: public void addVariables(Parameters params) {
57: String[] names = params.getNames();
58: for (int i = 0; i < names.length; i++) {
59: String paramVal = params.getParameter(names[i], null);
60: if (paramVal != null) {
61: vars.put(names[i], paramVal);
62: }
63: }
64: }
65:
66: /**
67: * Get a generated Configuration with interpolated variable values.
68: * @return The Configuration passed in the constructor, with {variable}
69: * tokens in attributes and element bodies replaced with values (if
70: * specified), or <code>null</code>.
71: */
72: public Configuration getConfiguration() throws SAXException,
73: ConfigurationException {
74:
75: if (this .conf == null)
76: return null;
77: InterpolatingConfigurationHandler handler = new InterpolatingConfigurationHandler(
78: this .vars, this .conf.getLocation());
79: DefaultConfigurationSerializer ser = new DefaultConfigurationSerializer();
80: ser.serialize(handler, this.conf);
81: return handler.getConfiguration();
82: }
83:
84: }
|