001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hwpf.model;
019:
020: import java.io.UnsupportedEncodingException;
021:
022: /**
023: * Lightweight representation of a text piece.
024: *
025: * @author Ryan Ackley
026: */
027:
028: public class TextPiece extends PropertyNode implements Comparable {
029: private boolean _usesUnicode;
030:
031: private PieceDescriptor _pd;
032:
033: private int _cpStart;
034:
035: /**
036: * @param start Offset in main document stream.
037: */
038: public TextPiece(int start, int end, byte[] text,
039: PieceDescriptor pd, int cpStart)
040: throws UnsupportedEncodingException {
041: /** start - end is length on file. This is double the expected when its
042: * unicode.*/
043: super (start, end, new StringBuffer(new String(text, pd
044: .isUnicode() ? "UTF-16LE" : "Cp1252")));
045: _usesUnicode = pd.isUnicode();
046: _pd = pd;
047: _cpStart = cpStart;
048: }
049:
050: /**
051: * @return If this text piece uses unicode
052: */
053: public boolean usesUnicode() {
054: return _usesUnicode;
055: }
056:
057: public PieceDescriptor getPieceDescriptor() {
058: return _pd;
059: }
060:
061: public StringBuffer getStringBuffer() {
062: return (StringBuffer) _buf;
063: }
064:
065: public byte[] getRawBytes() {
066: try {
067: return ((StringBuffer) _buf).toString().getBytes(
068: _usesUnicode ? "UTF-16LE" : "Cp1252");
069: } catch (UnsupportedEncodingException ignore) {
070: // shouldn't ever happen considering we wouldn't have been able to
071: // create the StringBuffer w/o getting this exception
072: return ((StringBuffer) _buf).toString().getBytes();
073: }
074:
075: }
076:
077: public String substring(int start, int end) {
078: int denominator = _usesUnicode ? 2 : 1;
079:
080: return ((StringBuffer) _buf).substring(start / denominator, end
081: / denominator);
082: }
083:
084: public void adjustForDelete(int start, int length) {
085:
086: }
087:
088: public int characterLength() {
089: return (getEnd() - getStart()) / (_usesUnicode ? 2 : 1);
090: }
091:
092: public boolean equals(Object o) {
093: if (limitsAreEqual(o)) {
094: TextPiece tp = (TextPiece) o;
095: return getStringBuffer().toString().equals(
096: tp.getStringBuffer().toString())
097: && tp._usesUnicode == _usesUnicode
098: && _pd.equals(tp._pd);
099: }
100: return false;
101: }
102:
103: public int getCP() {
104: return _cpStart;
105: }
106:
107: }
|