PageRenderTime 99ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/apache/poi/hwpf/usermodel/HWPFList.java

https://github.com/minstrelsy/SimpleAndroidDocView
Java | 249 lines | 150 code | 31 blank | 68 comment | 16 complexity | 8bb8ee10eff23db02cff3526d2c5e375 MD5 | raw file
Possible License(s): Apache-2.0
  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.usermodel;
  16. import org.apache.poi.util.POILogFactory;
  17. import org.apache.poi.util.POILogger;
  18. import org.apache.poi.hwpf.model.ListTables;
  19. import org.apache.poi.util.Internal;
  20. import org.apache.poi.hwpf.model.LFO;
  21. import org.apache.poi.hwpf.model.LFOData;
  22. import org.apache.poi.hwpf.model.ListData;
  23. import org.apache.poi.hwpf.model.ListFormatOverrideLevel;
  24. import org.apache.poi.hwpf.model.ListLevel;
  25. import org.apache.poi.hwpf.model.StyleSheet;
  26. import org.apache.poi.hwpf.sprm.CharacterSprmCompressor;
  27. import org.apache.poi.hwpf.sprm.ParagraphSprmCompressor;
  28. /**
  29. * This class is used to create a list in a Word document. It is used in
  30. * conjunction with
  31. * {@link org.apache.poi.hwpf.HWPFDocument#registerList(HWPFList) registerList}
  32. * in {@link org.apache.poi.hwpf.HWPFDocument HWPFDocument}.
  33. *
  34. * In Word, lists are not ranged entities, meaning you can't actually add one to
  35. * the document. Lists only act as properties for list entries. Once you
  36. * register a list, you can add list entries to a document that are a part of
  37. * the list.
  38. *
  39. * The only benefit of this that I see, is that you can add a list entry
  40. * anywhere in the document and continue numbering from the previous list.
  41. *
  42. * @author Ryan Ackley
  43. */
  44. public final class HWPFList
  45. {
  46. private static POILogger log = POILogFactory.getLogger( HWPFList.class );
  47. private boolean _ignoreLogicalLeftIdentation = false;
  48. private LFO _lfo;
  49. private LFOData _lfoData;
  50. private ListData _listData;
  51. private ListTables _listTables;
  52. private boolean _registered;
  53. private StyleSheet _styleSheet;
  54. /**
  55. *
  56. * @param numbered
  57. * true if the list should be numbered; false if it should be
  58. * bulleted.
  59. * @param styleSheet
  60. * The document's stylesheet.
  61. */
  62. public HWPFList( boolean numbered, StyleSheet styleSheet )
  63. {
  64. _listData = new ListData(
  65. (int) ( Math.random() * System.currentTimeMillis() ), numbered );
  66. _lfo = new LFO();
  67. _lfo.setLsid( _listData.getLsid() );
  68. _lfoData = new LFOData();
  69. _styleSheet = styleSheet;
  70. }
  71. public HWPFList( StyleSheet styleSheet, ListTables listTables, int ilfo )
  72. {
  73. _listTables = listTables;
  74. _styleSheet = styleSheet;
  75. _registered = true;
  76. /* See documentation for sprmPIlfo (0x460B) */
  77. if ( ilfo == 0 || ilfo == 0xF801 )
  78. {
  79. throw new IllegalArgumentException( "Paragraph not in list" );
  80. }
  81. else if ( 0x0001 <= ilfo && ilfo <= 0x07FE )
  82. {
  83. _lfo = listTables.getLfo( ilfo );
  84. _lfoData = listTables.getLfoData( ilfo );
  85. }
  86. else if ( 0xF802 <= ilfo && ilfo <= 0xFFFF )
  87. {
  88. int nilfo = ilfo ^ 0xFFFF;
  89. _lfo = listTables.getLfo( nilfo );
  90. _lfoData = listTables.getLfoData( nilfo );
  91. _ignoreLogicalLeftIdentation = true;
  92. }
  93. else
  94. {
  95. throw new IllegalArgumentException( "Incorrect ilfo: " + ilfo );
  96. }
  97. _listData = listTables.getListData( _lfo.getLsid() );
  98. }
  99. @Internal
  100. public LFO getLFO()
  101. {
  102. return _lfo;
  103. }
  104. @Internal
  105. public LFOData getLFOData()
  106. {
  107. return _lfoData;
  108. }
  109. @Internal
  110. public ListData getListData()
  111. {
  112. return _listData;
  113. }
  114. public int getLsid()
  115. {
  116. return _lfo.getLsid();
  117. }
  118. @Internal
  119. ListLevel getLVL( char level )
  120. {
  121. if ( level >= _listData.numLevels() )
  122. {
  123. throw new IllegalArgumentException( "Required level "
  124. + ( (int) level )
  125. + " is more than number of level for list ("
  126. + _listData.numLevels() + ")" );
  127. }
  128. ListLevel lvl = _listData.getLevels()[level];
  129. return lvl;
  130. }
  131. /**
  132. * An MSONFC, as specified in [MS-OSHARED] section 2.2.1.3, that specifies
  133. * the format of the level numbers that replace the placeholders for this
  134. * level in the xst fields of the LVLs in this list. This value MUST not be
  135. * equal to 0x08, 0x09, 0x0F, or 0x13. If this is equal to 0xFF or 0x17,
  136. * this level does not have a number sequence and therefore has no number
  137. * formatting. If this is equal to 0x17, the level uses bullets.
  138. */
  139. public int getNumberFormat( char level )
  140. {
  141. return getLVL( level ).getNumberFormat();
  142. }
  143. public String getNumberText( char level )
  144. {
  145. return getLVL( level ).getNumberText();
  146. }
  147. public int getStartAt( char level )
  148. {
  149. if ( isStartAtOverriden( level ) )
  150. {
  151. return _lfoData.getRgLfoLvl()[level].getIStartAt();
  152. }
  153. return getLVL( level ).getStartAt();
  154. }
  155. /**
  156. * "The type of character following the number text for the paragraph: 0 == tab, 1 == space, 2 == nothing."
  157. */
  158. public byte getTypeOfCharFollowingTheNumber( char level )
  159. {
  160. return getLVL( level ).getTypeOfCharFollowingTheNumber();
  161. }
  162. public boolean isIgnoreLogicalLeftIdentation()
  163. {
  164. return _ignoreLogicalLeftIdentation;
  165. }
  166. public boolean isStartAtOverriden( char level )
  167. {
  168. ListFormatOverrideLevel lfolvl = _lfoData.getRgLfoLvl().length > level ? _lfoData
  169. .getRgLfoLvl()[level] : null;
  170. return lfolvl != null && lfolvl.getIStartAt() != 0
  171. && !lfolvl.isFormatting();
  172. }
  173. public void setIgnoreLogicalLeftIdentation(
  174. boolean ignoreLogicalLeftIdentation )
  175. {
  176. this._ignoreLogicalLeftIdentation = ignoreLogicalLeftIdentation;
  177. }
  178. /**
  179. * Sets the character properties of the list numbers.
  180. *
  181. * @param level
  182. * the level number that the properties should apply to.
  183. * @param chp
  184. * The character properties.
  185. */
  186. public void setLevelNumberProperties( int level, CharacterProperties chp )
  187. {
  188. ListLevel listLevel = _listData.getLevel( level );
  189. int styleIndex = _listData.getLevelStyle( level );
  190. CharacterProperties base = _styleSheet.getCharacterStyle( styleIndex );
  191. byte[] grpprl = CharacterSprmCompressor.compressCharacterProperty( chp,
  192. base );
  193. listLevel.setNumberProperties( grpprl );
  194. }
  195. /**
  196. * Sets the paragraph properties for a particular level of the list.
  197. *
  198. * @param level
  199. * The level number.
  200. * @param pap
  201. * The paragraph properties
  202. */
  203. public void setLevelParagraphProperties( int level, ParagraphProperties pap )
  204. {
  205. ListLevel listLevel = _listData.getLevel( level );
  206. int styleIndex = _listData.getLevelStyle( level );
  207. ParagraphProperties base = _styleSheet.getParagraphStyle( styleIndex );
  208. byte[] grpprl = ParagraphSprmCompressor.compressParagraphProperty( pap,
  209. base );
  210. listLevel.setLevelProperties( grpprl );
  211. }
  212. public void setLevelStyle( int level, int styleIndex )
  213. {
  214. _listData.setLevelStyle( level, styleIndex );
  215. }
  216. }