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.

TextPiece.java 7.2KB

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