1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.fulcrum.yaafi.framework.util;
20
21 import java.util.Map;
22
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.configuration.DefaultConfiguration;
26 import org.apache.avalon.framework.logger.Logger;
27
28 /**
29 * Helper class to expand the value and all attributes.
30 *
31 * @author <a href="mailto:siegfried.goeschl@it20one.at">Siegfried Goeschl</a>
32 */
33 public class ConfigurationUtil
34 {
35 /**
36 * Expand place holders found in values or attrbute values with the
37 * content of the given variables. The implementation assumes that
38 * the given configuration can be cast to a DefaultConfiguration
39 * otherwise we can't use any setters.
40 *
41 * @param logger the logger to write diagnostic messages
42 * @param defaultConfiguration the configuration
43 * @param vars the map holding the variables
44 * @throws ConfigurationException parsing the configuration failed
45 */
46 public static void expand(Logger logger, DefaultConfiguration defaultConfiguration, Map vars) throws ConfigurationException
47 {
48 if((vars == null) || (vars.size() == 0))
49 {
50 return;
51 }
52
53
54
55 if(defaultConfiguration.getValue(null) != null)
56 {
57 String oldValue = defaultConfiguration.getValue();
58 String newValue = ConfigurationUtil.expand(oldValue, vars);
59 defaultConfiguration.setValue(newValue);
60
61 if(oldValue.equals(newValue) == false)
62 {
63 logger.debug("Changed element <"
64 + defaultConfiguration.getName()
65 + "> from '"
66 + oldValue
67 + "' ==> '"
68 + newValue
69 + "'"
70 );
71 }
72 }
73
74
75
76 String attributeName = null;
77 String[] attributeNames = defaultConfiguration.getAttributeNames();
78
79 for(int i=0; i<attributeNames.length; i++)
80 {
81 attributeName = attributeNames[i];
82 String oldAttributeValue = defaultConfiguration.getAttribute(attributeName);
83 String newAttributeValue = ConfigurationUtil.expand(oldAttributeValue, vars);
84 defaultConfiguration.setAttribute(attributeName, newAttributeValue);
85
86 if(oldAttributeValue.equals(newAttributeValue) == false)
87 {
88 logger.debug("Changed attribute '"
89 + defaultConfiguration.getName() + "@" + attributeName
90 + "' from '"
91 + oldAttributeValue
92 + "' ==> '"
93 + newAttributeValue
94 + "'"
95 );
96 }
97 }
98
99
100
101 Configuration[] children = defaultConfiguration.getChildren();
102
103 for(int i=0; i<children.length; i++)
104 {
105 ConfigurationUtil.expand(logger, ((DefaultConfiguration) children[i]), vars);
106 }
107 }
108
109 /**
110 * @return the expand a string
111 */
112 private static String expand(String value, Map vars)
113 {
114 return StringUtils.stringSubstitution(value, vars, true).toString();
115 }
116 }