KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > definitions > reducedmodel > Brace


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.definitions.reducedmodel;
35
36 /**
37  * This class acts as the representation of a brace in the reduced
38  * view. It also includes information about the gap of plaintext
39  * preceding the actual brace before the previous brace or the start
40  * of the file.
41  * @version $Id: Brace.java 3903 2006-07-05 20:03:06Z rcartwright $
42  */

43 class Brace extends ReducedToken implements ReducedModelStates {
44   /**
45    * An array of the special characters that signify areas of text other than gaps.
46    * This really isn't the best datastructure to hold this information; there is
47    * more structure to the elements than a flat array. Specifically, matching
48    * characters are placed next to each other (except for the trailing elements,
49    * which have no matches). Notice that single and double quotes match themselves.
50    * @see String
51    */

52   public static final String JavaDoc[] braces = {
53     "{", "}", "(", ")", "[", "]", "/*", "*/", "//", "\n", "/", "*", "\"", "\"",
54     "'", "'", "\\\\", "\\", "\\'", "\\\"", ""
55   };
56   public static final String JavaDoc BLK_CMT_BEG = "/*";
57   public static final String JavaDoc BLK_CMT_END = "*/";
58   public static final String JavaDoc EOLN = "\n";
59   public static final String JavaDoc LINE_CMT = "//";
60   public static final String JavaDoc SINGLE_QUOTE = "'";
61   public static final String JavaDoc DOUBLE_QUOTE = "\"";
62   public static final String JavaDoc STAR = "*";
63   public static final String JavaDoc SLASH = "/";
64
65   /** the type of the brace */
66   protected int _type;
67
68   /**
69    * Virtual constructor.
70    * @param type the brace text
71    * @param state whether the brace is shadwowed by a comment, quote etc
72    * @return a new Brace if type is valid, otherwise null
73    * @throws BraceException if the given type is not a valid brace type.
74    */

75   public static Brace MakeBrace(String JavaDoc type, ReducedModelState state) {
76     int index = findBrace(type);
77     if (index == braces.length) {
78       throw new BraceException("Invalid brace type \"" + type + "\"");
79     }
80     else {
81       return new Brace(index, state);
82     }
83   }
84
85   /**
86    * Constructor.
87    * @param type the brace type
88    * @param state the state of the reduced model
89    */

90   private Brace(int type, ReducedModelState state) {
91     super(state);
92     _type = type;
93   }
94
95   /**
96    * Get the text of the brace.
97    * @return the text of the Brace
98    */

99   public String JavaDoc getType() {
100     return (_type == braces.length) ? "!" : braces[_type];
101   }
102
103   /**
104    * @return the size of the brace and its preceding gap
105    */

106   public int getSize() {
107     return getType().length();
108   }
109
110   /**
111    * Converts a Brace to a String.
112    * Used for debugging.
113    * @return the string representation of the Brace.
114    */

115   public String JavaDoc toString() {
116     //String val = "Brace(size: "+ getSize() +"): ";
117
final StringBuilder JavaDoc val = new StringBuilder JavaDoc();
118     int i;
119     for (i = 0; i < getSize(); i++) {
120       val.append(' ');
121       val.append(getType().charAt(i));
122     }
123     return val.toString();
124   }
125
126   /** Flips the orientation of the brace.
127    * Useful for updating quote information.
128    */

129   public void flip() {
130     if (isOpen()) _type += 1;
131     else if (_type < braces.length - 1) _type -= 1;
132   }
133
134   /**
135    * Indicates whether this is an opening brace.
136    * @return true if the brace is an opening brace.
137    */

138   public boolean isOpen() {
139     return (((_type%2) == 0) && (_type < braces.length - 1));
140   }
141
142   /**
143    * @return true if this is {|(|[
144    */

145   public boolean isOpenBrace() {
146     return ((_type == 0) || (_type == 2) || (_type == 4));
147   }
148
149   /**
150    * @return true if this is }|)|]
151    */

152   public boolean isClosedBrace() {
153     return ((_type == 1) || (_type == 3) || (_type == 5));
154   }
155
156   /**
157    * Indicates whether this is a closing brace.
158    * @return true if the brace is a closing brace.
159    */

160   public boolean isClosed() {
161     return !isOpen();
162   }
163
164   /**
165    * Reset the type of this brace.
166    * @param type the new String type for the brace
167    */

168   public void setType(String JavaDoc type) {
169     int index = findBrace(type);
170     if (index == braces.length) {
171       throw new BraceException("Invalid brace type \"" + type + "\"");
172     }
173     else {
174       _type = index;
175     }
176   }
177
178   /**
179    * Determine the brace type of a given String.
180    * The integer value returned is only used internally.
181    * Externally, the brace shows the text as its "type".
182    * @param type the text of the brace
183    * @return an integer indicating the type of brace
184    */

185   protected static int findBrace(String JavaDoc type) {
186     int i;
187     for (i = 0; i < braces.length; i++) {
188       if (type.equals(braces[i]))
189         break;
190     }
191     return i;
192   }
193
194   /**
195    * Check if two braces match.
196    * @param other the brace to compare
197    * @return true if this is a match for other.
198    */

199   public boolean isMatch(ReducedToken other) {
200     if (this.getType().equals("")) {
201       return false;
202     }
203     int off = (this.isOpen()) ? 1 : -1;
204     return (braces[_type + off].equals(other.getType()));
205   }
206
207   /**
208    * @return true if this is a quote
209    */

210   public boolean isDoubleQuote() {
211     return this.getType().equals(DOUBLE_QUOTE);
212   }
213
214   public boolean isSingleQuote() {
215     return this.getType().equals(SINGLE_QUOTE);
216   }
217
218
219   /**
220    * @return true if this is a line comment delimiter
221    */

222   public boolean isLineComment() {
223     return this.getType().equals(LINE_CMT);
224   }
225
226   /**
227    * @return true if this is a block comment open delimiter
228    */

229   public boolean isBlockCommentStart() {
230     return this.getType().equals(BLK_CMT_BEG);
231   }
232
233   /**7
234    * @return true if this is a block comment close delimiter
235    */

236   public boolean isBlockCommentEnd() {
237     return this.getType().equals(BLK_CMT_END);
238   }
239
240   /** @return true if this is a newline delimiter
241    */

242   public boolean isNewline() {
243     return this.getType().equals(EOLN);
244   }
245
246   /** @return true if this is a multiple character brace
247    */

248   public boolean isMultipleCharBrace() {
249     return isLineComment() || isBlockCommentStart() ||
250            isBlockCommentEnd() || isDoubleEscapeSequence();
251   }
252
253   /**
254    * @return true if this is \\ or \"
255    */

256   public boolean isDoubleEscapeSequence() {
257     return isDoubleEscape() || isEscapedDoubleQuote() ||
258       isEscapedSingleQuote();
259   }
260
261   /**
262    * @return true if this is \\
263    */

264   public boolean isDoubleEscape() {
265     return this.getType().equals("\\\\");
266   }
267
268   /**
269    * @return true if this is \"
270    */

271   public boolean isEscapedDoubleQuote() {
272     return this.getType().equals("\\\"");
273   }
274
275   /**
276    * @return true if this is \'
277    */

278   public boolean isEscapedSingleQuote() {
279     return this.getType().equals("\\'");
280   }
281
282   /**
283    * Implementation of abstract function.
284    * Braces, of course, are never Gaps.
285    * @return false
286    */

287   public boolean isGap() {
288     return false;
289   }
290
291   /**
292    * @return true if this is /
293    */

294   public boolean isSlash() {
295     return this.getType().equals(SLASH);
296   }
297
298   /**
299    * @return true if this is *
300    */

301   public boolean isStar() {
302     return this.getType().equals(STAR);
303   }
304
305   /**
306    * Braces can't grow.
307    * @throws RuntimeException
308    */

309   public void grow(int delta) {
310     throw new RuntimeException JavaDoc("Braces can't grow.");
311   }
312
313   /**
314    * Braces can't shrink.
315    * @throws RuntimeException
316    */

317   public void shrink(int delta) {
318     throw new RuntimeException JavaDoc("Braces can't shrink.");
319   }
320
321 }
322
323
324 /**
325  * An exception class used by methods in this class.
326  */

327 class BraceException extends RuntimeException JavaDoc {
328
329   /**
330    * Creates a new BraceException
331    * @param s the message
332    */

333   public BraceException(String JavaDoc s) {
334     super(s);
335   }
336 }
337
338
339
340
Popular Tags