1 16 17 18 package org.apache.poi.hwpf.model; 19 20 21 import java.io.UnsupportedEncodingException ; 22 27 28 public class TextPiece extends PropertyNode implements Comparable 29 { 30 private boolean _usesUnicode; 31 32 private PieceDescriptor _pd; 33 34 private int _cpStart; 35 36 39 public TextPiece(int start, int end, byte[] text, PieceDescriptor pd, int cpStart) 40 throws UnsupportedEncodingException 41 { 42 44 super(start, end, new StringBuffer (new String (text, pd.isUnicode() ? "UTF-16LE" : "Cp1252"))); 45 _usesUnicode = pd.isUnicode(); 46 _pd = pd; 47 _cpStart = cpStart; 48 } 49 52 public boolean usesUnicode() 53 { 54 return _usesUnicode; 55 } 56 57 public PieceDescriptor getPieceDescriptor() 58 { 59 return _pd; 60 } 61 62 public StringBuffer getStringBuffer() 63 { 64 return (StringBuffer )_buf; 65 } 66 67 public byte[] getRawBytes() 68 { 69 try 70 { 71 return ((StringBuffer )_buf).toString().getBytes(_usesUnicode ? 72 "UTF-16LE" : "Cp1252"); 73 } 74 catch (UnsupportedEncodingException ignore) 75 { 76 return ((StringBuffer )_buf).toString().getBytes(); 79 } 80 81 } 82 83 public String substring(int start, int end) 84 { 85 int denominator = _usesUnicode ? 2 : 1; 86 87 return ((StringBuffer )_buf).substring(start/denominator, end/denominator); 88 } 89 90 public void adjustForDelete(int start, int length) 91 { 92 93 } 94 95 public int characterLength() 96 { 97 return (getEnd() - getStart()) / (_usesUnicode ? 2 : 1); 98 } 99 100 public boolean equals(Object o) 101 { 102 if (limitsAreEqual(o)) 103 { 104 TextPiece tp = (TextPiece)o; 105 return getStringBuffer().toString().equals(tp.getStringBuffer().toString()) && 106 tp._usesUnicode == _usesUnicode && _pd.equals(tp._pd); 107 } 108 return false; 109 } 110 111 112 public int getCP() 113 { 114 return _cpStart; 115 } 116 117 } 118 | Popular Tags |