1 package org.apache.velocity.tools.config;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.InputStream;
23 import java.io.IOException;
24 import java.util.Iterator;
25 import org.apache.commons.beanutils.BeanUtils;
26 import org.apache.commons.collections.ExtendedProperties;
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 public class PropertiesFactoryConfiguration extends FileFactoryConfiguration
64 {
65 public PropertiesFactoryConfiguration()
66 {
67 this("");
68 }
69
70
71
72
73
74
75
76
77 public PropertiesFactoryConfiguration(String id)
78 {
79 super(PropertiesFactoryConfiguration.class, id);
80 }
81
82
83
84
85
86
87
88 public void read(InputStream input) throws IOException
89 {
90 ExtendedProperties props = new ExtendedProperties();
91 props.load(input);
92
93
94 read(props.subset("tools"));
95 }
96
97 public void read(ExtendedProperties factory)
98 {
99
100 readProperties(factory, this);
101
102
103 readToolboxes(factory);
104
105
106 readData(factory.subset("data"));
107 }
108
109
110 protected void readProperties(ExtendedProperties configProps,
111 Configuration config)
112 {
113 ExtendedProperties properties = configProps.subset("property");
114 if (properties != null)
115 {
116 for (Iterator i = properties.getKeys(); i.hasNext(); )
117 {
118 String name = (String)i.next();
119 String value = properties.getString(name);
120
121 ExtendedProperties propProps = properties.subset(name);
122 if (propProps.size() == 1)
123 {
124
125 config.setProperty(name, value);
126 }
127 else
128 {
129
130 Property property = new Property();
131 property.setName(name);
132 property.setValue(value);
133
134
135 setProperties(propProps, property);
136 }
137 }
138 }
139 }
140
141 protected void readToolboxes(ExtendedProperties factory)
142 {
143 String[] scopes = factory.getStringArray("toolbox");
144 for (String scope : scopes)
145 {
146 ToolboxConfiguration toolbox = new ToolboxConfiguration();
147 toolbox.setScope(scope);
148 addToolbox(toolbox);
149
150 ExtendedProperties toolboxProps = factory.subset(scope);
151 readTools(toolboxProps, toolbox);
152 readProperties(toolboxProps, toolbox);
153 }
154 }
155
156 protected void readTools(ExtendedProperties tools,
157 ToolboxConfiguration toolbox)
158 {
159 for (Iterator i = tools.getKeys(); i.hasNext(); )
160 {
161 String key = (String)i.next();
162
163
164 if (key.indexOf('.') >= 0)
165 {
166 continue;
167 }
168
169 String classname = tools.getString(key);
170 ToolConfiguration tool = new ToolConfiguration();
171 tool.setClassname(classname);
172 tool.setKey(key);
173 toolbox.addTool(tool);
174
175
176 ExtendedProperties toolProps = tools.subset(key);
177 readProperties(toolProps, tool);
178
179
180 for (Iterator j = toolProps.getKeys(); j.hasNext(); )
181 {
182 String name = (String)j.next();
183 if (!name.equals(tool.getKey()))
184 {
185 tool.setProperty(name, toolProps.getString(name));
186 }
187 }
188
189
190 String restrictTo = toolProps.getString("restrictTo");
191 tool.setRestrictTo(restrictTo);
192 }
193 }
194
195 protected void readData(ExtendedProperties dataset)
196 {
197 if (dataset != null)
198 {
199 for (Iterator i = dataset.getKeys(); i.hasNext(); )
200 {
201 String key = (String)i.next();
202
203
204 if (key.indexOf('.') >= 0)
205 {
206 continue;
207 }
208
209 Data data = new Data();
210 data.setKey(key);
211 data.setValue(dataset.getString(key));
212
213
214 ExtendedProperties props = dataset.subset(key);
215 setProperties(props, data);
216
217 addData(data);
218 }
219 }
220 }
221
222 protected void setProperties(ExtendedProperties props, Data data)
223 {
224
225
226 try
227 {
228 BeanUtils.populate(data, props);
229 }
230 catch (Exception e)
231 {
232 throw new RuntimeException(e);
233 }
234 }
235
236 }