001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hwpf.model;
019:
020: import org.apache.poi.util.BitField;
021: import org.apache.poi.util.LittleEndian;
022:
023: import org.apache.poi.hwpf.usermodel.CharacterProperties;
024: import org.apache.poi.hwpf.usermodel.ParagraphProperties;
025:
026: import org.apache.poi.hwpf.sprm.ParagraphSprmCompressor;
027: import org.apache.poi.hwpf.sprm.CharacterSprmCompressor;
028:
029: import java.util.Arrays;
030:
031: public class ListLevel {
032: private int _iStartAt;
033: private byte _nfc;
034: private byte _info;
035: private static BitField _jc;
036: private static BitField _fLegal;
037: private static BitField _fNoRestart;
038: private static BitField _fPrev;
039: private static BitField _fPrevSpace;
040: private static BitField _fWord6;
041: private byte[] _rgbxchNums;
042: private byte _ixchFollow;
043: private int _dxaSpace;
044: private int _dxaIndent;
045: private int _cbGrpprlChpx;
046: private int _cbGrpprlPapx;
047: private short _reserved;
048: private byte[] _grpprlPapx;
049: private byte[] _grpprlChpx;
050: private char[] _numberText = null;
051:
052: public ListLevel(int startAt, int numberFormatCode, int alignment,
053: byte[] numberProperties, byte[] entryProperties,
054: String numberText) {
055: _iStartAt = startAt;
056: _nfc = (byte) numberFormatCode;
057: _jc.setValue(_info, alignment);
058: _grpprlChpx = numberProperties;
059: _grpprlPapx = entryProperties;
060: _numberText = numberText.toCharArray();
061: }
062:
063: public ListLevel(int level, boolean numbered) {
064: _iStartAt = 1;
065: _grpprlPapx = new byte[0];
066: _grpprlChpx = new byte[0];
067: _numberText = new char[0];
068: _rgbxchNums = new byte[9];
069:
070: if (numbered) {
071: _rgbxchNums[0] = 1;
072: _numberText = new char[] { (char) level, '.' };
073: } else {
074: _numberText = new char[] { '\u2022' };
075: }
076: }
077:
078: public ListLevel(byte[] buf, int offset) {
079: _iStartAt = LittleEndian.getInt(buf, offset);
080: offset += LittleEndian.INT_SIZE;
081: _nfc = buf[offset++];
082: _info = buf[offset++];
083:
084: _rgbxchNums = new byte[9];
085: for (int x = 0; x < 9; x++) {
086: _rgbxchNums[x] = buf[offset++];
087: }
088: _ixchFollow = buf[offset++];
089: _dxaSpace = LittleEndian.getInt(buf, offset);
090: offset += LittleEndian.INT_SIZE;
091: _dxaIndent = LittleEndian.getInt(buf, offset);
092: offset += LittleEndian.INT_SIZE;
093: _cbGrpprlChpx = LittleEndian.getUnsignedByte(buf, offset++);
094: _cbGrpprlPapx = LittleEndian.getUnsignedByte(buf, offset++);
095: _reserved = LittleEndian.getShort(buf, offset);
096: offset += LittleEndian.SHORT_SIZE;
097:
098: _grpprlPapx = new byte[_cbGrpprlPapx];
099: _grpprlChpx = new byte[_cbGrpprlChpx];
100: System.arraycopy(buf, offset, _grpprlPapx, 0, _cbGrpprlPapx);
101: offset += _cbGrpprlPapx;
102: System.arraycopy(buf, offset, _grpprlChpx, 0, _cbGrpprlChpx);
103: offset += _cbGrpprlChpx;
104:
105: int numberTextLength = LittleEndian.getShort(buf, offset);
106: /* sometimes numberTextLength<0 */
107: /* by derjohng */
108: if (numberTextLength > 0) {
109: _numberText = new char[numberTextLength];
110: offset += LittleEndian.SHORT_SIZE;
111: for (int x = 0; x < numberTextLength; x++) {
112: _numberText[x] = (char) LittleEndian.getShort(buf,
113: offset);
114: offset += LittleEndian.SHORT_SIZE;
115: }
116: }
117:
118: }
119:
120: public int getStartAt() {
121: return _iStartAt;
122: }
123:
124: public int getNumberFormat() {
125: return _nfc;
126: }
127:
128: public int getAlignment() {
129: return _jc.getValue(_info);
130: }
131:
132: public String getNumberText() {
133: return new String(_numberText);
134: }
135:
136: public void setStartAt(int startAt) {
137: _iStartAt = startAt;
138: }
139:
140: public void setNumberFormat(int numberFormatCode) {
141: _nfc = (byte) numberFormatCode;
142: }
143:
144: public void setAlignment(int alignment) {
145: _jc.setValue(_info, alignment);
146: }
147:
148: public void setNumberProperties(byte[] grpprl) {
149: _grpprlChpx = grpprl;
150:
151: }
152:
153: public void setLevelProperties(byte[] grpprl) {
154: _grpprlPapx = grpprl;
155: }
156:
157: public byte[] getLevelProperties() {
158: return _grpprlPapx;
159: }
160:
161: public boolean equals(Object obj) {
162: if (obj == null) {
163: return false;
164: }
165:
166: ListLevel lvl = (ListLevel) obj;
167: return _cbGrpprlChpx == lvl._cbGrpprlChpx
168: && lvl._cbGrpprlPapx == _cbGrpprlPapx
169: && lvl._dxaIndent == _dxaIndent
170: && lvl._dxaSpace == _dxaSpace
171: && Arrays.equals(lvl._grpprlChpx, _grpprlChpx)
172: && Arrays.equals(lvl._grpprlPapx, _grpprlPapx)
173: && lvl._info == _info && lvl._iStartAt == _iStartAt
174: && lvl._ixchFollow == _ixchFollow && lvl._nfc == _nfc
175: && Arrays.equals(lvl._numberText, _numberText)
176: && Arrays.equals(lvl._rgbxchNums, _rgbxchNums)
177: && lvl._reserved == _reserved;
178:
179: }
180:
181: public byte[] toByteArray() {
182: byte[] buf = new byte[getSizeInBytes()];
183: int offset = 0;
184: LittleEndian.putInt(buf, offset, _iStartAt);
185: offset += LittleEndian.INT_SIZE;
186: buf[offset++] = _nfc;
187: buf[offset++] = _info;
188: System.arraycopy(_rgbxchNums, 0, buf, offset,
189: _rgbxchNums.length);
190: offset += _rgbxchNums.length;
191: buf[offset++] = _ixchFollow;
192: LittleEndian.putInt(buf, offset, _dxaSpace);
193: offset += LittleEndian.INT_SIZE;
194: LittleEndian.putInt(buf, offset, _dxaIndent);
195: offset += LittleEndian.INT_SIZE;
196:
197: buf[offset++] = (byte) _cbGrpprlChpx;
198: buf[offset++] = (byte) _cbGrpprlPapx;
199: LittleEndian.putShort(buf, offset, _reserved);
200: offset += LittleEndian.SHORT_SIZE;
201:
202: System.arraycopy(_grpprlChpx, 0, buf, offset, _cbGrpprlChpx);
203: offset += _cbGrpprlChpx;
204: System.arraycopy(_grpprlPapx, 0, buf, offset, _cbGrpprlPapx);
205: offset += _cbGrpprlPapx;
206:
207: LittleEndian.putShort(buf, offset, (short) _numberText.length);
208: offset += LittleEndian.SHORT_SIZE;
209: for (int x = 0; x < _numberText.length; x++) {
210: LittleEndian.putShort(buf, offset, (short) _numberText[x]);
211: offset += LittleEndian.SHORT_SIZE;
212: }
213: return buf;
214: }
215:
216: public int getSizeInBytes() {
217: if (_numberText != null) {
218: return 28 + _cbGrpprlChpx + _cbGrpprlPapx
219: + (_numberText.length * LittleEndian.SHORT_SIZE)
220: + 2;
221: } else {
222: return 28 + _cbGrpprlChpx + _cbGrpprlPapx + 2;
223: }
224: }
225:
226: }
|