001: /*
002: * WebLine.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: WebLine.java,v 1.1.1.1 2002/09/24 16:09:26 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.io.UnsupportedEncodingException;
025:
026: public class WebLine extends AbstractLine implements Line {
027: private int flags;
028: private String text;
029: private LineSegmentList segmentList;
030:
031: // Offset of start of line in original document.
032: private int sourceOffset;
033:
034: // Constructs an empty line.
035: public WebLine(int sourceOffset) {
036: this .sourceOffset = sourceOffset;
037: }
038:
039: public WebLine(LineSegmentList segmentList, int sourceOffset) {
040: this .segmentList = segmentList;
041: this .sourceOffset = sourceOffset;
042: }
043:
044: public final int flags() {
045: return flags;
046: }
047:
048: public final void setFlags(int flags) {
049: this .flags = flags;
050: }
051:
052: public final String getText() {
053: if (text == null) {
054: if (segmentList != null) {
055: FastStringBuffer sb = new FastStringBuffer(256);
056: for (int i = 0; i < segmentList.size(); i++)
057: sb.append(segmentList.getSegment(i).getText());
058: text = sb.toString();
059: } else
060: text = "";
061: }
062: return text;
063: }
064:
065: public final int getSourceOffset() {
066: return sourceOffset;
067: }
068:
069: public HtmlLineSegment findSegment(int offset) {
070: if (segmentList == null)
071: return null;
072: int begin = 0;
073: for (int i = 0; i < segmentList.size(); i++) {
074: HtmlLineSegment segment = (HtmlLineSegment) segmentList
075: .getSegment(i);
076: String segmentText = segment.getText();
077: int end = begin + segmentText.length();
078: if (offset >= begin && offset < end)
079: return segment;
080: begin = end;
081: }
082: return null;
083: }
084:
085: public final void setText(String s) {
086: text = s;
087: }
088:
089: public final char charAt(int i) {
090: return getText().charAt(i);
091: }
092:
093: public final String substring(int beginIndex) {
094: return getText().substring(beginIndex);
095: }
096:
097: public final String substring(int beginIndex, int endIndex) {
098: return getText().substring(beginIndex, endIndex);
099: }
100:
101: public final String trim() {
102: return getText().trim();
103: }
104:
105: public final int length() {
106: return getText().length();
107: }
108:
109: public final byte[] getBytes(String encoding)
110: throws UnsupportedEncodingException {
111: return getText().getBytes(encoding);
112: }
113:
114: public final boolean isBlank() {
115: if (text == null)
116: text = getText();
117: for (int i = text.length(); i-- > 0;)
118: if (!Character.isWhitespace(text.charAt(i)))
119: return false;
120: return true;
121: }
122:
123: public final LineSegmentList getSegmentList() {
124: return segmentList;
125: }
126: }
|