001: /*BEGIN_COPYRIGHT_BLOCK
002: *
003: * Copyright (c) 2001-2007, JavaPLT group at Rice University (javaplt@rice.edu)
004: * All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are met:
008: * * Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.
010: * * Redistributions in binary form must reproduce the above copyright
011: * notice, this list of conditions and the following disclaimer in the
012: * documentation and/or other materials provided with the distribution.
013: * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
014: * names of its contributors may be used to endorse or promote products
015: * derived from this software without specific prior written permission.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
018: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
019: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
020: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
021: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
022: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
023: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
024: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
025: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
026: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
027: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
028: *
029: * This software is Open Source Initiative approved Open Source Software.
030: * Open Source Initative Approved is a trademark of the Open Source Initiative.
031: *
032: * This file is part of DrJava. Download the current version of this project
033: * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
034: *
035: * END_COPYRIGHT_BLOCK*/
036:
037: package edu.rice.cs.drjava.model;
038:
039: import java.io.*;
040:
041: /** Class with getClassName method for finding the name of the first class or
042: * interface defined in a file */
043:
044: public class ClassAndInterfaceFinder {
045:
046: private StreamTokenizer tokenizer;
047:
048: /* constructor to support unit testing */
049: public ClassAndInterfaceFinder(Reader r) {
050: initialize(r);
051: }
052:
053: /* normal constructor */
054: public ClassAndInterfaceFinder(File f) {
055: Reader r;
056: try {
057: r = new FileReader(f);
058: } catch (FileNotFoundException e) { /* create a Reader for an "empty" file */
059: r = new StringReader("");
060: }
061: initialize(r);
062: }
063:
064: private void initialize(Reader r) {
065:
066: tokenizer = new StreamTokenizer(r);
067: tokenizer.slashSlashComments(true);
068: tokenizer.slashStarComments(true);
069: tokenizer.lowerCaseMode(false);
070: tokenizer.wordChars('_', '_');
071: tokenizer.wordChars('.', '.');
072: }
073:
074: /** Finds the the name of the first class or interface defined in this file.
075: * @return the String containing this name or "" if no such class or interface
076: * is found.
077: */
078: public String getClassOrInterfaceName() {
079: return getName(true);
080: }
081:
082: /** Finds the the name of the first class (excluding interfaces) defined in this file.
083: * @return the String containing this name or "" if no such class or interface
084: * is found.
085: */
086: public String getClassName() {
087: return getName(false);
088: }
089:
090: /** Finds the the name of the first class or interface in this file, respecting the
091: * value of the interfaceOK flag.
092: * I hate flags but did not see a simpler way to avoid duplicated code.
093: * This method has package (rather than private) visibility for testing purposes.
094: */
095: String getName(boolean interfaceOK) {
096: try {
097: String package_name = "";
098: int tokenType;
099:
100: // find the "class"/"interface" or "package" keyword (may encounter EOF)
101: do {
102: tokenType = tokenizer.nextToken();
103: } while (!isClassOrInterfaceWord(tokenType, interfaceOK)
104: && !isPackageWord(tokenType));
105:
106: if (isEOF(tokenType))
107: return "";
108:
109: /* Save opening keyword */
110: String keyword = tokenizer.sval;
111:
112: // find the name of the class or package (may encounter EOF)
113: do {
114: tokenType = tokenizer.nextToken();
115: } while (!isWord(tokenType));
116:
117: if (isEOF(tokenType))
118: return "";
119:
120: if (keyword.equals("class"))
121: return tokenizer.sval; // a class defined without a package
122:
123: if (interfaceOK && keyword.equals("interface"))
124: return tokenizer.sval; // an interface without a package
125:
126: if (keyword.equals("package"))
127: package_name = tokenizer.sval;
128:
129: // find the "class" keyword
130: do {
131: tokenType = tokenizer.nextToken();
132: } while (!isClassOrInterfaceWord(tokenType, interfaceOK));
133:
134: // find the name of the class or interface
135: do {
136: tokenType = tokenizer.nextToken();
137: } while (!isWord(tokenType));
138:
139: if (tokenType == StreamTokenizer.TT_EOF)
140: return "";
141:
142: if (package_name.length() > 0)
143: return package_name + "." + tokenizer.sval;
144:
145: return tokenizer.sval;
146:
147: } catch (IOException e) {
148: return "";
149: }
150: }
151:
152: /**
153: * returns true iff the token is a word (as defined by StreamTokenizer)
154: */
155: private static boolean isWord(int tt) {
156: return tt == StreamTokenizer.TT_WORD || isEOF(tt);
157: }
158:
159: private static boolean isEOF(int tt) {
160: return tt == StreamTokenizer.TT_EOF;
161: }
162:
163: /**
164: * returns true iff the token is "class" or we're at the end of the file
165: */
166: private boolean isClassOrInterfaceWord(int tt, boolean interfaceOK) {
167: return isEOF(tt)
168: || (tt == StreamTokenizer.TT_WORD && tokenizer.sval
169: .equals("class"))
170: || (tt == StreamTokenizer.TT_WORD && interfaceOK && tokenizer.sval
171: .equals("interface"));
172: }
173:
174: /**
175: * returns true iff the token is "package" or we're at the end of the file
176: */
177: private boolean isPackageWord(int tt) {
178: return (tt == StreamTokenizer.TT_WORD
179: && tokenizer.sval.equals("package") || tt == StreamTokenizer.TT_EOF);
180: }
181: }
|