updated Makefile.common
[org.ibex.core.git] / src / org / ibex / util / XML.java
1 // Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
2 //
3 // You may modify, copy, and redistribute this code under the terms of
4 // the GNU Library Public License version 2.1, with the exception of
5 // the portion of clause 6a after the semicolon (aka the "obnoxious
6 // relink clause")
7
8 package org.ibex.util;
9
10 import java.io.Reader;
11 import java.io.IOException;
12 import java.io.EOFException;
13
14 /**
15  * An Event-Driving, Non-Validating XML Parser with Namespace support.
16  *
17  * A subclass can implement the abstract functions for receiving details
18  * about an xml file as it is parsed. To initate a parse, use the parse()
19  * function. 
20  *
21  * <h3>Implementation Notes</h3>
22  * <p>As the parser traverses into an element, it adds it to the linked list
23  * called <tt>elements</tt>. However, <tt>elements</tt> has been pre-filled
24  * with instances of the Element inner class. So in the vast majority of
25  * cases, the pointer current is moved along one, and the values for the
26  * new element are filled into the current object.</p>
27  *
28  * <p>This parser supports all the unicode ranges required by the XML
29  * Specification. However, it is optimised for well-formed ASCII documents.
30  * Documents containing unicode Names and Attributes will take much longer
31  * to process, and invalid documents (badly formed Names or invalid attributes)
32  * will be run through a test on every single unicode character range before 
33  * being declared invalid.</p> 
34  *
35  * <ul>
36  *  <li>Each time the buffer offset <tt>off</tt> is moved, the length
37  *   <tt>len</tt> must be decreased.</li>
38  *  <li>Each time the buffer length is decreased, it must be checked to make
39  *   sure it is &gt;0.</li>
40  *  <li><i>error</i> is defined as a Validity Constraint Violation and
41  *   is recoverable</li>
42  *  <li><i>fatal error</i> is defined as a Well-formedness Constraint
43  *   Violation and is not recoverable</li>
44  * </ul> 
45  *
46  * @author David Crawshaw 
47  * @see <a href="http://w3.org/TR/REC-xml">XML Specification</a> 
48  * @see <a href="http://w3.org/TR/REC-xml-names">XML Namespaces</a>
49  */
50 public abstract class XML
51 {
52     /////////////////////////////////////////////////////////////////////////////////////////////
53     // XML Parser
54     /////////////////////////////////////////////////////////////////////////////////////////////
55
56     public static final int BUFFER_SIZE = 255;
57
58     /** static pool of XML.Element instances shared by all XML Parsers. */
59     private static final Queue elements = new Queue(30);
60
61     private static final char[] single_amp  = new char[] { '&'  };
62     private static final char[] single_apos = new char[] { '\'' };
63     private static final char[] single_gt   = new char[] { '>'  };
64     private static final char[] single_lt   = new char[] { '<'  };
65     private static final char[] single_quot = new char[] { '"'  };
66
67     private int line;
68     private int col;
69
70     private Reader in;
71     private char[] buf;
72     private int    off;
73     private int    base;  // base+off == distance into the stream
74     private int    len;
75
76     private Element current;
77
78     // used in readEntity() to process a single character without creating a new array
79     private char[] singlechar = new char[1];
80
81
82     public XML() { this(BUFFER_SIZE); }
83
84     public XML(int bSize) {
85         buf = new char[bSize];
86
87         current = (Element)elements.remove(false);
88         if (current == null) current = new Element();
89     }
90
91     /** Returns the line number at the beginning of the last process call. */
92     public int getLine() { return line; }
93
94     /** Returns the column number at the beginning of the last process call. */
95     public int getCol()  { return col; }
96
97     /** Returns the global file offset at the beginning of the last process call. */
98     public int getGlobalOffset() { return base + off; }
99
100     /**
101      * Parse given input and call the abstract event functions.
102      *
103      * Careful with threading, as this function is not synchronized.
104      */ 
105     public final void parse(Reader reader) throws IOException, Exn {
106         in  = reader;
107         off = len = 0;
108         line = col = 1;
109
110         clear(); // clean up possible mid-way linked-list element
111
112         try {
113             // process the stream
114             while (true) {
115                 if (!buffer(1)) {
116                     if (current.qName == null) break;
117                     throw new Exn("reached eof without closing <"+current.qName+"> element", Exn.WFC, getLine(), getCol());
118                 }
119
120                 if (buf[off] == '<') readTag();
121                 readChars(current.qName != null);
122             }
123         } finally { clear(); } // clean up elements
124     }
125
126     /** remove any leftover elements from the linked list and queue them */
127     private final void clear() {
128         for (Element last = current; current.parent != null; ) {
129             current = current.parent;
130             last.clear();
131             elements.append(last);
132         }
133         current.clear();
134     }
135
136     /** reads in a tag. expects <tt>buf[off] == '&#60;'</tt> */
137     private final void readTag() throws IOException, Exn {
138         // Start Tag    '<' Name (S Attribute)* S? '>'
139         boolean starttag  = true;
140
141         // End Tag     '</' Name S? '>'
142         boolean endtag    = false;
143
144         // if (starttag & endtag) then: EmptyElemTag '<' Name (S Attribute)* S? '/>'
145
146         // Position in the name of the ':' namespace prefix
147         int prefix = -1;
148
149         int namelen   = 0;
150
151         col++; off++; len--;
152         if (!buffer(1)) throw new EOFException("Unexpected EOF processing element tag");
153
154         // work out what we can from the beginning of the tag
155         char s = buf[off]; 
156         if (s == '!') {
157             // definitions here don't necessarily conform to xml spec (as DTDs not yet implemented)
158             col++; off++; len--; 
159             if (!buffer(4)) throw new EOFException("Unexpected EOF processing <! element");
160
161             boolean bad = false;
162             switch (buf[off]) {
163                 case '-':
164                     if (buf[off+1] != '-') { bad = true; break; }
165                     col += 2; off += 2; len -= 2;
166
167                     // Comment        '<!--'      ((Char - '-') | ('-' (Char - '-')))* '-->'
168                     readChars(false, "-->", false); 
169                     col += 3; off += 3; len -= 3;
170                     break;
171
172                 // we don't care about the following definitions
173
174                 case 'A':
175                     if (!buffer(7)
176                             || buf[off+1] != 'T' || buf[off+2] != 'T' || buf[off+3] != 'L'
177                             || buf[off+4] != 'I' || buf[off+5] != 'S' || buf[off+6] != 'T') {
178                         bad = true; break;
179                     } 
180                     col += 7; off += 7; len -= 7; 
181
182                     // ATTLIST        '<!ATTLIST'   (Char* - '>') '>'
183                     readChars(false, ">", true); 
184                     col++; off++; len--;
185                     break;
186                 case 'D':
187                     if (!buffer(7)
188                             || buf[off+1] != 'O' || buf[off+2] != 'C' || buf[off+3] != 'T'
189                             || buf[off+4] != 'Y' || buf[off+5] != 'P' || buf[off+6] != 'E') {
190                         bad = true; break;
191                     }
192                     col += 7; off += 7; len -= 7;
193
194                     // DTD            '<!DOCTYPE'   (Char* - '>') '>'
195                     readChars(false, ">", true); 
196                     col++; off++; len--;
197                     break; 
198                 case 'E':
199                     if (!buffer(7)) {
200                         bad = true;
201                     } else if (buf[off+1] == 'L' && buf[off+2] == 'E' && buf[off+3] == 'M'
202                             && buf[off+4] == 'E' && buf[off+5] == 'N' && buf[off+6] == 'T') {
203                         // ELEMENT        '<!ELEMENT'   (Char* - '>') '>'
204                         readChars(false, ">", true); 
205                         col++; off++; len--;
206
207                     } else if (buf[off+1] == 'N' && buf[off+2] == 'T' && buf[off+3] == 'I'
208                             && buf[off+4] == 'T' && buf[off+5] == 'Y') {
209                         // ENTITY         '<!ENTITY'    (Char* - '>') '>'
210                         readChars(false, ">", true); 
211                         col++; off++; len--;
212
213                     } else {
214                         bad = true;
215                     }
216                     break;
217
218                 case 'N':
219                     if (!buffer(8)
220                             || buf[off+1] != 'O' || buf[off+2] != 'T' || buf[off+3] != 'A' || buf[off+4] != 'T'
221                             || buf[off+5] != 'I' || buf[off+6] != 'O' || buf[off+7] != 'N') {
222                         bad = true; break;
223                     }
224                     col += 8; off += 8; len -= 8;
225                     // NOTATION       '<!NOTATION'  (Char* - '>') '>'
226                     readChars(false, ">", true); 
227                     col++; off++; len--;
228
229                     break;
230                 default: bad = true;
231             }
232
233             if (bad) throw new Exn("element tag start character is invalid", Exn.MARKUP, getLine(), getCol());
234
235         } else if (s == '?') {
236             // PI (Ignored)   '<?'  (Char* - (Char* '?>' Char*))  '?>'
237             col++; off++; len--;
238             readChars(false, "?>", true);
239             if (!buffer(2)) throw new EOFException("Unexpected EOF at end of Processing Instruction");
240             col += 2; off += 2; len -= 2;
241
242         } else if (s == '[') {
243             if (!buffer(7)
244                     || buf[off+1] != 'C' || buf[off+2] != 'D' || buf[off+3] != 'A'
245                     || buf[off+4] != 'T' || buf[off+5] != 'A' || buf[off+6] != '[') {
246                 col++; off--; len++; 
247                 // Conditional    '<![' (Char* - (Char* ']]>' Char*)) ']]>'
248                 readChars(false, "]]>", false); 
249             } else {
250                 col += 7; off += 7; len -=7;
251                 // CDATA          '<![CDATA[' (Char* - (Char* ']]>' Char*))        ']]>'
252                 readChars(true, "]]>", false);
253             } 
254             col += 3; off += 3; len -= 3;
255         } else {
256             if (s == '/') {
257                 // End Tag        '</' Name S? '>'
258                 starttag = false; 
259                 endtag = true;
260
261                 col++; off++; len--;
262                 if (!buffer(1)) throw new EOFException("Unexpected EOF processing end tag");
263                 s = buf[off];
264             }
265
266             if (!Name(s)) throw new Exn("invalid starting character in element name", Exn.MARKUP, getLine(), getCol()); 
267
268             // find the element name (defined in XML Spec: section 2.3)
269             for (namelen = 0; ; namelen++) {
270                 if (!buffer(namelen+1)) throw new EOFException("Unexpected EOF in element tag name");
271
272                 s = buf[off+namelen];
273
274                 if (S(s) || s == '>') {
275                     break;
276                 } else if (s == '/') {
277                     endtag = true;
278                     break;
279                 } else if (s == ':' && namelen > 0 && prefix < 1) {
280                     // we have a definition of the prefix range available
281                     prefix = namelen; 
282                 } else if (!NameChar(s)) {
283                     throw new Exn("element name contains invalid character", Exn.MARKUP, getLine(), getCol());
284                 }
285             }
286
287             // process name (based on calculated region)
288             if (namelen < 1) throw new Exn("element name is null", Exn.MARKUP, getLine(), getCol()); 
289
290             // we have marked out the name region, so turn it into a string and move on
291             String qName = new String(buf, off, namelen);
292
293             col += namelen; off += namelen; len -= namelen;
294
295             if (starttag) {
296                 // create the in-memory element representation of this beast
297                 // if current.qName == null then this is the root element we're dealing with
298                 if (current.qName != null) {
299                     Element next = (Element)elements.remove(false);
300                     if (next == null) next = new Element();
301                     //next.clear(); // TODO: remove as elements now checked as they're added to the queue
302                     next.parent = current;
303                     current = next;
304                 }
305
306                 current.qName = qName;
307
308                 if (prefix > 0) {
309                     current.prefix = current.qName.substring(0, prefix);
310                     current.localName = current.qName.substring(prefix+1);
311                 } else {
312                     current.prefix = null;
313                     current.localName = current.qName;
314                 }
315
316                 // process attributes
317                 readWhitespace(); 
318                 if (!buffer(1)) throw new EOFException("Unexpected EOF - processing attributes part 1");
319                 while (buf[off] != '/' && buf[off] != '>') {
320                     readAttribute();
321                     if (!buffer(1)) throw new EOFException("Unexpected EOF - processing attributes part 2");
322                     readWhitespace();
323                 }
324
325                 // work out the uri of this element
326                 current.uri = current.getUri(current.getPrefix()); 
327                 if (current.getUri().equals("") && current.getPrefix() != null)
328                     current.addError(new Exn("undefined prefix '"+current.getPrefix()+"'", Exn.NC, getLine(), getCol()));
329
330             } else {
331                 // this is an end-of-element tag
332                 if (!qName.equals(current.getQName())) throw new Exn(
333                     "end tag </"+qName+"> does not line up with start tag <"+current.getQName()+">", Exn.WFC, getLine(), getCol()
334                 );
335             }
336
337             // deal with whitespace
338             readWhitespace(); 
339
340             // process tag close
341             if (!buffer(1)) throw new EOFException("Unexpected EOF before end of tag"); 
342             if (buf[off] == '/') {
343                 endtag = true;
344                 off++; len--; col++;
345             }
346             if (!buffer(1)) throw new EOFException("Unexpected EOF before end of endtag"); 
347             if (buf[off] == '>') {
348                 off++; len--; col++;
349             } else {
350                 throw new Exn("missing '>' character from element '"+qName+"'", Exn.MARKUP, getLine(), getCol());
351             }
352
353             // send element signals
354             if (starttag) startElement(current);
355             if (endtag) {
356                 endElement(current);
357
358                 // we just closed an element, so remove it from the element 'stack'
359                 if (current.getParent() == null) {
360                     // we just finished the root element
361                     current.clear(); 
362                 } else {
363                     Element last = current;
364                     current = current.parent;
365                     last.clear();
366                     elements.append(last);
367                 }
368             }
369         }
370     }
371
372     /** reads in an attribute of an element. expects Name(buf[off]) */
373     private final void readAttribute() throws IOException, Exn {
374         int ref = 0;
375         int prefix = 0;
376         String n, v, p, u; // attribute name, value, prefix and uri respectively
377         n = v = p = u = null;
378         char s;
379
380         // find the element name (defined in XML Spec: section 2.3)
381         for (ref= 0; ; ref++) {
382             if (!buffer(ref+1)) throw new EOFException("Unexpected EOF in read attribute loop part 1");
383
384             s = buf[off+ref];
385
386             if (s == '=' || S(s)) {
387                 break;
388             } else if (s == ':' && ref > 0 && prefix < 1) {
389                 // we have a definition of the prefix range available
390                 prefix = ref+1;
391             } else if (!NameChar(s)) {
392                 throw new Exn("attribute name contains invalid characters", Exn.MARKUP, getLine(), getCol());
393             }
394         }
395
396         // determine prefix and key name
397         if (prefix > 0) {
398             p = new String(buf, off, prefix-1);
399             col += prefix; off += prefix; len -= prefix; ref -= prefix;
400         }
401         n = new String(buf, off, ref);
402         col += ref; off += ref; len -= ref;
403
404         // find name/value divider ('=')
405         readWhitespace();
406         if (!buffer(1)) throw new EOFException("Unexpected EOF before attribute '=' divider");
407         if (buf[off] != '=') throw new Exn("attribute name not followed by '=' sign", Exn.MARKUP, getLine(), getCol());
408
409         col++; off++; len--;
410         readWhitespace();
411
412         if (!buffer(1)) throw new EOFException("Unexpected EOF after attribute '=' divider");
413
414         char wrap;
415         if (buf[off] == '\'' || buf[off] == '"') {
416             wrap = buf[off];
417         } else {
418             throw new Exn("attribute '"+n+"' must have attribute wrapped in ' or \"", Exn.MARKUP, getLine(), getCol());
419         }
420         col++; off++; len--;
421
422         // find the attribute value
423         attval: for (ref = 0; ; ref++) {
424             if (!buffer(ref+1)) throw new EOFException("Unexpected EOF in attribute value");
425
426             if (buf[off+ref] == wrap) {
427                 break attval;
428             } else if (buf[off+ref] == '<') {
429                 throw new Exn("attribute value for '"+n+"' must not contain '<'", Exn.WFC, getLine(), getCol());
430             } 
431         }
432
433         v = new String(buf, off, ref);
434         col += ref; off += ref; len -= ref;
435
436         // remove end wrapper character
437         col++; off++; len--;
438
439         // process attribute
440         if (p != null && p.equals("xmlns")) {
441             current.addUri(n, v);
442         } else if (n.equals("xmlns")) {
443             if (current.getUri().equals("")) {
444                 current.addUri("", v);
445             } else {
446                 current.addError(new Exn("default namespace definition repeated", Exn.NC, getLine(), getCol()));
447             }
448         } else {
449             // find attribute uri
450             u = current.getUri(p); 
451             if (p != null && u.equals("")) current.addError(new Exn("undefined attribute prefix '"+p+"'", Exn.NC, getLine(), getCol()));
452
453             // check to see if attribute is a repeat
454             for (int i=0; current.len > i; i++) if (n.equals(current.getAttrKey(i)) && u.equals(current.getAttrUri(i))) throw new Exn(
455                 "attribute name '"+n+"' may not appear more than once in the same element tag", Exn.WFC, getLine(), getCol()
456             );
457
458             current.addAttr(n, v, u); 
459         }
460     }
461
462     /** reads an entity and processes out its value. expects buf[off] == '&amp;' */
463     private final void readEntity() throws IOException, Exn {
464         off++; len--;
465         if (!buffer(2)) throw new EOFException("Unexpected EOF reading entity");
466
467         boolean unknown = false;
468         switch (buf[off]) {
469             case '#':
470                 off++; len--;
471
472                 int radix;
473                 if (buf[off] == 'x') { off++; len--; radix = 16; } else { radix = 10; }
474                 int c = 0;
475
476                 // read in each char, then shift total value to the left and add the extra
477                 // style of loop is slightly different from all the others, as this should run a limited number of times 
478                 findchar: while (true) {
479                     if (!buffer(1)) throw new EOFException("Unexpected EOF reading entity");
480                     int d = Character.digit(buf[off], radix);
481                     if (d == -1) {
482                         if (buf[off] != ';') throw new Exn("illegal characters in entity reference", Exn.WFC, getLine(), getCol());
483                         off++; len--; col++;
484                         break findchar;
485                     }
486                     c = (c * radix) + d;
487
488                     off++; len--;
489                 }
490
491                 singlechar[0] = Character.forDigit(c, radix);
492                 characters(singlechar, 0, 1);
493                 break;
494
495             case 'a':
496                 if (buffer(4) && buf[off+1] == 'm' && buf[off+2] == 'p' && buf[off+3] == ';') {
497                     characters(single_amp, 0, 1); // &amp;
498                     off += 4; len -= 4; col++;
499                 } else if (buffer(5) && buf[off+1] == 'p' && buf[off+2] == 'o' && buf[off+3] == 's' && buf[off+4] == ';') {
500                     characters(single_apos, 0, 1); // &apos;
501                     off += 5; len -= 5; col++;
502                 } else {
503                     unknown = true;
504                 }
505                 break;
506
507             case 'g':
508                 if (buffer(3) && buf[off+1] == 't' && buf[off+2] == ';') {
509                     characters(single_gt, 0, 1); // &gt;
510                     off += 3; len -= 3; col++;
511                 } else {
512                     unknown = true;
513                 }
514                 break;
515
516             case 'l':
517                 if (buffer(3) && buf[off+1] == 't' && buf[off+2] == ';') {
518                     characters(single_lt, 0, 1); // &lt;
519                     off += 3; len -= 3; col++;
520                 } else {
521                     unknown = true;
522                 }
523                 break;
524
525             case 'q':
526                 if (buffer(5) && buf[off+1] == 'u' && buf[off+2] == 'o' && buf[off+3] == 't' && buf[off+4] == ';') {
527                     characters(single_quot, 0, 1); // &quot;
528                     off += 5; len -= 5; col++;
529                 } else {
530                     unknown = true;
531                 }
532                 break;
533
534             // TODO: check a parser-level Hash of defined entities
535         }
536
537         if (unknown) throw new Exn("unknown entity (<!ENTITY> not supported)", Exn.WFC, getLine(), getCol());
538     }
539
540     /** reads until the passed string is encountered. */
541     private final void readChars(boolean p, String match, boolean entities) throws IOException, Exn {
542         int ref;
543         char[] end = match.toCharArray();
544
545         for (boolean more = true; more;) {
546             if (!buffer(1)) return;
547
548             buf: for (ref = 0; ref < len; ref++) {
549                 switch (buf[off+ref]) {
550                     case '\r': // windows or macos9 newline
551                         // normalise and process
552                         buf[off+ref] = '\n'; ref++;
553                         if (p) characters(buf, off, ref);
554                         off += ref; len -= ref; ref = -1;
555                         line++; col = 1;
556
557                         // windows double-char newline; skip the next char
558                         if (!buffer(1)) return;
559                         if (buf[off] == '\n') { off++; len--; }
560                         break;
561
562                     case '\n': // unix newline
563                         ref++;
564                         if (p) characters(buf, off, ref);
565                         off += ref; len -= ref; ref = -1;
566                         line++; col = 1;
567                         break;
568
569                     case '&':  // entity
570                         if (entities) {
571                             if (p) {
572                                 if (ref > 0) characters(buf, off, ref);
573                                 off += ref; len -= ref; ref = -1;
574                                 readEntity();
575                             }
576                             break;
577                         }
578
579                     default:
580                         if (!buffer(ref+end.length)) continue buf;
581                         for (int i=0; end.length > i; i++) if (end[i] != buf[off+ref+i]) continue buf;
582                         more = false;
583                         break buf;
584                 }
585             }
586
587             if (p && ref > 0) characters(buf, off, ref);
588             off += ref; len -= ref; col += ref;
589         }
590     }
591
592     /**
593      * reads until a <tt>&#60;</tt> symbol is encountered
594      * @param p If true call the characters(char[],int,int) funciton for the processed characters 
595      */
596     private final void readChars(boolean p) throws IOException, Exn {
597         int ref;
598
599         for (boolean more = true; more;) {
600             if (!buffer(1)) return;
601
602             buf: for (ref = 0; ref < len; ref++) {
603                 switch (buf[off+ref]) {
604                     case '\r': // windows or macos9 newline
605                         // normalise and process
606                         buf[off+ref] = '\n'; ref++;
607                         if (p) characters(buf, off, ref);
608                         off += ref; len -= ref; ref = -1;
609                         line++; col = 1;
610
611                         // windows double-char newline; skip the next char
612                         if (!buffer(1)) return;
613                         if (buf[off] == '\n') { off++; len--; }
614                         break;
615
616                     case '\n': // unix newline
617                         ref++;
618                         if (p) characters(buf, off, ref);
619                         off += ref; len -= ref; ref = -1;
620                         line++; col = 1;
621                         break;
622
623                     case '&':  // entity
624                         if (p) {
625                             if (ref > 0) characters(buf, off, ref);
626                             off += ref; len -= ref; ref = -1;
627                             readEntity();
628                         }
629                         break;
630
631                     case '<':  // end of chars section
632                         more = false;
633                         break buf;
634                 }
635             }
636
637             if (p && ref > 0) characters(buf, off, ref);
638             off += ref; len -= ref; col += ref;
639         }
640     }
641
642     /** reads until a non-whitespace symbol is encountered */
643     private final void readWhitespace() throws IOException, Exn {
644         int ref;
645
646         for (boolean more = true; more;) {
647             if (!buffer(1)) return;
648
649             buf: for (ref = 0; ref < len; ref++) {
650                 switch (buf[off+ref]) {
651                     case '\r': // windows or macos9 newline
652                         // normalise and process
653                         buf[off+ref] = '\n';
654                         whitespace(buf, off, ++ref);
655                         off += ref; len -= ref; ref = -1;
656                         line++; col = 1;
657
658                         // windows double-char newline; skip the next char
659                         if (!buffer(1)) return;
660                         if (buf[off] == '\n') { off++; len--; }
661                         break;
662
663                     case '\n': // unix newline
664                         whitespace(buf, off, ++ref);
665                         off += ref; len -= ref; ref = -1;
666                         line++; col = 1;
667                         break;
668
669                     case ' ':  // space
670                     case '\t': // tab
671                         break;
672
673                     default:   // end of whitespace
674                         more = false;
675                         break buf;
676                 }
677             }
678
679             off += ref; len -= ref; col += ref;
680         }
681     }
682
683     /**
684      * attempt to fill the buffer.
685      *
686      * @param min Minimum number of characters to read (even if we have to block to do it).
687      * @return return false if min can't be reached.
688      */
689     private final boolean buffer(int min) throws IOException {
690         if (len > min) return true;
691
692         if (buf.length - (off+len) >= min) {
693             // plenty of space left on the end of the buffer
694         } else if (off >= min) {
695             // moving offset data to start will leave enough free space on the end
696             System.arraycopy(buf, off, buf, 0, len); 
697             base += off;
698             off = 0;
699         } else {
700             // buffer size will have to be increased
701             char[] newbuf = new char[buf.length * 2];
702             System.arraycopy(buf, off, newbuf, 0, len);
703             buf = newbuf;
704             base += off;
705             off = 0;
706         }
707
708         while (min > len) {
709             int newlen = in.read(buf, off+len, buf.length-(off+len));
710             if (newlen < 0) return false; 
711             len += newlen;
712         }
713
714         return true;
715     }
716
717
718     /////////////////////////////////////////////////////////////////////////////////////////////
719     // Abstract SAX-Like Interface
720     /////////////////////////////////////////////////////////////////////////////////////////////
721
722     /**
723      * Called when the start of an element is processed.
724      *
725      * <p><b>DO NOT</b> store a reference to the Element object, as
726      * they are reused by XML Parser.</p>
727      */ 
728     public abstract void startElement(Element e) throws Exn;
729
730     /**
731      * Represents up to a line of character data. 
732      *
733      * <p>Newlines are all normalised to the Unix \n as per the XML Spec,
734      * and a newline will only appear as the last character in the passed
735      * array segment.</p>
736      *
737      * <p>XML.getLine() and XML.getCol() report the position at the
738      * beginning of this character segment, which can be processed in a
739      * line-by-line fashion due to the above newline restriction.</p>
740      */
741     public abstract void characters(char[] ch, int start, int length) throws Exn, IOException;
742
743     /** Represents up to a line of ignorable whitespace. */
744     public abstract void whitespace(char[] ch, int start, int length) throws Exn, IOException;
745
746     /** Represents the end of an Element. */
747     public abstract void endElement(Element e) throws Exn, IOException;
748
749
750     /////////////////////////////////////////////////////////////////////////////////////////////
751     // Inner Classes for Parser Support
752     /////////////////////////////////////////////////////////////////////////////////////////////
753
754     /**
755      * Represents an element in an XML document. Stores a reference to its
756      * parent, forming a one-way linked list.
757      *
758      * Element objects are reused, so client code making use of them must
759      * drop their references after the specific element process function
760      * has returned.
761      */
762     public static final class Element {
763
764         private static final int DEFAULT_ATTR_SIZE = 10;
765
766         protected Element parent = null;
767
768         protected String uri = null;
769         protected String localName = null;
770         protected String qName = null;
771         protected String prefix = null;
772
773         protected Hash urimap = new Hash(3,3);
774
775         protected String[] keys = new String[DEFAULT_ATTR_SIZE];
776         protected String[] vals = new String[DEFAULT_ATTR_SIZE];
777         protected String[] uris = new String[DEFAULT_ATTR_SIZE];
778         protected int len = 0;
779
780         protected Exn[] errors = new Exn[] {};
781
782         /** Parent of current element. */
783         public Element getParent() { return parent; }
784
785         /** Qualified Name of current element.  XML Namespace Spec 14-Jan-1999 [6] */
786         public String getQName() { return qName; }
787
788         /** LocalPart of current element. XML Namespace Spec 14-Jan-1999 [8] */
789         public String getLocalName() { return localName; }
790
791         /** Prefix of current element. Substring of qName. XML Namespace Spec 14-Jan-1999 [7] */
792         public String getPrefix() { return prefix; }
793
794         // HACK
795         public Hash getUriMap() {
796             Hash map = new Hash();
797             for (Element e = this; e != null; e = e.getParent()) {
798                 java.util.Enumeration en = e.urimap.keys();
799                 while(en.hasMoreElements()) {
800                     String key = (String)en.nextElement();
801                     String val = getUri(key);
802                     map.put(key, val);
803                 }
804             }
805             return map;
806         }
807
808         /** URI of current tag. XML Namespace Spec 14-Jan-1999 section 1 */
809         public String getUri() { return getUri(prefix); }
810
811         /** URI of a given prefix. Never returns null, instead gives "". */
812         public String getUri(String p) {
813             String ret = null;
814             for (Element e = this; e != null && ret == null; e = e.getParent()) {
815                 ret = (String)e.urimap.get(p == null ? "" : p);
816             }
817             return ret == null ? "" : ret;
818         }
819
820         /** An array of attribute names. */
821         public String getAttrKey(int pos) { return len > pos ? keys[pos] : null; }
822
823         /** An array of attribute values. */
824         public String getAttrVal(int pos) { return len > pos ? vals[pos] : null; }
825
826         /** An array of attribute uris. */
827         public String getAttrUri(int pos) { return len > pos ? uris[pos] : null; }
828
829         /** Current number of attributes in the element. */
830         public int getAttrLen() { return len; }
831
832         /** Poor performance, but easier to use when speed is not a concern */
833         public Hash getAttrHash() {
834             Hash ret = new Hash(getAttrLen() * 2, 3);
835             for(int i=0; i<len; i++)
836                 ret.put(getAttrKey(i), getAttrVal(i));
837             return ret;
838         }
839
840         /** Poor performance, but easier to use */
841         public String getAttrVal(String key) {
842             for(int i=0; i<len; i++) if (keys[i].equals(key)) return vals[i];
843             return null;
844         }
845
846         /** An array of non-fatal errors related to this element. */
847         public Exn[] getErrors() { return errors; }
848
849         protected Element() { }
850
851         /** Add (replace if exists in current element) a Namespace prefix/uri map. */
852         public void addUri(String name, String value) {
853             urimap.put(name, value);
854         }
855
856         /** Add an attribute. */
857         protected void addAttr(String key, String val, String uri) {
858             if (len == keys.length) {
859                 // increase the size of the attributes arrays
860                 String[] newkeys = new String[keys.length*2];
861                 String[] newvals = new String[vals.length*2];
862                 String[] newuris = new String[uris.length*2];
863                 System.arraycopy(keys, 0, newkeys, 0, keys.length);
864                 System.arraycopy(vals, 0, newvals, 0, vals.length);
865                 System.arraycopy(uris, 0, newuris, 0, uris.length);
866                 keys = newkeys; vals = newvals; uris = newuris;
867             }
868
869             keys[len] = key;
870             vals[len] = val;
871             uris[len] = uri;
872             len++;
873         }
874
875         /** Add an error. */
876         protected void addError(Exn e) {
877             // it doesn't really matter about continually expanding the array, as this case is quite rare
878             Exn[] newe = new Exn[errors.length+1];
879             System.arraycopy(errors, 0, newe, 0, errors.length);
880             newe[errors.length] = e;
881             errors = newe;
882         }
883
884         /** Empty out all the data from the Element. */
885         protected void clear() {
886             parent = null;
887             uri = localName = qName = prefix = null;
888             urimap.clear();
889
890             if (keys.length != vals.length || vals.length != uris.length) {
891                 keys = new String[DEFAULT_ATTR_SIZE];
892                 vals = new String[DEFAULT_ATTR_SIZE];
893                 uris = new String[DEFAULT_ATTR_SIZE];
894             } else {
895                 for (int i=0; keys.length > i; i++) { keys[i] = null; vals[i] = null; uris[i] = null; };
896             }
897             len = 0;
898
899             errors = new Exn[] {};
900         }
901     }
902
903     /** Parse or Structural Error */
904     public static class Exn extends Exception {
905         /** Violation of Markup restrictions in XML Specification - Fatal Error */
906         public static final int MARKUP = 1;
907
908         /** Well-Formedness Constraint Violation - Fatal Error */
909         public static final int WFC = 2;
910
911         /** Namespace Constraint Violation - Recoverable Error */
912         public static final int NC = 3;
913
914         /** Schema Violation - Fatal Error */
915         public static final int SCHEMA = 4;
916
917         private String error;
918         private int type;
919         private int line;
920         private int col;
921
922         public Exn(String e) { this(e, MARKUP, -1, -1); }
923
924         public Exn(String e, int type, int line, int col) {
925             this.error = e;
926             this.type  = type;
927             this.line  = line;
928             this.col   = col;
929         }
930
931         public int getType() { return this.type; }
932         public int getLine() { return this.line; }
933         public int getCol()  { return this.col;  }
934         public String getMessage() { return this.error + (line >= 0 && col >= 0 ? " at " + line + ":" + col: ""); }
935     }
936
937
938     /////////////////////////////////////////////////////////////////////////////////////////////
939     // Static Support Functions for the XML Specification 
940     /////////////////////////////////////////////////////////////////////////////////////////////
941  
942     // attempt to avoid these functions unless you *expect* the input to fall in the given range.
943
944     /** First Character of Name - XML Specification 1.0 [5] */
945     private static final boolean Name(char c) {
946         return BaseCharAscii(c) || c == '_' || c == ':' || Letter(c);
947     } 
948
949     /** NameChar - XML Specification 1.0 [4] */
950     private static final boolean NameChar(char c) {
951         return BaseCharAscii(c) || c == '.' || c == '-' || c == '_' || c == ':'
952             || Digit(c) || Letter(c) || Extender(c); // TODO: || CombiningChar(c);
953     } 
954
955     /** BaseChar - XMl Specification 1.0 [84] */
956     private static final boolean Letter(char c) {
957         return BaseChar(c) || Ideographic(c);
958     }
959
960     /** Elements of BaseChar that exist in ASCII. */
961     private static final boolean BaseCharAscii(char c) {
962         return (c >= '\u0041' && c <= '\u005A') || (c >= '\u0061' && c <= '\u007A');
963     }
964
965     /** Char - XML Specification 1.0 [2] */
966     private static final boolean Char(char c) {
967         // u000A == r and u000D == n, but the javac compiler can't handle the \ u form
968         return c == '\u0009' || c == '\r' || c == '\n'
969             || (c >= '\u0020' && c <= '\uD7FF')
970             || (c >= '\uE000' && c <= '\uFFFD');
971     }
972
973     /** BaseChar - XML Specification 1.0 [85] */
974     private static final boolean BaseChar(char c) {
975         return  BaseCharAscii(c) || (c >= '\u00C0' && c <= '\u00D6')
976             || (c >= '\u00D8' && c <= '\u00F6') || (c >= '\u00F8' && c <= '\u00FF') || (c >= '\u0100' && c <= '\u0131')
977             || (c >= '\u0134' && c <= '\u013E') || (c >= '\u0141' && c <= '\u0148') || (c >= '\u014A' && c <= '\u017E')
978             || (c >= '\u0180' && c <= '\u01C3') || (c >= '\u01CD' && c <= '\u01F0') || (c >= '\u01F4' && c <= '\u01F5')
979             || (c >= '\u01FA' && c <= '\u0217') || (c >= '\u0250' && c <= '\u02A8') || (c >= '\u02BB' && c <= '\u02C1')
980             || (c == '\u0386')                  || (c >= '\u0388' && c <= '\u038A') || (c == '\u038C')
981             || (c >= '\u038E' && c <= '\u03A1') || (c >= '\u03A3' && c <= '\u03CE') || (c >= '\u03D0' && c <= '\u03D6')
982             || (c == '\u03DA')                  || (c == '\u03DC')                  || (c == '\u03DE')
983             || (c == '\u03E0')
984             || (c >= '\u03E2' && c <= '\u03F3') || (c >= '\u0401' && c <= '\u040C') || (c >= '\u040E' && c <= '\u044F')
985             || (c >= '\u0451' && c <= '\u045C') || (c >= '\u045E' && c <= '\u0481') || (c >= '\u0490' && c <= '\u04C4')
986             || (c >= '\u04C7' && c <= '\u04C8') || (c >= '\u04CB' && c <= '\u04CC') || (c >= '\u04D0' && c <= '\u04EB')
987             || (c >= '\u04EE' && c <= '\u04F5') || (c >= '\u04F8' && c <= '\u04F9') || (c >= '\u0531' && c <= '\u0556')
988             || (c == '\u0559')
989             || (c >= '\u0561' && c <= '\u0586') || (c >= '\u05D0' && c <= '\u05EA') || (c >= '\u05F0' && c <= '\u05F2')
990             || (c >= '\u0621' && c <= '\u063A') || (c >= '\u0641' && c <= '\u064A') || (c >= '\u0671' && c <= '\u06B7')
991             || (c >= '\u06BA' && c <= '\u06BE') || (c >= '\u06C0' && c <= '\u06CE') || (c >= '\u06D0' && c <= '\u06D3')
992             || (c == '\u06D5')
993             || (c >= '\u06E5' && c <= '\u06E6') || (c >= '\u0905' && c <= '\u0939')
994             || (c == '\u093D')
995             || (c >= '\u0958' && c <= '\u0961') || (c >= '\u0985' && c <= '\u098C') || (c >= '\u098F' && c <= '\u0990')
996             || (c >= '\u0993' && c <= '\u09A8') || (c >= '\u09AA' && c <= '\u09B0')
997             || (c == '\u09B2')
998             || (c >= '\u09B6' && c <= '\u09B9') || (c >= '\u09DF' && c <= '\u09E1') || (c >= '\u09F0' && c <= '\u09F1')
999             || (c >= '\u0A05' && c <= '\u0A0A') || (c >= '\u0A0F' && c <= '\u0A10') || (c >= '\u0A13' && c <= '\u0A28')
1000             || (c >= '\u0A2A' && c <= '\u0A30') || (c >= '\u0A32' && c <= '\u0A33') || (c >= '\u0A35' && c <= '\u0A36')
1001             || (c >= '\u0A38' && c <= '\u0A39') || (c >= '\u0A59' && c <= '\u0A5C')
1002             || (c == '\u0A5E')
1003             || (c >= '\u0A72' && c <= '\u0A74') || (c >= '\u0A85' && c <= '\u0A8B')
1004             || (c == '\u0A8D')
1005             || (c >= '\u0A8F' && c <= '\u0A91') || (c >= '\u0A93' && c <= '\u0AA8') || (c >= '\u0AAA' && c <= '\u0AB0') 
1006             || (c >= '\u0AB2' && c <= '\u0AB3') || (c >= '\u0AB5' && c <= '\u0AB9') 
1007             || (c == '\u0ABD') 
1008             || (c == '\u0AE0') 
1009             || (c >= '\u0B05' && c <= '\u0B0C') || (c >= '\u0B0F' && c <= '\u0B10') || (c >= '\u0B13' && c <= '\u0B28') 
1010             || (c >= '\u0B2A' && c <= '\u0B30') || (c >= '\u0B32' && c <= '\u0B33') || (c >= '\u0B36' && c <= '\u0B39') 
1011             || (c == '\u0B3D') 
1012             || (c >= '\u0B5C' && c <= '\u0B5D') || (c >= '\u0B5F' && c <= '\u0B61') || (c >= '\u0B85' && c <= '\u0B8A') 
1013             || (c >= '\u0B8E' && c <= '\u0B90') || (c >= '\u0B92' && c <= '\u0B95') || (c >= '\u0B99' && c <= '\u0B9A') 
1014             || (c == '\u0B9C') 
1015             || (c >= '\u0B9E' && c <= '\u0B9F') || (c >= '\u0BA3' && c <= '\u0BA4') || (c >= '\u0BA8' && c <= '\u0BAA') 
1016             || (c >= '\u0BAE' && c <= '\u0BB5') || (c >= '\u0BB7' && c <= '\u0BB9') || (c >= '\u0C05' && c <= '\u0C0C') 
1017             || (c >= '\u0C0E' && c <= '\u0C10') || (c >= '\u0C12' && c <= '\u0C28') || (c >= '\u0C2A' && c <= '\u0C33') 
1018             || (c >= '\u0C35' && c <= '\u0C39') || (c >= '\u0C60' && c <= '\u0C61') || (c >= '\u0C85' && c <= '\u0C8C') 
1019             || (c >= '\u0C8E' && c <= '\u0C90') || (c >= '\u0C92' && c <= '\u0CA8') || (c >= '\u0CAA' && c <= '\u0CB3') 
1020             || (c >= '\u0CB5' && c <= '\u0CB9') 
1021             || (c == '\u0CDE') 
1022             || (c >= '\u0CE0' && c <= '\u0CE1') || (c >= '\u0D05' && c <= '\u0D0C') || (c >= '\u0D0E' && c <= '\u0D10') 
1023             || (c >= '\u0D12' && c <= '\u0D28') || (c >= '\u0D2A' && c <= '\u0D39') || (c >= '\u0D60' && c <= '\u0D61') 
1024             || (c >= '\u0E01' && c <= '\u0E2E') 
1025             || (c == '\u0E30') 
1026             || (c >= '\u0E32' && c <= '\u0E33') || (c >= '\u0E40' && c <= '\u0E45') || (c >= '\u0E81' && c <= '\u0E82') 
1027             || (c == '\u0E84') 
1028             || (c >= '\u0E87' && c <= '\u0E88') 
1029             || (c == '\u0E8A') 
1030             || (c == '\u0E8D') 
1031             || (c >= '\u0E94' && c <= '\u0E97') || (c >= '\u0E99' && c <= '\u0E9F') || (c >= '\u0EA1' && c <= '\u0EA3') 
1032             || (c == '\u0EA5') 
1033             || (c == '\u0EA7') 
1034             || (c >= '\u0EAA' && c <= '\u0EAB') || (c >= '\u0EAD' && c <= '\u0EAE') 
1035             || (c == '\u0EB0') 
1036             || (c >= '\u0EB2' && c <= '\u0EB3') 
1037             || (c == '\u0EBD') 
1038             || (c >= '\u0EC0' && c <= '\u0EC4') || (c >= '\u0F40' && c <= '\u0F47') || (c >= '\u0F49' && c <= '\u0F69') 
1039             || (c >= '\u10A0' && c <= '\u10C5') || (c >= '\u10D0' && c <= '\u10F6') 
1040             || (c == '\u1100') 
1041             || (c >= '\u1102' && c <= '\u1103') || (c >= '\u1105' && c <= '\u1107') 
1042             || (c == '\u1109') 
1043             || (c >= '\u110B' && c <= '\u110C') || (c >= '\u110E' && c <= '\u1112') 
1044             || (c == '\u113C') 
1045             || (c == '\u113E') 
1046             || (c == '\u1140') 
1047             || (c == '\u114C') 
1048             || (c == '\u114E') 
1049             || (c == '\u1150') 
1050             || (c >= '\u1154' && c <= '\u1155') 
1051             || (c == '\u1159') 
1052             || (c >= '\u115F' && c <= '\u1161') 
1053             || (c == '\u1163') 
1054             || (c == '\u1165') 
1055             || (c == '\u1167') 
1056             || (c == '\u1169') 
1057             || (c >= '\u116D' && c <= '\u116E') || (c >= '\u1172' && c <= '\u1173') 
1058             || (c == '\u1175') 
1059             || (c == '\u119E') 
1060             || (c == '\u11A8') 
1061             || (c == '\u11AB') 
1062             || (c >= '\u11AE' && c <= '\u11AF') || (c >= '\u11B7' && c <= '\u11B8') 
1063             || (c == '\u11BA') 
1064             || (c >= '\u11BC' && c <= '\u11C2') 
1065             || (c == '\u11EB') 
1066             || (c == '\u11F0') 
1067             || (c == '\u11F9') 
1068             || (c >= '\u1E00' && c <= '\u1E9B') || (c >= '\u1EA0' && c <= '\u1EF9') || (c >= '\u1F00' && c <= '\u1F15') 
1069             || (c >= '\u1F18' && c <= '\u1F1D') || (c >= '\u1F20' && c <= '\u1F45') || (c >= '\u1F48' && c <= '\u1F4D') 
1070             || (c >= '\u1F50' && c <= '\u1F57') 
1071             || (c == '\u1F59') 
1072             || (c == '\u1F5B') 
1073             || (c == '\u1F5D') 
1074             || (c >= '\u1F5F' && c <= '\u1F7D') || (c >= '\u1F80' && c <= '\u1FB4') || (c >= '\u1FB6' && c <= '\u1FBC') 
1075             || (c == '\u1FBE') 
1076             || (c >= '\u1FC2' && c <= '\u1FC4') || (c >= '\u1FC6' && c <= '\u1FCC') || (c >= '\u1FD0' && c <= '\u1FD3') 
1077             || (c >= '\u1FD6' && c <= '\u1FDB') || (c >= '\u1FE0' && c <= '\u1FEC') || (c >= '\u1FF2' && c <= '\u1FF4') 
1078             || (c >= '\u1FF6' && c <= '\u1FFC') 
1079             || (c == '\u2126') 
1080             || (c >= '\u212A' && c <= '\u212B') 
1081             || (c == '\u212E') 
1082             || (c >= '\u2180' && c <= '\u2182') || (c >= '\u3041' && c <= '\u3094') || (c >= '\u30A1' && c <= '\u30FA') 
1083             || (c >= '\u3105' && c <= '\u312C') || (c >= '\uAC00' && c <= '\uD7A3');
1084     }
1085
1086     /** BaseChar - XMl Specification 1.0 [86] */
1087     private static final boolean Ideographic(char c) {
1088         return (c >= '\u4E00' && c <= '\u9FA5') || c == '\u3007' || (c >= '\u3021' && c <= '\u3029');
1089     }
1090  
1091     /** CombiningChar - XMl Specification 1.0 [87] */
1092     /*private static final boolean CombiningChar(char c) {
1093         return (c >= '\u0300' && c <= '\u0345')
1094             || (c >= '\u0360' && c <= '\u0361') || (c >= '\u0483' && c <= '\u0486') || (c >= '\u0591' && c <= '\u05A1') 
1095             || (c >= '\u05A3' && c <= '\u05B9') || (c >= '\u05BB' && c <= '\u05BD') 
1096             || (c == '\u05BF') 
1097             || (c >= '\u05C1' && c <= '\u05C2') 
1098             || (c == '\u05C4') 
1099             || (c >= '\u064B' && c <= '\u0652') 
1100             || (c == '\u0670') 
1101             || (c >= '\u06D6' && c <= '\u06DC') || (c >= '\u06DD' && c <= '\u06DF') || (c >= '\u06E0' && c <= '\u06E4') 
1102             || (c >= '\u06E7' && c <= '\u06E8') || (c >= '\u06EA' && c <= '\u06ED') || (c >= '\u0901' && c <= '\u0903') 
1103             || (c == '\u093C') 
1104             || (c >= '\u093E' && c <= '\u094C') 
1105             || (c == '\u094D') 
1106             || (c >= '\u0951' && c <= '\u0954') || (c >= '\u0962' && c <= '\u0963') || (c >= '\u0981' && c <= '\u0983') 
1107             || (c == '\u09BC') 
1108             || (c == '\u09BE') 
1109             || (c == '\u09BF') 
1110             || (c >= '\u09C0' && c <= '\u09C4') || (c >= '\u09C7' && c <= '\u09C8') || (c >= '\u09CB' && c <= '\u09CD') 
1111             || (c == '\u09D7') 
1112             || (c >= '\u09E2' && c <= '\u09E3') 
1113             || (c == '\u0A02') 
1114             || (c == '\u0A3C') 
1115             || (c == '\u0A3E') 
1116             || (c == '\u0A3F') 
1117             || (c >= '\u0A40' && c <= '\u0A42') || (c >= '\u0A47' && c <= '\u0A48') || (c >= '\u0A4B' && c <= '\u0A4D') 
1118             || (c >= '\u0A70' && c <= '\u0A71') || (c >= '\u0A81' && c <= '\u0A83') 
1119             || (c == '\u0ABC') 
1120             || (c >= '\u0ABE' && c <= '\u0AC5') || (c >= '\u0AC7' && c <= '\u0AC9') || (c >= '\u0ACB' && c <= '\u0ACD') 
1121             || (c >= '\u0B01' && c <= '\u0B03') 
1122             || (c == '\u0B3C') 
1123             || (c >= '\u0B3E' && c <= '\u0B43') || (c >= '\u0B47' && c <= '\u0B48') || (c >= '\u0B4B' && c <= '\u0B4D') 
1124             || (c >= '\u0B56' && c <= '\u0B57') || (c >= '\u0B82' && c <= '\u0B83') || (c >= '\u0BBE' && c <= '\u0BC2') 
1125             || (c >= '\u0BC6' && c <= '\u0BC8') || (c >= '\u0BCA' && c <= '\u0BCD') 
1126             || (c == '\u0BD7') 
1127             || (c >= '\u0C01' && c <= '\u0C03') || (c >= '\u0C3E' && c <= '\u0C44') || (c >= '\u0C46' && c <= '\u0C48') 
1128             || (c >= '\u0C4A' && c <= '\u0C4D') || (c >= '\u0C55' && c <= '\u0C56') || (c >= '\u0C82' && c <= '\u0C83') 
1129             || (c >= '\u0CBE' && c <= '\u0CC4') || (c >= '\u0CC6' && c <= '\u0CC8') || (c >= '\u0CCA' && c <= '\u0CCD') 
1130             || (c >= '\u0CD5' && c <= '\u0CD6') || (c >= '\u0D02' && c <= '\u0D03') || (c >= '\u0D3E' && c <= '\u0D43') 
1131             || (c >= '\u0D46' && c <= '\u0D48') || (c >= '\u0D4A' && c <= '\u0D4D') 
1132             || (c == '\u0D57') 
1133             || (c == '\u0E31') 
1134             || (c >= '\u0E34' && c <= '\u0E3A') || (c >= '\u0E47' && c <= '\u0E4E') 
1135             || (c == '\u0EB1') 
1136             || (c >= '\u0EB4' && c <= '\u0EB9') || (c >= '\u0EBB' && c <= '\u0EBC') || (c >= '\u0EC8' && c <= '\u0ECD') 
1137             || (c >= '\u0F18' && c <= '\u0F19') 
1138             || (c == '\u0F35') 
1139             || (c == '\u0F37') 
1140             || (c == '\u0F39') 
1141             || (c == '\u0F3E') 
1142             || (c == '\u0F3F') 
1143             || (c >= '\u0F71' && c <= '\u0F84') || (c >= '\u0F86' && c <= '\u0F8B') || (c >= '\u0F90' && c <= '\u0F95') 
1144             || (c == '\u0F97') 
1145             || (c >= '\u0F99' && c <= '\u0FAD') || (c >= '\u0FB1' && c <= '\u0FB7') 
1146             || (c == '\u0FB9') 
1147             || (c >= '\u20D0' && c <= '\u20DC') 
1148             || (c == '\u20E1') 
1149             || (c >= '\u302A' && c <= '\u302F') 
1150             || (c == '\u3099') 
1151             || (c == '\u309A');
1152     }*/
1153
1154     /** Digit - XMl Specification 1.0 [88] */
1155     private static final boolean Digit(char c) {
1156         return (c >= '\u0030' && c <= '\u0039') || (c >= '\u0660' && c <= '\u0669') || (c >= '\u06F0' && c <= '\u06F9')
1157             || (c >= '\u0966' && c <= '\u096F') || (c >= '\u09E6' && c <= '\u09EF') || (c >= '\u0A66' && c <= '\u0A6F')
1158             || (c >= '\u0AE6' && c <= '\u0AEF') || (c >= '\u0B66' && c <= '\u0B6F') || (c >= '\u0BE7' && c <= '\u0BEF')
1159             || (c >= '\u0C66' && c <= '\u0C6F') || (c >= '\u0CE6' && c <= '\u0CEF') || (c >= '\u0D66' && c <= '\u0D6F')
1160             || (c >= '\u0E50' && c <= '\u0E59') || (c >= '\u0ED0' && c <= '\u0ED9') || (c >= '\u0F20' && c <= '\u0F29');
1161     }
1162
1163     /** Extender - XMl Specification 1.0 [89] */
1164     private static final boolean Extender(char c) {
1165         return c == '\u00B7' || c == '\u02D0' || c == '\u02D1' || c == '\u0387'
1166             || c == '\u0640' || c == '\u0E46' || c == '\u0EC6' || c == '\u3005'
1167             || (c >= '\u3031' && c <= '\u3035') || (c >= '\u309D' && c <= '\u309E') || (c >= '\u30FC' && c <= '\u30FE');
1168     }
1169
1170     /** Whitespace - XML Specification 1.0 [3] */
1171     private static final boolean S(char c) {
1172         return c == '\u0020' || c == '\u0009' || c == '\r' || c == '\n';
1173     }
1174 }