001: /* ====================================================================
002: * The JRefactory License, Version 1.0
003: *
004: * Copyright (c) 2001 JRefactory. 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
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by the
021: * JRefactory (http://www.sourceforge.org/projects/jrefactory)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. The names "JRefactory" must not be used to endorse or promote
026: * products derived from this software without prior written
027: * permission. For written permission, please contact seguin@acm.org.
028: *
029: * 5. Products derived from this software may not be called "JRefactory",
030: * nor may "JRefactory" appear in their name, without prior written
031: * permission of Chris Seguin.
032: *
033: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
034: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
035: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
036: * DISCLAIMED. IN NO EVENT SHALL THE CHRIS SEGUIN OR
037: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
038: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
039: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
040: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
041: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
042: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
043: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
044: * SUCH DAMAGE.
045: * ====================================================================
046: *
047: * This software consists of voluntary contributions made by many
048: * individuals on behalf of JRefactory. For more information on
049: * JRefactory, please see
050: * <http://www.sourceforge.org/projects/jrefactory>.
051: */
052: package org.acm.seguin.summary.query;
053:
054: import java.io.File;
055: import java.io.IOException;
056: import java.util.Iterator;
057: import java.util.StringTokenizer;
058: import net.sourceforge.jrefactory.ast.ASTName;
059: import net.sourceforge.jrefactory.ast.SimpleNode;
060: import net.sourceforge.jrefactory.factory.FileParserFactory;
061: import net.sourceforge.jrefactory.factory.ParserFactory;
062: import net.sourceforge.jrefactory.query.PackageNameGetter;
063: import org.acm.seguin.awt.ExceptionPrinter;
064: import org.acm.seguin.summary.FileSummary;
065: import org.acm.seguin.summary.PackageSummary;
066: import org.acm.seguin.summary.Summary;
067:
068: /**
069: * This class is used to infer the top level directory
070: *
071: *@author Chris Seguin
072: *@created September 12, 2001
073: */
074: public class TopLevelDirectory {
075: /**
076: * Gets the PackageDirectory attribute of the TopLevelDirectory class
077: *
078: *@param initialSummary Description of Parameter
079: *@param packageName Description of Parameter
080: *@return The PackageDirectory value
081: */
082: public static File getPackageDirectory(Summary initialSummary,
083: String packageName) {
084: File rootDir = null;
085:
086: if (initialSummary != null) {
087: FileSummary fileSummary = getFileSummary(initialSummary);
088: rootDir = query(fileSummary);
089: }
090:
091: if (rootDir == null) {
092: rootDir = query();
093: }
094:
095: StringTokenizer tok = new StringTokenizer(packageName, ".");
096: File current = rootDir;
097: while (tok.hasMoreTokens()) {
098: current = new File(current, tok.nextToken());
099: }
100:
101: return current;
102: }
103:
104: /**
105: * Return the top level directory from a FileSummary
106: *
107: *@param fileSummary Description of Parameter
108: *@return Description of the Returned Value
109: */
110: public static File query(FileSummary fileSummary) {
111: File current = fileSummary.getFile();
112: if (current == null) {
113: return null;
114: }
115:
116: File currentDir = current.getParentFile();
117: PackageSummary packageSummary = (PackageSummary) fileSummary
118: .getParent();
119:
120: String name = packageSummary.getName();
121: if (name.length() == 0) {
122: return currentDir;
123: }
124:
125: int index = name.indexOf(".");
126: currentDir = currentDir.getParentFile();
127:
128: while (index != -1) {
129: index = name.indexOf(".", index + 1);
130: currentDir = currentDir.getParentFile();
131: }
132:
133: return currentDir;
134: }
135:
136: /**
137: * Return the top level directory
138: *
139: *@param initialDir Description of Parameter
140: *@param filename Description of Parameter
141: *@return the top level directory
142: */
143: public static File query(File initialDir, String filename) {
144: // Create a factory to get a root
145: File inputFile = new File(initialDir, filename);
146: ParserFactory factory = new FileParserFactory(inputFile);
147: SimpleNode root = factory.getAbstractSyntaxTree(false,
148: ExceptionPrinter.getInstance());
149:
150: File topLevel = getParent(inputFile);
151: ASTName packageName = PackageNameGetter.query(root);
152: if (packageName != null) {
153: for (int ndx = 0; ndx < packageName.getNameSize(); ndx++) {
154: topLevel = getParent(topLevel);
155: }
156: }
157:
158: // Return that directory
159: return topLevel;
160: }
161:
162: /**
163: * Given a file, it returns the parent file
164: *
165: *@param input the input file
166: *@return the parent of that file
167: */
168: private static File getParent(File input) {
169: try {
170: String path = input.getCanonicalPath();
171: File temp = new File(path);
172: return temp.getParentFile();
173: } catch (IOException ioe) {
174: }
175:
176: return input.getParentFile();
177: }
178:
179: /**
180: * Gets the FileSummary attribute of the TopLevelDirectory class
181: *
182: *@param initialSummary Description of Parameter
183: *@return The FileSummary value
184: */
185: private static FileSummary getFileSummary(Summary initialSummary) {
186: Summary currentSummary = initialSummary;
187: while (!(currentSummary instanceof FileSummary)) {
188: currentSummary = currentSummary.getParent();
189: if (currentSummary == null) {
190: return null;
191: }
192: }
193: return (FileSummary) currentSummary;
194: }
195:
196: /**
197: * Searches all the packages for an appropriate file and infers the source
198: * root directory
199: *
200: *@return the root directory
201: */
202: private static File query() {
203: FileSummary appropriate = findFileSummary();
204: if (appropriate == null) {
205: return new File(System.getProperty("user.dir"));
206: }
207:
208: PackageSummary packageSummary = (PackageSummary) appropriate
209: .getParent();
210: String packageName = packageSummary.getName();
211: StringTokenizer tok = new StringTokenizer(packageName, ".");
212: File startingPoint = null;
213: try {
214: startingPoint = new File(appropriate.getFile()
215: .getCanonicalPath());
216: } catch (IOException ioe) {
217: startingPoint = appropriate.getFile();
218: }
219:
220: File current = startingPoint.getParentFile();
221: while (tok.hasMoreTokens()) {
222: current = current.getParentFile();
223: String value = tok.nextToken();
224: }
225:
226: return current;
227: }
228:
229: /**
230: * Searches for a file summary with a file (rather than a null) in the file
231: *
232: *@return the file summary
233: */
234: private static FileSummary findFileSummary() {
235: Iterator iter = PackageSummary.getAllPackages();
236: if (iter != null) {
237: while (iter.hasNext()) {
238: PackageSummary next = (PackageSummary) iter.next();
239: Iterator iter2 = next.getFileSummaries();
240: while ((iter2 != null) && iter2.hasNext()) {
241: FileSummary fileSummary = (FileSummary) iter2
242: .next();
243: if (fileSummary.getFile() != null) {
244: return fileSummary;
245: }
246: }
247: }
248: }
249: return null;
250: }
251: }
|