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.

BookmarksTables.java 6.4KB

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.ArrayList;
  19. import java.util.Arrays;
  20. import java.util.List;
  21. import org.apache.poi.hwpf.model.types.BKFAbstractType;
  22. import org.apache.poi.util.Internal;
  23. import org.apache.poi.util.POILogFactory;
  24. import org.apache.poi.util.POILogger;
  25. @Internal
  26. public class BookmarksTables
  27. {
  28. private static final POILogger logger = POILogFactory
  29. .getLogger( BookmarksTables.class );
  30. private PlexOfCps descriptorsFirst = new PlexOfCps( 4 );
  31. private PlexOfCps descriptorsLim = new PlexOfCps( 0 );
  32. private List<String> names = new ArrayList<>(0);
  33. public BookmarksTables( byte[] tableStream, FileInformationBlock fib )
  34. {
  35. read( tableStream, fib );
  36. }
  37. public void afterDelete( int startCp, int length )
  38. {
  39. descriptorsFirst.adjust( startCp, -length );
  40. descriptorsLim.adjust( startCp, -length );
  41. for ( int i = 0; i < descriptorsFirst.length(); i++ )
  42. {
  43. GenericPropertyNode startNode = descriptorsFirst.getProperty( i );
  44. GenericPropertyNode endNode = descriptorsLim.getProperty( i );
  45. if ( startNode.getStart() == endNode.getStart() )
  46. {
  47. logger.log( POILogger.DEBUG, "Removing bookmark #",
  48. Integer.valueOf( i ), "..." );
  49. remove( i );
  50. i--;
  51. continue;
  52. }
  53. }
  54. }
  55. public void afterInsert( int startCp, int length )
  56. {
  57. descriptorsFirst.adjust( startCp, length );
  58. descriptorsLim.adjust( startCp - 1, length );
  59. }
  60. public int getBookmarksCount()
  61. {
  62. return descriptorsFirst.length();
  63. }
  64. public GenericPropertyNode getDescriptorFirst( int index )
  65. throws IndexOutOfBoundsException
  66. {
  67. return descriptorsFirst.getProperty( index );
  68. }
  69. public int getDescriptorFirstIndex( GenericPropertyNode descriptorFirst )
  70. {
  71. // TODO: very non-optimal
  72. return Arrays.asList( descriptorsFirst.toPropertiesArray() ).indexOf(
  73. descriptorFirst );
  74. }
  75. public GenericPropertyNode getDescriptorLim( int index )
  76. throws IndexOutOfBoundsException
  77. {
  78. return descriptorsLim.getProperty( index );
  79. }
  80. public int getDescriptorsFirstCount()
  81. {
  82. return descriptorsFirst.length();
  83. }
  84. public int getDescriptorsLimCount()
  85. {
  86. return descriptorsLim.length();
  87. }
  88. public String getName( int index )
  89. {
  90. return names.get( index );
  91. }
  92. public int getNamesCount()
  93. {
  94. return names.size();
  95. }
  96. private void read( byte[] tableStream, FileInformationBlock fib )
  97. {
  98. int namesStart = fib.getFcSttbfbkmk();
  99. int namesLength = fib.getLcbSttbfbkmk();
  100. if ( namesStart != 0 && namesLength != 0 )
  101. this.names = new ArrayList<>(Arrays.asList(SttbUtils
  102. .readSttbfBkmk(tableStream, namesStart)));
  103. int firstDescriptorsStart = fib.getFcPlcfbkf();
  104. int firstDescriptorsLength = fib.getLcbPlcfbkf();
  105. if ( firstDescriptorsStart != 0 && firstDescriptorsLength != 0 )
  106. descriptorsFirst = new PlexOfCps(tableStream,
  107. firstDescriptorsStart, firstDescriptorsLength,
  108. BKFAbstractType.getSize() );
  109. int limDescriptorsStart = fib.getFcPlcfbkl();
  110. int limDescriptorsLength = fib.getLcbPlcfbkl();
  111. if ( limDescriptorsStart != 0 && limDescriptorsLength != 0 )
  112. descriptorsLim = new PlexOfCps( tableStream, limDescriptorsStart,
  113. limDescriptorsLength, 0 );
  114. }
  115. public void remove( int index )
  116. {
  117. descriptorsFirst.remove( index );
  118. descriptorsLim.remove( index );
  119. names.remove( index );
  120. }
  121. public void setName( int index, String name )
  122. {
  123. names.set( index, name );
  124. }
  125. public void writePlcfBkmkf( FileInformationBlock fib,
  126. ByteArrayOutputStream tableStream ) throws IOException
  127. {
  128. if ( descriptorsFirst == null || descriptorsFirst.length() == 0 )
  129. {
  130. fib.setFcPlcfbkf( 0 );
  131. fib.setLcbPlcfbkf( 0 );
  132. return;
  133. }
  134. int start = tableStream.size();
  135. tableStream.write( descriptorsFirst.toByteArray() );
  136. int end = tableStream.size();
  137. fib.setFcPlcfbkf( start );
  138. fib.setLcbPlcfbkf( end - start );
  139. }
  140. public void writePlcfBkmkl( FileInformationBlock fib,
  141. ByteArrayOutputStream tableStream ) throws IOException
  142. {
  143. if ( descriptorsLim == null || descriptorsLim.length() == 0 )
  144. {
  145. fib.setFcPlcfbkl( 0 );
  146. fib.setLcbPlcfbkl( 0 );
  147. return;
  148. }
  149. int start = tableStream.size();
  150. tableStream.write( descriptorsLim.toByteArray() );
  151. int end = tableStream.size();
  152. fib.setFcPlcfbkl( start );
  153. fib.setLcbPlcfbkl( end - start );
  154. }
  155. public void writeSttbfBkmk( FileInformationBlock fib,
  156. ByteArrayOutputStream tableStream ) throws IOException
  157. {
  158. if ( names == null || names.isEmpty() )
  159. {
  160. fib.setFcSttbfbkmk( 0 );
  161. fib.setLcbSttbfbkmk( 0 );
  162. return;
  163. }
  164. int start = tableStream.size();
  165. SttbUtils.writeSttbfBkmk( names.toArray(new String[0]),
  166. tableStream );
  167. int end = tableStream.size();
  168. fib.setFcSttbfbkmk( start );
  169. fib.setLcbSttbfbkmk( end - start );
  170. }
  171. }