1 21 22 package org.armedbear.j; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.util.StringTokenizer ; 27 28 public final class Man extends Buffer 29 { 30 private boolean apropos; 31 32 public Man(String topic, File tempFile) 33 { 34 super(); 35 StringTokenizer st = new StringTokenizer (topic); 37 while (st.hasMoreTokens()) { 38 String 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 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 e) { 77 Log.error(e); 78 } 79 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 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 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 if (lineSeparator == null) 138 lineSeparator = "\r\n"; 139 skipLF = false; 140 } else { 141 if (lineSeparator == null) 143 lineSeparator = "\n"; 144 appendLine(sb.toString()); 145 sb.setLength(0); 146 } 147 break; 148 default: 149 if (skipLF) { 151 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 e) { 166 Log.error(e); 167 } 168 } 169 170 public final void appendLine(String 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 getFileNameForDisplay() 191 { 192 return ""; 193 } 194 } 195 | Popular Tags |