KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > ClassAndInterfaceFinder


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-2006 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;
35
36 import java.io.*;
37
38 /** Class with getClassName method for finding the name of the first class or
39  * interface defined in a file */

40
41 public class ClassAndInterfaceFinder {
42   
43   private StreamTokenizer tokenizer;
44   
45   /* constructor to support unit testing */
46   public ClassAndInterfaceFinder(Reader r) {
47     initialize(r);
48   }
49   
50   /* normal constructor */
51   public ClassAndInterfaceFinder(File f) {
52     Reader r;
53     try { r = new FileReader(f); }
54     catch(FileNotFoundException e) { /* create a Reader for an "empty" file */
55       r = new StringReader("");
56     }
57     initialize(r);
58   }
59   
60   private void initialize(Reader r) {
61     
62     tokenizer = new StreamTokenizer(r);
63     tokenizer.slashSlashComments(true);
64     tokenizer.slashStarComments(true);
65     tokenizer.lowerCaseMode(false);
66     tokenizer.wordChars('_','_');
67     tokenizer.wordChars('.','.');
68   }
69   
70   /** Finds the the name of the first class or interface defined in this file.
71    * @return the String containing this name or "" if no such class or interface
72    * is found.
73    */

74   public String JavaDoc getClassOrInterfaceName() { return getName(true); }
75   
76   /** Finds the the name of the first class (excluding interfaces) defined in this file.
77    * @return the String containing this name or "" if no such class or interface
78    * is found.
79    */

80   public String JavaDoc getClassName() { return getName(false); }
81   
82   /** Finds the the name of the first class or interface in this file, respecting the
83    * value of the interfaceOK flag.
84    * I hate flags but did not see a simpler way to avoid duplicated code.
85    * This method has package (rather than private) visibility for testing purposes.
86    */

87   String JavaDoc getName(boolean interfaceOK) {
88     try {
89       String JavaDoc package_name = "";
90       int tokenType;
91       
92       // find the "class"/"interface" or "package" keyword (may encounter EOF)
93
do {
94         tokenType = tokenizer.nextToken();
95       } while(! isClassOrInterfaceWord(tokenType,interfaceOK) && ! isPackageWord(tokenType));
96
97       if (isEOF(tokenType)) return "";
98       
99       /* Save opening keyword */
100       String JavaDoc keyword = tokenizer.sval;
101       
102       // find the name of the class or package (may encounter EOF)
103
do {
104         tokenType = tokenizer.nextToken();
105       } while (! isWord(tokenType));
106
107       if (isEOF(tokenType)) return "";
108       
109       if (keyword.equals("class")) return tokenizer.sval; // a class defined without a package
110

111       if (interfaceOK && keyword.equals("interface")) return tokenizer.sval; // an interface without a package
112

113       if (keyword.equals("package")) package_name = tokenizer.sval;
114       
115       // find the "class" keyword
116
do { tokenType = tokenizer.nextToken(); } while (! isClassOrInterfaceWord(tokenType, interfaceOK));
117       
118       // find the name of the class or interface
119
do { tokenType = tokenizer.nextToken(); } while (! isWord(tokenType));
120       
121       if (tokenType == StreamTokenizer.TT_EOF) return "";
122       
123       if (package_name.length() > 0) return package_name + "." + tokenizer.sval;
124       
125       return tokenizer.sval;
126       
127     } catch(IOException e) {
128       return "";
129     }
130   }
131   
132   /**
133    * returns true iff the token is a word (as defined by StreamTokenizer)
134    */

135   private static boolean isWord(int tt) { return tt == StreamTokenizer.TT_WORD || isEOF(tt); }
136   
137   private static boolean isEOF(int tt) { return tt == StreamTokenizer.TT_EOF; }
138   
139   
140   /**
141    * returns true iff the token is "class" or we're at the end of the file
142    */

143   private boolean isClassOrInterfaceWord(int tt, boolean interfaceOK) {
144     return isEOF(tt) ||
145       (tt == StreamTokenizer.TT_WORD && tokenizer.sval.equals("class")) ||
146       (tt == StreamTokenizer.TT_WORD && interfaceOK && tokenizer.sval.equals("interface"));
147   }
148
149   /**
150    * returns true iff the token is "package" or we're at the end of the file
151    */

152   private boolean isPackageWord(int tt) {
153     return (tt == StreamTokenizer.TT_WORD && tokenizer.sval.equals("package") ||
154             tt == StreamTokenizer.TT_EOF);
155   }
156 }
157
158
159
160
Popular Tags