| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.build.doc; |
| 7 |
|
| 8 |
import java.util.Arrays; |
| 9 |
|
| 10 |
/** |
| 11 |
* This class implements a simple XML pull parser. |
| 12 |
* Only a subset of the XML pull parser API is implemented. |
| 13 |
*/ |
| 14 |
public class XMLParser { |
| 15 |
|
| 16 |
/** |
| 17 |
* This event type means an error occurred. |
| 18 |
*/ |
| 19 |
public static final int ERROR = 0; |
| 20 |
|
| 21 |
/** |
| 22 |
* This event type means a start element has been read. |
| 23 |
*/ |
| 24 |
public static final int START_ELEMENT = 1; |
| 25 |
|
| 26 |
/** |
| 27 |
* This event type means an end element has been read. |
| 28 |
*/ |
| 29 |
public static final int END_ELEMENT = 2; |
| 30 |
|
| 31 |
/** |
| 32 |
* This event type means a processing instruction has been read. |
| 33 |
*/ |
| 34 |
public static final int PROCESSING_INSTRUCTION = 3; |
| 35 |
|
| 36 |
/** |
| 37 |
* This event type means text has been read. |
| 38 |
*/ |
| 39 |
public static final int CHARACTERS = 4; |
| 40 |
|
| 41 |
/** |
| 42 |
* This event type means a comment has been read. |
| 43 |
*/ |
| 44 |
public static final int COMMENT = 5; |
| 45 |
|
| 46 |
// public static final int SPACE = 6; |
| 47 |
|
| 48 |
/** |
| 49 |
* This event type is used before reading. |
| 50 |
*/ |
| 51 |
public static final int START_DOCUMENT = 7; |
| 52 |
|
| 53 |
/** |
| 54 |
* This event type means the end of the document has been reached. |
| 55 |
*/ |
| 56 |
public static final int END_DOCUMENT = 8; |
| 57 |
|
| 58 |
// public static final int ENTITY_REFERENCE = 9; |
| 59 |
// public static final int ATTRIBUTE = 10; |
| 60 |
|
| 61 |
/** |
| 62 |
* This event type means a DTD element has been read. |
| 63 |
*/ |
| 64 |
public static final int DTD = 11; |
| 65 |
|
| 66 |
private final String xml; |
| 67 |
private int pos; |
| 68 |
private int eventType; |
| 69 |
private String currentText; |
| 70 |
private String currentToken; |
| 71 |
private String prefix, localName; |
| 72 |
private String[] attributeValues = new String[3]; |
| 73 |
private int currentAttribute; |
| 74 |
private boolean endElement; |
| 75 |
private boolean html; |
| 76 |
|
| 77 |
/** |
| 78 |
* Construct a new XML parser. |
| 79 |
* |
| 80 |
* @param xml the document |
| 81 |
*/ |
| 82 |
public XMLParser(String xml) { |
| 83 |
this.xml = xml; |
| 84 |
eventType = START_DOCUMENT; |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Enable or disable HTML processing. When enabled, attributes don't need to |
| 89 |
* have values. |
| 90 |
* |
| 91 |
* @param html true if HTML processing is enabled. |
| 92 |
*/ |
| 93 |
public void setHTML(boolean html) { |
| 94 |
this.html = html; |
| 95 |
} |
| 96 |
|
| 97 |
private void addAttributeName(String pre, String name) { |
| 98 |
if (attributeValues.length <= currentAttribute) { |
| 99 |
attributeValues = Arrays.copyOf(attributeValues, attributeValues.length * 2); |
| 100 |
} |
| 101 |
attributeValues[currentAttribute++] = pre; |
| 102 |
attributeValues[currentAttribute++] = name; |
| 103 |
} |
| 104 |
|
| 105 |
private void addAttributeValue(String v) { |
| 106 |
attributeValues[currentAttribute++] = v; |
| 107 |
} |
| 108 |
|
| 109 |
private int readChar() { |
| 110 |
if (pos >= xml.length()) { |
| 111 |
return -1; |
| 112 |
} |
| 113 |
return xml.charAt(pos++); |
| 114 |
} |
| 115 |
|
| 116 |
private void back() { |
| 117 |
pos--; |
| 118 |
} |
| 119 |
|
| 120 |
private void error(String expected) { |
| 121 |
throw new RuntimeException("Expected: " + expected + " got: " |
| 122 |
+ xml.substring(pos, Math.min(pos + 1000, xml.length()))); |
| 123 |
} |
| 124 |
|
| 125 |
private void read(String chars) { |
| 126 |
for (int i = 0; i < chars.length(); i++) { |
| 127 |
if (readChar() != chars.charAt(i)) { |
| 128 |
error(chars); |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
private void skipSpaces() { |
| 134 |
while (pos < xml.length() && xml.charAt(pos) <= ' ') { |
| 135 |
pos++; |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
private void read() { |
| 140 |
currentText = null; |
| 141 |
currentAttribute = 0; |
| 142 |
int tokenStart = pos, currentStart = pos; |
| 143 |
int ch = readChar(); |
| 144 |
if (ch < 0) { |
| 145 |
eventType = END_DOCUMENT; |
| 146 |
} else if (ch == '<') { |
| 147 |
currentStart = pos; |
| 148 |
ch = readChar(); |
| 149 |
if (ch < 0) { |
| 150 |
eventType = ERROR; |
| 151 |
} else if (ch == '?') { |
| 152 |
eventType = PROCESSING_INSTRUCTION; |
| 153 |
currentStart = pos; |
| 154 |
while (true) { |
| 155 |
ch = readChar(); |
| 156 |
if (ch < 0) { |
| 157 |
error("?>"); |
| 158 |
} |
| 159 |
if (ch == '?' && readChar() == '>') { |
| 160 |
break; |
| 161 |
} |
| 162 |
} |
| 163 |
if (xml.substring(currentStart).startsWith("xml")) { |
| 164 |
int back = tokenStart; |
| 165 |
read(); |
| 166 |
tokenStart = back; |
| 167 |
} else { |
| 168 |
currentText = xml.substring(currentStart, pos - 1); |
| 169 |
} |
| 170 |
} else if (ch == '!') { |
| 171 |
ch = readChar(); |
| 172 |
if (ch == '-') { |
| 173 |
eventType = COMMENT; |
| 174 |
if (readChar() != '-') { |
| 175 |
error("-"); |
| 176 |
} |
| 177 |
currentStart = pos; |
| 178 |
while (true) { |
| 179 |
ch = readChar(); |
| 180 |
if (ch < 0) { |
| 181 |
error("-->"); |
| 182 |
} |
| 183 |
if (ch == '-' && readChar() == '-') { |
| 184 |
read(">"); |
| 185 |
break; |
| 186 |
} |
| 187 |
} |
| 188 |
currentText = xml.substring(currentStart, pos - 1); |
| 189 |
} else if (ch == 'D') { |
| 190 |
read("OCTYPE"); |
| 191 |
eventType = DTD; |
| 192 |
while (true) { |
| 193 |
ch = readChar(); |
| 194 |
if (ch < 0) { |
| 195 |
break; |
| 196 |
} |
| 197 |
if (ch == '>') { |
| 198 |
break; |
| 199 |
} |
| 200 |
} |
| 201 |
} else if (ch == '[') { |
| 202 |
read("CDATA["); |
| 203 |
currentStart = pos; |
| 204 |
eventType = CHARACTERS; |
| 205 |
while (true) { |
| 206 |
ch = readChar(); |
| 207 |
if (ch < 0) { |
| 208 |
error("]]>"); |
| 209 |
} else if (ch != ']') { |
| 210 |
continue; |
| 211 |
} |
| 212 |
ch = readChar(); |
| 213 |
if (ch < 0) { |
| 214 |
error("]]>"); |
| 215 |
} else if (ch == ']') { |
| 216 |
do { |
| 217 |
ch = readChar(); |
| 218 |
if (ch < 0) { |
| 219 |
error("]]>"); |
| 220 |
} |
| 221 |
} while (ch == ']'); |
| 222 |
if (ch == '>') { |
| 223 |
currentText = xml.substring(currentStart, pos - 3); |
| 224 |
break; |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
} else if (ch == '/') { |
| 230 |
currentStart = pos; |
| 231 |
prefix = null; |
| 232 |
eventType = END_ELEMENT; |
| 233 |
while (true) { |
| 234 |
ch = readChar(); |
| 235 |
if (ch < 0) { |
| 236 |
error(">"); |
| 237 |
} else if (ch == ':') { |
| 238 |
prefix = xml.substring(currentStart, pos - 1); |
| 239 |
currentStart = pos + 1; |
| 240 |
} else if (ch == '>') { |
| 241 |
localName = xml.substring(currentStart, pos - 1); |
| 242 |
break; |
| 243 |
} else if (ch <= ' ') { |
| 244 |
localName = xml.substring(currentStart, pos - 1); |
| 245 |
skipSpaces(); |
| 246 |
read(">"); |
| 247 |
break; |
| 248 |
} |
| 249 |
} |
| 250 |
} else { |
| 251 |
prefix = null; |
| 252 |
localName = null; |
| 253 |
eventType = START_ELEMENT; |
| 254 |
while (true) { |
| 255 |
ch = readChar(); |
| 256 |
if (ch < 0) { |
| 257 |
error(">"); |
| 258 |
} else if (ch == ':') { |
| 259 |
prefix = xml.substring(currentStart, pos - 1); |
| 260 |
currentStart = pos + 1; |
| 261 |
} else if (ch <= ' ') { |
| 262 |
localName = xml.substring(currentStart, pos - 1); |
| 263 |
readAttributeValues(); |
| 264 |
ch = readChar(); |
| 265 |
} |
| 266 |
if (ch == '/') { |
| 267 |
if (localName == null) { |
| 268 |
localName = xml.substring(currentStart, pos - 1); |
| 269 |
} |
| 270 |
read(">"); |
| 271 |
endElement = true; |
| 272 |
break; |
| 273 |
} else if (ch == '>') { |
| 274 |
if (localName == null) { |
| 275 |
localName = xml.substring(currentStart, pos - 1); |
| 276 |
} |
| 277 |
break; |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
} else { |
| 282 |
// TODO need to replace &#xx;? |
| 283 |
eventType = CHARACTERS; |
| 284 |
while (true) { |
| 285 |
ch = readChar(); |
| 286 |
if (ch < 0) { |
| 287 |
break; |
| 288 |
} else if (ch == '<') { |
| 289 |
back(); |
| 290 |
break; |
| 291 |
} |
| 292 |
} |
| 293 |
currentText = xml.substring(currentStart, pos); |
| 294 |
} |
| 295 |
currentToken = xml.substring(tokenStart, pos); |
| 296 |
} |
| 297 |
|
| 298 |
private void readAttributeValues() { |
| 299 |
while (true) { |
| 300 |
int start = pos; |
| 301 |
int ch = readChar(); |
| 302 |
if (ch < 0) { |
| 303 |
error(">"); |
| 304 |
} else if (ch <= ' ') { |
| 305 |
continue; |
| 306 |
} else if (ch == '/' || ch == '>') { |
| 307 |
back(); |
| 308 |
return; |
| 309 |
} |
| 310 |
int end; |
| 311 |
int localNameStart = start; |
| 312 |
boolean noValue = false; |
| 313 |
while (true) { |
| 314 |
end = pos; |
| 315 |
ch = readChar(); |
| 316 |
if (ch < 0) { |
| 317 |
error("="); |
| 318 |
} else if (ch <= ' ') { |
| 319 |
skipSpaces(); |
| 320 |
ch = readChar(); |
| 321 |
if (ch != '=') { |
| 322 |
if (html) { |
| 323 |
back(); |
| 324 |
noValue = true; |
| 325 |
} else { |
| 326 |
error("="); |
| 327 |
} |
| 328 |
} |
| 329 |
break; |
| 330 |
} else if (ch == '=') { |
| 331 |
break; |
| 332 |
} else if (ch == ':') { |
| 333 |
localNameStart = pos; |
| 334 |
} else if (ch == '/' || ch == '>') { |
| 335 |
if (html) { |
| 336 |
back(); |
| 337 |
noValue = true; |
| 338 |
break; |
| 339 |
} |
| 340 |
error("="); |
| 341 |
} |
| 342 |
} |
| 343 |
if (localNameStart == start) { |
| 344 |
addAttributeName("", xml.substring(localNameStart, end)); |
| 345 |
} else { |
| 346 |
addAttributeName(xml.substring(start, localNameStart - 1), |
| 347 |
xml.substring(localNameStart, end)); |
| 348 |
} |
| 349 |
if (noValue) { |
| 350 |
noValue = false; |
| 351 |
} else { |
| 352 |
skipSpaces(); |
| 353 |
ch = readChar(); |
| 354 |
if (ch != '\"') { |
| 355 |
error("\""); |
| 356 |
} |
| 357 |
start = pos; |
| 358 |
while (true) { |
| 359 |
end = pos; |
| 360 |
ch = readChar(); |
| 361 |
if (ch < 0) { |
| 362 |
error("\""); |
| 363 |
} else if (ch == '\"') { |
| 364 |
break; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
addAttributeValue(xml.substring(start, end)); |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Check if there are more tags to read. |
| 374 |
* |
| 375 |
* @return true if there are more tags |
| 376 |
*/ |
| 377 |
public boolean hasNext() { |
| 378 |
return pos < xml.length(); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Read the next tag. |
| 383 |
* |
| 384 |
* @return the event type of the next tag |
| 385 |
*/ |
| 386 |
public int next() { |
| 387 |
if (endElement) { |
| 388 |
endElement = false; |
| 389 |
eventType = END_ELEMENT; |
| 390 |
currentToken = ""; |
| 391 |
} else { |
| 392 |
read(); |
| 393 |
} |
| 394 |
return eventType; |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Get the event type of the current token. |
| 399 |
* |
| 400 |
* @return the event type |
| 401 |
*/ |
| 402 |
public int getEventType() { |
| 403 |
return eventType; |
| 404 |
} |
| 405 |
|
| 406 |
/** |
| 407 |
* Get the current text. |
| 408 |
* |
| 409 |
* @return the text |
| 410 |
*/ |
| 411 |
public String getText() { |
| 412 |
return currentText; |
| 413 |
} |
| 414 |
|
| 415 |
/** |
| 416 |
* Get the current token text. |
| 417 |
* |
| 418 |
* @return the token |
| 419 |
*/ |
| 420 |
public String getToken() { |
| 421 |
return currentToken; |
| 422 |
} |
| 423 |
|
| 424 |
/** |
| 425 |
* Get the number of attributes. |
| 426 |
* |
| 427 |
* @return the attribute count |
| 428 |
*/ |
| 429 |
public int getAttributeCount() { |
| 430 |
return currentAttribute / 3; |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Get the prefix of the attribute. |
| 435 |
* |
| 436 |
* @param index the index of the attribute (starting with 0) |
| 437 |
* @return the prefix |
| 438 |
*/ |
| 439 |
public String getAttributePrefix(int index) { |
| 440 |
return attributeValues[index * 3]; |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Get the local name of the attribute. |
| 445 |
* |
| 446 |
* @param index the index of the attribute (starting with 0) |
| 447 |
* @return the local name |
| 448 |
*/ |
| 449 |
public String getAttributeLocalName(int index) { |
| 450 |
return attributeValues[index * 3 + 1]; |
| 451 |
} |
| 452 |
|
| 453 |
/** |
| 454 |
* Get the full name of the current start or end element. If there is no |
| 455 |
* prefix, only the local name is returned, otherwise the prefix, ':', and |
| 456 |
* the local name. |
| 457 |
* |
| 458 |
* @return the full name |
| 459 |
*/ |
| 460 |
public String getName() { |
| 461 |
return prefix == null || prefix.length() == 0 ? localName : prefix |
| 462 |
+ ":" + localName; |
| 463 |
} |
| 464 |
|
| 465 |
/** |
| 466 |
* Get the local name of the current start or end element. |
| 467 |
* |
| 468 |
* @return the local name |
| 469 |
*/ |
| 470 |
public String getLocalName() { |
| 471 |
return localName; |
| 472 |
} |
| 473 |
|
| 474 |
/** |
| 475 |
* Get the prefix of the current start or end element. |
| 476 |
* |
| 477 |
* @return the prefix |
| 478 |
*/ |
| 479 |
public String getPrefix() { |
| 480 |
return prefix; |
| 481 |
} |
| 482 |
|
| 483 |
/** |
| 484 |
* Check if the current character tag only contains spaces or other |
| 485 |
* non-printable characters. |
| 486 |
* |
| 487 |
* @return if the trimmed text is empty |
| 488 |
*/ |
| 489 |
public boolean isWhiteSpace() { |
| 490 |
return getText().trim().length() == 0; |
| 491 |
} |
| 492 |
|
| 493 |
/** |
| 494 |
* Get the remaining XML text of the document. |
| 495 |
* |
| 496 |
* @return the remaining XML |
| 497 |
*/ |
| 498 |
public String getRemaining() { |
| 499 |
return xml.substring(pos); |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Get the current character position in the XML document. |
| 504 |
* |
| 505 |
* @return the position |
| 506 |
*/ |
| 507 |
public int getPos() { |
| 508 |
return pos; |
| 509 |
} |
| 510 |
|
| 511 |
} |