1 4 package com.tc.config.schema.test; 5 6 import com.tc.util.Assert; 7 8 import java.util.ArrayList ; 9 import java.util.Arrays ; 10 import java.util.Collections ; 11 import java.util.HashMap ; 12 import java.util.HashSet ; 13 import java.util.Iterator ; 14 import java.util.List ; 15 import java.util.Map ; 16 import java.util.Set ; 17 18 21 public abstract class BaseConfigBuilder { 22 23 private final Map setProperties; 24 private final Map arrayPropertyTagNames; 25 private final Set allPropertyNames; 26 27 protected int currentIndentLevel; 28 29 protected BaseConfigBuilder(int indentLevel, String [] allProperties) { 30 Assert.eval(indentLevel >= 0); 31 32 this.setProperties = new HashMap (); 33 this.arrayPropertyTagNames = new HashMap (); 34 this.allPropertyNames = new HashSet (); 35 this.allPropertyNames.addAll(Arrays.asList(allProperties)); 36 37 this.currentIndentLevel = indentLevel; 38 } 39 40 protected final void setArrayPropertyTagName(String property, String tagName) { 41 Assert.assertNotNull(property); 42 Assert.assertNotNull(tagName); 43 this.arrayPropertyTagNames.put(property, tagName); 44 } 45 46 protected final void setProperty(String property, int value) { 47 setProperty(property, Integer.toString(value)); 48 } 49 50 protected final void setProperty(String property, boolean value) { 51 setProperty(property, new Boolean (value)); 52 } 53 54 protected final void setProperty(String property, Object value) { 55 Assert.assertNotBlank(property); 56 Assert.assertNotNull(value); 57 Assert.eval(this.allPropertyNames.contains(property)); 58 59 if (value instanceof Object []) { 60 if (this.arrayPropertyTagNames.containsKey(property)) { 61 setProperty(property, array((String ) this.arrayPropertyTagNames.get(property), (Object []) value)); 62 } else { 63 setProperty(property, array(null, (Object []) value)); 64 } 65 } else this.setProperties.put(property, value); 66 } 67 68 protected final void unsetProperty(String property) { 69 Assert.assertNotBlank(property); 70 Assert.eval(this.allPropertyNames.contains(property)); 71 this.setProperties.remove(property); 72 } 73 74 protected String getProperty(String 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 getRawProperty(String 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 [] values; 90 91 public SelfTaggingArray(Object [] values) { 92 Assert.assertNoNullElements(values); 93 this.values = values; 94 } 95 96 public Object [] values() { 97 return this.values; 98 } 99 100 public String toString() { 101 ++currentIndentLevel; 102 103 String out = "\n"; 104 for (int i = 0; i < this.values.length; ++i) { 105 String 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 selfTaggingArray(Object [] values) { 117 Assert.assertNoNullElements(values); 118 return new SelfTaggingArray(values); 119 } 120 121 private class Array { 122 private final String elementTag; 123 private final Object [] values; 124 125 public Array(String elementTag, Object [] values) { 126 Assert.assertNoNullElements(values); 127 128 this.elementTag = elementTag; 129 this.values = values; 130 } 131 132 public String toString() { 133 ++currentIndentLevel; 134 135 String out = ""; 136 for (int i = 0; i < this.values.length; ++i) { 137 String 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 array(String elementTag, Object [] values) { 158 Assert.assertNoNullElements(values); 159 return new Array(elementTag, values); 160 } 161 162 public final boolean isSet(String property) { 163 Assert.assertNotBlank(property); 164 return this.setProperties.containsKey(property); 165 } 166 167 protected final String indent() { 168 return indent(this.currentIndentLevel); 169 } 170 171 private String indent(int level) { 172 String out = "\n"; 173 174 for (int i = 0; i < level; ++i) { 175 out += " "; 176 } 177 178 return out; 179 } 180 181 protected final String elementGroup(String tagName, String [] elementNames) { 182 return openElement(tagName, elementNames) + elements(elementNames) + closeElement(tagName, elementNames); 183 } 184 185 protected final String elements(String [] names) { 187 String out = ""; 188 189 List list = new ArrayList (); 190 list.addAll(Arrays.asList(names)); 191 Collections.shuffle(list); 192 193 Iterator iter = list.iterator(); 194 while (iter.hasNext()) { 195 out += element((String ) iter.next()); 196 } 197 198 return out; 199 } 200 201 protected final String element(String tagName, String 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 element(String name) { 210 return element(name, name); 211 } 212 213 protected final String openElement(String tagName) { 214 String out = indent() + "<" + tagName + ">\n"; 215 ++this.currentIndentLevel; 216 return out; 217 } 218 219 protected final String openElement(String tagName, String [] properties) { 220 Assert.assertNotBlank(tagName); 221 if (anyAreSet(properties)) return openElement(tagName); 222 else return ""; 223 } 224 225 protected final boolean anyAreSet(String [] 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 closeElement(String tagName) { 236 --this.currentIndentLevel; 237 return indent() + "\n</" + tagName + ">"; 238 } 239 240 protected final String closeElement(String tagName, String [] properties) { 241 Assert.assertNotBlank(tagName); 242 if (anyAreSet(properties)) return closeElement(tagName); 243 else return ""; 244 } 245 246 protected String openElement(String tagName, Map attributes) { 247 String out = indent() + "<" + tagName + attributesToString(attributes) + ">\n"; 248 ++this.currentIndentLevel; 249 return out; 250 } 251 252 private String attributesToString(Map attributes) { 253 StringBuffer sb = new StringBuffer (); 254 for (Iterator it = attributes.entrySet().iterator(); it.hasNext();) { 255 Map.Entry entry = (Map.Entry ) 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 propertyAsString(String propertyName) { 266 return getProperty(propertyName).toString(); 267 } 268 269 protected static String [] concat(Object [] data) { 270 List out = new ArrayList (); 271 272 for (int i = 0; i < data.length; ++i) { 273 if (data[i] instanceof String ) out.add(data[i]); 274 else { 275 String [] theData = (String []) 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 []) out.toArray(new String [out.size()]); 283 } 284 285 } 286 | Popular Tags |