KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > test > BaseConfigBuilder


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.test;
5
6 import com.tc.util.Assert;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.Collections JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.HashSet JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16 import java.util.Set JavaDoc;
17
18 /**
19  * The root for all config-builder classes.
20  */

21 public abstract class BaseConfigBuilder {
22
23   private final Map JavaDoc setProperties;
24   private final Map JavaDoc arrayPropertyTagNames;
25   private final Set JavaDoc allPropertyNames;
26
27   protected int currentIndentLevel;
28
29   protected BaseConfigBuilder(int indentLevel, String JavaDoc[] allProperties) {
30     Assert.eval(indentLevel >= 0);
31
32     this.setProperties = new HashMap JavaDoc();
33     this.arrayPropertyTagNames = new HashMap JavaDoc();
34     this.allPropertyNames = new HashSet JavaDoc();
35     this.allPropertyNames.addAll(Arrays.asList(allProperties));
36
37     this.currentIndentLevel = indentLevel;
38   }
39
40   protected final void setArrayPropertyTagName(String JavaDoc property, String JavaDoc tagName) {
41     Assert.assertNotNull(property);
42     Assert.assertNotNull(tagName);
43     this.arrayPropertyTagNames.put(property, tagName);
44   }
45
46   protected final void setProperty(String JavaDoc property, int value) {
47     setProperty(property, Integer.toString(value));
48   }
49
50   protected final void setProperty(String JavaDoc property, boolean value) {
51     setProperty(property, new Boolean JavaDoc(value));
52   }
53
54   protected final void setProperty(String JavaDoc property, Object JavaDoc value) {
55     Assert.assertNotBlank(property);
56     Assert.assertNotNull(value);
57     Assert.eval(this.allPropertyNames.contains(property));
58
59     if (value instanceof Object JavaDoc[]) {
60       if (this.arrayPropertyTagNames.containsKey(property)) {
61         setProperty(property, array((String JavaDoc) this.arrayPropertyTagNames.get(property), (Object JavaDoc[]) value));
62       } else {
63         setProperty(property, array(null, (Object JavaDoc[]) value));
64       }
65     } else this.setProperties.put(property, value);
66   }
67
68   protected final void unsetProperty(String JavaDoc property) {
69     Assert.assertNotBlank(property);
70     Assert.eval(this.allPropertyNames.contains(property));
71     this.setProperties.remove(property);
72   }
73
74   protected String JavaDoc getProperty(String JavaDoc property) {
75     Assert.assertNotBlank(property);
76     Assert.eval(this.allPropertyNames.contains(property));
77     Assert.eval(this.setProperties.containsKey(property));
78     return this.setProperties.get(property).toString();
79   }
80
81   protected final Object JavaDoc getRawProperty(String JavaDoc property) {
82     Assert.assertNotBlank(property);
83     Assert.eval(this.allPropertyNames.contains(property));
84     Assert.eval(this.setProperties.containsKey(property));
85     return this.setProperties.get(property);
86   }
87
88   protected final class SelfTaggingArray {
89     private final Object JavaDoc[] values;
90
91     public SelfTaggingArray(Object JavaDoc[] values) {
92       Assert.assertNoNullElements(values);
93       this.values = values;
94     }
95
96     public Object JavaDoc[] values() {
97       return this.values;
98     }
99
100     public String JavaDoc toString() {
101       ++currentIndentLevel;
102
103       String JavaDoc out = "\n";
104       for (int i = 0; i < this.values.length; ++i) {
105         String JavaDoc value = this.values[i].toString();
106         out += value;
107         if (value.indexOf("\n") < 0) value += "\n";
108       }
109
110       --currentIndentLevel;
111       out += indent();
112       return out;
113     }
114   }
115
116   protected final Object JavaDoc selfTaggingArray(Object JavaDoc[] values) {
117     Assert.assertNoNullElements(values);
118     return new SelfTaggingArray(values);
119   }
120
121   private class Array {
122     private final String JavaDoc elementTag;
123     private final Object JavaDoc[] values;
124
125     public Array(String JavaDoc elementTag, Object JavaDoc[] values) {
126       Assert.assertNoNullElements(values);
127
128       this.elementTag = elementTag;
129       this.values = values;
130     }
131
132     public String JavaDoc toString() {
133       ++currentIndentLevel;
134
135       String JavaDoc out = "";
136       for (int i = 0; i < this.values.length; ++i) {
137         String JavaDoc value = this.values[i].toString();
138
139         if (this.elementTag != null) {
140           out += indent() + "<" + this.elementTag + ">";
141           if (value.indexOf("\n") >= 0) out += "\n";
142         }
143         out += this.values[i];
144         if (this.elementTag != null) {
145           if (value.indexOf("\n") >= 0 && (!value.endsWith("\n"))) out += "\n";
146           if (value.indexOf("\n") >= 0) out += indent();
147           out += "</" + this.elementTag + ">\n";
148         }
149       }
150
151       --currentIndentLevel;
152       out += indent();
153       return out;
154     }
155   }
156
157   private Object JavaDoc array(String JavaDoc elementTag, Object JavaDoc[] values) {
158     Assert.assertNoNullElements(values);
159     return new Array(elementTag, values);
160   }
161
162   public final boolean isSet(String JavaDoc property) {
163     Assert.assertNotBlank(property);
164     return this.setProperties.containsKey(property);
165   }
166
167   protected final String JavaDoc indent() {
168     return indent(this.currentIndentLevel);
169   }
170
171   private String JavaDoc indent(int level) {
172     String JavaDoc out = "\n";
173
174     for (int i = 0; i < level; ++i) {
175       out += " ";
176     }
177
178     return out;
179   }
180
181   protected final String JavaDoc elementGroup(String JavaDoc tagName, String JavaDoc[] elementNames) {
182     return openElement(tagName, elementNames) + elements(elementNames) + closeElement(tagName, elementNames);
183   }
184
185   // We do this to make sure that order doesn't matter in our config file, which is generally what should be the case.
186
protected final String JavaDoc elements(String JavaDoc[] names) {
187     String JavaDoc out = "";
188
189     List JavaDoc list = new ArrayList JavaDoc();
190     list.addAll(Arrays.asList(names));
191     Collections.shuffle(list);
192
193     Iterator JavaDoc iter = list.iterator();
194     while (iter.hasNext()) {
195       out += element((String JavaDoc) iter.next());
196     }
197
198     return out;
199   }
200
201   protected final String JavaDoc element(String JavaDoc tagName, String JavaDoc propertyName) {
202     Assert.assertNotBlank(tagName);
203     Assert.assertNotBlank(propertyName);
204
205     if (isSet(propertyName)) return indent() + "<" + tagName + ">" + getProperty(propertyName) + "</" + tagName + ">";
206     else return "";
207   }
208
209   protected final String JavaDoc element(String JavaDoc name) {
210     return element(name, name);
211   }
212
213   protected final String JavaDoc openElement(String JavaDoc tagName) {
214     String JavaDoc out = indent() + "<" + tagName + ">\n";
215     ++this.currentIndentLevel;
216     return out;
217   }
218
219   protected final String JavaDoc openElement(String JavaDoc tagName, String JavaDoc[] properties) {
220     Assert.assertNotBlank(tagName);
221     if (anyAreSet(properties)) return openElement(tagName);
222     else return "";
223   }
224
225   protected final boolean anyAreSet(String JavaDoc[] properties) {
226     boolean needIt = false;
227
228     for (int i = 0; i < properties.length; ++i) {
229       if (isSet(properties[i])) needIt = true;
230     }
231
232     return needIt;
233   }
234
235   protected final String JavaDoc closeElement(String JavaDoc tagName) {
236     --this.currentIndentLevel;
237     return indent() + "\n</" + tagName + ">";
238   }
239
240   protected final String JavaDoc closeElement(String JavaDoc tagName, String JavaDoc[] properties) {
241     Assert.assertNotBlank(tagName);
242     if (anyAreSet(properties)) return closeElement(tagName);
243     else return "";
244   }
245
246   protected String JavaDoc openElement(String JavaDoc tagName, Map JavaDoc attributes) {
247     String JavaDoc out = indent() + "<" + tagName + attributesToString(attributes) + ">\n";
248     ++this.currentIndentLevel;
249     return out;
250   }
251
252   private String JavaDoc attributesToString(Map JavaDoc attributes) {
253     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
254     for (Iterator JavaDoc it = attributes.entrySet().iterator(); it.hasNext();) {
255       Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
256       sb.append(' ');
257       sb.append(entry.getKey());
258       sb.append("=\"");
259       sb.append(entry.getValue());
260       sb.append("\"");
261     }
262     return sb.toString();
263   }
264
265   protected String JavaDoc propertyAsString(String JavaDoc propertyName) {
266     return getProperty(propertyName).toString();
267   }
268
269   protected static String JavaDoc[] concat(Object JavaDoc[] data) {
270     List JavaDoc out = new ArrayList JavaDoc();
271
272     for (int i = 0; i < data.length; ++i) {
273       if (data[i] instanceof String JavaDoc) out.add(data[i]);
274       else {
275         String JavaDoc[] theData = (String JavaDoc[]) data[i];
276         for (int j = 0; j < theData.length; ++j)
277           out.add(theData[j]);
278       }
279     }
280
281     Collections.shuffle(out);
282     return (String JavaDoc[]) out.toArray(new String JavaDoc[out.size()]);
283   }
284
285 }
286
Popular Tags