Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TextPiece.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* ====================================================================
  2. Copyright 2002-2004 Apache Software Foundation
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ==================================================================== */
  13. package org.apache.poi.hwpf.model;
  14. import java.io.UnsupportedEncodingException;
  15. /**
  16. * Lightweight representation of a text piece.
  17. *
  18. * @author Ryan Ackley
  19. */
  20. public class TextPiece extends PropertyNode implements Comparable
  21. {
  22. private boolean _usesUnicode;
  23. private PieceDescriptor _pd;
  24. private int _cpStart;
  25. /**
  26. * @param start Offset in main document stream.
  27. * @param length The total length of the text in bytes. Note: 1 character
  28. * does not necessarily refer to 1 byte.
  29. * @param unicode true if this text is unicode.
  30. */
  31. public TextPiece(int start, int end, byte[] text, PieceDescriptor pd, int cpStart)
  32. throws UnsupportedEncodingException
  33. {
  34. /** start - end is length on file. This is double the expected when its
  35. * unicode.*/
  36. super(start, end, new StringBuffer(new String(text, pd.isUnicode() ? "UTF-16LE" : "Cp1252")));
  37. _usesUnicode = pd.isUnicode();
  38. _pd = pd;
  39. _cpStart = cpStart;
  40. }
  41. /**
  42. * @return If this text piece uses unicode
  43. */
  44. public boolean usesUnicode()
  45. {
  46. return _usesUnicode;
  47. }
  48. public PieceDescriptor getPieceDescriptor()
  49. {
  50. return _pd;
  51. }
  52. public StringBuffer getStringBuffer()
  53. {
  54. return (StringBuffer)_buf;
  55. }
  56. public byte[] getRawBytes()
  57. {
  58. try
  59. {
  60. return ((StringBuffer)_buf).toString().getBytes(_usesUnicode ?
  61. "UTF-16LE" : "Cp1252");
  62. }
  63. catch (UnsupportedEncodingException ignore)
  64. {
  65. // shouldn't ever happen considering we wouldn't have been able to
  66. // create the StringBuffer w/o getting this exception
  67. return ((StringBuffer)_buf).toString().getBytes();
  68. }
  69. }
  70. public String substring(int start, int end)
  71. {
  72. int denominator = _usesUnicode ? 2 : 1;
  73. return ((StringBuffer)_buf).substring(start/denominator, end/denominator);
  74. }
  75. public void adjustForDelete(int start, int length)
  76. {
  77. }
  78. public int characterLength()
  79. {
  80. return (getEnd() - getStart()) / (_usesUnicode ? 2 : 1);
  81. }
  82. public boolean equals(Object o)
  83. {
  84. if (limitsAreEqual(o))
  85. {
  86. TextPiece tp = (TextPiece)o;
  87. return getStringBuffer().toString().equals(tp.getStringBuffer().toString()) &&
  88. tp._usesUnicode == _usesUnicode && _pd.equals(tp._pd);
  89. }
  90. return false;
  91. }
  92. public int getCP()
  93. {
  94. return _cpStart;
  95. }
  96. }