KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > util > ConfigFileUtil


1 /* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the IDE support for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25
26 package org.aspectj.util;
27
28 import java.util.*;
29 import java.io.*;
30
31 /**
32  * @deprecated use <CODE>ConfigParser</CODE> instead.
33  */

34 public class ConfigFileUtil {
35
36     /** Represents the current working dir - per System.properties("user.dir") */
37     private static final File USERDIR;
38     /** Represents the "." dir - used for USERDIR when some Throwable prevents its initialization */
39     private static final File DOTDIR;
40     /** Filter that accepts any file name ending with ".java" */
41     private static final FileFilter SOURCEFILTER;
42     static {
43         File userDir = null;
44         File dotDir = null;
45         try {
46             dotDir = new File(".");
47             userDir = new File(System.getProperty("user.dir")).getCanonicalFile();
48         } catch (Throwable JavaDoc t) {
49             if (null == userDir) userDir = dotDir; // still might be null...
50
} finally {
51             DOTDIR = dotDir;
52             USERDIR = userDir;
53         }
54         SOURCEFILTER = new FileFilter() {
55                 public boolean accept(File f) {
56                     return ((f != null) &&
57                         (f.getName().endsWith(".java")));
58                 }
59             };
60     }
61
62     /**
63      * Retrieves all of the .lst files in a directory, replacing any '\' characters with '/'.
64      *
65      * @returns an empty List if no files were found
66      *
67      * @deprecated use <CODE>ConfigParser</CODE> instead.
68      */

69     public static List getLstFilesInDir(String JavaDoc dirPath) { // todo: why not use a FileFilter?
70
List configs = new ArrayList();
71         File f = new File(dirPath);
72         File[] dirContents = f.listFiles();
73         for (int j = 0; j < dirContents.length; j++) {
74             if (dirContents[j].isDirectory()) {
75                 configs.addAll(getLstFilesInDir(dirContents[j].getAbsolutePath()));
76             } else if (dirContents[j].getName().endsWith(".lst")) {
77                 configs.add(dirContents[j].getAbsolutePath().replace('\\', '/'));
78             }
79         }
80         return configs;
81     }
82
83     /**
84      * Render filename as a canonical absolute File, using relativeDir if necessary.
85      * Does guarantee that the result is valid using the expensive
86      * <code>{File}.getCanonicalFile()</code>.
87      * If the filename is not absolute, then resolve using currentWorkingDir.
88      * If currentWorkingDir is null, then use
89      * <code>new File(System.getProperties("user.dir"))</code>.
90      * See also org.aspectj.tools.ajc.Main.qualifiedFile(..).
91      * @param filename the String to render as a qualified File (absolute preferred)
92      * @param currentWorkingDir the File directory base for the name (may be null)
93      * @return the File represented by filename, rendered absolute and canonical
94      * @throws IOException if the input cannot represent a valid file on this filesystem
95      *
96      * @deprecated use <CODE>ConfigParser</CODE> instead.
97      */

98     public static File qualifiedFile(String JavaDoc name, File currentWorkingDir)
99         throws IOException {
100         name = name.replace('/', File.separatorChar); // uncertain how this helps...
101
File file = new File(name);
102         if (!file.isAbsolute()) {
103             if (null == currentWorkingDir) {
104                 currentWorkingDir = USERDIR;
105             }
106             file = new File(currentWorkingDir, name).getAbsoluteFile();
107         }
108         return file.getCanonicalFile();
109     }
110
111     /**
112      * Same as <code>expandLstFile(filename, null)<code>
113      * @see #expandLstFile(String, File)
114      * @param filename must be resolvable by qualifiedFile(filename, null)
115      * i.e., either absolute or relative to user.dir
116      * System property (current working dir)
117      *
118      * @deprecated use <CODE>ConfigParser</CODE> instead.
119      */

120     public static List expandLstFile(String JavaDoc filename) throws IOException, LstFileEntryInvalidException {
121         return expandLstFile(filename, null);
122     }
123
124     /**
125      * Create the list of java files reachable via the input filename,
126      * taken as a file listing (per line) .java files or other list files
127      * either of which may be absolute or relative to the parent directory
128      * of the containing list file.
129      * Prefer this API over <code>expandLstFile(String)</code>.
130      * @param filename must be resolvable by qualifiedFile(filename, relativeTo)
131      * i.e., either absolute or relative to "."
132      * @return list of files reachable from filename
133      * @throws IOException same as expandAtFile
134      * @throws LstFileEntryInvalidException same as expandAtFile
135      *
136      * @deprecated use <CODE>ConfigParser</CODE> instead.
137      */

138     public static List expandLstFile(String JavaDoc filename, File relativeTo)
139         throws IOException, LstFileEntryInvalidException {
140         List fileContents = new ArrayList();
141         expandAtFile(qualifiedFile(filename, relativeTo), fileContents, null);
142         return fileContents;
143     }
144
145     /**
146      * Implement <code>List expandLstFile(String, File)</code>.
147      * This differs from org.aspectj.tools.ajc.Main in not relying on
148      * a parseArgs(..) method to do recursive calls.
149      * Instead it tracks in atFiles the stack of atFiles called,
150      * rendering each in absolute canonical form and interning the string
151      * to detect differences in naming.
152      * @param file the File to expand - should be readable and contain list
153      * @param args the List of files reachable from earlier calls (results go here)
154      * - not null
155      * @param atFiles the List of list files traversed
156      * - should be null from any external caller unless you want to exclude a file
157      */

158     private static void expandAtFile(File file, List args, List atFiles)
159         throws IOException, LstFileEntryInvalidException {
160         BufferedReader in = new BufferedReader(new FileReader(file));
161         final File parentDir = (null == file? null : file.getParentFile());
162         String JavaDoc line;
163         while ((line = in.readLine()) != null) {
164             if (line == null || line.length() < 1) continue;
165             line = line.trim();
166             if (line.startsWith("//")) continue;
167
168             int indexStarJava = line.lastIndexOf("*.java");
169             int indexStar = line.lastIndexOf("*");
170             String JavaDoc dirName = null;
171             if (indexStarJava != -1) {
172                 dirName = line.substring(0, indexStarJava);
173             } else if (indexStar != -1) {
174                 dirName = line.substring(0, indexStar);
175             }
176             if (dirName != null) {
177                 File newDirFile = qualifiedFile(dirName, parentDir);
178                 File[] javafiles = newDirFile.listFiles(SOURCEFILTER);
179                 if (javafiles == null) {
180                     cantResolve(newDirFile, "unable to file java files in wildcard directory "
181                                 + newDirFile.getPath());
182                 } else {
183                     for (int i = 0; i < javafiles.length; i++) {
184                         if (!maybeAdd(javafiles[i], args)) {
185                             cantResolve(javafiles[i], "unable to add wildcard to collection");
186                         }
187                     }
188                 }
189             } else if (line.startsWith("@")) {
190                 File newfile = qualifiedFile(line.substring(1).trim(), parentDir);
191                 if (!newfile.exists()) {
192                     cantResolve(newfile, "@file does not exist");
193                 } else { // recursive call unless cycle detected
194
String JavaDoc filePath = newfile.getCanonicalPath().intern(); // exists, s.b. no IOException
195
if (null == atFiles) {
196                         atFiles = new ArrayList();
197                         atFiles.add(file.getCanonicalPath().intern());
198                     }
199                     if (!atFiles.contains(filePath)) {
200                         atFiles.add(filePath);
201                         expandAtFile(newfile, args, atFiles);
202                     } // else ignore newfile when cycle detected
203
}
204             } else {
205                 File newfile = qualifiedFile(line, parentDir);
206                 if (newfile.exists()) {
207                     if (!maybeAdd(newfile, args)) {
208                         cantResolve(newfile, "unable to add to collection");
209                     }
210                 } else if (!(newfile.getName().equals("*.java") ||
211                            newfile.getName().equals("*"))) {
212                     cantResolve(newfile, "expecting @{listFile}, *, *.java, or {path}.java");
213                 } else { // maybe *.java or ...
214
// File newFileParentDir = newfile.getParentFile();
215
// if (null == newFileParentDir) {
216
// cantResolve(newfile);
217
// } else {
218
// File[] javafiles = newFileParentDir.listFiles(SOURCEFILTER);
219
// if (javafiles == null) {
220
cantResolve(newfile, "unable to find file "
221                                         + newfile);
222 // } else {
223
// for (int i = 0; i < javafiles.length; i++) {
224
// if (!maybeAdd(javafiles[i], args)) {
225
// cantResolve(javafiles[i], "unable to add wildcard to collection");
226
// }
227
// }
228
// }
229
// }
230
}
231             }
232         }
233         in.close();
234     }
235
236     public static class LstFileEntryInvalidException extends Exception JavaDoc {
237         public final String JavaDoc lstFile;
238         public final int lineNumber;
239
240         public LstFileEntryInvalidException(String JavaDoc lstFile, int lineNumber) {
241             this(lstFile, lineNumber, "");
242         }
243         public LstFileEntryInvalidException(String JavaDoc lstFile, int lineNumber, String JavaDoc reason) {
244             super("" + lstFile + ":" + lineNumber + ": " + reason
245                   + " (invalid entry - expecting *.java or {path}.java or @{listFile})");
246             this.lstFile = lstFile;
247             this.lineNumber = lineNumber;
248         }
249     }
250
251     /**
252      * @todo: make LstFileEntryInvalidException put in the right line numbers
253      */

254     private static void cantResolve(File file, String JavaDoc reason) throws LstFileEntryInvalidException{
255         throw new LstFileEntryInvalidException(file.getAbsolutePath(), -1, reason);
256     }
257     private static void cantResolve(File file) throws LstFileEntryInvalidException{
258         throw new LstFileEntryInvalidException(file.getAbsolutePath(), -1);
259     }
260
261     private static boolean maybeAdd(File file, Collection files) {
262         if (isJavaFile(file)) {
263             files.add(file.getAbsolutePath());
264             return true;
265         }
266         return false;
267     }
268
269     private static boolean isJavaFile(File file) {
270         return file != null && file.exists() && !file.isDirectory()
271             && file.getName().endsWith(".java");
272     }
273 }
274
Popular Tags