|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| package org.jconfig; |
|
8 |
| |
|
9 |
| import java.io.Serializable; |
|
10 |
| import java.util.Collection; |
|
11 |
| import java.util.Iterator; |
|
12 |
| import java.util.StringTokenizer; |
|
13 |
| import java.util.Vector; |
|
14 |
| import java.util.Set; |
|
15 |
| import java.util.SortedMap; |
|
16 |
| import javax.swing.event.EventListenerList; |
|
17 |
| |
|
18 |
| import org.jconfig.event.ConfigurationChangedEvent; |
|
19 |
| import org.jconfig.event.ConfigurationChangedEventImpl; |
|
20 |
| |
|
21 |
| |
|
22 |
| |
|
23 |
| |
|
24 |
| |
|
25 |
| |
|
26 |
| |
|
27 |
| |
|
28 |
| |
|
29 |
| public class NestedConfiguration extends DefaultConfiguration implements Serializable, Configuration { |
|
30 |
| |
|
31 |
| private static final VariableManager vm = VariableManager.getInstance(); |
|
32 |
| |
|
33 |
| private EventListenerList configurationListenerList = new EventListenerList(); |
|
34 |
| |
|
35 |
0
| protected NestedConfiguration() {
|
|
36 |
| } |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
26
| public NestedConfiguration(String configName) {
|
|
46 |
26
| super(configName);
|
|
47 |
| } |
|
48 |
| |
|
49 |
5
| public String[] getCategoryNames() {
|
|
50 |
5
| Set allCategories = categories.keySet();
|
|
51 |
5
| Vector all = new Vector();
|
|
52 |
5
| Iterator it = allCategories.iterator();
|
|
53 |
5
| while ( it.hasNext() ) {
|
|
54 |
24
| String name = (String)it.next();
|
|
55 |
24
| addCategoryName(all,name);
|
|
56 |
| } |
|
57 |
5
| if ( baseConfigName != null ) {
|
|
58 |
2
| Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
|
|
59 |
2
| String[] parentCategories = cfg.getCategoryNames();
|
|
60 |
2
| for ( int i = 0 ; i < parentCategories.length ; i++) {
|
|
61 |
14
| if ( all.indexOf(parentCategories[i]) == -1 ) {
|
|
62 |
8
| all.add(parentCategories[i]);
|
|
63 |
| } |
|
64 |
| } |
|
65 |
| } |
|
66 |
5
| return (String[]) all.toArray(new String[0]);
|
|
67 |
| } |
|
68 |
| |
|
69 |
32
| private void addCategoryName(Vector all,String name) {
|
|
70 |
32
| all.add(name);
|
|
71 |
32
| NestedCategory myCat = (NestedCategory)getCategory(name);
|
|
72 |
32
| Collection children = myCat.getChildCategories();
|
|
73 |
32
| if ( children.size() > 0 ) {
|
|
74 |
8
| Iterator it = children.iterator();
|
|
75 |
8
| while ( it.hasNext() ) {
|
|
76 |
8
| NestedCategory nc = (NestedCategory)it.next();
|
|
77 |
8
| String cp = name + "/"+ nc.getCategoryName();
|
|
78 |
8
| addCategoryName(all,cp);
|
|
79 |
| } |
|
80 |
| } |
|
81 |
| } |
|
82 |
| |
|
83 |
2
| protected String[] getCategoryNames(boolean includeParent) {
|
|
84 |
2
| Set allCategories = categories.keySet();
|
|
85 |
2
| Vector all = new Vector(allCategories);
|
|
86 |
2
| if ( baseConfigName != null && includeParent ) {
|
|
87 |
0
| Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
|
|
88 |
0
| String[] parentCategories = cfg.getCategoryNames();
|
|
89 |
0
| for ( int i = 0 ; i < parentCategories.length ; i++) {
|
|
90 |
0
| if ( all.indexOf(parentCategories[i]) == -1 ) {
|
|
91 |
0
| all.add(parentCategories[i]);
|
|
92 |
| } |
|
93 |
| } |
|
94 |
| } |
|
95 |
2
| return (String[]) all.toArray(new String[0]);
|
|
96 |
| } |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| |
|
105 |
| |
|
106 |
| |
|
107 |
30
| public void setCategory(String name, boolean main) {
|
|
108 |
30
| if (name != null) {
|
|
109 |
30
| if (main) {
|
|
110 |
26
| mainCategory = name;
|
|
111 |
| } |
|
112 |
30
| if (!categories.containsKey(name)) {
|
|
113 |
30
| Category category = new NestedCategory(name);
|
|
114 |
30
| category.setConfigurationName(configName);
|
|
115 |
30
| category.addCategoryListener(new MyCategoryListener());
|
|
116 |
30
| categories.put(name, category);
|
|
117 |
30
| markDirty();
|
|
118 |
30
| category.fireCategoryChangedEvent(
|
|
119 |
| new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null )); |
|
120 |
| } |
|
121 |
| } |
|
122 |
| } |
|
123 |
| |
|
124 |
122
| public void setCategory(Category category) {
|
|
125 |
| |
|
126 |
122
| category.setConfigurationName(configName);
|
|
127 |
122
| category.addCategoryListener(new MyCategoryListener());
|
|
128 |
122
| categories.put(category.getCategoryName(), category);
|
|
129 |
122
| markDirty();
|
|
130 |
122
| category.fireCategoryChangedEvent(
|
|
131 |
| new ConfigurationChangedEventImpl(ConfigurationChangedEvent.CATEGORY_ADDED, category, null, null, null )); |
|
132 |
| |
|
133 |
| } |
|
134 |
| |
|
135 |
| |
|
136 |
| |
|
137 |
| |
|
138 |
| |
|
139 |
| |
|
140 |
| |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
42
| public String getProperty(String key,String defaultValue,String categoryName) {
|
|
149 |
42
| boolean isMainCat = false;
|
|
150 |
42
| if (key == null) {
|
|
151 |
0
| return defaultValue;
|
|
152 |
| } |
|
153 |
42
| if ( categoryName == null ) {
|
|
154 |
0
| isMainCat = true;
|
|
155 |
0
| categoryName = mainCategory;
|
|
156 |
| } |
|
157 |
42
| else if ( categoryName.indexOf("/") == -1 ) {
|
|
158 |
15
| if (!categories.containsKey(categoryName)) {
|
|
159 |
1
| isMainCat = true;
|
|
160 |
1
| categoryName = mainCategory;
|
|
161 |
| } |
|
162 |
| } |
|
163 |
42
| String tmp = null;
|
|
164 |
42
| Category category = getCategory(categoryName);
|
|
165 |
42
| if ( category != null ) {
|
|
166 |
42
| if ( category.getCategoryName().equals(mainCategory)) {
|
|
167 |
7
| isMainCat = true;
|
|
168 |
| } |
|
169 |
42
| tmp = category.getProperty(key);
|
|
170 |
| } |
|
171 |
| |
|
172 |
| |
|
173 |
42
| if ( tmp == null && !isMainCat) {
|
|
174 |
19
| category = getCategory(mainCategory);
|
|
175 |
19
| tmp = category.getProperty(key);
|
|
176 |
| } |
|
177 |
| |
|
178 |
| |
|
179 |
42
| if ( tmp == null ) {
|
|
180 |
20
| if ( baseConfigName != null ) {
|
|
181 |
12
| Configuration cfg = ConfigurationManager.getConfiguration(baseConfigName);
|
|
182 |
12
| tmp = cfg.getProperty(key,defaultValue,categoryName);
|
|
183 |
| } |
|
184 |
| else { |
|
185 |
8
| tmp = defaultValue;
|
|
186 |
| } |
|
187 |
| } |
|
188 |
42
| return tmp;
|
|
189 |
| } |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
0
| public String toString() {
|
|
197 |
0
| StringBuffer buffer = new StringBuffer();
|
|
198 |
0
| String[] cats = getCategoryNames();
|
|
199 |
0
| for (int i = 0; i < cats.length; i++) {
|
|
200 |
0
| buffer.append("Category=");
|
|
201 |
0
| buffer.append(cats[i]);
|
|
202 |
0
| String[] propNames = getPropertyNames(cats[i]);
|
|
203 |
0
| if (propNames != null) {
|
|
204 |
0
| buffer.append("\n");
|
|
205 |
0
| for (int j = 0; j < propNames.length; j++) {
|
|
206 |
0
| buffer.append(" ");
|
|
207 |
0
| buffer.append(propNames[j]);
|
|
208 |
0
| buffer.append("=");
|
|
209 |
| |
|
210 |
0
| buffer.append("\n");
|
|
211 |
| } |
|
212 |
| } |
|
213 |
| } |
|
214 |
0
| return buffer.toString();
|
|
215 |
| } |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
| |
|
222 |
| |
|
223 |
2
| public String getXMLAsString() {
|
|
224 |
2
| StringBuffer buffer = new StringBuffer();
|
|
225 |
| |
|
226 |
| |
|
227 |
2
| buffer.append("<?xml version=\"1.0\"");
|
|
228 |
2
| if ( getEncoding() != null ) {
|
|
229 |
0
| buffer.append(" encoding=\""+getEncoding()+"\"");
|
|
230 |
| } |
|
231 |
2
| buffer.append(" ?>\n");
|
|
232 |
2
| buffer.append("<properties");
|
|
233 |
2
| if ( baseConfigName != null ) {
|
|
234 |
1
| buffer.append(" extends=\"");
|
|
235 |
1
| buffer.append(baseConfigName);
|
|
236 |
1
| buffer.append("\"");
|
|
237 |
| } |
|
238 |
2
| buffer.append(">\n");
|
|
239 |
2
| addIncludeBlock(buffer);
|
|
240 |
2
| addVariableBlock(buffer);
|
|
241 |
| |
|
242 |
2
| String[] cats = getCategoryNames(false);
|
|
243 |
2
| for (int i = 0; i < cats.length; i++) {
|
|
244 |
10
| NestedCategory nc = (NestedCategory)getCategory(cats[i]);
|
|
245 |
10
| generateCategoryString(buffer, nc,nc.getCategoryName(),0);
|
|
246 |
| } |
|
247 |
2
| buffer.append("</properties>\n");
|
|
248 |
2
| return buffer.toString();
|
|
249 |
| } |
|
250 |
| |
|
251 |
16
| private void generateCategoryString(StringBuffer buffer,NestedCategory category,String categoryPath,int indent) {
|
|
252 |
16
| indent +=2;
|
|
253 |
16
| for ( int i = 0; i < indent;i++) {
|
|
254 |
48
| buffer.append(" ");
|
|
255 |
| } |
|
256 |
16
| buffer.append("<category name=\"");
|
|
257 |
16
| buffer.append(escapeForXML(category.getCategoryName()));
|
|
258 |
16
| buffer.append("\">\n");
|
|
259 |
16
| SortedMap sm = getSortedProperties(categoryPath,false);
|
|
260 |
16
| if (sm != null) {
|
|
261 |
16
| Iterator nit = sm.keySet().iterator();
|
|
262 |
16
| while (nit.hasNext()) {
|
|
263 |
21
| String name = (String) nit.next();
|
|
264 |
21
| String value = (String)sm.get(name);
|
|
265 |
21
| for ( int i = 0; i < indent+2;i++) {
|
|
266 |
102
| buffer.append(" ");
|
|
267 |
| } |
|
268 |
21
| buffer.append("<");
|
|
269 |
21
| buffer.append(escapeForXML(name));
|
|
270 |
21
| buffer.append(">");
|
|
271 |
| |
|
272 |
21
| buffer.append(escapeForXML(value));
|
|
273 |
21
| buffer.append("</");
|
|
274 |
21
| buffer.append(escapeForXML(name));
|
|
275 |
21
| buffer.append(">\n");
|
|
276 |
| } |
|
277 |
16
| Collection children = category.getChildCategories();
|
|
278 |
16
| if ( children.size() > 0 ) {
|
|
279 |
4
| Iterator it = children.iterator();
|
|
280 |
4
| while ( it.hasNext() ) {
|
|
281 |
6
| NestedCategory nc = (NestedCategory)it.next();
|
|
282 |
6
| String cp = categoryPath + "/"+ nc.getCategoryName();
|
|
283 |
6
| generateCategoryString(buffer, nc,cp,indent);
|
|
284 |
| } |
|
285 |
| } |
|
286 |
16
| for ( int i = 0; i < indent;i++) {
|
|
287 |
48
| buffer.append(" ");
|
|
288 |
| } |
|
289 |
16
| buffer.append("</category>\n");
|
|
290 |
16
| indent -=2;
|
|
291 |
| } |
|
292 |
| } |
|
293 |
| |
|
294 |
| |
|
295 |
| |
|
296 |
| |
|
297 |
| |
|
298 |
| |
|
299 |
136
| public Category getCategory(String name) {
|
|
300 |
136
| return getCategory(name,true);
|
|
301 |
| } |
|
302 |
| |
|
303 |
136
| private Category getCategory(String name,boolean create) {
|
|
304 |
136
| if(name == null) {
|
|
305 |
0
| name = mainCategory;
|
|
306 |
| } |
|
307 |
136
| NestedCategory category = null;
|
|
308 |
136
| if ( name.indexOf("/") != -1 ) {
|
|
309 |
50
| boolean first = true;
|
|
310 |
50
| StringTokenizer sto = new StringTokenizer(name,"/");
|
|
311 |
50
| while ( sto.hasMoreElements() ) {
|
|
312 |
125
| String current = (String)sto.nextElement();
|
|
313 |
125
| if ( first ) {
|
|
314 |
50
| category = (NestedCategory)categories.get(current);
|
|
315 |
50
| first = false;
|
|
316 |
| } |
|
317 |
| else { |
|
318 |
75
| if ( category != null ) {
|
|
319 |
71
| category = category.getCategory(current);
|
|
320 |
| } |
|
321 |
| } |
|
322 |
| } |
|
323 |
| } |
|
324 |
| else { |
|
325 |
86
| category = (NestedCategory)categories.get(name);
|
|
326 |
86
| if( category == null && create ) {
|
|
327 |
0
| category = new NestedCategory(name);
|
|
328 |
0
| categories.put(name, category);
|
|
329 |
| } |
|
330 |
| } |
|
331 |
136
| if ( category == null && create ) {
|
|
332 |
8
| category = createCategory(name);
|
|
333 |
| } |
|
334 |
136
| return category;
|
|
335 |
| } |
|
336 |
| |
|
337 |
8
| private NestedCategory createCategory(String name) {
|
|
338 |
8
| StringTokenizer sto = new StringTokenizer(name,"/");
|
|
339 |
8
| String dir = null;
|
|
340 |
8
| boolean first = true;
|
|
341 |
8
| NestedCategory category = null;
|
|
342 |
8
| NestedCategory parent = null;
|
|
343 |
8
| while ( sto.hasMoreElements() ) {
|
|
344 |
18
| String current = (String)sto.nextElement();
|
|
345 |
18
| if ( first ) {
|
|
346 |
8
| category = (NestedCategory)categories.get(current);
|
|
347 |
8
| if ( category == null ) {
|
|
348 |
4
| setCategory(current);
|
|
349 |
4
| category = (NestedCategory)categories.get(current);
|
|
350 |
| } |
|
351 |
8
| parent = category;
|
|
352 |
8
| first = false;
|
|
353 |
| } |
|
354 |
| else { |
|
355 |
10
| if ( category != null ) {
|
|
356 |
10
| category = category.getCategory(current);
|
|
357 |
10
| if ( category == null ) {
|
|
358 |
8
| NestedCategory newCategory = new NestedCategory(current);
|
|
359 |
8
| parent.addCategory(newCategory);
|
|
360 |
8
| category = newCategory;
|
|
361 |
| } |
|
362 |
| } |
|
363 |
10
| parent = category;
|
|
364 |
| } |
|
365 |
| } |
|
366 |
8
| return category;
|
|
367 |
| } |
|
368 |
| |
|
369 |
0
| public boolean containsCategory(String categoryName) {
|
|
370 |
0
| Category cat = getCategory(categoryName,false);
|
|
371 |
0
| if ( cat == null ) {
|
|
372 |
0
| return false;
|
|
373 |
| } |
|
374 |
| else { |
|
375 |
0
| return true;
|
|
376 |
| } |
|
377 |
| } |
|
378 |
| |
|
379 |
| } |