Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

TextPiece.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 org.apache.poi.util.Internal;
  18. import org.apache.poi.util.StringUtil;
  19. /**
  20. * Lightweight representation of a text piece.
  21. * Works in the character domain, not the byte domain, so you
  22. * need to have turned byte references into character
  23. * references before getting here.
  24. *
  25. * @author Ryan Ackley
  26. */
  27. @Internal
  28. public class TextPiece extends PropertyNode<TextPiece> {
  29. private boolean _usesUnicode;
  30. private PieceDescriptor _pd;
  31. /**
  32. * @param start Beginning offset in main document stream, in characters.
  33. * @param end Ending offset in main document stream, in characters.
  34. * @param text The raw bytes of our text
  35. * instead
  36. */
  37. public TextPiece(int start, int end, byte[] text, PieceDescriptor pd,
  38. int cpStart) {
  39. this(start, end, text, pd);
  40. }
  41. /**
  42. * @param start Beginning offset in main document stream, in characters.
  43. * @param end Ending offset in main document stream, in characters.
  44. * @param text The raw bytes of our text
  45. */
  46. public TextPiece(int start, int end, byte[] text, PieceDescriptor pd) {
  47. super(start, end, buildInitSB(text, pd));
  48. _usesUnicode = pd.isUnicode();
  49. _pd = pd;
  50. // Validate
  51. int textLength = ((CharSequence) _buf).length();
  52. if (end - start != textLength) {
  53. throw new IllegalStateException("Told we're for characters " + start + " -> " + end + ", but actually covers " + textLength + " characters!");
  54. }
  55. if (end < start) {
  56. throw new IllegalStateException("Told we're of negative size! start=" + start + " end=" + end);
  57. }
  58. }
  59. /**
  60. * Create the StringBuilder from the text and unicode flag
  61. */
  62. private static StringBuilder buildInitSB(byte[] text, PieceDescriptor pd) {
  63. byte[] textBuffer = text;
  64. if (StringUtil.BIG5.equals(pd.getCharset())) {
  65. String txt = new StringBuilder(StringUtil.littleEndianBig5Stream(text, 0, text.length)).toString();
  66. return new StringBuilder(txt);
  67. }
  68. String str = new String(textBuffer, 0, textBuffer.length, (pd.isUnicode()) ? StringUtil.UTF16LE : pd.getCharset());
  69. return new StringBuilder(str);
  70. }
  71. /**
  72. * @return If this text piece is unicode
  73. */
  74. public boolean isUnicode() {
  75. return _usesUnicode;
  76. }
  77. public PieceDescriptor getPieceDescriptor() {
  78. return _pd;
  79. }
  80. @Deprecated
  81. public StringBuffer getStringBuffer() {
  82. return new StringBuffer(getStringBuilder());
  83. }
  84. public StringBuilder getStringBuilder() {
  85. return (StringBuilder) _buf;
  86. }
  87. public byte[] getRawBytes() {
  88. return ((CharSequence) _buf).toString().getBytes(
  89. Charset.forName(_usesUnicode ? "UTF-16LE" : "Cp1252")
  90. );
  91. }
  92. /**
  93. * Returns part of the string.
  94. * Works only in characters, not in bytes!
  95. *
  96. * @param start Local start position, in characters
  97. * @param end Local end position, in characters
  98. */
  99. @Deprecated
  100. public String substring(int start, int end) {
  101. StringBuilder buf = (StringBuilder) _buf;
  102. // Validate
  103. if (start < 0) {
  104. throw new StringIndexOutOfBoundsException("Can't request a substring before 0 - asked for " + start);
  105. }
  106. if (end > buf.length()) {
  107. throw new StringIndexOutOfBoundsException("Index " + end + " out of range 0 -> " + buf.length());
  108. }
  109. if (end < start) {
  110. throw new StringIndexOutOfBoundsException("Asked for text from " + start + " to " + end + ", which has an end before the start!");
  111. }
  112. return buf.substring(start, end);
  113. }
  114. /**
  115. * Adjusts the internal string for deletinging
  116. * some characters within this.
  117. *
  118. * @param start The start position for the delete, in characters
  119. * @param length The number of characters to delete
  120. */
  121. @Deprecated
  122. public void adjustForDelete(int start, int length) {
  123. int myStart = getStart();
  124. int myEnd = getEnd();
  125. int end = start + length;
  126. /* do we have to delete from this text piece? */
  127. if (start <= myEnd && end >= myStart) {
  128. /* find where the deleted area overlaps with this text piece */
  129. int overlapStart = Math.max(myStart, start);
  130. int overlapEnd = Math.min(myEnd, end);
  131. int bufStart = overlapStart - myStart;
  132. int bufEnd = overlapEnd - myStart;
  133. ((StringBuilder) _buf).delete(bufStart, bufEnd);
  134. }
  135. // We need to invoke this even if text from this piece is not being
  136. // deleted because the adjustment must propagate to all subsequent
  137. // text pieces i.e., if text from tp[n] is being deleted, then
  138. // tp[n + 1], tp[n + 2], etc. will need to be adjusted.
  139. // The superclass is expected to use a separate sentry for this.
  140. super.adjustForDelete(start, length);
  141. }
  142. /**
  143. * Returns the length, in characters
  144. */
  145. @Deprecated
  146. public int characterLength() {
  147. return (getEnd() - getStart());
  148. }
  149. /**
  150. * Returns the length, in bytes
  151. */
  152. public int bytesLength() {
  153. return (getEnd() - getStart()) * (_usesUnicode ? 2 : 1);
  154. }
  155. @Override
  156. public boolean equals(Object o) {
  157. if (!(o instanceof TextPiece)) return false;
  158. TextPiece tp = (TextPiece) o;
  159. assert (_buf != null && tp._buf != null && _pd != null && tp._pd != null);
  160. return (
  161. limitsAreEqual(o)
  162. && tp._usesUnicode == this._usesUnicode
  163. && tp._buf.toString().equals(this._buf.toString())
  164. && tp._pd.equals(this._pd)
  165. );
  166. }
  167. @Override
  168. public int hashCode() {
  169. assert false : "hashCode not designed";
  170. return 42; // any arbitrary constant will do
  171. }
  172. /**
  173. * Returns the character position we start at.
  174. */
  175. public int getCP() {
  176. return getStart();
  177. }
  178. public String toString() {
  179. return "TextPiece from " + getStart() + " to " + getEnd() + " ("
  180. + getPieceDescriptor() + ")";
  181. }
  182. }