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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 java.util.Objects;
  21. import org.apache.poi.hwpf.model.types.LSTFAbstractType;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.poi.util.LittleEndian;
  24. import org.apache.poi.util.LittleEndianConsts;
  25. import org.apache.poi.util.POILogFactory;
  26. import org.apache.poi.util.POILogger;
  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<>();
  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 += LittleEndianConsts.SHORT_SIZE;
  50. int levelOffset = offset + ( cLst * LSTFAbstractType.getSize() );
  51. for ( int x = 0; x < cLst; x++ )
  52. {
  53. ListData lst = new ListData( tableStream, offset );
  54. _listMap.put(lst.getLsid(), lst );
  55. offset += LSTFAbstractType.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 (ListLevel lvl : lvls) {
  81. levelBuf.write(lvl.toByteArray());
  82. }
  83. }
  84. /*
  85. * An array of LVLs is appended to the PlfLst. lcbPlfLst does not
  86. * account for the array of LVLs. -- Page 76 of 621 -- [MS-DOC] --
  87. * v20110315 Word (.doc) Binary File Format
  88. */
  89. fib.setLcbPlfLst( tableStream.size() - startOffset );
  90. tableStream.write( levelBuf.toByteArray() );
  91. }
  92. public void writeListOverridesTo( FileInformationBlock fib,
  93. ByteArrayOutputStream tableStream ) throws IOException
  94. {
  95. _plfLfo.writeTo( fib, tableStream );
  96. }
  97. public LFO getLfo( int ilfo ) throws NoSuchElementException
  98. {
  99. return _plfLfo.getLfo( ilfo );
  100. }
  101. public LFOData getLfoData( int ilfo ) throws NoSuchElementException
  102. {
  103. return _plfLfo.getLfoData( ilfo );
  104. }
  105. public int getOverrideIndexFromListID( int lsid )
  106. throws NoSuchElementException
  107. {
  108. return _plfLfo.getIlfoByLsid( lsid );
  109. }
  110. /**
  111. * Get the ListLevel for a given lsid and level
  112. * @return ListLevel if found, or <code>null</code> if ListData can't be found or if level is > that available
  113. */
  114. public ListLevel getLevel(int lsid, int level)
  115. {
  116. ListData lst = _listMap.get(lsid);
  117. if (lst == null) {
  118. if (log.check(POILogger.WARN)) {
  119. log.log(POILogger.WARN, "ListData for " +
  120. lsid + " was null.");
  121. }
  122. return null;
  123. }
  124. if(level < lst.numLevels()) {
  125. return lst.getLevels()[level];
  126. }
  127. if (log.check(POILogger.WARN)) {
  128. log.log(POILogger.WARN, "Requested level " + level + " which was greater than the maximum defined (" + lst.numLevels() + ")");
  129. }
  130. return null;
  131. }
  132. public ListData getListData(int lsid)
  133. {
  134. return _listMap.get(lsid);
  135. }
  136. @Override
  137. public int hashCode() {
  138. return Objects.hash(_listMap,_plfLfo);
  139. }
  140. @Override
  141. public boolean equals( Object obj )
  142. {
  143. if ( this == obj )
  144. return true;
  145. if ( obj == null )
  146. return false;
  147. if ( getClass() != obj.getClass() )
  148. return false;
  149. ListTables other = (ListTables) obj;
  150. if ( !_listMap.equals( other._listMap ) )
  151. return false;
  152. return Objects.equals(_plfLfo, other._plfLfo);
  153. }
  154. public int addList( ListData lst, LFO lfo, LFOData lfoData )
  155. {
  156. int lsid = lst.getLsid();
  157. while (_listMap.containsKey(lsid))
  158. {
  159. lsid = lst.resetListID();
  160. lfo.setLsid( lsid );
  161. }
  162. _listMap.put(lsid, lst );
  163. if ( lfo == null && lfoData != null )
  164. {
  165. throw new IllegalArgumentException(
  166. "LFO and LFOData should be specified both or noone" );
  167. }
  168. if ( lfo != null )
  169. {
  170. _plfLfo.add( lfo, lfoData );
  171. }
  172. return lsid;
  173. }
  174. }