1 17 package org.apache.ws.jaxme.js; 18 19 import java.io.IOException ; 20 import java.io.StringWriter ; 21 import java.lang.reflect.Array ; 22 import java.util.ArrayList ; 23 import java.util.Collection ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 29 30 34 public abstract class IndentationEngineImpl implements IndentationEngine { 35 private int level = 0; 36 final private List lines = new ArrayList (); 37 private int cursor = -1; 38 39 private class IndentedLine { 40 int indLevel; 41 Object [] tokens; 42 } 43 44 private class PlaceHolderImpl implements PlaceHolder { 45 private final boolean autoRemovable; 46 private final String name; 47 private final int plLevel; 48 private Map properties; 49 public PlaceHolderImpl(String pName, boolean pAutoRemovable) { 50 name = pName; 51 autoRemovable = pAutoRemovable; 52 plLevel = IndentationEngineImpl.this.getLevel(); 53 } 54 public String getName() { return name; } 55 public boolean isAutoRemovable() { return autoRemovable; } 56 public void remove() { removePlaceHolder(this); } 57 public void setProperty(String pKey, Object pValue) { 58 if (properties == null) { 59 properties = new HashMap (); 60 } 61 properties.put(pKey, pValue); 62 } 63 public Object getProperty(String pKey) { return properties == null ? null : properties.get(pKey); } 64 public int getLevel() { return plLevel; } 65 } 66 67 protected IndentationEngineImpl() {} 68 69 public boolean isEmpty() { return lines.size() == 0; } 70 public void clear() { lines.clear(); } 71 public void indent() { ++level; } 72 public void unindent() { --level; } 73 public void setLevel(int pLevel) { level = pLevel; } 74 public int getLevel() { return level; } 75 76 public void addLine(int pLevel, Object [] pTokens) { 77 IndentedLine l = new IndentedLine(); 78 l.indLevel = pLevel; 79 l.tokens = pTokens; 80 if (cursor == -1) { 81 lines.add(l); 82 } else { 83 lines.add(cursor++, l); 84 } 85 } 86 87 public void moveToTop() { 88 cursor = 0; 89 } 90 91 public void moveToBottom() { 92 cursor = -1; 93 } 94 95 public PlaceHolder newPlaceHolder(String pName, boolean pAutoRemovable) { 96 PlaceHolder result = new PlaceHolderImpl(pName, pAutoRemovable); 97 addLine(result); 98 return result; 99 } 100 101 public PlaceHolder getPlaceHolder(String pName) { 102 for (int i = 0; i < lines.size(); i++) { 103 IndentedLine l = (IndentedLine) lines.get(i); 104 Object [] tokens = l.tokens; 105 if (tokens.length > 0 && tokens[0] instanceof PlaceHolder) { 106 PlaceHolder placeHolder = (PlaceHolder) tokens[0]; 107 if (pName.equals(placeHolder.getName())) { 108 cursor = i+1; 109 level = placeHolder.getLevel(); 110 return placeHolder; 111 } 112 } 113 } 114 return null; 115 } 116 117 protected void removePlaceHolder(PlaceHolder pPlaceHolder) { 118 String name = pPlaceHolder.getName(); 119 for (int i = 0; i < lines.size(); i++) { 120 IndentedLine l = (IndentedLine) lines.get(i); 121 Object [] tokens = l.tokens; 122 if (tokens.length > 0 && tokens[0] instanceof PlaceHolder) { 123 PlaceHolder placeHolder = (PlaceHolder) tokens[0]; 124 if (name.equals(placeHolder.getName())) { 125 lines.remove(i); 126 if (cursor > i) { --cursor; } 127 return; 128 } 129 } 130 } 131 throw new IllegalStateException ("The placeholder " + pPlaceHolder.getName() + 132 " was not found and cannot be removed."); 133 } 134 135 protected void checkNulls(Object pTokens) { 136 if (pTokens == null) { 137 throw new NullPointerException ("Null token detected."); 138 } 139 if (pTokens.getClass().isArray()) { 140 if (!pTokens.getClass().getComponentType().isPrimitive()) { 141 Object [] tokens = (Object []) pTokens; 142 for (int i = 0; i < tokens.length; i++) { 143 checkNulls(tokens[i]); 144 } 145 } 146 } else if (pTokens instanceof Collection ) { 147 for (Iterator iter = ((Collection ) pTokens).iterator(); iter.hasNext(); ) { 148 checkNulls(iter.next()); 149 } 150 } else if (pTokens instanceof JavaSource) { 151 if (!(this instanceof JavaSource)) { 152 throw new IllegalStateException ("An instance of JavaSource cannot be added to an instance of " + 153 getClass().getName()); 154 } 155 if (!(pTokens instanceof JavaInnerClass)) { 156 throw new IllegalStateException ("Only inner classes can be added, outer classes are standalone objects."); 157 } 158 JavaInnerClass jic = (JavaInnerClass) pTokens; 159 JavaSource js = (JavaSource) this; 160 if (!jic.getOuterClass().equals(js)) { 161 throw new IllegalStateException ("The inner class " + jic.getQName() + 162 " cannot be added to a different outer class " + 163 js.getQName()); 164 } 165 } 166 } 167 168 public void addLine(Object [] pTokens) { 169 checkNulls(pTokens); 170 addLine(getLevel(), pTokens); 171 } 172 173 public void addLine() { 174 addLine(new Object [0]); 175 } 176 public void addLine(Object pLine) { 177 addLine(new Object []{pLine}); 178 } 179 public void addLine(Object pToken1, Object pToken2) { 180 addLine(new Object []{pToken1, pToken2}); 181 } 182 public void addLine(Object pToken1, Object pToken2, 183 Object pToken3) { 184 addLine(new Object []{pToken1, pToken2, pToken3}); 185 } 186 public void addLine(Object pToken1, Object pToken2, 187 Object pToken3, Object pToken4) { 188 addLine(new Object []{pToken1, pToken2, pToken3, pToken4}); 189 } 190 public void addLine(Object pToken1, Object pToken2, 191 Object pToken3, Object pToken4, Object pToken5) { 192 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5}); 193 } 194 public void addLine(Object pToken1, Object pToken2, 195 Object pToken3, Object pToken4, Object pToken5, 196 Object pToken6) { 197 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5, 198 pToken6}); 199 } 200 public void addLine(Object pToken1, Object pToken2, 201 Object pToken3, Object pToken4, Object pToken5, 202 Object pToken6, Object pToken7) { 203 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5, 204 pToken6, pToken7}); 205 } 206 public void addLine(Object pToken1, Object pToken2, 207 Object pToken3, Object pToken4, Object pToken5, 208 Object pToken6, Object pToken7, Object pToken8) { 209 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5, 210 pToken6, pToken7, pToken8}); 211 } 212 public void addLine(Object pToken1, Object pToken2, 213 Object pToken3, Object pToken4, Object pToken5, 214 Object pToken6, Object pToken7, Object pToken8, 215 Object pToken9) { 216 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5, 217 pToken6, pToken7, pToken8, pToken9}); 218 } 219 public void addLine(Object pToken1, Object pToken2, 220 Object pToken3, Object pToken4, Object pToken5, 221 Object pToken6, Object pToken7, Object pToken8, 222 Object pToken9, Object pToken10) { 223 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5, 224 pToken6, pToken7, pToken8, pToken9, pToken10}); 225 } 226 public void addLine(Object pToken1, Object pToken2, 227 Object pToken3, Object pToken4, Object pToken5, 228 Object pToken6, Object pToken7, Object pToken8, 229 Object pToken9, Object pToken10, Object pToken11) { 230 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5, 231 pToken6, pToken7, pToken8, pToken9, pToken10, 232 pToken11}); 233 } 234 public void addLine(Object pToken1, Object pToken2, 235 Object pToken3, Object pToken4, Object pToken5, 236 Object pToken6, Object pToken7, Object pToken8, 237 Object pToken9, Object pToken10, Object pToken11, 238 Object pToken12) { 239 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5, 240 pToken6, pToken7, pToken8, pToken9, pToken10, 241 pToken11, pToken12}); 242 } 243 public void addLine(Object pToken1, Object pToken2, 244 Object pToken3, Object pToken4, Object pToken5, 245 Object pToken6, Object pToken7, Object pToken8, 246 Object pToken9, Object pToken10, Object pToken11, 247 Object pToken12, Object pToken13) { 248 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5, 249 pToken6, pToken7, pToken8, pToken9, pToken10, 250 pToken11, pToken12, pToken13}); 251 } 252 public void addLine(Object pToken1, Object pToken2, 253 Object pToken3, Object pToken4, Object pToken5, 254 Object pToken6, Object pToken7, Object pToken8, 255 Object pToken9, Object pToken10, Object pToken11, 256 Object pToken12, Object pToken13, Object pToken14) { 257 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5, 258 pToken6, pToken7, pToken8, pToken9, pToken10, 259 pToken11, pToken12, pToken13, pToken14}); 260 } 261 public void addLine(Object pToken1, Object pToken2, 262 Object pToken3, Object pToken4, Object pToken5, 263 Object pToken6, Object pToken7, Object pToken8, 264 Object pToken9, Object pToken10, Object pToken11, 265 Object pToken12, Object pToken13, Object pToken14, 266 Object pToken15) { 267 addLine(new Object []{pToken1, pToken2, pToken3, pToken4, pToken5, 268 pToken6, pToken7, pToken8, pToken9, pToken10, 269 pToken11, pToken12, pToken13, pToken14, pToken15}); 270 } 271 272 public String [] getLines(int pLevel) { 273 String [] result = new String [lines.size()]; 274 return result; 275 } 276 277 protected Iterator getLines() { 278 return lines.iterator(); 279 } 280 281 public void write(IndentationTarget pTarget, Object pObject) 282 throws IOException { 283 if (pObject.getClass().isArray()) { 284 int arrayLength = Array.getLength(pObject); 287 for (int i = 0; i < arrayLength; i++) { 288 write(pTarget, Array.get(pObject, i)); 289 } 290 } else if (pObject instanceof JavaSourceObject) { 291 pTarget.write(((JavaSourceObject) pObject).getName()); 292 } else if (pObject instanceof List ) { 293 for (Iterator iter = ((List ) pObject).iterator(); iter.hasNext(); ) { 294 write(pTarget, iter.next()); 295 } 296 } else if (pObject instanceof Class ) { 297 JavaQName name = JavaQNameImpl.getInstance((Class ) pObject); 298 write(pTarget, pTarget.asString(name)); 299 } else if (pObject instanceof JavaQName) { 300 write(pTarget, pTarget.asString((JavaQName) pObject)); 301 } else if (pObject instanceof IndentedObject) { 302 ((IndentedObject) pObject).write(this, pTarget); 303 } else { 304 pTarget.write(pObject.toString()); 305 } 306 } 307 308 public void write(IndentationTarget pTarget) throws IOException { 309 for (Iterator iter = getLines(); iter.hasNext(); ) { 310 IndentedLine l = (IndentedLine) iter.next(); 311 Object [] tokens = l.tokens; 312 if (tokens.length == 1 && tokens[0] instanceof PlaceHolder) { 313 PlaceHolder placeHolder = (PlaceHolder) tokens[0]; 314 if (!placeHolder.isAutoRemovable()) { 315 throw new IllegalStateException ("The PlaceHolder " + placeHolder.getName() + " has not been removed."); 316 } 317 } else { 318 pTarget.indent(l.indLevel); 319 for (int i = 0; i < tokens.length; i++) { 320 write(pTarget, tokens[i]); 321 } 322 pTarget.write(); 323 } 324 } 325 } 326 327 public String asString() { 328 StringWriter sw = new StringWriter (); 329 IndentationTarget target = new WriterTarget(sw); 330 try { 331 write(target); 332 } catch (IOException e) { 333 throw new IllegalStateException (e.getMessage()); 334 } 335 return sw.toString(); 336 } 337 } 338 | Popular Tags |