forked from arduino/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXMLUtil.java
758 lines (671 loc) · 23.5 KB
/
XMLUtil.java
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
/* XMLUtil.java NanoXML/Java
*
* $Revision: 1.5 $
* $Date: 2002/02/03 21:19:38 $
* $Name: RELEASE_2_2_1 $
*
* This file is part of NanoXML 2 for Java.
* Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
*
* This software is provided 'as-is', without any express or implied warranty.
* In no event will the authors be held liable for any damages arising from the
* use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software in
* a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source distribution.
*/
package processing.xml;
import java.io.IOException;
import java.io.Reader;
/**
* Utility methods for NanoXML.
*
* @author Marc De Scheemaecker
* @version $Name: RELEASE_2_2_1 $, $Revision: 1.5 $
*/
class XMLUtil
{
/**
* Skips the remainder of a comment.
* It is assumed that <!- is already read.
*
* @param reader the reader
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static void skipComment(StdXMLReader reader)
throws IOException,
XMLParseException
{
if (reader.read() != '-') {
XMLUtil.errorExpectedInput(reader.getSystemID(),
reader.getLineNr(),
"<!--");
}
int dashesRead = 0;
for (;;) {
char ch = reader.read();
switch (ch) {
case '-':
dashesRead++;
break;
case '>':
if (dashesRead == 2) {
return;
}
dashesRead = 0;
break;
default:
dashesRead = 0;
}
}
}
/**
* Skips the remainder of the current XML tag.
*
* @param reader the reader
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static void skipTag(StdXMLReader reader)
throws IOException,
XMLParseException
{
int level = 1;
while (level > 0) {
char ch = reader.read();
switch (ch) {
case '<':
++level;
break;
case '>':
--level;
break;
}
}
}
/**
* Scans a public ID.
*
* @param publicID will contain the public ID
* @param reader the reader
*
* @return the system ID
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static String scanPublicID(StringBuffer publicID,
StdXMLReader reader)
throws IOException,
XMLParseException
{
if (! XMLUtil.checkLiteral(reader, "UBLIC")) {
return null;
}
XMLUtil.skipWhitespace(reader, null);
publicID.append(XMLUtil.scanString(reader, '\0', null));
XMLUtil.skipWhitespace(reader, null);
return XMLUtil.scanString(reader, '\0', null);
}
/**
* Scans a system ID.
*
* @param reader the reader
*
* @return the system ID
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static String scanSystemID(StdXMLReader reader)
throws IOException,
XMLParseException
{
if (! XMLUtil.checkLiteral(reader, "YSTEM")) {
return null;
}
XMLUtil.skipWhitespace(reader, null);
return XMLUtil.scanString(reader, '\0', null);
}
/**
* Retrieves an identifier from the data.
*
* @param reader the reader
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static String scanIdentifier(StdXMLReader reader)
throws IOException,
XMLParseException
{
StringBuffer result = new StringBuffer();
for (;;) {
char ch = reader.read();
if ((ch == '_') || (ch == ':') || (ch == '-') || (ch == '.')
|| ((ch >= 'a') && (ch <= 'z'))
|| ((ch >= 'A') && (ch <= 'Z'))
|| ((ch >= '0') && (ch <= '9')) || (ch > '\u007E')) {
result.append(ch);
} else {
reader.unread(ch);
break;
}
}
return result.toString();
}
/**
* Retrieves a delimited string from the data.
*
* @param reader the reader
* @param entityChar the escape character (& or %)
* @param entityResolver the entity resolver
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static String scanString(StdXMLReader reader,
char entityChar,
XMLEntityResolver entityResolver)
throws IOException,
XMLParseException
{
StringBuffer result = new StringBuffer();
int startingLevel = reader.getStreamLevel();
char delim = reader.read();
if ((delim != '\'') && (delim != '"')) {
XMLUtil.errorExpectedInput(reader.getSystemID(),
reader.getLineNr(),
"delimited string");
}
for (;;) {
String str = XMLUtil.read(reader, entityChar);
char ch = str.charAt(0);
if (ch == entityChar) {
if (str.charAt(1) == '#') {
result.append(XMLUtil.processCharLiteral(str));
} else {
XMLUtil.processEntity(str, reader, entityResolver);
}
} else if (ch == '&') {
reader.unread(ch);
str = XMLUtil.read(reader, '&');
if (str.charAt(1) == '#') {
result.append(XMLUtil.processCharLiteral(str));
} else {
result.append(str);
}
} else if (reader.getStreamLevel() == startingLevel) {
if (ch == delim) {
break;
} else if ((ch == 9) || (ch == 10) || (ch == 13)) {
result.append(' ');
} else {
result.append(ch);
}
} else {
result.append(ch);
}
}
return result.toString();
}
/**
* Processes an entity.
*
* @param entity the entity
* @param reader the reader
* @param entityResolver the entity resolver
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static void processEntity(String entity,
StdXMLReader reader,
XMLEntityResolver entityResolver)
throws IOException,
XMLParseException
{
entity = entity.substring(1, entity.length() - 1);
Reader entityReader = entityResolver.getEntity(reader, entity);
if (entityReader == null) {
XMLUtil.errorInvalidEntity(reader.getSystemID(),
reader.getLineNr(),
entity);
}
boolean externalEntity = entityResolver.isExternalEntity(entity);
reader.startNewStream(entityReader, !externalEntity);
}
/**
* Processes a character literal.
*
* @param entity the entity
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static char processCharLiteral(String entity)
throws IOException,
XMLParseException
{
if (entity.charAt(2) == 'x') {
entity = entity.substring(3, entity.length() - 1);
return (char) Integer.parseInt(entity, 16);
} else {
entity = entity.substring(2, entity.length() - 1);
return (char) Integer.parseInt(entity, 10);
}
}
/**
* Skips whitespace from the reader.
*
* @param reader the reader
* @param buffer where to put the whitespace; null if the
* whitespace does not have to be stored.
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static void skipWhitespace(StdXMLReader reader,
StringBuffer buffer)
throws IOException
{
char ch;
if (buffer == null) {
do {
ch = reader.read();
} while ((ch == ' ') || (ch == '\t') || (ch == '\n'));
} else {
for (;;) {
ch = reader.read();
if ((ch != ' ') && (ch != '\t') && (ch != '\n')) {
break;
}
if (ch == '\n') {
buffer.append('\n');
} else {
buffer.append(' ');
}
}
}
reader.unread(ch);
}
/**
* Reads a character from the reader.
*
* @param reader the reader
* @param entityChar the escape character (& or %) used to indicate
* an entity
*
* @return the character, or an entity expression (like e.g. &lt;)
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static String read(StdXMLReader reader,
char entityChar) throws IOException, XMLParseException {
char ch = reader.read();
StringBuffer buf = new StringBuffer();
buf.append(ch);
if (ch == entityChar) {
while (ch != ';') {
ch = reader.read();
buf.append(ch);
}
}
return buf.toString();
}
/**
* Reads a character from the reader disallowing entities.
*
* @param reader the reader
* @param entityChar the escape character (& or %) used to indicate
* an entity
*/
static char readChar(StdXMLReader reader,
char entityChar) throws IOException, XMLParseException {
String str = XMLUtil.read(reader, entityChar);
char ch = str.charAt(0);
if (ch == entityChar) {
XMLUtil.errorUnexpectedEntity(reader.getSystemID(),
reader.getLineNr(),
str);
}
return ch;
}
/**
* Returns true if the data starts with <I>literal</I>.
* Enough chars are read to determine this result.
*
* @param reader the reader
* @param literal the literal to check
*
* @throws java.io.IOException
* if an error occurred reading the data
*/
static boolean checkLiteral(StdXMLReader reader,
String literal)
throws IOException,
XMLParseException
{
for (int i = 0; i < literal.length(); i++) {
if (reader.read() != literal.charAt(i)) {
return false;
}
}
return true;
}
/**
* Throws an XMLParseException to indicate that an expected string is not
* encountered.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param expectedString the string that is expected
*/
static void errorExpectedInput(String systemID,
int lineNr,
String expectedString)
throws XMLParseException
{
throw new XMLParseException(systemID, lineNr,
"Expected: " + expectedString);
}
/**
* Throws an XMLParseException to indicate that an entity could not be
* resolved.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param entity the name of the entity
*/
static void errorInvalidEntity(String systemID,
int lineNr,
String entity)
throws XMLParseException
{
throw new XMLParseException(systemID, lineNr,
"Invalid entity: `&" + entity + ";'");
}
/**
* Throws an XMLParseException to indicate that an entity reference is
* unexpected at this point.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param entity the name of the entity
*/
static void errorUnexpectedEntity(String systemID,
int lineNr,
String entity)
throws XMLParseException
{
throw new XMLParseException(systemID, lineNr,
"No entity reference is expected here ("
+ entity + ")");
}
/**
* Throws an XMLParseException to indicate that a CDATA section is
* unexpected at this point.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
*/
static void errorUnexpectedCDATA(String systemID,
int lineNr)
throws XMLParseException
{
throw new XMLParseException(systemID, lineNr,
"No CDATA section is expected here");
}
/**
* Throws an XMLParseException to indicate that a string is not expected
* at this point.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param unexpectedString the string that is unexpected
*/
static void errorInvalidInput(String systemID,
int lineNr,
String unexpectedString)
throws XMLParseException
{
throw new XMLParseException(systemID, lineNr,
"Invalid input: " + unexpectedString);
}
/**
* Throws an XMLParseException to indicate that the closing tag of an
* element does not match the opening tag.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param expectedName the name of the opening tag
* @param wrongName the name of the closing tag
*/
static void errorWrongClosingTag(String systemID,
int lineNr,
String expectedName,
String wrongName)
throws XMLParseException
{
throw new XMLParseException(systemID, lineNr,
"Closing tag does not match opening tag: `"
+ wrongName + "' != `" + expectedName
+ "'");
}
/**
* Throws an XMLParseException to indicate that extra data is encountered
* in a closing tag.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
*/
static void errorClosingTagNotEmpty(String systemID,
int lineNr)
throws XMLParseException
{
throw new XMLParseException(systemID, lineNr,
"Closing tag must be empty");
}
/**
* Throws an XMLValidationException to indicate that an element is missing.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param parentElementName the name of the parent element
* @param missingElementName the name of the missing element
*/
static void errorMissingElement(String systemID,
int lineNr,
String parentElementName,
String missingElementName)
throws XMLValidationException
{
throw new XMLValidationException(
XMLValidationException.MISSING_ELEMENT,
systemID, lineNr,
missingElementName,
/*attributeName*/ null,
/*attributeValue*/ null,
"Element " + parentElementName
+ " expects to have a " + missingElementName);
}
/**
* Throws an XMLValidationException to indicate that an element is
* unexpected.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param parentElementName the name of the parent element
* @param unexpectedElementName the name of the unexpected element
*/
static void errorUnexpectedElement(String systemID,
int lineNr,
String parentElementName,
String unexpectedElementName)
throws XMLValidationException
{
throw new XMLValidationException(
XMLValidationException.UNEXPECTED_ELEMENT,
systemID, lineNr,
unexpectedElementName,
/*attributeName*/ null,
/*attributeValue*/ null,
"Unexpected " + unexpectedElementName + " in a "
+ parentElementName);
}
/**
* Throws an XMLValidationException to indicate that an attribute is
* missing.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param elementName the name of the element
* @param attributeName the name of the missing attribute
*/
static void errorMissingAttribute(String systemID,
int lineNr,
String elementName,
String attributeName)
throws XMLValidationException
{
throw new XMLValidationException(
XMLValidationException.MISSING_ATTRIBUTE,
systemID, lineNr,
elementName,
attributeName,
/*attributeValue*/ null,
"Element " + elementName + " expects an attribute named "
+ attributeName);
}
/**
* Throws an XMLValidationException to indicate that an attribute is
* unexpected.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param elementName the name of the element
* @param attributeName the name of the unexpected attribute
*/
static void errorUnexpectedAttribute(String systemID,
int lineNr,
String elementName,
String attributeName)
throws XMLValidationException
{
throw new XMLValidationException(
XMLValidationException.UNEXPECTED_ATTRIBUTE,
systemID, lineNr,
elementName,
attributeName,
/*attributeValue*/ null,
"Element " + elementName + " did not expect an attribute "
+ "named " + attributeName);
}
/**
* Throws an XMLValidationException to indicate that an attribute has an
* invalid value.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param elementName the name of the element
* @param attributeName the name of the attribute
* @param attributeValue the value of that attribute
*/
static void errorInvalidAttributeValue(String systemID,
int lineNr,
String elementName,
String attributeName,
String attributeValue)
throws XMLValidationException
{
throw new XMLValidationException(
XMLValidationException.ATTRIBUTE_WITH_INVALID_VALUE,
systemID, lineNr,
elementName,
attributeName,
attributeValue,
"Invalid value for attribute " + attributeName);
}
/**
* Throws an XMLValidationException to indicate that a #PCDATA element was
* missing.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param parentElementName the name of the parent element
*/
static void errorMissingPCData(String systemID,
int lineNr,
String parentElementName)
throws XMLValidationException
{
throw new XMLValidationException(
XMLValidationException.MISSING_PCDATA,
systemID, lineNr,
/*elementName*/ null,
/*attributeName*/ null,
/*attributeValue*/ null,
"Missing #PCDATA in element " + parentElementName);
}
/**
* Throws an XMLValidationException to indicate that a #PCDATA element was
* unexpected.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param parentElementName the name of the parent element
*/
static void errorUnexpectedPCData(String systemID,
int lineNr,
String parentElementName)
throws XMLValidationException
{
throw new XMLValidationException(
XMLValidationException.UNEXPECTED_PCDATA,
systemID, lineNr,
/*elementName*/ null,
/*attributeName*/ null,
/*attributeValue*/ null,
"Unexpected #PCDATA in element " + parentElementName);
}
/**
* Throws an XMLValidationException.
*
* @param systemID the system ID of the data source
* @param lineNr the line number in the data source
* @param message the error message
* @param elementName the name of the element
* @param attributeName the name of the attribute
* @param attributeValue the value of that attribute
*/
static void validationError(String systemID,
int lineNr,
String message,
String elementName,
String attributeName,
String attributeValue)
throws XMLValidationException
{
throw new XMLValidationException(XMLValidationException.MISC_ERROR,
systemID, lineNr,
elementName,
attributeName,
attributeValue,
message);
}
}