KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hwpf > model > TextPiece


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16
17
18 package org.apache.poi.hwpf.model;
19
20
21 import java.io.UnsupportedEncodingException JavaDoc;
22 /**
23  * Lightweight representation of a text piece.
24  *
25  * @author Ryan Ackley
26  */

27
28 public class TextPiece extends PropertyNode implements Comparable JavaDoc
29 {
30   private boolean _usesUnicode;
31
32   private PieceDescriptor _pd;
33
34   private int _cpStart;
35
36   /**
37    * @param start Offset in main document stream.
38    */

39   public TextPiece(int start, int end, byte[] text, PieceDescriptor pd, int cpStart)
40     throws UnsupportedEncodingException JavaDoc
41   {
42      /** start - end is length on file. This is double the expected when its
43      * unicode.*/

44     super(start, end, new StringBuffer JavaDoc(new String JavaDoc(text, pd.isUnicode() ? "UTF-16LE" : "Cp1252")));
45     _usesUnicode = pd.isUnicode();
46     _pd = pd;
47     _cpStart = cpStart;
48   }
49   /**
50    * @return If this text piece uses unicode
51    */

52    public boolean usesUnicode()
53    {
54       return _usesUnicode;
55    }
56
57    public PieceDescriptor getPieceDescriptor()
58    {
59      return _pd;
60    }
61
62    public StringBuffer JavaDoc getStringBuffer()
63    {
64      return (StringBuffer JavaDoc)_buf;
65    }
66
67    public byte[] getRawBytes()
68    {
69      try
70      {
71        return ((StringBuffer JavaDoc)_buf).toString().getBytes(_usesUnicode ?
72            "UTF-16LE" : "Cp1252");
73      }
74      catch (UnsupportedEncodingException JavaDoc ignore)
75      {
76        // shouldn't ever happen considering we wouldn't have been able to
77
// create the StringBuffer w/o getting this exception
78
return ((StringBuffer JavaDoc)_buf).toString().getBytes();
79      }
80
81    }
82
83    public String JavaDoc substring(int start, int end)
84    {
85      int denominator = _usesUnicode ? 2 : 1;
86
87      return ((StringBuffer JavaDoc)_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 JavaDoc 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