38 |
|
|
39 |
package javax.swing.text.html.parser; |
package javax.swing.text.html.parser; |
40 |
|
|
41 |
|
import gnu.javax.swing.text.html.parser.htmlAttributeSet; |
42 |
|
import gnu.javax.swing.text.html.parser.support.Parser; |
43 |
|
|
44 |
import java.io.IOException; |
import java.io.IOException; |
45 |
import java.io.Reader; |
import java.io.Reader; |
46 |
|
|
47 |
|
import javax.swing.text.BadLocationException; |
48 |
|
import javax.swing.text.html.HTMLEditorKit; |
49 |
|
|
50 |
/** |
/** |
51 |
* <p>A simple error-tolerant HTML parser that uses a DTD document |
* <p>A simple error-tolerant HTML parser that uses a DTD document |
52 |
* to access data on the possible tokens, arguments and syntax.</p> |
* to access data on the possible tokens, arguments and syntax.</p> |
77 |
implements DTDConstants |
implements DTDConstants |
78 |
{ |
{ |
79 |
/** |
/** |
80 |
|
* The enclosed working parser class. |
81 |
|
*/ |
82 |
|
private class gnuParser |
83 |
|
extends gnu.javax.swing.text.html.parser.support.Parser |
84 |
|
{ |
85 |
|
private gnuParser(DTD d) |
86 |
|
{ |
87 |
|
super(d); |
88 |
|
} |
89 |
|
|
90 |
|
protected final void handleComment(char[] comment) |
91 |
|
{ |
92 |
|
parser.handleComment(comment); |
93 |
|
callBack.handleComment(comment, hTag.where.startPosition); |
94 |
|
} |
95 |
|
|
96 |
|
protected final void handleEmptyTag(TagElement tag) |
97 |
|
throws javax.swing.text.ChangedCharSetException |
98 |
|
{ |
99 |
|
parser.handleEmptyTag(tag); |
100 |
|
callBack.handleSimpleTag(tag.getHTMLTag(), getAttributes(), |
101 |
|
hTag.where.startPosition |
102 |
|
); |
103 |
|
} |
104 |
|
|
105 |
|
protected final void handleEndTag(TagElement tag) |
106 |
|
{ |
107 |
|
parser.handleEndTag(tag); |
108 |
|
callBack.handleEndTag(tag.getHTMLTag(), hTag.where.startPosition); |
109 |
|
} |
110 |
|
|
111 |
|
protected final void handleError(int line, String message) |
112 |
|
{ |
113 |
|
parser.handleError(line, message); |
114 |
|
callBack.handleError(message, hTag.where.startPosition); |
115 |
|
} |
116 |
|
|
117 |
|
protected final void handleStartTag(TagElement tag) |
118 |
|
{ |
119 |
|
parser.handleStartTag(tag); |
120 |
|
htmlAttributeSet attributes = gnu.getAttributes(); |
121 |
|
|
122 |
|
if (tag.fictional()) |
123 |
|
attributes.addAttribute(HTMLEditorKit.ParserCallback.IMPLIED, |
124 |
|
Boolean.TRUE |
125 |
|
); |
126 |
|
|
127 |
|
callBack.handleStartTag(tag.getHTMLTag(), attributes, |
128 |
|
hTag.where.startPosition |
129 |
|
); |
130 |
|
} |
131 |
|
|
132 |
|
protected final void handleText(char[] text) |
133 |
|
{ |
134 |
|
parser.handleText(text); |
135 |
|
callBack.handleText(text, hTag.where.startPosition); |
136 |
|
} |
137 |
|
|
138 |
|
DTD getDTD() |
139 |
|
{ |
140 |
|
return dtd; |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
|
* This field is used to access the identically named |
146 |
|
* methods of the outer class. |
147 |
|
*/ |
148 |
|
private DocumentParser parser = this; |
149 |
|
|
150 |
|
/** |
151 |
|
* The callback. |
152 |
|
*/ |
153 |
|
private HTMLEditorKit.ParserCallback callBack; |
154 |
|
|
155 |
|
/** |
156 |
|
* The reference to the working class of HTML parser that is |
157 |
|
* actually used to parse the document. |
158 |
|
*/ |
159 |
|
private gnuParser gnu; |
160 |
|
|
161 |
|
/** |
162 |
* Creates a new parser that uses the given DTD to access data on the |
* Creates a new parser that uses the given DTD to access data on the |
163 |
* possible tokens, arguments and syntax. There is no single - step way |
* possible tokens, arguments and syntax. There is no single - step way |
164 |
* to get a default DTD; you must either refer to the implementation - |
* to get a default DTD; you must either refer to the implementation - |
165 |
* specific packages, write your own DTD or obtain the working instance |
* specific packages, write your own DTD or obtain the working instance |
166 |
* of parser in other way, for example, by calling |
* of parser in other way, for example, by calling |
167 |
* {@link javax.swing.text.html.HTMLEditorKit#getParser() }. |
* {@link javax.swing.text.html.HTMLEditorKit#getParser() }. |
168 |
* @param a_dtd A DTD to use. |
* @param a_dtd a DTD to use. |
169 |
*/ |
*/ |
170 |
public DocumentParser(DTD a_dtd) |
public DocumentParser(DTD a_dtd) |
171 |
{ |
{ |
172 |
super(a_dtd); |
super(a_dtd); |
173 |
|
gnu = new gnuParser(a_dtd); |
174 |
} |
} |
175 |
|
|
176 |
/** |
/** |
177 |
* Parse the HTML text, calling various methods in response to the |
* Parses the HTML document, calling methods of the provided |
178 |
* occurence of the corresponding HTML constructions. |
* callback. This method must be multithread - safe. |
179 |
* @param reader The reader to read the source HTML from. |
* @param reader The reader to read the HTML document from |
180 |
* @throws IOException If the reader throws one. |
* @param callback The callback that is notifyed about the presence |
181 |
*/ |
* of HTML elements in the document. |
182 |
public synchronized void parse(Reader reader) |
* @param ignoreCharSet If thrue, any charset changes during parsing |
183 |
throws IOException |
* are ignored. |
184 |
{ |
* @throws java.io.IOException |
185 |
super.parse(reader); |
*/ |
186 |
|
public void parse(Reader reader, HTMLEditorKit.ParserCallback a_callback, |
187 |
|
boolean ignoreCharSet |
188 |
|
) |
189 |
|
throws IOException |
190 |
|
{ |
191 |
|
callBack = a_callback; |
192 |
|
gnu.parse(reader); |
193 |
|
|
194 |
|
callBack.handleEndOfLineString(gnu.getEndOfLineSequence()); |
195 |
|
try |
196 |
|
{ |
197 |
|
callBack.flush(); |
198 |
|
} |
199 |
|
catch (BadLocationException ex) |
200 |
|
{ |
201 |
|
// Convert this into the supported type of exception. |
202 |
|
throw new IOException(ex.getMessage()); |
203 |
|
} |
204 |
} |
} |
205 |
|
|
206 |
/** |
/** |
207 |
* Handle HTML comment. The default method returns without action. |
* Handle HTML comment. The default method returns without action. |
208 |
* @param comment The comment being handled |
* @param comment the comment being handled |
209 |
*/ |
*/ |
210 |
protected void handleComment(char[] comment) |
protected void handleComment(char[] comment) |
211 |
{ |
{ |
215 |
* Handle the tag with no content, like <br>. The method is |
* Handle the tag with no content, like <br>. The method is |
216 |
* called for the elements that, in accordance with the current DTD, |
* called for the elements that, in accordance with the current DTD, |
217 |
* has an empty content. |
* has an empty content. |
218 |
* @param tag The tag being handled. |
* @param tag the tag being handled. |
219 |
* @throws javax.swing.text.ChangedCharSetException |
* @throws javax.swing.text.ChangedCharSetException |
220 |
*/ |
*/ |
221 |
protected void handleEmptyTag(TagElement tag) |
protected void handleEmptyTag(TagElement tag) |
250 |
|
|
251 |
/** |
/** |
252 |
* Handle the text section. |
* Handle the text section. |
253 |
* @param text A section text. |
* @param text a section text. |
254 |
*/ |
*/ |
255 |
protected void handleText(char[] text) |
protected void handleText(char[] text) |
256 |
{ |
{ |