001: /*
002: * Man.java
003: *
004: * Copyright (C) 2000-2003 Peter Graves
005: * $Id: Man.java,v 1.5 2003/06/29 00:19:34 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.IOException;
025: import java.io.InputStream;
026: import java.util.StringTokenizer;
027:
028: public final class Man extends Buffer {
029: private boolean apropos;
030:
031: public Man(String topic, File tempFile) {
032: super ();
033: // Check for -k switch.
034: StringTokenizer st = new StringTokenizer(topic);
035: while (st.hasMoreTokens()) {
036: String s = st.nextToken();
037: if (s.equals("-k")) {
038: apropos = true;
039: break;
040: }
041: }
042: initializeUndo();
043: setFile(tempFile);
044: type = TYPE_MAN;
045: readOnly = true;
046: title = ManMode.getTitle(topic);
047: mode = Editor.getModeList().getMode(MAN_MODE);
048: formatter = new ManFormatter(this , apropos);
049: setTransient(true);
050: setInitialized(true);
051: }
052:
053: public final boolean isApropos() {
054: return apropos;
055: }
056:
057: public int load() {
058: if (!isLoaded()) {
059: try {
060: lockWrite();
061: } catch (InterruptedException e) {
062: Log.debug(e);
063: return LOAD_FAILED;
064: }
065: try {
066: final File toBeLoaded = getFile();
067: if (toBeLoaded.isFile()) {
068: try {
069: _load(toBeLoaded.getInputStream());
070: } catch (IOException e) {
071: Log.error(e);
072: }
073: // Handle zero length files.
074: if (getFirstLine() == null) {
075: appendLine("");
076: lineSeparator = System
077: .getProperty("line.separator");
078: }
079: setLastModified(toBeLoaded.lastModified());
080: renumber();
081: formatter.parseBuffer();
082: }
083: Line line = getFirstLine();
084: while (line != null && line.isBlank()) {
085: line = line.next();
086: setFirstLine(line);
087: }
088: final Line firstLine = getFirstLine();
089: if (firstLine == null) {
090: appendLine("");
091: setLoaded(true);
092: return LOAD_FAILED;
093: }
094: firstLine.setPrevious(null);
095: String header = firstLine.getText();
096: for (line = firstLine.next(); line != null; line = line
097: .next()) {
098: if (line.isBlank() && line.previous() != null
099: && line.previous().isBlank())
100: remove(line);
101: else if (line.getText().equals(header))
102: remove(line);
103: }
104: renumber();
105: setLoaded(true);
106: } finally {
107: unlockWrite();
108: }
109: }
110: return LOAD_COMPLETED;
111: }
112:
113: private void _load(InputStream istream) {
114: byte[] buf = new byte[4096];
115: int totalBytes = 0;
116: FastStringBuffer sb = new FastStringBuffer(256);
117: boolean skipLF = false;
118: int bytesRead;
119: try {
120: while ((bytesRead = istream.read(buf)) > 0) {
121: for (int i = 0; i < bytesRead; i++) {
122: byte b = buf[i];
123: switch (b) {
124: case 13:
125: appendLine(sb.toString());
126: sb.setLength(0);
127: skipLF = true;
128: break;
129: case 10:
130: if (skipLF) {
131: // LF after CR.
132: if (lineSeparator == null)
133: lineSeparator = "\r\n";
134: skipLF = false;
135: } else {
136: // LF without preceding CR.
137: if (lineSeparator == null)
138: lineSeparator = "\n";
139: appendLine(sb.toString());
140: sb.setLength(0);
141: }
142: break;
143: default:
144: // Normal char.
145: if (skipLF) {
146: // Something other than LF after CR.
147: if (lineSeparator == null)
148: lineSeparator = "\r";
149: skipLF = false;
150: }
151: sb.append((char) (b & 0xff));
152: break;
153: }
154: }
155: }
156: if (sb.length() > 0)
157: appendLine(sb.toString());
158: setLoaded(true);
159: } catch (IOException e) {
160: Log.error(e);
161: }
162: }
163:
164: public final void appendLine(String s) {
165: appendLine(new ManLine(s));
166: }
167:
168: private void remove(Line line) {
169: Line prev = line.previous();
170: Line next = line.next();
171: if (prev != null)
172: prev.setNext(next);
173: if (next != null)
174: next.setPrevious(prev);
175: }
176:
177: public final File getCurrentDirectory() {
178: return Directories.getUserHomeDirectory();
179: }
180:
181: public final String getFileNameForDisplay() {
182: return "";
183: }
184: }
|