19,9 → 19,11 |
FileInputStream in = new FileInputStream(args[0]); |
|
try { |
XmlDocument doc = new XmlDocument(); |
XmlListenerImpl listener = new XmlListenerImpl(); |
XmlDocument doc = new XmlDocument(listener); |
|
doc.parse(in); |
System.out.println(listener.count + " elements found"); |
} |
catch(XmlException ex) { |
System.out.println(ex); |
30,11 → 32,47 |
in.close(); |
} |
|
System.err.println("Elapsed: " + (System.nanoTime() - startTime) / 1000000 + "ms"); |
System.out.println("Elapsed: " + (System.nanoTime() - startTime) / 1000000 + "ms"); |
} |
} |
|
// -------------------------------------------------------------------------------------------------------------------- |
class XmlListenerImpl |
implements XmlListener |
{ |
public long count = 0; |
|
private XmlDocument document; |
|
public void init(XmlDocument document) |
throws XmlException, IOException |
{ |
this.document = document; |
} |
|
public void processElementBegin(XmlElement element) |
throws XmlException, IOException |
{ |
++count; |
} |
|
public void processElementEnd(XmlElement element) |
throws XmlException, IOException |
{ |
} |
|
public void processCharData(XmlSelection sel) |
throws XmlException, IOException |
{ |
} |
|
public void processCData(XmlSelection sel) |
throws XmlException, IOException |
{ |
} |
} |
|
// -------------------------------------------------------------------------------------------------------------------- |
class XmlException |
extends Exception |
{ |
71,22 → 109,48 |
} |
|
// -------------------------------------------------------------------------------------------------------------------- |
interface XmlListener |
{ |
public void init(XmlDocument document) |
throws XmlException, IOException; |
|
public void processElementBegin(XmlElement element) |
throws XmlException, IOException; |
|
public void processElementEnd(XmlElement element) |
throws XmlException, IOException; |
|
public void processCharData(XmlSelection sel) |
throws XmlException, IOException; |
|
public void processCData(XmlSelection sel) |
throws XmlException, IOException; |
} |
|
// -------------------------------------------------------------------------------------------------------------------- |
class XmlElement |
{ |
public boolean isEmpty = false; |
public boolean isEmpty = false; |
public XmlSelection elementSel; |
public XmlSelection nameSel; |
} |
|
// -------------------------------------------------------------------------------------------------------------------- |
class XmlDocument |
{ |
private XmlListener listener; |
private InputStream in; |
private byte[] buf = new byte[2048]; // (buf.length % 16 == 0) |
private int bufLen; |
private int bufPos; |
private long bufOffset; |
private long line; |
private long linePos; |
private int level; |
|
public XmlDocument(XmlListener listener) |
{ |
this.listener = listener; |
} |
|
public void parse(InputStream in) |
throws XmlException, IOException |
{ |
98,16 → 162,16 |
this.bufLen = 0; |
this.bufPos = 0; |
this.bufOffset = 0; |
this.line = 1; |
this.linePos = 0; |
this.level = 0; |
|
if(listener != null) listener.init(this); |
|
if(parseProlog()) { |
parseElement(); |
|
while(parseMisc()); |
/* |
if(this.pos < this.len) throwException("trash at end of text"); |
*/ |
|
if(bufPos < bufLen) throwException("EoF expected"); |
} |
} |
|
114,7 → 178,7 |
private void throwException(String message) |
throws XmlException, IOException |
{ |
throw new XmlException(message, line, /*linePos*/ bufPos); |
throw new XmlException(message, 1, bufPos); |
} |
|
private void log(String message) |
122,12 → 186,17 |
System.out.println(message); |
} |
|
private String toString(XmlSelection sel) |
public String selectionToString(XmlSelection sel) |
throws IOException |
{ |
return new String(buf, (int)(sel.begin - bufOffset), (int)sel.getLength(), "UTF-8"); |
} |
|
public int getLevel() |
{ |
return level; |
} |
|
private void saveSelBegin(XmlSelection sel) |
{ |
sel.begin = bufOffset + bufPos; |
142,7 → 211,6 |
throws XmlException, IOException |
{ |
if(bufPos + count >= bufLen) { |
//log("ensureNext start " + bufPos + " " + count); |
if(bufLen == 0) { |
// read full buffer at begin |
bufLen = in.read(buf); |
183,7 → 251,6 |
private boolean skipSpaces() |
throws XmlException, IOException |
{ |
//log("skipSpaces begin " + bufPos); |
boolean found = false; |
|
for(;;) { |
199,7 → 266,6 |
} |
} |
|
//log("skipSpaces " + found + " " + bufPos); |
return found; |
} |
|
206,13 → 272,10 |
private boolean parseDecl() |
throws XmlException, IOException |
{ |
//log("parseDecl begin " + bufPos); |
|
// begin |
if(!testChar('<') || !testChar('?') || !testChar('x') |
|| !testChar('m') || !testChar('l')) |
{ |
//log("parseDecl no 'xml' " + bufPos); |
return false; |
} |
|
229,7 → 292,6 |
throwException("end of XML declaration expected"); |
} |
|
//log("parseDecl ok " + bufPos); |
return true; |
} |
|
244,7 → 306,6 |
private boolean parseName(XmlSelection sel) |
throws XmlException, IOException |
{ |
//log("parseName begin " + bufPos); |
saveSelBegin(sel); |
int start = bufPos; |
|
257,7 → 318,6 |
++bufPos; |
} |
else { |
//log("parseName not a name " + bufPos); |
bufPos = start; |
return false; |
} |
279,7 → 339,6 |
} |
saveSelEnd(sel); |
|
//log("parseName ok " + bufPos); |
return true; |
} |
|
302,10 → 361,8 |
private boolean parseAttribute(XmlSelection selName, XmlSelection selValue) |
throws XmlException, IOException |
{ |
//log("parseAttribute begin " + bufPos); |
// name |
if(!parseName(selName)) { |
//log("parseAttribute no name " + bufPos); |
return false; |
} |
|
342,7 → 399,6 |
|
// FIXME check '[WFC: No External Entity References]' |
|
//log("parseAttribute ok " + bufPos); |
return true; |
} |
|
349,21 → 405,16 |
private boolean parseMisc() |
throws XmlException, IOException |
{ |
//log("parseMisc begin " + bufPos); |
if(parseComment()) { |
//log("parseMisc comment ok " + bufPos); |
return true; |
} |
else if(parseProcessInstruction()) { |
//log("parseMisc pi ok " + bufPos); |
return true; |
} |
else if(skipSpaces()) { |
//log("parseMisc spaces ok " + bufPos); |
return true; |
} |
else { |
//log("parseMisc false " + bufPos); |
return false; |
} |
} |
371,13 → 422,10 |
private boolean parseComment() |
throws XmlException, IOException |
{ |
//log("parseComment begin " + bufPos); |
if(bufPos+3 >= bufLen && !ensureNext(4)) { |
//log("parseComment no data " + bufPos); |
return false; |
} |
if(buf[bufPos] != '<' || buf[bufPos+1] != '!' || buf[bufPos+2] != '-' || buf[bufPos+3] != '-') { |
//log("parseComment no signature " + bufPos); |
return false; |
} |
|
390,7 → 438,7 |
if(buf[bufPos] == '-' && buf[bufPos+1] == '-') { |
if(buf[bufPos+2] == '>') { |
bufPos += 3; |
//log("parseComment ok " + bufPos); |
|
return true; |
} |
else { |
418,6 → 466,7 |
|
if(buf[bufPos] == '?' && buf[bufPos+1] == '>') { |
bufPos += 2; |
|
return true; |
} |
} |
426,15 → 475,18 |
private XmlElement parseElement() |
throws XmlException, IOException |
{ |
//log("parseElement begin " + bufPos); |
|
XmlElement element = new XmlElement(); |
element.elementSel = new XmlSelection(); |
saveSelBegin(element.elementSel); |
|
if(!parseStartTag(element)) { |
//log("parseElement no start tag " + bufPos); |
return null; |
} |
|
++level; |
|
if(listener != null) listener.processElementBegin(element); |
|
if(!element.isEmpty) { |
if(!parseTagContent(element)) { |
throwException("cannot parse tag content"); |
443,11 → 495,16 |
XmlSelection selEndName = new XmlSelection(); |
parseEndTag(selEndName); |
|
//if(element.name != selEndName |
//if(!selectionToString(element.nameSel).equals(selectionToString(selEndName))) { |
// throwException("tag names do not match"); |
//} |
} |
|
//log("parseElement ok " + bufPos); |
saveSelEnd(element.elementSel); |
if(listener != null) listener.processElementEnd(element); |
|
--level; |
|
return element; |
} |
|
454,23 → 511,20 |
private boolean parseStartTag(XmlElement element) |
throws XmlException, IOException |
{ |
//log("parseStartTag begin " + bufPos); |
int start = bufPos; |
|
// begin |
if(!testChar('<')) { |
//log("parseStartTag no signature " + bufPos); |
return false; |
} |
|
// name |
XmlSelection sel = new XmlSelection(); |
if(!parseName(sel)) { |
//log("parseStartTag no name " + bufPos); |
element.nameSel = new XmlSelection(); |
if(!parseName(element.nameSel)) { |
bufPos = start; |
|
return false; |
} |
//System.out.print("[" + toString(sel) + "]"); |
|
// attributes |
XmlSelection selName = new XmlSelection(); |
478,9 → 532,7 |
for(;;) { |
if(!skipSpaces()) break; |
if(!parseAttribute(selName, selValue)) break; |
//System.out.print(" [" + toString(selName) + "]=[" + toString(selValue) + "]"); |
} |
//System.out.println(); |
|
// end |
element.isEmpty = testChar('/'); |
488,7 → 540,6 |
throwException("end of tag expected"); |
} |
|
//log("parseStartTag ok " + bufPos); |
return true; |
} |
|
495,7 → 546,6 |
private void parseEndTag(XmlSelection sel) |
throws XmlException, IOException |
{ |
//log("parseEndTag begin " + bufPos); |
// begin |
if(!testChar('<') || !testChar('/')) { |
throwException("cannot find tag end"); |
513,14 → 563,11 |
if(!testChar('>')) { |
throwException("end of tag expected"); |
} |
|
//log("parseEndTag ok " + bufPos); |
} |
|
private boolean parseTagContent(XmlElement element) |
throws XmlException, IOException |
{ |
//log("parseTagContent begin " + bufPos); |
XmlSelection sel = new XmlSelection(); |
for(;;) { |
if(bufPos+1 >= bufLen && !ensureNext(2)) { |
538,29 → 585,31 |
} |
else if(parseCharData(sel)) { |
} |
else { |
throwException("unexpected tag content"); |
} |
|
// FIXME allow 'Reference' here |
} |
|
//log("parseTagContent ok " + bufPos); |
return true; |
} |
|
// FIXME not fully conform the standard |
private boolean parseCharData(XmlSelection sel) |
throws XmlException, IOException |
{ |
//log("parseCharData begin " + bufPos); |
// FIXME allow 'Reference' here |
|
boolean found = false; |
saveSelBegin(sel); |
|
for(;; ++bufPos) { |
if(bufPos >= bufLen && !ensureNext(1)) { |
return true; |
throwException("unexpected EoF"); |
} |
|
byte c = buf[bufPos]; |
if(c == '<') { |
if(c == '<' || c == '&') { |
break; |
} |
|
567,8 → 616,9 |
found = true; |
} |
|
//log("parseCharData " + found + " " + bufPos); |
saveSelEnd(sel); |
if(listener != null) listener.processCharData(sel); |
|
return found; |
} |
|
598,6 → 648,8 |
if(buf[bufPos] == ']' && buf[bufPos+1] == ']' && buf[bufPos+2] == '>') { |
saveSelEnd(sel); |
bufPos += 3; |
if(listener != null) listener.processCData(sel); |
|
return true; |
} |
} |