KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Man.java
3  *
4  * Copyright (C) 2000-2003 Peter Graves
5  * $Id: Man.java,v 1.5 2003/06/29 00:19:34 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.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.util.StringTokenizer JavaDoc;
27
28 public final class Man extends Buffer
29 {
30     private boolean apropos;
31
32     public Man(String JavaDoc topic, File tempFile)
33     {
34         super();
35         // Check for -k switch.
36
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(topic);
37         while (st.hasMoreTokens()) {
38             String JavaDoc s = st.nextToken();
39             if (s.equals("-k")) {
40                 apropos = true;
41                 break;
42             }
43         }
44         initializeUndo();
45         setFile(tempFile);
46         type = TYPE_MAN;
47         readOnly = true;
48         title = ManMode.getTitle(topic);
49         mode = Editor.getModeList().getMode(MAN_MODE);
50         formatter = new ManFormatter(this, apropos);
51         setTransient(true);
52         setInitialized(true);
53     }
54
55     public final boolean isApropos()
56     {
57         return apropos;
58     }
59
60     public int load()
61     {
62         if (!isLoaded()) {
63             try {
64                 lockWrite();
65             }
66             catch (InterruptedException JavaDoc e) {
67                 Log.debug(e);
68                 return LOAD_FAILED;
69             }
70             try {
71                 final File toBeLoaded = getFile();
72                 if (toBeLoaded.isFile()) {
73                     try {
74                         _load(toBeLoaded.getInputStream());
75                     }
76                     catch (IOException JavaDoc e) {
77                         Log.error(e);
78                     }
79                     // Handle zero length files.
80
if (getFirstLine() == null) {
81                         appendLine("");
82                         lineSeparator = System.getProperty("line.separator");
83                     }
84                     setLastModified(toBeLoaded.lastModified());
85                     renumber();
86                     formatter.parseBuffer();
87                 }
88                 Line line = getFirstLine();
89                 while (line != null && line.isBlank()) {
90                     line = line.next();
91                     setFirstLine(line);
92                 }
93                 final Line firstLine = getFirstLine();
94                 if (firstLine == null) {
95                     appendLine("");
96                     setLoaded(true);
97                     return LOAD_FAILED;
98                 }
99                 firstLine.setPrevious(null);
100                 String JavaDoc header = firstLine.getText();
101                 for (line = firstLine.next(); line != null; line = line.next()) {
102                     if (line.isBlank() && line.previous() != null && line.previous().isBlank())
103                         remove(line);
104                     else if (line.getText().equals(header))
105                         remove(line);
106                 }
107                 renumber();
108                 setLoaded(true);
109             }
110             finally {
111                 unlockWrite();
112             }
113         }
114         return LOAD_COMPLETED;
115     }
116
117     private void _load(InputStream JavaDoc istream)
118     {
119         byte[] buf = new byte[4096];
120         int totalBytes = 0;
121         FastStringBuffer sb = new FastStringBuffer(256);
122         boolean skipLF = false;
123         int bytesRead;
124         try {
125             while ((bytesRead = istream.read(buf)) > 0) {
126                 for (int i = 0; i < bytesRead; i++) {
127                     byte b = buf[i];
128                     switch (b) {
129                         case 13:
130                             appendLine(sb.toString());
131                             sb.setLength(0);
132                             skipLF = true;
133                             break;
134                         case 10:
135                             if (skipLF) {
136                                 // LF after CR.
137
if (lineSeparator == null)
138                                     lineSeparator = "\r\n";
139                                 skipLF = false;
140                             } else {
141                                 // LF without preceding CR.
142
if (lineSeparator == null)
143                                     lineSeparator = "\n";
144                                 appendLine(sb.toString());
145                                 sb.setLength(0);
146                             }
147                             break;
148                         default:
149                             // Normal char.
150
if (skipLF) {
151                                 // Something other than LF after CR.
152
if (lineSeparator == null)
153                                     lineSeparator = "\r";
154                                 skipLF = false;
155                             }
156                             sb.append((char) (b & 0xff));
157                             break;
158                     }
159                 }
160             }
161             if (sb.length() > 0)
162                 appendLine(sb.toString());
163             setLoaded(true);
164         }
165         catch (IOException JavaDoc e) {
166             Log.error(e);
167         }
168     }
169
170     public final void appendLine(String JavaDoc s)
171     {
172         appendLine(new ManLine(s));
173     }
174
175     private void remove(Line line)
176     {
177         Line prev = line.previous();
178         Line next = line.next();
179         if (prev != null)
180             prev.setNext(next);
181         if (next != null)
182             next.setPrevious(prev);
183     }
184
185     public final File getCurrentDirectory()
186     {
187         return Directories.getUserHomeDirectory();
188     }
189
190     public final String JavaDoc getFileNameForDisplay()
191     {
192         return "";
193     }
194 }
195
Popular Tags