You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ListLevel.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. 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. package org.apache.poi.hwpf.model;
  16. import java.util.Arrays;
  17. import org.apache.poi.util.BitField;
  18. import org.apache.poi.util.LittleEndian;
  19. /**
  20. *
  21. */
  22. public final class ListLevel
  23. {
  24. private static final int RGBXCH_NUMS_SIZE = 9;
  25. private int _iStartAt;
  26. private byte _nfc;
  27. private byte _info;
  28. private static BitField _jc;
  29. private static BitField _fLegal;
  30. private static BitField _fNoRestart;
  31. private static BitField _fPrev;
  32. private static BitField _fPrevSpace;
  33. private static BitField _fWord6;
  34. private byte[] _rgbxchNums;
  35. private byte _ixchFollow;
  36. private int _dxaSpace;
  37. private int _dxaIndent;
  38. private int _cbGrpprlChpx;
  39. private int _cbGrpprlPapx;
  40. private short _reserved;
  41. private byte[] _grpprlPapx;
  42. private byte[] _grpprlChpx;
  43. private char[] _numberText=null;
  44. public ListLevel(int startAt, int numberFormatCode, int alignment,
  45. byte[] numberProperties, byte[] entryProperties,
  46. String numberText)
  47. {
  48. _iStartAt = startAt;
  49. _nfc = (byte)numberFormatCode;
  50. _jc.setValue(_info, alignment);
  51. _grpprlChpx = numberProperties;
  52. _grpprlPapx = entryProperties;
  53. _numberText = numberText.toCharArray();
  54. }
  55. public ListLevel(int level, boolean numbered)
  56. {
  57. _iStartAt = 1;
  58. _grpprlPapx = new byte[0];
  59. _grpprlChpx = new byte[0];
  60. _numberText = new char[0];
  61. _rgbxchNums = new byte[RGBXCH_NUMS_SIZE];
  62. if (numbered)
  63. {
  64. _rgbxchNums[0] = 1;
  65. _numberText = new char[]{(char)level, '.'};
  66. }
  67. else
  68. {
  69. _numberText = new char[]{'\u2022'};
  70. }
  71. }
  72. public ListLevel(byte[] buf, int offset)
  73. {
  74. _iStartAt = LittleEndian.getInt(buf, offset);
  75. offset += LittleEndian.INT_SIZE;
  76. _nfc = buf[offset++];
  77. _info = buf[offset++];
  78. _rgbxchNums = new byte[RGBXCH_NUMS_SIZE];
  79. System.arraycopy(buf, offset, _rgbxchNums, 0, RGBXCH_NUMS_SIZE);
  80. offset += RGBXCH_NUMS_SIZE;
  81. _ixchFollow = buf[offset++];
  82. _dxaSpace = LittleEndian.getInt(buf, offset);
  83. offset += LittleEndian.INT_SIZE;
  84. _dxaIndent = LittleEndian.getInt(buf, offset);
  85. offset += LittleEndian.INT_SIZE;
  86. _cbGrpprlChpx = LittleEndian.getUnsignedByte(buf, offset++);
  87. _cbGrpprlPapx = LittleEndian.getUnsignedByte(buf, offset++);
  88. _reserved = LittleEndian.getShort(buf, offset);
  89. offset += LittleEndian.SHORT_SIZE;
  90. _grpprlPapx = new byte[_cbGrpprlPapx];
  91. _grpprlChpx = new byte[_cbGrpprlChpx];
  92. System.arraycopy(buf, offset, _grpprlPapx, 0, _cbGrpprlPapx);
  93. offset += _cbGrpprlPapx;
  94. System.arraycopy(buf, offset, _grpprlChpx, 0, _cbGrpprlChpx);
  95. offset += _cbGrpprlChpx;
  96. int numberTextLength = LittleEndian.getShort(buf, offset);
  97. /* sometimes numberTextLength<0 */
  98. /* by derjohng */
  99. if (numberTextLength>0)
  100. {
  101. _numberText = new char[numberTextLength];
  102. offset += LittleEndian.SHORT_SIZE;
  103. for (int x = 0; x < numberTextLength; x++)
  104. {
  105. _numberText[x] = (char)LittleEndian.getShort(buf, offset);
  106. offset += LittleEndian.SHORT_SIZE;
  107. }
  108. }
  109. }
  110. public int getStartAt()
  111. {
  112. return _iStartAt;
  113. }
  114. public int getNumberFormat()
  115. {
  116. return _nfc;
  117. }
  118. public int getAlignment()
  119. {
  120. return _jc.getValue(_info);
  121. }
  122. public String getNumberText()
  123. {
  124. if (_numberText != null)
  125. return new String(_numberText);
  126. else
  127. return null;
  128. }
  129. public void setStartAt(int startAt)
  130. {
  131. _iStartAt = startAt;
  132. }
  133. public void setNumberFormat(int numberFormatCode)
  134. {
  135. _nfc = (byte)numberFormatCode;
  136. }
  137. public void setAlignment(int alignment)
  138. {
  139. _jc.setValue(_info, alignment);
  140. }
  141. public void setNumberProperties(byte[] grpprl)
  142. {
  143. _grpprlChpx = grpprl;
  144. }
  145. public void setLevelProperties(byte[] grpprl)
  146. {
  147. _grpprlPapx = grpprl;
  148. }
  149. public byte[] getLevelProperties()
  150. {
  151. return _grpprlPapx;
  152. }
  153. public boolean equals(Object obj)
  154. {
  155. if (obj == null)
  156. {
  157. return false;
  158. }
  159. ListLevel lvl = (ListLevel)obj;
  160. return _cbGrpprlChpx == lvl._cbGrpprlChpx && lvl._cbGrpprlPapx == _cbGrpprlPapx &&
  161. lvl._dxaIndent == _dxaIndent && lvl._dxaSpace == _dxaSpace &&
  162. Arrays.equals(lvl._grpprlChpx, _grpprlChpx) &&
  163. Arrays.equals(lvl._grpprlPapx, _grpprlPapx) &&
  164. lvl._info == _info && lvl._iStartAt == _iStartAt &&
  165. lvl._ixchFollow == _ixchFollow && lvl._nfc == _nfc &&
  166. Arrays.equals(lvl._numberText, _numberText) &&
  167. Arrays.equals(lvl._rgbxchNums, _rgbxchNums) &&
  168. lvl._reserved == _reserved;
  169. }
  170. public byte[] toByteArray()
  171. {
  172. byte[] buf = new byte[getSizeInBytes()];
  173. int offset = 0;
  174. LittleEndian.putInt(buf, offset, _iStartAt);
  175. offset += LittleEndian.INT_SIZE;
  176. buf[offset++] = _nfc;
  177. buf[offset++] = _info;
  178. System.arraycopy(_rgbxchNums, 0, buf, offset, RGBXCH_NUMS_SIZE);
  179. offset += RGBXCH_NUMS_SIZE;
  180. buf[offset++] = _ixchFollow;
  181. LittleEndian.putInt(buf, offset, _dxaSpace);
  182. offset += LittleEndian.INT_SIZE;
  183. LittleEndian.putInt(buf, offset, _dxaIndent);
  184. offset += LittleEndian.INT_SIZE;
  185. buf[offset++] = (byte)_cbGrpprlChpx;
  186. buf[offset++] = (byte)_cbGrpprlPapx;
  187. LittleEndian.putShort(buf, offset, _reserved);
  188. offset += LittleEndian.SHORT_SIZE;
  189. System.arraycopy(_grpprlPapx, 0, buf, offset, _cbGrpprlPapx);
  190. offset += _cbGrpprlPapx;
  191. System.arraycopy(_grpprlChpx, 0, buf, offset, _cbGrpprlChpx);
  192. offset += _cbGrpprlChpx;
  193. if (_numberText == null) {
  194. // TODO - write junit to test this flow
  195. LittleEndian.putUShort(buf, offset, 0);
  196. } else {
  197. LittleEndian.putUShort(buf, offset, _numberText.length);
  198. offset += LittleEndian.SHORT_SIZE;
  199. for (int x = 0; x < _numberText.length; x++)
  200. {
  201. LittleEndian.putUShort(buf, offset, _numberText[x]);
  202. offset += LittleEndian.SHORT_SIZE;
  203. }
  204. }
  205. return buf;
  206. }
  207. public int getSizeInBytes()
  208. {
  209. int result =
  210. 6 // int byte byte
  211. + RGBXCH_NUMS_SIZE
  212. + 13 // byte int int byte byte short
  213. + _cbGrpprlChpx
  214. + _cbGrpprlPapx
  215. + 2; // numberText length
  216. if (_numberText != null) {
  217. result += _numberText.length * LittleEndian.SHORT_SIZE;
  218. }
  219. return result;
  220. }
  221. }