01: /* Copyright 2004 Ryan Ackley
02: *
03: * Licensed under the Apache License, Version 2.0 (the "License");
04: * you may not use this file except in compliance with the License.
05: * You may obtain a copy of the License at
06: *
07: * http://www.apache.org/licenses/LICENSE-2.0
08: *
09: * Unless required by applicable law or agreed to in writing, software
10: * distributed under the License is distributed on an "AS IS" BASIS,
11: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12: * See the License for the specific language governing permissions and
13: * limitations under the License.
14: */
15:
16: package org.textmining.text.extraction.sprm;
17:
18: import org.apache.poi.util.BitField;
19: import org.apache.poi.util.LittleEndian;
20:
21: public class SprmOperation {
22: final static private BitField OP_BITFIELD = new BitField(0x1ff);
23: final static private BitField SPECIAL_BITFIELD = new BitField(0x200);
24: final static private BitField TYPE_BITFIELD = new BitField(0x1c00);
25: final static private BitField SIZECODE_BITFIELD = new BitField(
26: 0xe000);
27:
28: private int _type;
29: //private boolean _variableLen;
30: private int _operation;
31: private int _operand;
32: private byte[] _varOperand;
33: private int _sizeNeeded;
34:
35: public SprmOperation(byte[] grpprl, int offset) {
36: short sprmStart = LittleEndian.getShort(grpprl, offset);
37: offset += 2;
38:
39: _operation = OP_BITFIELD.getValue(sprmStart);
40: _type = TYPE_BITFIELD.getValue(sprmStart);
41: int sizeCode = SIZECODE_BITFIELD.getValue(sprmStart);
42:
43: switch (sizeCode) {
44: case 0:
45: case 1:
46: _operand = LittleEndian.getUnsignedByte(grpprl, offset);
47: _sizeNeeded = 3;
48: break;
49: case 2:
50: case 4:
51: case 5:
52: _operand = LittleEndian.getShort(grpprl, offset);
53: _sizeNeeded = 4;
54: break;
55: case 3:
56: _operand = LittleEndian.getInt(grpprl, offset);
57: _sizeNeeded = 6;
58: break;
59: case 6:
60: _varOperand = new byte[grpprl[offset++]];
61: System.arraycopy(grpprl, offset, _varOperand, 0,
62: _varOperand.length);
63: _sizeNeeded = _varOperand.length + 3;
64: break;
65: case 7:
66: byte threeByteInt[] = new byte[4];
67: threeByteInt[0] = grpprl[offset];
68: threeByteInt[1] = grpprl[offset + 1];
69: threeByteInt[2] = grpprl[offset + 2];
70: threeByteInt[3] = (byte) 0;
71: _operand = LittleEndian.getInt(threeByteInt, 0);
72: _sizeNeeded = 5;
73: break;
74:
75: }
76: }
77:
78: public int getType() {
79: return _type;
80: }
81:
82: public int getOperation() {
83: return _operation;
84: }
85:
86: public int getOperand() {
87: return _operand;
88: }
89:
90: public int size() {
91: return _sizeNeeded;
92: }
93: }
|