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.

ListTables.java 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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.io.ByteArrayOutputStream;
  17. import java.io.IOException;
  18. import java.util.LinkedHashMap;
  19. import java.util.NoSuchElementException;
  20. import org.apache.poi.util.Internal;
  21. import org.apache.poi.util.LittleEndian;
  22. import org.apache.poi.util.POILogFactory;
  23. import org.apache.poi.util.POILogger;
  24. /**
  25. * @author Ryan Ackley
  26. */
  27. @Internal
  28. public final class ListTables
  29. {
  30. private final static POILogger log = POILogFactory.getLogger(ListTables.class);
  31. /**
  32. * Both PlfLst and the following LVLs
  33. */
  34. private final LinkedHashMap<Integer, ListData> _listMap = new LinkedHashMap<Integer, ListData>();
  35. private PlfLfo _plfLfo;
  36. public ListTables()
  37. {
  38. }
  39. public ListTables( byte[] tableStream, final int lstOffset,
  40. final int fcPlfLfo, final int lcbPlfLfo )
  41. {
  42. /*
  43. * The PlfLst structure contains the list formatting information for the
  44. * document. -- Page 425 of 621. [MS-DOC] -- v20110315 Word (.doc)
  45. * Binary File Format
  46. */
  47. int offset = lstOffset;
  48. int cLst = LittleEndian.getShort( tableStream, offset );
  49. offset += LittleEndian.SHORT_SIZE;
  50. int levelOffset = offset + ( cLst * LSTF.getSize() );
  51. for ( int x = 0; x < cLst; x++ )
  52. {
  53. ListData lst = new ListData( tableStream, offset );
  54. _listMap.put( Integer.valueOf( lst.getLsid() ), lst );
  55. offset += LSTF.getSize();
  56. int num = lst.numLevels();
  57. for ( int y = 0; y < num; y++ )
  58. {
  59. ListLevel lvl = new ListLevel();
  60. levelOffset += lvl.read( tableStream, levelOffset );
  61. lst.setLevel( y, lvl );
  62. }
  63. }
  64. this._plfLfo = new PlfLfo( tableStream, fcPlfLfo, lcbPlfLfo );
  65. }
  66. public void writeListDataTo( FileInformationBlock fib,
  67. ByteArrayOutputStream tableStream ) throws IOException
  68. {
  69. final int startOffset = tableStream.size();
  70. fib.setFcPlfLst( startOffset );
  71. int listSize = _listMap.size();
  72. // use this stream as a buffer for the levels since their size varies.
  73. ByteArrayOutputStream levelBuf = new ByteArrayOutputStream();
  74. byte[] shortHolder = new byte[2];
  75. LittleEndian.putShort(shortHolder, 0, (short)listSize);
  76. tableStream.write(shortHolder);
  77. for(ListData lst : _listMap.values()) {
  78. tableStream.write(lst.toByteArray());
  79. ListLevel[] lvls = lst.getLevels();
  80. for (int y = 0; y < lvls.length; y++)
  81. {
  82. levelBuf.write(lvls[y].toByteArray());
  83. }
  84. }
  85. /*
  86. * An array of LVLs is appended to the PlfLst. lcbPlfLst does not
  87. * account for the array of LVLs. -- Page 76 of 621 -- [MS-DOC] --
  88. * v20110315 Word (.doc) Binary File Format
  89. */
  90. fib.setLcbPlfLst( tableStream.size() - startOffset );
  91. tableStream.write( levelBuf.toByteArray() );
  92. }
  93. public void writeListOverridesTo( FileInformationBlock fib,
  94. ByteArrayOutputStream tableStream ) throws IOException
  95. {
  96. _plfLfo.writeTo( fib, tableStream );
  97. }
  98. public LFO getLfo( int ilfo ) throws NoSuchElementException
  99. {
  100. return _plfLfo.getLfo( ilfo );
  101. }
  102. public LFOData getLfoData( int ilfo ) throws NoSuchElementException
  103. {
  104. return _plfLfo.getLfoData( ilfo );
  105. }
  106. public int getOverrideIndexFromListID( int lsid )
  107. throws NoSuchElementException
  108. {
  109. return _plfLfo.getIlfoByLsid( lsid );
  110. }
  111. /**
  112. * Get the ListLevel for a given lsid and level
  113. * @param lsid
  114. * @param level
  115. * @return ListLevel if found, or <code>null</code> if ListData can't be found or if level is > that available
  116. */
  117. public ListLevel getLevel(int lsid, int level)
  118. {
  119. ListData lst = _listMap.get(Integer.valueOf(lsid));
  120. if (lst == null) {
  121. if (log.check(POILogger.WARN)) {
  122. log.log(POILogger.WARN, "ListData for " +
  123. lsid + " was null.");
  124. }
  125. return null;
  126. }
  127. if(level < lst.numLevels()) {
  128. ListLevel lvl = lst.getLevels()[level];
  129. return lvl;
  130. }
  131. if (log.check(POILogger.WARN)) {
  132. log.log(POILogger.WARN, "Requested level " + level + " which was greater than the maximum defined (" + lst.numLevels() + ")");
  133. }
  134. return null;
  135. }
  136. public ListData getListData(int lsid)
  137. {
  138. return _listMap.get(Integer.valueOf(lsid));
  139. }
  140. @Override
  141. public int hashCode()
  142. {
  143. final int prime = 31;
  144. int result = 1;
  145. result = prime * result
  146. + ( ( _listMap == null ) ? 0 : _listMap.hashCode() );
  147. result = prime * result
  148. + ( ( _plfLfo == null ) ? 0 : _plfLfo.hashCode() );
  149. return result;
  150. }
  151. @Override
  152. public boolean equals( Object obj )
  153. {
  154. if ( this == obj )
  155. return true;
  156. if ( obj == null )
  157. return false;
  158. if ( getClass() != obj.getClass() )
  159. return false;
  160. ListTables other = (ListTables) obj;
  161. if ( _listMap == null )
  162. {
  163. if ( other._listMap != null )
  164. return false;
  165. }
  166. else if ( !_listMap.equals( other._listMap ) )
  167. return false;
  168. if ( _plfLfo == null )
  169. {
  170. if ( other._plfLfo != null )
  171. return false;
  172. }
  173. else if ( !_plfLfo.equals( other._plfLfo ) )
  174. return false;
  175. return true;
  176. }
  177. public int addList( ListData lst, LFO lfo, LFOData lfoData )
  178. {
  179. int lsid = lst.getLsid();
  180. while ( _listMap.get( Integer.valueOf( lsid ) ) != null )
  181. {
  182. lsid = lst.resetListID();
  183. lfo.setLsid( lsid );
  184. }
  185. _listMap.put( Integer.valueOf( lsid ), lst );
  186. if ( lfo == null && lfoData != null )
  187. {
  188. throw new IllegalArgumentException(
  189. "LFO and LFOData should be specified both or noone" );
  190. }
  191. if ( lfo != null )
  192. {
  193. _plfLfo.add( lfo, lfoData );
  194. }
  195. return lsid;
  196. }
  197. }