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

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