38 |
|
|
39 |
package javax.swing.text.html; |
package javax.swing.text.html; |
40 |
|
|
41 |
|
import java.net.URL; |
42 |
|
|
43 |
|
import javax.swing.text.AbstractDocument; |
44 |
|
import javax.swing.text.AttributeSet; |
45 |
import javax.swing.text.DefaultStyledDocument; |
import javax.swing.text.DefaultStyledDocument; |
46 |
|
import javax.swing.text.Element; |
47 |
|
import javax.swing.text.ElementIterator; |
48 |
|
import javax.swing.text.html.HTML.Tag; |
49 |
|
|
50 |
/** |
/** |
51 |
* TODO: This class is not yet completetely implemented. |
* TODO: This class is not yet completetely implemented. |
54 |
*/ |
*/ |
55 |
public class HTMLDocument extends DefaultStyledDocument |
public class HTMLDocument extends DefaultStyledDocument |
56 |
{ |
{ |
57 |
|
/** A key for document properies. The value for the key is |
58 |
|
* a Vector of Strings of comments not found in the body. |
59 |
|
*/ |
60 |
|
public static final String AdditionalComments = "AdditionalComments"; |
61 |
|
URL baseURL = null; |
62 |
|
boolean preservesUnknownTags = true; |
63 |
|
|
64 |
|
/** |
65 |
|
* Returns the location against which to resolve relative URLs. |
66 |
|
* This is the document's URL if the document was loaded from a URL. |
67 |
|
* If a <code>base</code> tag is found, it will be used. |
68 |
|
* @return the base URL |
69 |
|
*/ |
70 |
|
public URL getBase() |
71 |
|
{ |
72 |
|
return baseURL; |
73 |
|
} |
74 |
|
|
75 |
|
/** |
76 |
|
* Sets the location against which to resolve relative URLs. |
77 |
|
* @param u the new base URL |
78 |
|
*/ |
79 |
|
public void setBase(URL u) |
80 |
|
{ |
81 |
|
baseURL = u; |
82 |
|
//TODO: also set the base of the StyleSheet |
83 |
|
} |
84 |
|
|
85 |
|
/** |
86 |
|
* Returns whether or not the parser preserves unknown HTML tags. |
87 |
|
* @return true if the parser preserves unknown tags |
88 |
|
*/ |
89 |
|
public boolean getPreservesUnknownTags() |
90 |
|
{ |
91 |
|
return preservesUnknownTags; |
92 |
|
} |
93 |
|
|
94 |
|
/** |
95 |
|
* Sets the behaviour of the parser when it encounters unknown HTML tags. |
96 |
|
* @param preservesTags true if the parser should preserve unknown tags. |
97 |
|
*/ |
98 |
|
public void setPreservesUnknownTags(boolean preservesTags) |
99 |
|
{ |
100 |
|
preservesUnknownTags = preservesTags; |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
|
* An iterator to iterate through LeafElements in the document. |
105 |
|
*/ |
106 |
|
class LeafIterator extends Iterator |
107 |
|
{ |
108 |
|
HTML.Tag tag; |
109 |
|
HTMLDocument doc; |
110 |
|
ElementIterator it; |
111 |
|
|
112 |
|
public LeafIterator (HTML.Tag t, HTMLDocument d) |
113 |
|
{ |
114 |
|
doc = d; |
115 |
|
tag = t; |
116 |
|
it = new ElementIterator(doc); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
120 |
|
* Return the attributes for the tag associated with this iteartor |
121 |
|
* @return the AttributeSet |
122 |
|
*/ |
123 |
|
public AttributeSet getAttributes() |
124 |
|
{ |
125 |
|
if (it.current() != null) |
126 |
|
return it.current().getAttributes(); |
127 |
|
return null; |
128 |
|
} |
129 |
|
|
130 |
|
/** |
131 |
|
* Get the end of the range for the current occurrence of the tag |
132 |
|
* being defined and having the same attributes. |
133 |
|
* @return the end of the range |
134 |
|
*/ |
135 |
|
public int getEndOffset() |
136 |
|
{ |
137 |
|
if (it.current() != null) |
138 |
|
return it.current().getEndOffset(); |
139 |
|
return -1; |
140 |
|
} |
141 |
|
|
142 |
|
/** |
143 |
|
* Get the start of the range for the current occurrence of the tag |
144 |
|
* being defined and having the same attributes. |
145 |
|
* @return the start of the range (-1 if it can't be found). |
146 |
|
*/ |
147 |
|
|
148 |
|
public int getStartOffset() |
149 |
|
{ |
150 |
|
if (it.current() != null) |
151 |
|
return it.current().getStartOffset(); |
152 |
|
return -1; |
153 |
|
} |
154 |
|
|
155 |
|
/** |
156 |
|
* Advance the iterator to the next LeafElement . |
157 |
|
*/ |
158 |
|
public void next() |
159 |
|
{ |
160 |
|
it.next(); |
161 |
|
while (it.current()!= null && !it.current().isLeaf()) |
162 |
|
it.next(); |
163 |
|
} |
164 |
|
|
165 |
|
/** |
166 |
|
* Indicates whether or not the iterator currently represents an occurrence |
167 |
|
* of the tag. |
168 |
|
* @return true if the iterator currently represents an occurrence of the |
169 |
|
* tag. |
170 |
|
*/ |
171 |
|
public boolean isValid() |
172 |
|
{ |
173 |
|
return it.current() != null; |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
|
* Type of tag for this iterator. |
178 |
|
*/ |
179 |
|
public Tag getTag() |
180 |
|
{ |
181 |
|
return tag; |
182 |
|
} |
183 |
|
|
184 |
|
} |
185 |
|
|
186 |
public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent event) |
public void processHTMLFrameHyperlinkEvent(HTMLFrameHyperlinkEvent event) |
187 |
{ |
{ |
188 |
|
// TODO: Implement this properly. |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
|
* Gets an iterator for the given HTML.Tag. |
193 |
|
* @param t the requested HTML.Tag |
194 |
|
* @return the Iterator |
195 |
|
*/ |
196 |
|
public HTMLDocument.Iterator getIterator (HTML.Tag t) |
197 |
|
{ |
198 |
|
return new HTMLDocument.LeafIterator(t, this); |
199 |
|
} |
200 |
|
|
201 |
|
/** |
202 |
|
* An iterator over a particular type of tag. |
203 |
|
*/ |
204 |
|
public abstract static class Iterator |
205 |
|
{ |
206 |
|
/** |
207 |
|
* Return the attribute set for this tag. |
208 |
|
* @return the <code>AttributeSet</code> (null if none found). |
209 |
|
*/ |
210 |
|
public abstract AttributeSet getAttributes(); |
211 |
|
|
212 |
|
/** |
213 |
|
* Get the end of the range for the current occurrence of the tag |
214 |
|
* being defined and having the same attributes. |
215 |
|
* @return the end of the range |
216 |
|
*/ |
217 |
|
public abstract int getEndOffset(); |
218 |
|
|
219 |
|
/** |
220 |
|
* Get the start of the range for the current occurrence of the tag |
221 |
|
* being defined and having the same attributes. |
222 |
|
* @return the start of the range (-1 if it can't be found). |
223 |
|
*/ |
224 |
|
public abstract int getStartOffset(); |
225 |
|
|
226 |
|
/** |
227 |
|
* Move the iterator forward. |
228 |
|
*/ |
229 |
|
public abstract void next(); |
230 |
|
|
231 |
|
/** |
232 |
|
* Indicates whether or not the iterator currently represents an occurrence |
233 |
|
* of the tag. |
234 |
|
* @return true if the iterator currently represents an occurrence of the |
235 |
|
* tag. |
236 |
|
*/ |
237 |
|
public abstract boolean isValid(); |
238 |
|
|
239 |
|
/** |
240 |
|
* Type of tag this iterator represents. |
241 |
|
* @return the tag. |
242 |
|
*/ |
243 |
|
public abstract HTML.Tag getTag(); |
244 |
|
} |
245 |
|
|
246 |
|
public class BlockElement extends AbstractDocument.BranchElement |
247 |
|
{ |
248 |
|
public BlockElement (Element parent, AttributeSet a) |
249 |
|
{ |
250 |
|
super (parent, a); |
251 |
|
} |
252 |
|
|
253 |
|
/** |
254 |
|
* Gets the resolving parent. Since HTML attributes are not |
255 |
|
* inherited at the model level, this returns null. |
256 |
|
*/ |
257 |
|
public AttributeSet getResolveParent() |
258 |
|
{ |
259 |
|
return null; |
260 |
|
} |
261 |
|
|
262 |
|
public String getName() |
263 |
|
{ |
264 |
|
//FIXME: this is supposed to do something different from the super class |
265 |
|
return super.getName(); |
266 |
|
} |
267 |
} |
} |
268 |
} |
} |