KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * WordMode.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: WordMode.java,v 1.3 2002/10/11 01:42:37 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.awt.event.KeyEvent JavaDoc;
25 import java.io.IOException JavaDoc;
26 import java.io.InputStream JavaDoc;
27
28 public final class WordMode extends AbstractMode implements Constants, Mode
29 {
30     private static final WordMode mode = new WordMode();
31
32     private WordMode()
33     {
34         super(WORD_MODE, WORD_MODE_NAME);
35         setProperty(Property.VERTICAL_RULE, 0);
36         setProperty(Property.SHOW_LINE_NUMBERS, false);
37     }
38
39     public static final WordMode getMode()
40     {
41         return mode;
42     }
43
44     public final Formatter getFormatter(Buffer buffer)
45     {
46         return new PlainTextFormatter(buffer);
47     }
48
49     protected void setKeyMapDefaults(KeyMap km)
50     {
51         km.mapKey(KeyEvent.VK_B, CTRL_MASK | ALT_MASK, "binaryMode");
52     }
53
54     public void loadFile(Buffer buffer, File toBeLoaded)
55     {
56         try {
57             load(buffer, toBeLoaded.getInputStream());
58         }
59         catch (IOException JavaDoc e) {
60             Log.debug(e);
61         }
62     }
63
64     private static void load(Buffer buffer, InputStream JavaDoc inputStream)
65     {
66         byte[] bytes = new byte[4096];
67         int totalBytes = 0;
68         try {
69             int bytesRead = inputStream.read(bytes);
70             if (bytesRead < 0x400)
71                 return;
72             int magic = getWord(bytes, 0x200);
73             Log.debug("magic = 0x" + Integer.toHexString(magic));
74             if (magic != 0xa5ec)
75                 return;
76             int version = getWord(bytes, 0x202);
77             Log.debug("version = " + version);
78             if (version < 101)
79                 return;
80             int status = getWord(bytes, 0x20a);
81             Log.debug("status = 0x" + Integer.toHexString(status));
82             if ((status & 4) != 0)
83                 Log.debug("document is fast saved");
84             long beginText = getLong(bytes, 0x218) + 0x200;
85             Log.debug("beginText = 0x" + Long.toHexString(beginText));
86             if (beginText > bytesRead)
87                 return; // BUG! We could handle this case.
88
long endText = getLong(bytes, 0x21c) + 0x200;
89             long textLength = endText - beginText;
90             Log.debug("textLength = " + textLength);
91             final String JavaDoc encoding = "Cp1252";
92             ByteBuffer bb = new ByteBuffer(256);
93             boolean done = false;
94             Debug.assertTrue(beginText < Integer.MAX_VALUE);
95             int start = (int) beginText;
96             while (bytesRead > 0) {
97                 for (int i = start; i < bytesRead; i++) {
98                     byte b = bytes[i];
99                     switch (b) {
100                         case 13:
101                             if (bb.length() > 0)
102                                 wrapAndAppend(buffer, new String JavaDoc(bb.getBytes(), 0, bb.length(), encoding));
103                             buffer.appendLine("");
104                             bb.setLength(0);
105                             break;
106                         case 0:
107                             if (bb.length() > 0)
108                                 wrapAndAppend(buffer, new String JavaDoc(bb.getBytes(), 0, bb.length(), encoding));
109                             bb.setLength(0);
110                             done = true;
111                             break;
112                         default:
113                             // Normal char.
114
bb.append(b);
115                             break;
116                     }
117                     if (done)
118                         break;
119                 }
120                 if (done)
121                     break;
122                 bytesRead = inputStream.read(bytes);
123                 start = 0;
124                 if (bytesRead > 0)
125                     buffer.loadProgress(totalBytes = totalBytes + bytesRead);
126             }
127             buffer.unmodified();
128             buffer.setLoaded(true);
129             buffer.setForceReadOnly(true);
130         }
131         catch (Exception JavaDoc e) {
132             Log.error(e);
133         }
134         buffer.loadFinished(buffer.isLoaded());
135     }
136
137     private static final void wrapAndAppend(Buffer buffer, String JavaDoc s)
138     {
139         buffer.append(Utilities.wrap(s, 65, 8));
140     }
141
142     // 4 bytes
143
private static long getLong(byte[] bytes, int offset)
144     {
145         long byte0 = bytes[offset] & 0xff;
146         long byte1 = bytes[offset + 1] & 0xff;
147         long byte2 = bytes[offset + 2] & 0xff;
148         long byte3 = bytes[offset + 3] & 0xff;
149         return (byte3 << 24) + (byte2 << 16) + (byte1 << 8) + byte0;
150     }
151
152     // 2 bytes
153
private static int getWord(byte[] bytes, int offset)
154     {
155         int hi = bytes[offset + 1] & 0xff;
156         int lo = bytes[offset] & 0xff;
157         return (hi << 8) + lo;
158     }
159 }
160
Popular Tags