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.

NotesTables.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 org.apache.poi.hwpf.model.types.FRDAbstractType;
  19. import org.apache.poi.util.Internal;
  20. /**
  21. * Holds information about document notes (footnotes or ending notes)
  22. *
  23. * @author Sergey Vladimirov (vlsergey {at} gmail {doc} com)
  24. */
  25. @Internal
  26. public class NotesTables
  27. {
  28. private PlexOfCps descriptors = new PlexOfCps(FRDAbstractType.getSize() );
  29. private final NoteType noteType;
  30. private PlexOfCps textPositions = new PlexOfCps( 0 );
  31. public NotesTables( final NoteType noteType )
  32. {
  33. this.noteType = noteType;
  34. textPositions
  35. .addProperty( new GenericPropertyNode( 0, 1, new byte[0] ) );
  36. }
  37. public NotesTables( final NoteType noteType, byte[] tableStream,
  38. FileInformationBlock fib )
  39. {
  40. this.noteType = noteType;
  41. read( tableStream, fib );
  42. }
  43. public GenericPropertyNode getDescriptor( int index )
  44. {
  45. return descriptors.getProperty( index );
  46. }
  47. public int getDescriptorsCount()
  48. {
  49. return descriptors.length();
  50. }
  51. public GenericPropertyNode getTextPosition( int index )
  52. {
  53. return textPositions.getProperty( index );
  54. }
  55. private void read( byte[] tableStream, FileInformationBlock fib )
  56. {
  57. int referencesStart = fib.getNotesDescriptorsOffset( noteType );
  58. int referencesLength = fib.getNotesDescriptorsSize( noteType );
  59. if ( referencesStart != 0 && referencesLength != 0 )
  60. this.descriptors = new PlexOfCps( tableStream, referencesStart,
  61. referencesLength, FRDAbstractType.getSize() );
  62. int textPositionsStart = fib.getNotesTextPositionsOffset( noteType );
  63. int textPositionsLength = fib.getNotesTextPositionsSize( noteType );
  64. if ( textPositionsStart != 0 && textPositionsLength != 0 )
  65. this.textPositions = new PlexOfCps( tableStream,
  66. textPositionsStart, textPositionsLength, 0 );
  67. }
  68. public void writeRef( FileInformationBlock fib, ByteArrayOutputStream tableStream )
  69. throws IOException
  70. {
  71. if ( descriptors == null || descriptors.length() == 0 )
  72. {
  73. fib.setNotesDescriptorsOffset( noteType, tableStream.size() );
  74. fib.setNotesDescriptorsSize( noteType, 0 );
  75. return;
  76. }
  77. int start = tableStream.size();
  78. tableStream.write( descriptors.toByteArray() );
  79. int end = tableStream.size();
  80. fib.setNotesDescriptorsOffset( noteType, start );
  81. fib.setNotesDescriptorsSize( noteType, end - start );
  82. }
  83. public void writeTxt( FileInformationBlock fib, ByteArrayOutputStream tableStream )
  84. throws IOException
  85. {
  86. if ( textPositions == null || textPositions.length() == 0 )
  87. {
  88. fib.setNotesTextPositionsOffset( noteType, tableStream.size() );
  89. fib.setNotesTextPositionsSize( noteType, 0 );
  90. return;
  91. }
  92. int start = tableStream.size();
  93. tableStream.write( textPositions.toByteArray() );
  94. int end = tableStream.size();
  95. fib.setNotesTextPositionsOffset( noteType, start );
  96. fib.setNotesTextPositionsSize( noteType, end - start );
  97. }
  98. }