KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hwpf > sprm > SprmOperation


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hwpf.sprm;
20
21 import org.apache.poi.util.BitField;
22 import org.apache.poi.util.LittleEndian;
23
24 /**
25  * This class is used to represent a sprm operation from a Word 97/2000/XP
26  * document.
27  * @author Ryan Ackley
28  * @version 1.0
29  */

30 public class SprmOperation
31 {
32   final static private BitField OP_BITFIELD = new BitField(0x1ff);
33   final static private BitField SPECIAL_BITFIELD = new BitField(0x200);
34   final static private BitField TYPE_BITFIELD = new BitField(0x1c00);
35   final static private BitField SIZECODE_BITFIELD = new BitField(0xe000);
36
37   final static private short LONG_SPRM_TABLE = (short)0xd608;
38   final static private short LONG_SPRM_PARAGRAPH = (short)0xc615;
39
40   final static public int PAP_TYPE = 1;
41   final static public int TAP_TYPE = 5;
42
43   private int _type;
44   private int _operation;
45   private int _gOffset;
46   private byte[] _grpprl;
47   private int _sizeCode;
48   private int _size;
49
50   public SprmOperation(byte[] grpprl, int offset)
51   {
52     _grpprl = grpprl;
53
54     short sprmStart = LittleEndian.getShort(grpprl, offset);
55
56     _gOffset = offset + 2;
57
58     _operation = OP_BITFIELD.getValue(sprmStart);
59     _type = TYPE_BITFIELD.getValue(sprmStart);
60     _sizeCode = SIZECODE_BITFIELD.getValue(sprmStart);
61     _size = initSize(sprmStart);
62   }
63
64   public static int getOperationFromOpcode(short opcode)
65   {
66     return OP_BITFIELD.getValue(opcode);
67   }
68
69   public static int getTypeFromOpcode(short opcode)
70   {
71     return TYPE_BITFIELD.getValue(opcode);
72   }
73
74   public int getType()
75   {
76     return _type;
77   }
78
79   public int getOperation()
80   {
81     return _operation;
82   }
83
84   public int getGrpprlOffset()
85   {
86     return _gOffset;
87   }
88   public int getOperand()
89   {
90     switch (_sizeCode)
91     {
92       case 0:
93       case 1:
94         return _grpprl[_gOffset];
95       case 2:
96       case 4:
97       case 5:
98         return LittleEndian.getShort(_grpprl, _gOffset);
99       case 3:
100         return LittleEndian.getInt(_grpprl, _gOffset);
101       case 6:
102         throw new UnsupportedOperationException JavaDoc("This SPRM contains a variable length operand");
103       case 7:
104         byte threeByteInt[] = new byte[4];
105         threeByteInt[0] = _grpprl[_gOffset];
106         threeByteInt[1] = _grpprl[_gOffset + 1];
107         threeByteInt[2] = _grpprl[_gOffset + 2];
108         threeByteInt[3] = (byte)0;
109         return LittleEndian.getInt(threeByteInt, 0);
110       default:
111         throw new IllegalArgumentException JavaDoc("SPRM contains an invalid size code");
112     }
113   }
114   public int getSizeCode()
115   {
116     return _sizeCode;
117   }
118
119   public int size()
120   {
121     return _size;
122   }
123
124   public byte[] getGrpprl()
125   {
126     return _grpprl;
127   }
128   private int initSize(short sprm)
129   {
130     switch (_sizeCode)
131     {
132       case 0:
133       case 1:
134         return 3;
135       case 2:
136       case 4:
137       case 5:
138         return 4;
139       case 3:
140         return 6;
141       case 6:
142         if (sprm == LONG_SPRM_TABLE || sprm == LONG_SPRM_PARAGRAPH)
143         {
144           int retVal = (0x0000ffff & LittleEndian.getShort(_grpprl, _gOffset)) + 3;
145           _gOffset += 2;
146           return retVal;
147         }
148         else
149         {
150           return (0x000000ff & _grpprl[_gOffset++]) + 3;
151         }
152       case 7:
153         return 5;
154       default:
155         throw new IllegalArgumentException JavaDoc("SPRM contains an invalid size code");
156     }
157   }
158 }
159
Popular Tags