KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > debugger > request > ReadRequest


1 /* -*- Mode: JDE; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the debugger and core tools 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
23 package org.aspectj.debugger.request;
24
25 import org.aspectj.debugger.base.*;
26
27 import java.io.*;
28 import java.util.*;
29
30 /**
31  * ReadRequest.java
32  *
33  *
34  * Created: Tue Aug 29 13:58:02 2000
35  *
36  * @author <a HREF="mailto:palm@parc.xerox.com"Jeffrey Palm</a>
37  */

38
39 public class ReadRequest extends Request {
40
41     private String JavaDoc fileName;
42     private boolean silent;
43
44     public ReadRequest(Debugger debugger, String JavaDoc fileName, boolean silent) {
45         super(debugger);
46         this.fileName = fileName;
47         this.silent = silent;
48     }
49
50     public ReadRequest(Debugger debugger, String JavaDoc fileName) {
51         this(debugger, fileName, false);
52     }
53
54     public Object JavaDoc go() throws NoVMException, DebuggerException {
55         List commands = new Vector();
56         FileReader fileReader = null;
57         File file;
58         String JavaDoc filePath = fileName;
59         try {
60             file = getFile();
61 // filePath = file.getCanonicalPath();
62
filePath = file.getAbsolutePath();
63             fileReader = new FileReader(file);
64         } catch (IOException ioe) {
65             throw new DebuggerException("Invalid file name '" + fileName + "'.");
66         }
67         BufferedReader in = new BufferedReader(fileReader);
68         String JavaDoc line = "";
69         try {
70             if (!silent) {
71                 debugger.outln("*** Reading commands from " + filePath);
72             }
73             while ((line = in.readLine()) != null) {
74                 Object JavaDoc o;
75                 if (!isComment(line)) {
76                     commands.add(line);
77                     if (!silent) {
78                         o = debugger.execute(line);
79                     }
80                 }
81             }
82         } catch (IOException ioe) {
83         }
84
85         return commands;
86     }
87
88     private boolean isComment(String JavaDoc line) {
89         line = line.trim();
90         if (line.startsWith("#")) {
91             return true;
92         }
93         return false;
94     }
95
96     private File getFile() throws IOException {
97         Iterator iter = getDirs().iterator();
98         File file = new File(fileName);
99         if (file.exists()) {
100             return file.getAbsoluteFile();
101         }
102         while (iter.hasNext()) {
103             String JavaDoc dir = iter.next() + "";
104 // try {
105
file = new File(dir + File.separator + fileName);
106                 if (file.exists()) {
107 // return file.getCanonicalFile();
108
return file.getAbsoluteFile();
109                 }
110 // } catch (IOException ioe) {
111
// }
112
}
113         throw new IOException("File not found '" + fileName + "'.");
114
115     }
116
117     private List getDirs() {
118         Vector dirs = new Vector();
119         dirs.add(".");
120         try {
121             dirs.add(System.getProperty("user.home") + "");
122         } catch (SecurityException JavaDoc se) {
123         }
124         try {
125             dirs.add(System.getProperty("user.dir") + "");
126         } catch (SecurityException JavaDoc se) {
127         }
128         dirs.add(debugger.getSourceManager().getSourcePath());
129         return dirs;
130     }
131 }
132
Popular Tags