KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > js > IndentationEngineImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.js;
18
19 import java.io.IOException JavaDoc;
20 import java.io.StringWriter JavaDoc;
21 import java.lang.reflect.Array JavaDoc;
22 import java.util.ArrayList JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Map JavaDoc;
28
29
30 /**
31  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
32  * @version $Id: IndentationEngineImpl.java,v 1.5 2005/04/21 11:25:23 mac Exp $
33  */

34 public abstract class IndentationEngineImpl implements IndentationEngine {
35   private int level = 0;
36   final private List JavaDoc lines = new ArrayList JavaDoc();
37   private int cursor = -1;
38
39   private class IndentedLine {
40     int indLevel;
41     Object JavaDoc[] tokens;
42   }
43
44   private class PlaceHolderImpl implements PlaceHolder {
45     private final boolean autoRemovable;
46     private final String JavaDoc name;
47     private final int plLevel;
48     private Map JavaDoc properties;
49     public PlaceHolderImpl(String JavaDoc pName, boolean pAutoRemovable) {
50       name = pName;
51       autoRemovable = pAutoRemovable;
52       plLevel = IndentationEngineImpl.this.getLevel();
53     }
54     public String JavaDoc getName() { return name; }
55     public boolean isAutoRemovable() { return autoRemovable; }
56     public void remove() { removePlaceHolder(this); }
57     public void setProperty(String JavaDoc pKey, Object JavaDoc pValue) {
58       if (properties == null) {
59         properties = new HashMap JavaDoc();
60       }
61       properties.put(pKey, pValue);
62     }
63     public Object JavaDoc getProperty(String JavaDoc 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 JavaDoc[] 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 JavaDoc pName, boolean pAutoRemovable) {
96     PlaceHolder result = new PlaceHolderImpl(pName, pAutoRemovable);
97     addLine(result);
98     return result;
99   }
100
101   public PlaceHolder getPlaceHolder(String JavaDoc pName) {
102     for (int i = 0; i < lines.size(); i++) {
103       IndentedLine l = (IndentedLine) lines.get(i);
104       Object JavaDoc [] 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 JavaDoc name = pPlaceHolder.getName();
119     for (int i = 0; i < lines.size(); i++) {
120       IndentedLine l = (IndentedLine) lines.get(i);
121       Object JavaDoc [] 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 JavaDoc("The placeholder " + pPlaceHolder.getName() +
132                                      " was not found and cannot be removed.");
133   }
134
135   protected void checkNulls(Object JavaDoc pTokens) {
136     if (pTokens == null) {
137       throw new NullPointerException JavaDoc("Null token detected.");
138     }
139     if (pTokens.getClass().isArray()) {
140         if (!pTokens.getClass().getComponentType().isPrimitive()) {
141             Object JavaDoc[] tokens = (Object JavaDoc[]) pTokens;
142             for (int i = 0; i < tokens.length; i++) {
143                 checkNulls(tokens[i]);
144             }
145         }
146     } else if (pTokens instanceof Collection JavaDoc) {
147       for (Iterator JavaDoc iter = ((Collection JavaDoc) pTokens).iterator(); iter.hasNext(); ) {
148         checkNulls(iter.next());
149       }
150     } else if (pTokens instanceof JavaSource) {
151       if (!(this instanceof JavaSource)) {
152         throw new IllegalStateException JavaDoc("An instance of JavaSource cannot be added to an instance of " +
153                                         getClass().getName());
154       }
155       if (!(pTokens instanceof JavaInnerClass)) {
156         throw new IllegalStateException JavaDoc("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 JavaDoc("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 JavaDoc[] pTokens) {
169     checkNulls(pTokens);
170     addLine(getLevel(), pTokens);
171   }
172
173   public void addLine() {
174     addLine(new Object JavaDoc[0]);
175   }
176   public void addLine(Object JavaDoc pLine) {
177     addLine(new Object JavaDoc[]{pLine});
178   }
179   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2) {
180     addLine(new Object JavaDoc[]{pToken1, pToken2});
181   }
182   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
183                        Object JavaDoc pToken3) {
184     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3});
185   }
186   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
187                        Object JavaDoc pToken3, Object JavaDoc pToken4) {
188     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4});
189   }
190   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
191                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5) {
192     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5});
193   }
194   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
195                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5,
196                        Object JavaDoc pToken6) {
197     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5,
198                          pToken6});
199   }
200   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
201                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5,
202                        Object JavaDoc pToken6, Object JavaDoc pToken7) {
203     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5,
204                          pToken6, pToken7});
205   }
206   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
207                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5,
208                        Object JavaDoc pToken6, Object JavaDoc pToken7, Object JavaDoc pToken8) {
209     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5,
210                          pToken6, pToken7, pToken8});
211   }
212   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
213                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5,
214                        Object JavaDoc pToken6, Object JavaDoc pToken7, Object JavaDoc pToken8,
215                        Object JavaDoc pToken9) {
216     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5,
217                          pToken6, pToken7, pToken8, pToken9});
218   }
219   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
220                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5,
221                        Object JavaDoc pToken6, Object JavaDoc pToken7, Object JavaDoc pToken8,
222                        Object JavaDoc pToken9, Object JavaDoc pToken10) {
223     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5,
224                          pToken6, pToken7, pToken8, pToken9, pToken10});
225   }
226   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
227                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5,
228                        Object JavaDoc pToken6, Object JavaDoc pToken7, Object JavaDoc pToken8,
229                        Object JavaDoc pToken9, Object JavaDoc pToken10, Object JavaDoc pToken11) {
230     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5,
231                          pToken6, pToken7, pToken8, pToken9, pToken10,
232                          pToken11});
233   }
234   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
235                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5,
236                        Object JavaDoc pToken6, Object JavaDoc pToken7, Object JavaDoc pToken8,
237                        Object JavaDoc pToken9, Object JavaDoc pToken10, Object JavaDoc pToken11,
238                        Object JavaDoc pToken12) {
239     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5,
240                          pToken6, pToken7, pToken8, pToken9, pToken10,
241                          pToken11, pToken12});
242   }
243   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
244                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5,
245                        Object JavaDoc pToken6, Object JavaDoc pToken7, Object JavaDoc pToken8,
246                        Object JavaDoc pToken9, Object JavaDoc pToken10, Object JavaDoc pToken11,
247                        Object JavaDoc pToken12, Object JavaDoc pToken13) {
248     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5,
249                          pToken6, pToken7, pToken8, pToken9, pToken10,
250                          pToken11, pToken12, pToken13});
251   }
252   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
253                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5,
254                        Object JavaDoc pToken6, Object JavaDoc pToken7, Object JavaDoc pToken8,
255                        Object JavaDoc pToken9, Object JavaDoc pToken10, Object JavaDoc pToken11,
256                        Object JavaDoc pToken12, Object JavaDoc pToken13, Object JavaDoc pToken14) {
257     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5,
258                          pToken6, pToken7, pToken8, pToken9, pToken10,
259                          pToken11, pToken12, pToken13, pToken14});
260   }
261   public void addLine(Object JavaDoc pToken1, Object JavaDoc pToken2,
262                        Object JavaDoc pToken3, Object JavaDoc pToken4, Object JavaDoc pToken5,
263                        Object JavaDoc pToken6, Object JavaDoc pToken7, Object JavaDoc pToken8,
264                        Object JavaDoc pToken9, Object JavaDoc pToken10, Object JavaDoc pToken11,
265                        Object JavaDoc pToken12, Object JavaDoc pToken13, Object JavaDoc pToken14,
266                        Object JavaDoc pToken15) {
267     addLine(new Object JavaDoc[]{pToken1, pToken2, pToken3, pToken4, pToken5,
268                          pToken6, pToken7, pToken8, pToken9, pToken10,
269                          pToken11, pToken12, pToken13, pToken14, pToken15});
270   }
271
272   public String JavaDoc[] getLines(int pLevel) {
273     String JavaDoc[] result = new String JavaDoc[lines.size()];
274     return result;
275   }
276
277   protected Iterator JavaDoc getLines() {
278     return lines.iterator();
279   }
280
281   public void write(IndentationTarget pTarget, Object JavaDoc pObject)
282       throws IOException JavaDoc {
283     if (pObject.getClass().isArray()) {
284       // We are *not* casting to Object[], because the array
285
// might be primitive.
286
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 JavaDoc) {
293         for (Iterator JavaDoc iter = ((List JavaDoc) pObject).iterator(); iter.hasNext(); ) {
294             write(pTarget, iter.next());
295         }
296     } else if (pObject instanceof Class JavaDoc) {
297       JavaQName name = JavaQNameImpl.getInstance((Class JavaDoc) 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 JavaDoc {
309     for (Iterator JavaDoc iter = getLines(); iter.hasNext(); ) {
310       IndentedLine l = (IndentedLine) iter.next();
311       Object JavaDoc[] 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 JavaDoc("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 JavaDoc asString() {
328      StringWriter JavaDoc sw = new StringWriter JavaDoc();
329      IndentationTarget target = new WriterTarget(sw);
330      try {
331         write(target);
332      } catch (IOException JavaDoc e) {
333         throw new IllegalStateException JavaDoc(e.getMessage());
334      }
335      return sw.toString();
336   }
337 }
338
Popular Tags