001: // Transmogrify License
002: //
003: // Copyright (c) 2001, ThoughtWorks, Inc.
004: // All rights reserved.
005: // Redistribution and use in source and binary forms, with or without
006: // modification, are permitted provided that the following conditions
007: // are met:
008: // - Redistributions of source code must retain the above copyright notice,
009: // 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 name of the ThoughtWorks, Inc. nor the names of its
014: // contributors may be used to endorse or promote products derived from this
015: // software without specific prior written permission.
016: // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
017: // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
018: // TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
019: // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
020: // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
021: // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
022: // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
023: // OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
024: // WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
025: // OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
026: // ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027:
028: package com.puppycrawl.tools.checkstyle.checks.usage.transmogrify;
029:
030: import java.util.Hashtable;
031: import java.util.Vector;
032:
033: /**
034: * <code>ScopeIndex</code> provides methods for finding <code>Scope</code>s
035: * related to a known <code>Occurrence</code>
036: */
037: public class ScopeIndex {
038:
039: //This is a Hashtable full of Vectors. The keys to this hashtable are filenames.
040: //Each vector contains all of the scope objects from the specific file.
041: private Hashtable indexOfFiles = new Hashtable();
042:
043: public Hashtable getIndex() {
044: return indexOfFiles;
045: }
046:
047: /**
048: * returns the most specific <code>Scope</code> to which the specified
049: * <code>Occurence</code> belongs.
050: *
051: * @param occ the <code>Occurrence</code> whose <code>Scope</code> we're interested in.
052: * @return Scope
053: */
054: public Scope lookup(Occurrence occ) {
055: String key = occ.getFile().getAbsolutePath();
056: Vector scopeList = getFileVector(key);
057:
058: Scope result = findScope(scopeList, occ);
059:
060: return result;
061: }
062:
063: /**
064: * returns the most specific <code>Scope</code> to which the specified
065: * <code>Occurence</code> belongs from the specified <code>Vector</code>
066: * of <code>Scope</code>s.
067: *
068: * @param occ the <code>Occurrence</code> whose <code>Scope</code> we're interested in.
069: * @param scopeList the <code>Vector</code> of <code>Scope</code>s to chose from.
070: * @return Scope
071: */
072: public Scope findScope(Vector scopeList, Occurrence occ) {
073: int i = 0;
074:
075: Scope bestSoFar = (Scope) scopeList.elementAt(i);
076:
077: while (!bestSoFar.getTreeNode().getSpan().contains(
078: occ.getLine(), occ.getColumn())) {
079: i++;
080: bestSoFar = (Scope) scopeList.elementAt(i);
081: }
082:
083: for (; i < scopeList.size(); i++) {
084: Scope currentScope = (Scope) scopeList.elementAt(i);
085:
086: if (currentScope.getTreeNode().getSpan().contains(
087: occ.getLine(), occ.getColumn())) {
088: if (bestSoFar.getTreeNode().getSpan().contains(
089: currentScope.getTreeNode().getSpan())) {
090: bestSoFar = currentScope;
091: }
092: }
093: }
094:
095: return bestSoFar;
096: }
097:
098: /**
099: * adds a <code>Scope</code> to the <code>ScopeIndex</code> for searching.
100: *
101: * @param scope the <code>Scope</code> to add.
102: */
103: public void addScope(Scope scope) {
104:
105: final SymTabAST SymTabAST = scope.getTreeNode();
106: SymTabAST.getFile();
107: Vector fileVector = getFileVector(scope.getTreeNode().getFile()
108: .getAbsolutePath());
109:
110: fileVector.addElement(scope);
111: }
112:
113: /**
114: * returns the <code>Vector</code> containing the <code>Scope</code>s related
115: * to the specified filename.
116: *
117: * @param fileName the fileName to find scopes for.
118: * @return Vector
119: */
120: private Vector getFileVector(String fileName) {
121: Vector result = (Vector) indexOfFiles.get(fileName);
122:
123: if (result == null) {
124: result = new Vector();
125: indexOfFiles.put(fileName, result);
126: }
127:
128: return result;
129: }
130: }
|