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,9 → 109,30 |
} |
|
// -------------------------------------------------------------------------------------------------------------------- |
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; |
} |
|
// -------------------------------------------------------------------------------------------------------------------- |
134,18 → 193,8 |
pos += n; |
} |
|
public long getOffset() |
public long getPosition() |
{ |
return offset; |
} |
|
public int getPosition() |
{ |
return pos; |
} |
|
public long getAbsPosition() |
{ |
return (offset + pos); |
} |
|
169,7 → 218,6 |
throws IOException |
{ |
if(pos + count >= len) { |
//log("ensureNext start " + pos + " " + count); |
if(len == 0) { |
// read full buffer at begin |
len = in.read(buf); |
209,24 → 257,29 |
// -------------------------------------------------------------------------------------------------------------------- |
class XmlDocument |
{ |
private XmlListener listener; |
private XmlBuffer buf; |
private long line; |
private long linePos; |
private int level; |
|
public XmlDocument(XmlListener listener) |
{ |
this.listener = listener; |
} |
|
public void parse(InputStream in) |
throws XmlException, IOException |
{ |
this.buf = new XmlBuffer(in); |
this.line = 1; |
this.linePos = 0; |
this.buf = new XmlBuffer(in); |
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(!buf.isEnd()) throwException("EoF expected"); |
} |
} |
|
233,7 → 286,7 |
private void throwException(String message) |
throws XmlException, IOException |
{ |
throw new XmlException(message, line, /*linePos*/ buf.getAbsPosition()); |
throw new XmlException(message, 1, buf.getPosition()); |
} |
|
private void log(String message) |
241,7 → 294,7 |
System.out.println(message); |
} |
|
private String toString(XmlSelection sel) |
public String selectionToString(XmlSelection sel) |
throws IOException |
{ |
return buf.toString(sel.begin, (int)sel.getLength()); |
249,12 → 302,12 |
|
private void saveSelBegin(XmlSelection sel) |
{ |
sel.begin = buf.getAbsPosition(); |
sel.begin = buf.getPosition(); |
} |
|
private void saveSelEnd(XmlSelection sel) |
{ |
sel.end = buf.getAbsPosition(); |
sel.end = buf.getPosition(); |
} |
|
private boolean parseProlog() |
272,7 → 325,6 |
private boolean skipSpaces() |
throws XmlException, IOException |
{ |
//log("skipSpaces begin " + bufPos); |
boolean found = false; |
|
for(;;) { |
289,7 → 341,6 |
} |
} |
|
//log("skipSpaces " + found + " " + bufPos); |
return found; |
} |
|
296,13 → 347,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; |
} |
|
319,7 → 367,6 |
throwException("end of XML declaration expected"); |
} |
|
//log("parseDecl ok " + bufPos); |
return true; |
} |
|
334,7 → 381,6 |
private boolean parseName(XmlSelection sel) |
throws XmlException, IOException |
{ |
//log("parseName begin " + bufPos); |
saveSelBegin(sel); |
|
byte c = buf.cur(); |
342,7 → 388,6 |
buf.toNext(); |
} |
else { |
//log("parseName not a name " + bufPos); |
return false; |
} |
|
363,7 → 408,6 |
} |
saveSelEnd(sel); |
|
//log("parseName ok " + bufPos); |
return true; |
} |
|
382,11 → 426,9 |
private boolean parseAttribute(XmlSelection selName, XmlSelection selValue) |
throws XmlException, IOException |
{ |
//log("parseAttribute begin " + bufPos); |
// name |
buf.mark(); |
if(!parseName(selName)) { |
//log("parseAttribute no name " + bufPos); |
buf.reset(); |
return false; |
} |
424,7 → 466,6 |
|
// FIXME check '[WFC: No External Entity References]' |
|
//log("parseAttribute ok " + bufPos); |
return true; |
} |
|
431,21 → 472,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; |
} |
} |
453,9 → 489,7 |
private boolean parseComment() |
throws XmlException, IOException |
{ |
//log("parseComment begin " + bufPos); |
if(buf.at(0) != '<' || buf.at(1) != '!' || buf.at(2) != '-' || buf.at(3) != '-') { |
//log("parseComment no signature " + bufPos); |
return false; |
} |
|
468,7 → 502,7 |
if(buf.at(0) == '-' && buf.at(1) == '-') { |
if(buf.at(2) == '>') { |
buf.skip(3); |
//log("parseComment ok " + bufPos); |
|
return true; |
} |
else { |
493,6 → 527,7 |
|
if(buf.at(0) == '?' && buf.at(1) == '>') { |
buf.skip(2); |
|
return true; |
} |
} |
501,15 → 536,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"); |
518,11 → 556,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; |
} |
|
529,21 → 572,19 |
private boolean parseStartTag(XmlElement element) |
throws XmlException, IOException |
{ |
//log("parseStartTag begin " + bufPos); |
// begin |
if(!testChar('<')) { |
//log("parseStartTag no signature " + bufPos); |
return false; |
} |
|
// name |
buf.mark(); |
XmlSelection sel = new XmlSelection(); |
if(!parseName(sel)) { |
element.nameSel = new XmlSelection(); |
if(!parseName(element.nameSel)) { |
buf.reset(); |
|
return false; |
} |
//System.out.print("[" + toString(sel) + "]"); |
|
// attributes |
XmlSelection selName = new XmlSelection(); |
551,9 → 592,7 |
for(;;) { |
if(!skipSpaces()) break; |
if(!parseAttribute(selName, selValue)) break; |
//System.out.print(" [" + toString(selName) + "]=[" + toString(selValue) + "]"); |
} |
//System.out.println(); |
|
// end |
element.isEmpty = testChar('/'); |
561,7 → 600,6 |
throwException("end of tag expected"); |
} |
|
//log("parseStartTag ok " + bufPos); |
return true; |
} |
|
568,7 → 606,6 |
private void parseEndTag(XmlSelection sel) |
throws XmlException, IOException |
{ |
//log("parseEndTag begin " + bufPos); |
// begin |
if(!testChar('<') || !testChar('/')) { |
throwException("cannot find tag end"); |
586,14 → 623,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(buf.isEnd()) { |
611,25 → 645,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(;; buf.toNext()) { |
byte c = buf.cur(); |
if(c == 0 || c == '<') { |
if(buf.isEnd()) { |
throwException("unexpected EoF"); |
} |
|
if(c == '<' || c == '&') { |
break; |
} |
|
636,8 → 676,9 |
found = true; |
} |
|
//log("parseCharData " + found + " " + bufPos); |
saveSelEnd(sel); |
if(listener != null) listener.processCharData(sel); |
|
return found; |
} |
|
664,6 → 705,8 |
if(buf.at(0) == ']' && buf.at(1) == ']' && buf.at(2) == '>') { |
saveSelEnd(sel); |
buf.skip(3); |
if(listener != null) listener.processCData(sel); |
|
return true; |
} |
} |