KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > WebLine


1 /*
2  * WebLine.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: WebLine.java,v 1.1.1.1 2002/09/24 16:09:26 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.io.UnsupportedEncodingException JavaDoc;
25
26 public class WebLine extends AbstractLine implements Line
27 {
28     private int flags;
29     private String JavaDoc text;
30     private LineSegmentList segmentList;
31
32     // Offset of start of line in original document.
33
private int sourceOffset;
34
35     // Constructs an empty line.
36
public WebLine(int sourceOffset)
37     {
38         this.sourceOffset = sourceOffset;
39     }
40
41     public WebLine(LineSegmentList segmentList, int sourceOffset)
42     {
43         this.segmentList = segmentList;
44         this.sourceOffset = sourceOffset;
45     }
46
47     public final int flags()
48     {
49         return flags;
50     }
51
52     public final void setFlags(int flags)
53     {
54         this.flags = flags;
55     }
56
57     public final String JavaDoc getText()
58     {
59         if (text == null) {
60             if (segmentList != null) {
61                 FastStringBuffer sb = new FastStringBuffer(256);
62                 for (int i = 0; i < segmentList.size(); i++)
63                     sb.append(segmentList.getSegment(i).getText());
64                 text = sb.toString();
65             } else
66                 text = "";
67         }
68         return text;
69     }
70
71     public final int getSourceOffset()
72     {
73         return sourceOffset;
74     }
75
76     public HtmlLineSegment findSegment(int offset)
77     {
78         if (segmentList == null)
79             return null;
80         int begin = 0;
81         for (int i = 0; i < segmentList.size(); i++) {
82             HtmlLineSegment segment = (HtmlLineSegment) segmentList.getSegment(i);
83             String JavaDoc segmentText = segment.getText();
84             int end = begin + segmentText.length();
85             if (offset >= begin && offset < end)
86                 return segment;
87             begin = end;
88         }
89         return null;
90     }
91
92     public final void setText(String JavaDoc s)
93     {
94         text = s;
95     }
96
97     public final char charAt(int i)
98     {
99         return getText().charAt(i);
100     }
101
102     public final String JavaDoc substring(int beginIndex)
103     {
104         return getText().substring(beginIndex);
105     }
106
107     public final String JavaDoc substring(int beginIndex, int endIndex)
108     {
109         return getText().substring(beginIndex, endIndex);
110     }
111
112     public final String JavaDoc trim()
113     {
114         return getText().trim();
115     }
116
117     public final int length()
118     {
119         return getText().length();
120     }
121
122     public final byte[] getBytes(String JavaDoc encoding) throws UnsupportedEncodingException JavaDoc
123     {
124         return getText().getBytes(encoding);
125     }
126
127     public final boolean isBlank()
128     {
129         if (text == null)
130             text = getText();
131         for (int i = text.length(); i-- > 0;)
132             if (!Character.isWhitespace(text.charAt(i)))
133                 return false;
134         return true;
135     }
136
137     public final LineSegmentList getSegmentList()
138     {
139         return segmentList;
140     }
141 }
142
Popular Tags