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.

OldTextPieceTable.java 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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.nio.charset.Charset;
  17. import java.util.ArrayList;
  18. import java.util.Collections;
  19. import org.apache.poi.util.CodePageUtil;
  20. import org.apache.poi.util.Internal;
  21. import org.apache.poi.util.POILogFactory;
  22. import org.apache.poi.util.POILogger;
  23. @Internal
  24. public class OldTextPieceTable extends TextPieceTable {
  25. private static final POILogger logger = POILogFactory
  26. .getLogger(OldTextPieceTable.class);
  27. public OldTextPieceTable() {
  28. super();
  29. }
  30. public OldTextPieceTable(byte[] documentStream, byte[] tableStream,
  31. int offset, int size, int fcMin, Charset charset) {
  32. //super(documentStream, tableStream, offset, size, fcMin, charset);
  33. // get our plex of PieceDescriptors
  34. PlexOfCps pieceTable = new PlexOfCps(tableStream, offset, size,
  35. PieceDescriptor.getSizeInBytes());
  36. int length = pieceTable.length();
  37. PieceDescriptor[] pieces = new PieceDescriptor[length];
  38. // iterate through piece descriptors raw bytes and create
  39. // PieceDescriptor objects
  40. for (int x = 0; x < length; x++) {
  41. GenericPropertyNode node = pieceTable.getProperty(x);
  42. pieces[x] = new PieceDescriptor(node.getBytes(), 0, charset);
  43. }
  44. // Figure out the cp of the earliest text piece
  45. // Note that text pieces don't have to be stored in order!
  46. _cpMin = pieces[0].getFilePosition() - fcMin;
  47. for (PieceDescriptor piece : pieces) {
  48. int start = piece.getFilePosition() - fcMin;
  49. if (start < _cpMin) {
  50. _cpMin = start;
  51. }
  52. }
  53. // using the PieceDescriptors, build our list of TextPieces.
  54. for (int x = 0; x < pieces.length; x++) {
  55. int start = pieces[x].getFilePosition();
  56. GenericPropertyNode node = pieceTable.getProperty(x);
  57. // Grab the start and end, which are in characters
  58. int nodeStartChars = node.getStart();
  59. int nodeEndChars = node.getEnd();
  60. // What's the relationship between bytes and characters?
  61. boolean unicode = pieces[x].isUnicode();
  62. int multiple = 1;
  63. if (unicode ||
  64. (charset != null && CodePageUtil.VARIABLE_BYTE_CHARSETS.contains(charset))) {
  65. multiple = 2;
  66. }
  67. // Figure out the length, in bytes and chars
  68. int textSizeChars = (nodeEndChars - nodeStartChars);
  69. int textSizeBytes = textSizeChars * multiple;
  70. // Grab the data that makes up the piece
  71. byte[] buf = new byte[textSizeBytes];
  72. System.arraycopy(documentStream, start, buf, 0, textSizeBytes);
  73. // And now build the piece
  74. final TextPiece newTextPiece = newTextPiece(nodeStartChars, nodeEndChars, buf,
  75. pieces[x]);
  76. _textPieces.add(newTextPiece);
  77. }
  78. // In the interest of our sanity, now sort the text pieces
  79. // into order, if they're not already
  80. Collections.sort(_textPieces);
  81. _textPiecesFCOrder = new ArrayList<TextPiece>(_textPieces);
  82. Collections.sort(_textPiecesFCOrder, new FCComparator());
  83. }
  84. @Override
  85. protected TextPiece newTextPiece(int nodeStartChars, int nodeEndChars, byte[] buf, PieceDescriptor pd) {
  86. return new OldTextPiece(nodeStartChars, nodeEndChars, buf, pd);
  87. }
  88. @Override
  89. protected int getEncodingMultiplier(TextPiece textPiece) {
  90. Charset charset = textPiece.getPieceDescriptor().getCharset();
  91. if (charset != null && CodePageUtil.VARIABLE_BYTE_CHARSETS.contains(charset)) {
  92. return 2;
  93. }
  94. return 1;
  95. }
  96. }