| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
|
package cave.common.xml;
import java.io.*;
import java.util.*;
public class Parser {
private final static int TEXT = 1;
private final static int ENTITY = 2;
private final static int OPEN_TAG = 3;
private final static int CLOSE_TAG = 4;
private final static int START_TAG = 5;
private final static int ATTRIBUTE_LVALUE = 6;
private final static int ATTRIBUTE_EQUAL = 9;
private final static int ATTRIBUTE_RVALUE = 10;
private final static int QUOTE = 7;
private final static int IN_TAG = 8;
private final static int SINGLE_TAG = 12;
private final static int COMMENT = 13;
private final static int DONE = 11;
private final static int DOCTYPE = 14;
private final static int PRE = 15;
private final static int CDATA = 16;
public static void parse(Document doc, Reader r) throws IOException, ParseException {
Stack st = new Stack();
int depth = 0;
int mode = PRE;
int c = 0;
int quotec = '"';
depth = 0;
StringBuffer sb = new StringBuffer();
StringBuffer etag = new StringBuffer();
String tagName = null;
String lvalue = null;
String rvalue = null;
Element attrs = null;
st = new Stack();
doc.startDocument();
int line=1, col=0;
boolean eol = false;
while((c = r.read()) != -1) {
// We need to map \r, \r\n, and \n to \n
// See XML spec section 2.11
if(c == '\n' && eol) {
eol = false;
continue;
} else if(eol) {
eol = false;
} else if(c == '\n') {
line++;
col=0;
} else if(c == '\r') {
eol = true;
c = '\n';
line++;
col=0;
} else {
col++;
}
if(mode == DONE) {
doc.endDocument();
return;
// We are between tags collecting text.
} else if(mode == TEXT) {
if(c == '<') {
st.push(new Integer(mode));
mode = START_TAG;
if(sb.length() > 0) {
doc.text(sb.toString());
sb.setLength(0);
}
} else if(c == '&') {
st.push(new Integer(mode));
mode = ENTITY;
etag.setLength(0);
} else
sb.append((char)c);
// we are processing a closing tag: e.g. </foo>
} else if(mode == CLOSE_TAG) {
if(c == '>') {
mode = popMode(st);
tagName = sb.toString();
sb.setLength(0);
depth--;
if(depth==0)
mode = DONE;
doc.endElement(tagName);
} else {
sb.append((char)c);
}
// we are processing CDATA
} else if(mode == CDATA) {
if(c == '>'
&& sb.toString().endsWith("]]")) {
sb.setLength(sb.length()-2);
doc.text(sb.toString());
sb.setLength(0);
mode = popMode(st);
} else
sb.append((char)c);
// we are processing a comment. We are inside
// the <!-- .... --> looking for the -->.
} else if(mode == COMMENT) {
if(c == '>'
&& sb.toString().endsWith("--")) {
sb.setLength(0);
mode = popMode(st);
} else
sb.append((char)c);
// We are outside the root tag element
} else if(mode == PRE) {
if(c == '<') {
mode = TEXT;
st.push(new Integer(mode));
mode = START_TAG;
}
// We are inside one of these <? ... ?>
// or one of these <!DOCTYPE ... >
} else if(mode == DOCTYPE) {
if(c == '>') {
mode = popMode(st);
if(mode == TEXT) mode = PRE;
}
// we have just seen a < and
// are wondering what we are looking at
// <foo>, </foo>, <!-- ... --->, etc.
} else if(mode == START_TAG) {
mode = popMode(st);
if(c == '/') {
st.push(new Integer(mode));
mode = CLOSE_TAG;
} else if (c == '?') {
mode = DOCTYPE;
} else {
st.push(new Integer(mode));
mode = OPEN_TAG;
tagName = null;
attrs = new Element();
sb.append((char)c);
}
// we are processing an entity, e.g. <, », etc.
} else if(mode == ENTITY) {
if(c == ';') {
mode = popMode(st);
String cent = etag.toString();
etag.setLength(0);
if(cent.equals("lt"))
sb.append('<');
else if(cent.equals("gt"))
sb.append('>');
else if(cent.equals("amp"))
sb.append('&');
else if(cent.equals("quot"))
sb.append('"');
else if(cent.equals("apos"))
sb.append('\'');
//Could parse hex entities if we wanted to
//else if(cent.startsWith("#x"))
//sb.append((char)Integer.parseInt(cent.substring(2), 16));
else if(cent.startsWith("#"))
sb.append((char)Integer.parseInt(cent.substring(1)));
// Insert custom entity definitions here
else
exc("Unknown entity: &"+cent+";", line, col);
} else {
etag.append((char)c);
}
// we have just seen something like this:
// <foo a="b"/
// and are looking for the final >.
} else if(mode == SINGLE_TAG) {
if(tagName == null)
tagName = sb.toString();
if(c != '>')
exc("Expected > for tag: <"+tagName+"/>", line, col);
doc.startElement(tagName,attrs);
doc.endElement(tagName);
if(depth==0) {
doc.endDocument();
return;
}
sb.setLength(0);
attrs = new Element();
tagName = null;
mode = popMode(st);
// we are processing something
// like this <foo ... >. It could
// still be a <!-- ... --> or something.
} else if(mode == OPEN_TAG) {
if(c == '>') {
if(tagName == null)
tagName = sb.toString();
sb.setLength(0);
depth++;
doc.startElement(tagName,attrs);
tagName = null;
attrs = new Element();
mode = popMode(st);
} else if(c == '/') {
mode = SINGLE_TAG;
} else if(c == '-' && sb.toString().equals("!-")) {
mode = COMMENT;
} else if(c == '[' && sb.toString().equals("![CDATA")) {
mode = CDATA;
sb.setLength(0);
} else if(c == 'E' && sb.toString().equals("!DOCTYP")) {
sb.setLength(0);
mode = DOCTYPE;
} else if(Character.isWhitespace((char)c)) {
tagName = sb.toString();
sb.setLength(0);
mode = IN_TAG;
} else {
sb.append((char)c);
}
// We are processing the quoted right-hand side
// of an element's attribute.
} else if(mode == QUOTE) {
if(c == quotec) {
rvalue = sb.toString();
sb.setLength(0);
attrs.put(lvalue,rvalue);
mode = IN_TAG;
// See section the XML spec, section 3.3.3
// on normalization processing.
} else if(" \r\n\u0009".indexOf(c)>=0) {
sb.append(' ');
} else if(c == '&') {
st.push(new Integer(mode));
mode = ENTITY;
etag.setLength(0);
} else {
sb.append((char)c);
}
} else if(mode == ATTRIBUTE_RVALUE) {
if(c == '"' || c == '\'') {
quotec = c;
mode = QUOTE;
} else if(Character.isWhitespace((char)c)) {
;
} else {
exc("Error in attribute processing",line,col);
}
} else if(mode == ATTRIBUTE_LVALUE) {
if(Character.isWhitespace((char)c)) {
lvalue = sb.toString();
sb.setLength(0);
mode = ATTRIBUTE_EQUAL;
} else if(c == '=') {
lvalue = sb.toString();
sb.setLength(0);
mode = ATTRIBUTE_RVALUE;
} else {
sb.append((char)c);
}
} else if(mode == ATTRIBUTE_EQUAL) {
if(c == '=') {
mode = ATTRIBUTE_RVALUE;
} else if(Character.isWhitespace((char)c)) {
;
} else {
exc("Error in attribute processing.", line, col);
}
} else if(mode == IN_TAG) {
if(c == '>') {
mode = popMode(st);
doc.startElement(tagName,attrs);
depth++;
tagName = null;
attrs = new Element();
} else if(c == '/') {
mode = SINGLE_TAG;
} else if(Character.isWhitespace((char)c)) {
;
} else {
mode = ATTRIBUTE_LVALUE;
sb.append((char)c);
}
}
}
if(mode == DONE)
doc.endDocument();
else
exc("missing end tag", line, col);
}
private static void exc(String s, int line, int col) throws ParseException
{
throw new ParseException(s + " near line " + line + ", column " + col);
}
private static int popMode(Stack st) {
if(!st.empty())
return ((Integer)st.pop()).intValue();
else
return PRE;
}
}
|