1 21 22 package org.armedbear.j; 23 24 import gnu.regexp.RE; 25 import gnu.regexp.REMatch; 26 import gnu.regexp.UncheckedRE; 27 import java.awt.AWTEvent ; 28 import java.awt.event.KeyEvent ; 29 import java.awt.event.MouseEvent ; 30 import java.io.IOException ; 31 import java.io.OutputStream ; 32 import java.text.SimpleDateFormat ; 33 import java.util.Date ; 34 import java.util.zip.ZipEntry ; 35 import java.util.zip.ZipInputStream ; 36 37 public final class ArchiveMode extends AbstractMode implements Constants, Mode 38 { 39 private static final ArchiveMode mode = new ArchiveMode(); 40 private static final RE moveToFilenameRegExp = 41 new UncheckedRE(":[0-5][0-9] "); 42 43 private ArchiveMode() 44 { 45 super(ARCHIVE_MODE, ARCHIVE_MODE_NAME); 46 setProperty(Property.VERTICAL_RULE, 0); 47 setProperty(Property.SHOW_LINE_NUMBERS, false); 48 } 49 50 public static final ArchiveMode getMode() 51 { 52 return mode; 53 } 54 55 public final Formatter getFormatter(Buffer buffer) 56 { 57 return new PlainTextFormatter(buffer); 58 } 59 60 protected void setKeyMapDefaults(KeyMap km) 61 { 62 km.mapKey(KeyEvent.VK_ENTER, 0, "archiveOpenFile"); 63 km.mapKey(KeyEvent.VK_G, CTRL_MASK | SHIFT_MASK, "archiveOpenFile"); 64 km.mapKey(VK_DOUBLE_MOUSE_1, 0, "archiveOpenFile"); 65 km.mapKey(VK_MOUSE_2, 0, "archiveOpenFile"); 66 } 67 68 private static String getName(String s) 69 { 70 REMatch match = moveToFilenameRegExp.getMatch(s); 71 return match == null ? null : s.substring(match.getEndIndex()); 72 } 73 74 public static void openFileAtDot(Editor editor) 75 { 76 Buffer buffer = editor.getBuffer(); 77 String name = getName(editor.getDotLine().getText()); 78 if (name == null) 79 return; 80 String source = null; 81 if (buffer.getFile() != null) { 82 source = "[from " + buffer.getFile().netPath() + "]"; 83 } else { 84 Compression compression = buffer.getCompression(); 85 if (compression != null && compression.getType() == COMPRESSION_ZIP) 86 source = "[from " + compression.getEntryName() + " " + compression.getSource() + "]"; 87 } 88 String title = name + " " + source; 89 for (BufferIterator it = new BufferIterator(); it.hasNext();) { 90 Buffer maybe = it.nextBuffer(); 91 if (title.equals(maybe.getTitle())) { 92 editor.makeNext(maybe); 93 editor.activate(maybe); 94 return; 95 } 96 } 97 File toBeLoaded = null; 98 if (buffer.getCache() != null) 99 toBeLoaded = buffer.getCache(); 100 else 101 toBeLoaded = buffer.getFile(); 102 ZipInputStream in = null; 103 try { 104 in = new ZipInputStream (toBeLoaded.getInputStream()); 105 ZipEntry zipEntry; 106 while((zipEntry = in.getNextEntry()) != null) { 107 if (zipEntry.getName().equals(name)) { 108 if (zipEntry.isDirectory()) { 109 editor.status(name + " is a directory"); 110 } else { 111 Buffer buf = null; 112 File cache = cacheEntry(in); 113 if (Editor.getModeList().modeAccepts(IMAGE_MODE, name)) 115 buf = ImageBuffer.createImageBuffer(null, cache, null); 116 if (buf != null) { 117 buf.setCompression(new Compression(COMPRESSION_ZIP, 118 zipEntry, 119 source)); 120 buf.setTitle(title); 121 } else { 122 buf = new Buffer(); 123 buf.type = Buffer.TYPE_NORMAL; buf.initializeUndo(); 125 buf.setCache(cache); 126 buf.setCompression(new Compression(COMPRESSION_ZIP, 127 zipEntry, 128 source)); 129 buf.initialize(); buf.setTitle(title); 131 buf.readOnly = true; 132 } 133 editor.makeNext(buf); 134 editor.activate(buf); 135 } 136 break; 137 } 138 } 139 } 140 catch (Exception e) { 141 Log.error(e); 142 } 143 try { 144 if (in != null) 145 in.close(); 146 } 147 catch (Exception e) { 148 Log.error(e); 149 } 150 } 151 152 private static File cacheEntry(ZipInputStream in) 153 { 154 File cache = Utilities.getTempFile(); 155 if (cache != null) { 156 OutputStream out = null; 157 try { 158 out = cache.getOutputStream(); 159 byte[] bytes = new byte[16384]; 160 int bytesRead; 161 while ((bytesRead = in.read(bytes, 0, bytes.length)) > 0) 162 out.write(bytes, 0, bytesRead); 163 } 164 catch (IOException e) { 165 Log.error(e); 166 } 167 if (out != null) { 168 try { 169 out.close(); 170 } 171 catch (IOException e) { 172 Log.error(e); 173 } 174 } 175 } 176 return cache; 177 } 178 179 public void loadFile(Buffer buffer, File file) 180 { 181 if (!buffer.isLoaded()) { 182 ZipInputStream in = null; 183 try { 184 in = new ZipInputStream (file.getInputStream()); 185 ZipEntry ze; 186 while((ze = in.getNextEntry()) != null) 187 appendLine(buffer, ze); 188 buffer.renumber(); 189 190 buffer.setLastModified(file.lastModified()); 192 buffer.setLoaded(true); 193 } 194 catch (Exception e) { 195 Log.error(e); 196 } 197 try { 198 if (in != null) 199 in.close(); 200 } 201 catch (Exception e) { 202 Log.error(e); 203 } 204 } 205 } 206 207 private static final SimpleDateFormat zipEntryDateFormatter = 208 new SimpleDateFormat ("MMM dd yyyy HH:mm"); 209 210 private static void appendLine(Buffer buffer, ZipEntry ze) 211 { 212 FastStringBuffer sb = new FastStringBuffer(); 213 String sizeString = String.valueOf(ze.getSize()); 214 for (int i = 9 - sizeString.length(); i >= 0; i--) 215 sb.append(' '); 216 sb.append(sizeString); 217 sb.append(' '); 218 sb.append(zipEntryDateFormatter.format(new Date (ze.getTime()))); 219 sb.append(' '); 220 sb.append(ze.getName()); 221 buffer.appendLine(sb.toString()); 222 } 223 224 public static void archiveOpenFile() 225 { 226 final Editor editor = Editor.currentEditor(); 227 if (editor.getModeId() == ARCHIVE_MODE) { 228 AWTEvent e = editor.getDispatcher().getLastEvent(); 231 if (e instanceof MouseEvent ) 232 editor.mouseMoveDotToPoint((MouseEvent )e); 233 openFileAtDot(editor); 234 } 235 } 236 } 237 | Popular Tags |