01: package com.jclark.xml.tok;
02:
03: /**
04: * An <CODE>Encoding</CODE> for an arbitrary encoding
05: * that represents every character by exactly one byte.
06: *
07: * @version $Revision: 1.2 $ $Date: 1998/02/17 04:51:04 $
08: */
09: final class SingleByteEncoding extends Encoding {
10:
11: private final byte[] byteTypeTable = new byte[256];
12: private final int[] asciiToByte = new int[128];
13: private final String map;
14:
15: SingleByteEncoding(String map) {
16: super (1);
17: this .map = map;
18: for (int i = 0; i < 256; i++)
19: asciiToByte[i] = 0xFF;
20: for (int i = 0; i < 256; i++) {
21: char c = map.charAt(i);
22: if (c != 0xFFFD) {
23: byteTypeTable[i] = charTypeTable[c >> 8][c & 0xFF];
24: if (c < 128)
25: asciiToByte[c] = (byte) i;
26: } else
27: byteTypeTable[i] = BT_MALFORM;
28: }
29: }
30:
31: int byteType(byte[] buf, int off) {
32: return byteTypeTable[buf[off] & 0xFF];
33: }
34:
35: int byteToAscii(byte[] buf, int off) {
36: return map.charAt(off);
37: }
38:
39: // c is a significant ASCII character
40: boolean charMatches(byte[] buf, int off, char c) {
41: return asciiToByte[c] == buf[off];
42: }
43:
44: public int convert(byte[] sourceBuf, int sourceStart,
45: int sourceEnd, char[] targetBuf, int targetStart) {
46: int initTargetStart = targetStart;
47: int c;
48: while (sourceStart != sourceEnd)
49: targetBuf[targetStart++] = map
50: .charAt(sourceBuf[sourceStart++] & 0xFF);
51: return targetStart - initTargetStart;
52: }
53:
54: public int getFixedBytesPerChar() {
55: return 1;
56: }
57:
58: public void movePosition(final byte[] buf, int off, int end,
59: Position pos) {
60: /* Maintain the invariant: off - colStart == colNumber. */
61: int colStart = off - pos.columnNumber;
62: int lineNumber = pos.lineNumber;
63: while (off != end) {
64: switch (byteTypeTable[buf[off++] & 0xFF]) {
65: case BT_CR:
66: lineNumber += 1;
67: colStart = off;
68: break;
69: case BT_LF:
70: lineNumber += 1;
71: if (off != end && buf[off] == asciiToByte['\n'])
72: off++;
73: colStart = off;
74: break;
75: }
76: }
77: pos.columnNumber = off - colStart;
78: pos.lineNumber = lineNumber;
79: }
80:
81: int extendData(final byte[] buf, int off, final int end) {
82: while (off != end && byteTypeTable[buf[off] & 0xFF] >= 0)
83: off++;
84: return off;
85: }
86:
87: }
|