KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > CheckPath


1 /*
2  * CheckPath.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: CheckPath.java,v 1.1.1.1 2002/09/24 16:08:20 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.BufferedReader JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStreamReader JavaDoc;
27 import java.util.HashSet JavaDoc;
28 import java.util.Stack JavaDoc;
29
30 public final class CheckPath implements Constants
31 {
32     private final Editor editor;
33     private final boolean showAll;
34     private final Buffer buffer;
35     private FastStringBuffer sb = new FastStringBuffer(16384);
36     private String JavaDoc path;
37     private File currentDirectory;
38     private HashSet JavaDoc checkedFiles = new HashSet JavaDoc(256);
39     private Stack JavaDoc stack = new Stack JavaDoc();
40     private int depthDisplayed;
41
42     private CheckPath(Editor editor, boolean showAll)
43     {
44         this.editor = editor;
45         this.showAll = showAll;
46         buffer = editor.getBuffer();
47         path = buffer.getStringProperty(Property.INCLUDE_PATH);
48         currentDirectory = editor.getCurrentDirectory();
49     }
50
51     private String JavaDoc getOutput()
52     {
53         return sb.toString();
54     }
55
56     private void run()
57     {
58         sb.append("File: ");
59         sb.append(buffer.getFile().netPath());
60         sb.append('\n');
61         sb.append("Include path: ");
62         sb.append(path);
63         sb.append('\n');
64         if (showAll)
65             sb.append("Included files:\n");
66         else
67             sb.append("The following included files were not found:\n");
68         checkBuffer(buffer, 0);
69     }
70
71     private void checkBuffer(Buffer b, int depth)
72     {
73         for (Line line = b.getFirstLine(); line != null; line = line.next()) {
74             String JavaDoc s = Utilities.extractInclude(line.getText());
75             if (s != null) {
76                 int result = checkFile(s, depth);
77                 if (showAll) {
78                     if (result == NOT_FOUND)
79                         sb.append(" NOT FOUND");
80                     else if (result == ALREADY_LISTED)
81                         sb.append(" (Already listed)");
82                     if (sb.length() == 0 || sb.charAt(sb.length()-1) != '\n')
83                         sb.append('\n');
84                 } else if (result == NOT_FOUND) {
85                     sb.append(s);
86                     sb.append(" NOT FOUND\n");
87                 }
88             }
89         }
90     }
91
92     private static final int NOT_FOUND = 0;
93     private static final int FOUND = 1;
94     private static final int ALREADY_LISTED = 2;
95
96     private int checkFile(final String JavaDoc s, final int depth)
97     {
98         if (showAll) {
99             sb.append(spaces(depth));
100             sb.append(s);
101         } else if (depth < depthDisplayed)
102             depthDisplayed = depth;
103         File file = Utilities.findInclude(s, path, currentDirectory);
104         if (file == null)
105             return NOT_FOUND;
106         if (checkedFiles.contains(file))
107             return ALREADY_LISTED;
108         checkedFiles.add(file);
109         int count = 0;
110         try {
111             BufferedReader JavaDoc reader =
112                 new BufferedReader JavaDoc(new InputStreamReader JavaDoc(file.getInputStream()));
113             String JavaDoc line;
114             while ((line = reader.readLine()) != null) {
115                 String JavaDoc name = Utilities.extractInclude(line);
116                 if (name != null) {
117                     if (showAll && count == 0) {
118                         sb.append('\n');
119                         sb.append(spaces(depth));
120                         sb.append(getDisplayName(file));
121                         sb.append(" -->\n");
122                     }
123                     // Recurse!
124
stack.push(file);
125                     int result = checkFile(name, depth+1);
126                     if (showAll) {
127                         if (result == NOT_FOUND)
128                             sb.append(" NOT FOUND");
129                         else if (result == ALREADY_LISTED)
130                             sb.append(" (Already listed)");
131                         if (sb.length() == 0 || sb.charAt(sb.length()-1) != '\n')
132                             sb.append('\n');
133                     } else if (result == NOT_FOUND) {
134                         while (depthDisplayed < stack.size()) {
135                             sb.append(spaces(depthDisplayed));
136                             sb.append(getDisplayName((File)stack.get(depthDisplayed)));
137                             sb.append(" -->\n");
138                             ++depthDisplayed;
139                         }
140                         sb.append(spaces(depth+1));
141                         sb.append(name);
142                         sb.append(" NOT FOUND\n");
143                     }
144                     stack.pop();
145                     ++count;
146                 }
147             }
148             reader.close();
149         }
150         catch (IOException JavaDoc e) {
151             Log.error(e);
152         }
153         return FOUND;
154     }
155
156     private String JavaDoc getDisplayName(File file)
157     {
158         File dir = file.getParentFile();
159         if (dir.equals(currentDirectory))
160             return file.getName();
161         return file.canonicalPath();
162     }
163
164     private static String JavaDoc spaces(int depth)
165     {
166         return Utilities.spaces(depth*2);
167     }
168
169     public static void checkPath()
170     {
171         checkPathInternal(false);
172     }
173
174     public static void listIncludes()
175     {
176         checkPathInternal(true);
177     }
178
179     private static void checkPathInternal(boolean showAll)
180     {
181         final Editor editor = Editor.currentEditor();
182         final int modeId = editor.getModeId();
183         if (modeId != C_MODE && modeId != CPP_MODE)
184             return;
185         editor.setWaitCursor();
186         CheckPath cp = new CheckPath(editor, showAll);
187         cp.run();
188         editor.setDefaultCursor();
189         Buffer buf = OutputBuffer.getOutputBuffer(cp.getOutput());
190         buf.setFormatter(new CheckPathFormatter(buf));
191         FastStringBuffer sb = new FastStringBuffer();
192         if (showAll)
193             sb.append("listIncludes ");
194         else
195             sb.append("checkPath");
196         sb.append(editor.getBuffer().getFile().getName());
197         buf.setTitle(sb.toString());
198         editor.makeNext(buf);
199         editor.activateInOtherWindow(buf);
200     }
201 }
202
Popular Tags