PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/apache/poi/hwpf/model/ListTables.java

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