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 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.IOUtils;
  18. import org.apache.poi.util.Internal;
  19. import org.apache.poi.util.POILogFactory;
  20. import org.apache.poi.util.POILogger;
  21. /**
  22. * "The LVL structure contains formatting information about a specific level in
  23. * a list. When a paragraph is formatted as part of this level, each placeholder
  24. * in xst is replaced with the inherited level number of the most recent or
  25. * current paragraph in the same list that is in the zero-based level specified
  26. * by that placeholder. The level number that replaces a placeholder is
  27. * formatted according to the lvlf.nfc of the LVL structure that corresponds to
  28. * the level that the placeholder specifies, unless the lvlf.fLegal of this LVL
  29. * structure is nonzero." -- Page 388 of 621 -- [MS-DOC] -- v20110315 Word
  30. * (.doc) Binary File Format
  31. */
  32. @Internal
  33. public final class ListLevel
  34. {
  35. //arbitrarily selected; may need to increase
  36. private static final int MAX_RECORD_LENGTH = 10_485_760;
  37. private static final POILogger logger = POILogFactory
  38. .getLogger( ListLevel.class );
  39. private byte[] _grpprlChpx;
  40. private byte[] _grpprlPapx;
  41. private LVLF _lvlf;
  42. /**
  43. * An Xst that specifies the number text that begins each paragraph in this
  44. * level. This can contain placeholders for level numbers that are inherited
  45. * from the other paragraphs in the list. Any element in the rgtchar field
  46. * of this Xst can be a placeholder. Each placeholder is an unsigned 2-byte
  47. * integer that specifies the zero-based level that the placeholder is for.
  48. *
  49. * Each placeholder MUST have a value that is less than or equal to the
  50. * zero-based level of the list that this LVL represents. The indexes of the
  51. * placeholders are specified by lvlf.rgbxchNums. Placeholders that
  52. * correspond to levels that do not have a number sequence (see lvlf.nfc)
  53. * MUST be ignored. If this level uses bullets (see lvlf.nfc), the cch field
  54. * of this Xst MUST be equal to 0x0001, and this MUST NOT contain any
  55. * placeholders.
  56. */
  57. private Xst _xst = new Xst();
  58. ListLevel()
  59. {
  60. }
  61. @Deprecated
  62. public ListLevel( final byte[] buf, final int startOffset )
  63. {
  64. read( buf, startOffset );
  65. }
  66. public ListLevel( int level, boolean numbered )
  67. {
  68. _lvlf = new LVLF();
  69. setStartAt( 1 );
  70. _grpprlPapx = new byte[0];
  71. _grpprlChpx = new byte[0];
  72. if ( numbered )
  73. {
  74. _lvlf.getRgbxchNums()[0] = 1;
  75. _xst = new Xst("" + (char) level + ".");
  76. }
  77. else
  78. {
  79. _xst = new Xst("\u2022");
  80. }
  81. }
  82. public ListLevel( int startAt, int numberFormatCode, int alignment,
  83. byte[] numberProperties, byte[] entryProperties, String numberText )
  84. {
  85. _lvlf = new LVLF();
  86. setStartAt( startAt );
  87. _lvlf.setNfc( (byte) numberFormatCode );
  88. _lvlf.setJc( (byte) alignment );
  89. _grpprlChpx = numberProperties.clone();
  90. _grpprlPapx = entryProperties.clone();
  91. _xst = new Xst(numberText);
  92. }
  93. @Override
  94. public boolean equals( Object obj )
  95. {
  96. if (!(obj instanceof ListLevel)) return false;
  97. ListLevel lvl = (ListLevel) obj;
  98. return lvl._lvlf.equals( this._lvlf )
  99. && Arrays.equals( lvl._grpprlChpx, _grpprlChpx )
  100. && Arrays.equals( lvl._grpprlPapx, _grpprlPapx )
  101. && lvl._xst.equals( this._xst );
  102. }
  103. @Override
  104. public int hashCode() {
  105. assert false : "hashCode not designed";
  106. return 42; // any arbitrary constant will do
  107. }
  108. /**
  109. * "Alignment (left, right, or centered) of the paragraph number."
  110. */
  111. public int getAlignment()
  112. {
  113. return _lvlf.getJc();
  114. }
  115. public byte[] getGrpprlChpx()
  116. {
  117. return _grpprlChpx;
  118. }
  119. public byte[] getGrpprlPapx()
  120. {
  121. return _grpprlPapx;
  122. }
  123. public byte[] getLevelProperties()
  124. {
  125. return _grpprlPapx;
  126. }
  127. /**
  128. * "Number format code (see anld.nfc for a list of options)"
  129. */
  130. public int getNumberFormat()
  131. {
  132. return _lvlf.getNfc();
  133. }
  134. public String getNumberText()
  135. {
  136. return _xst.getAsJavaString();
  137. }
  138. public int getSizeInBytes()
  139. {
  140. return LVLF.getSize() + _lvlf.getCbGrpprlChpx()
  141. + _lvlf.getCbGrpprlPapx() + _xst.getSize();
  142. }
  143. public int getStartAt()
  144. {
  145. return _lvlf.getIStartAt();
  146. }
  147. /**
  148. * "The type of character following the number text for the paragraph: 0 == tab, 1 == space, 2 == nothing."
  149. */
  150. public byte getTypeOfCharFollowingTheNumber()
  151. {
  152. return _lvlf.getIxchFollow();
  153. }
  154. /**
  155. * An unsigned integer that specifies the first (most-significant) zero-based level after which the number sequence of this level does not restart. The number sequence of this level does restart after any level that is more significant than the specified level. This MUST be less than or equal to the zero-based level of the list to which this LVLF corresponds.
  156. * <p>see [MS-DOC], v20140721, 2.9.150</p>
  157. *
  158. * @return the first ({@code 0} is the most significant) level after which
  159. * the numbering does not restart or {@code -1} if no restart is applicable
  160. */
  161. public short getRestart() {
  162. return _lvlf.isFNoRestart() ? _lvlf.getIlvlRestartLim() : -1;
  163. }
  164. /**
  165. * Determines if the number formatting shall be overridden by
  166. * {@code msonfcArabic}; unless it originally was {@code msonfcArabicLZ}
  167. * in which case it is preserved.
  168. * <p>see [MS-DOC], v20140721, 2.9.150 and [MS-OSHARED], v20140721, 2.2.1.3</p>
  169. *
  170. * @return {@code true} if the level numbering of this and all more
  171. * significant levels must be overridden; {@code false} otherwise
  172. */
  173. public boolean isLegalNumbering() {
  174. return _lvlf.isFLegal();
  175. }
  176. /**
  177. * Array which specifies the character offsets of the level numbers in a
  178. * level numbering string.
  179. * <p>see [MS-DOC], v20140721, 2.9.150</p>
  180. *
  181. * @return {@code 0}-terminated array, unless it is full
  182. */
  183. public byte[] getLevelNumberingPlaceholderOffsets() {
  184. return _lvlf.getRgbxchNums();
  185. }
  186. int read( final byte[] data, final int startOffset )
  187. {
  188. int offset = startOffset;
  189. _lvlf = new LVLF( data, offset );
  190. offset += LVLF.getSize();
  191. //short -- no need to safely allocate
  192. _grpprlPapx = new byte[_lvlf.getCbGrpprlPapx()];
  193. System.arraycopy( data, offset, _grpprlPapx, 0, _lvlf.getCbGrpprlPapx() );
  194. offset += _lvlf.getCbGrpprlPapx();
  195. //short -- no need to safely allocate
  196. _grpprlChpx = new byte[_lvlf.getCbGrpprlChpx()];
  197. System.arraycopy( data, offset, _grpprlChpx, 0, _lvlf.getCbGrpprlChpx() );
  198. offset += _lvlf.getCbGrpprlChpx();
  199. _xst = new Xst( data, offset );
  200. offset += _xst.getSize();
  201. /*
  202. * "If this level uses bullets (see lvlf.nfc), the cch field of this Xst
  203. * MUST be equal to 0x0001, and this MUST NOT contain any placeholders."
  204. * -- page 389 of 621 -- [MS-DOC] -- v20110315 Word (.doc) Binary File
  205. * Format
  206. */
  207. if ( _lvlf.getNfc() == 0x17 )
  208. {
  209. if ( _xst.getCch() != 1 )
  210. {
  211. logger.log( POILogger.WARN, "LVL at offset ",
  212. Integer.valueOf( startOffset ),
  213. " has nfc == 0x17 (bullets), but cch != 1 (",
  214. Integer.valueOf( _xst.getCch() ), ")" );
  215. }
  216. }
  217. return offset - startOffset;
  218. }
  219. public void setAlignment( int alignment )
  220. {
  221. _lvlf.setJc( (byte) alignment );
  222. }
  223. public void setLevelProperties( byte[] grpprl )
  224. {
  225. _grpprlPapx = grpprl;
  226. }
  227. public void setNumberFormat( int numberFormatCode )
  228. {
  229. _lvlf.setNfc( (byte) numberFormatCode );
  230. }
  231. public void setNumberProperties( byte[] grpprl )
  232. {
  233. _grpprlChpx = grpprl;
  234. }
  235. public void setStartAt( int startAt )
  236. {
  237. _lvlf.setIStartAt( startAt );
  238. }
  239. public void setTypeOfCharFollowingTheNumber( byte value )
  240. {
  241. _lvlf.setIxchFollow( value );
  242. }
  243. public byte[] toByteArray()
  244. {
  245. byte[] buf = IOUtils.safelyAllocate(getSizeInBytes(), MAX_RECORD_LENGTH);
  246. int offset = 0;
  247. _lvlf.setCbGrpprlChpx( (short) _grpprlChpx.length );
  248. _lvlf.setCbGrpprlPapx( (short) _grpprlPapx.length );
  249. _lvlf.serialize( buf, offset );
  250. offset += LVLF.getSize();
  251. System.arraycopy( _grpprlPapx, 0, buf, offset, _grpprlPapx.length );
  252. offset += _grpprlPapx.length;
  253. System.arraycopy( _grpprlChpx, 0, buf, offset, _grpprlChpx.length );
  254. offset += _grpprlChpx.length;
  255. _xst.serialize( buf, offset );
  256. offset += _xst.getSize();
  257. return buf;
  258. }
  259. @Override
  260. public String toString()
  261. {
  262. return "LVL: " + ( "\n" + _lvlf ).replaceAll( "\n", "\n " )
  263. + "\n"
  264. + ( "PAPX's grpprl: " + Arrays.toString( _grpprlPapx ) + "\n" )
  265. + ( "CHPX's grpprl: " + Arrays.toString( _grpprlChpx ) + "\n" )
  266. + ( "xst: " + _xst + "\n" );
  267. }
  268. }