001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
003: */
004: package com.tc.config.schema.test;
005:
006: import com.tc.util.Assert;
007:
008: import java.util.ArrayList;
009: import java.util.Arrays;
010: import java.util.Collections;
011: import java.util.HashMap;
012: import java.util.HashSet;
013: import java.util.Iterator;
014: import java.util.List;
015: import java.util.Map;
016: import java.util.Set;
017:
018: /**
019: * The root for all config-builder classes.
020: */
021: public abstract class BaseConfigBuilder {
022:
023: private final Map setProperties;
024: private final Map arrayPropertyTagNames;
025: private final Set allPropertyNames;
026:
027: protected int currentIndentLevel;
028:
029: protected BaseConfigBuilder(int indentLevel, String[] allProperties) {
030: Assert.eval(indentLevel >= 0);
031:
032: this .setProperties = new HashMap();
033: this .arrayPropertyTagNames = new HashMap();
034: this .allPropertyNames = new HashSet();
035: this .allPropertyNames.addAll(Arrays.asList(allProperties));
036:
037: this .currentIndentLevel = indentLevel;
038: }
039:
040: protected final void setArrayPropertyTagName(String property,
041: String tagName) {
042: Assert.assertNotNull(property);
043: Assert.assertNotNull(tagName);
044: this .arrayPropertyTagNames.put(property, tagName);
045: }
046:
047: protected final void setProperty(String property, int value) {
048: setProperty(property, Integer.toString(value));
049: }
050:
051: protected final void setProperty(String property, boolean value) {
052: setProperty(property, new Boolean(value));
053: }
054:
055: protected final void setProperty(String property, Object value) {
056: Assert.assertNotBlank(property);
057: Assert.assertNotNull(value);
058: Assert.eval(this .allPropertyNames.contains(property));
059:
060: if (value instanceof Object[]) {
061: if (this .arrayPropertyTagNames.containsKey(property)) {
062: setProperty(property, array(
063: (String) this .arrayPropertyTagNames
064: .get(property), (Object[]) value));
065: } else {
066: setProperty(property, array(null, (Object[]) value));
067: }
068: } else
069: this .setProperties.put(property, value);
070: }
071:
072: protected final void unsetProperty(String property) {
073: Assert.assertNotBlank(property);
074: Assert.eval(this .allPropertyNames.contains(property));
075: this .setProperties.remove(property);
076: }
077:
078: protected String getProperty(String property) {
079: Assert.assertNotBlank(property);
080: Assert.eval(this .allPropertyNames.contains(property));
081: Assert.eval(this .setProperties.containsKey(property));
082: return this .setProperties.get(property).toString();
083: }
084:
085: protected final Object getRawProperty(String property) {
086: Assert.assertNotBlank(property);
087: Assert.eval(this .allPropertyNames.contains(property));
088: Assert.eval(this .setProperties.containsKey(property));
089: return this .setProperties.get(property);
090: }
091:
092: protected final class SelfTaggingArray {
093: private final Object[] values;
094:
095: public SelfTaggingArray(Object[] values) {
096: Assert.assertNoNullElements(values);
097: this .values = values;
098: }
099:
100: public Object[] values() {
101: return this .values;
102: }
103:
104: public String toString() {
105: ++currentIndentLevel;
106:
107: String out = "\n";
108: for (int i = 0; i < this .values.length; ++i) {
109: String value = this .values[i].toString();
110: out += value;
111: if (value.indexOf("\n") < 0)
112: value += "\n";
113: }
114:
115: --currentIndentLevel;
116: out += indent();
117: return out;
118: }
119: }
120:
121: protected final Object selfTaggingArray(Object[] values) {
122: Assert.assertNoNullElements(values);
123: return new SelfTaggingArray(values);
124: }
125:
126: private class Array {
127: private final String elementTag;
128: private final Object[] values;
129:
130: public Array(String elementTag, Object[] values) {
131: Assert.assertNoNullElements(values);
132:
133: this .elementTag = elementTag;
134: this .values = values;
135: }
136:
137: public String toString() {
138: ++currentIndentLevel;
139:
140: String out = "";
141: for (int i = 0; i < this .values.length; ++i) {
142: String value = this .values[i].toString();
143:
144: if (this .elementTag != null) {
145: out += indent() + "<" + this .elementTag + ">";
146: if (value.indexOf("\n") >= 0)
147: out += "\n";
148: }
149: out += this .values[i];
150: if (this .elementTag != null) {
151: if (value.indexOf("\n") >= 0
152: && (!value.endsWith("\n")))
153: out += "\n";
154: if (value.indexOf("\n") >= 0)
155: out += indent();
156: out += "</" + this .elementTag + ">\n";
157: }
158: }
159:
160: --currentIndentLevel;
161: out += indent();
162: return out;
163: }
164: }
165:
166: private Object array(String elementTag, Object[] values) {
167: Assert.assertNoNullElements(values);
168: return new Array(elementTag, values);
169: }
170:
171: public final boolean isSet(String property) {
172: Assert.assertNotBlank(property);
173: return this .setProperties.containsKey(property);
174: }
175:
176: protected final String indent() {
177: return indent(this .currentIndentLevel);
178: }
179:
180: private String indent(int level) {
181: String out = "\n";
182:
183: for (int i = 0; i < level; ++i) {
184: out += " ";
185: }
186:
187: return out;
188: }
189:
190: protected final String elementGroup(String tagName,
191: String[] elementNames) {
192: return openElement(tagName, elementNames)
193: + elements(elementNames)
194: + closeElement(tagName, elementNames);
195: }
196:
197: // We do this to make sure that order doesn't matter in our config file, which is generally what should be the case.
198: protected final String elements(String[] names) {
199: String out = "";
200:
201: List list = new ArrayList();
202: list.addAll(Arrays.asList(names));
203: Collections.shuffle(list);
204:
205: Iterator iter = list.iterator();
206: while (iter.hasNext()) {
207: out += element((String) iter.next());
208: }
209:
210: return out;
211: }
212:
213: protected final String element(String tagName, String propertyName) {
214: Assert.assertNotBlank(tagName);
215: Assert.assertNotBlank(propertyName);
216:
217: if (isSet(propertyName))
218: return indent() + "<" + tagName + ">"
219: + getProperty(propertyName) + "</" + tagName + ">";
220: else
221: return "";
222: }
223:
224: protected final String element(String name) {
225: return element(name, name);
226: }
227:
228: protected final String openElement(String tagName) {
229: String out = indent() + "<" + tagName + ">\n";
230: ++this .currentIndentLevel;
231: return out;
232: }
233:
234: protected final String selfCloseElement(String tagName,
235: Map attributes) {
236: Assert.assertNotBlank(tagName);
237: String out = indent() + "<" + tagName + " "
238: + attributesToString(attributes) + "/>";
239: return out;
240: }
241:
242: protected final String openElement(String tagName,
243: String[] properties) {
244: Assert.assertNotBlank(tagName);
245: if (anyAreSet(properties))
246: return openElement(tagName);
247: else
248: return "";
249: }
250:
251: protected final boolean anyAreSet(String[] properties) {
252: boolean needIt = false;
253:
254: for (int i = 0; i < properties.length; ++i) {
255: if (isSet(properties[i]))
256: needIt = true;
257: }
258:
259: return needIt;
260: }
261:
262: protected final String closeElement(String tagName) {
263: --this .currentIndentLevel;
264: return indent() + "\n</" + tagName + ">";
265: }
266:
267: protected final String closeElement(String tagName,
268: String[] properties) {
269: Assert.assertNotBlank(tagName);
270: if (anyAreSet(properties))
271: return closeElement(tagName);
272: else
273: return "";
274: }
275:
276: protected String openElement(String tagName, Map attributes) {
277: String out = indent() + "<" + tagName
278: + attributesToString(attributes) + ">\n";
279: ++this .currentIndentLevel;
280: return out;
281: }
282:
283: private String attributesToString(Map attributes) {
284: StringBuffer sb = new StringBuffer();
285: for (Iterator it = attributes.entrySet().iterator(); it
286: .hasNext();) {
287: Map.Entry entry = (Map.Entry) it.next();
288: sb.append(' ');
289: sb.append(entry.getKey());
290: sb.append("=\"");
291: sb.append(entry.getValue());
292: sb.append("\"");
293: }
294: return sb.toString();
295: }
296:
297: protected String propertyAsString(String propertyName) {
298: return getProperty(propertyName).toString();
299: }
300:
301: protected static String[] concat(Object[] data) {
302: List out = new ArrayList();
303:
304: for (int i = 0; i < data.length; ++i) {
305: if (data[i] instanceof String)
306: out.add(data[i]);
307: else {
308: String[] theData = (String[]) data[i];
309: for (int j = 0; j < theData.length; ++j)
310: out.add(theData[j]);
311: }
312: }
313:
314: Collections.shuffle(out);
315: return (String[]) out.toArray(new String[out.size()]);
316: }
317:
318: }
|