Subversion Repositories general

Compare Revisions

No changes between revisions

Ignore whitespace Rev 1344 → Rev 1345

/xmlparser_java/branches/002_input_stream/trunk/XmlParser.java
0,0 → 1,679
/*
* input 2072160977 bytes; AMD Athlon 3100+, disk speed 38.0 MB/s
* 137.05s user 10.79s system 89% cpu 2:45.85 total; RES 9420K (empty java - 8200K)
*/
import java.io.*;
 
public class XmlParser
{
public static void main(String[] args)
throws Exception
{
if(args.length < 1) {
System.err.println("Need file name as parameter");
return;
}
long startTime = System.nanoTime();
FileInputStream in = new FileInputStream(args[0]);
try {
XmlDocument doc = new XmlDocument();
doc.parse(in);
}
catch(XmlException ex) {
System.out.println(ex);
}
finally {
in.close();
}
 
System.err.println("Elapsed: " + (System.nanoTime() - startTime) / 1000000 + "ms");
}
}
 
// --------------------------------------------------------------------------------------------------------------------
class XmlException
extends Exception
{
static final long serialVersionUID = 1;
 
private long line;
private long linePos;
public XmlException(String message, long line, long linePos)
{
super(message);
this.line = line;
this.linePos = linePos;
}
public long getLine() { return line; }
public long getLinePos() { return linePos; }
public String toString()
{
return "Error: " + getMessage() + " at " + line + ":" + linePos;
}
}
 
// --------------------------------------------------------------------------------------------------------------------
class XmlSelection
{
public long begin;
public long end;
public long getLength() { return end - begin; }
}
 
// --------------------------------------------------------------------------------------------------------------------
class XmlElement
{
public boolean isEmpty = false;
}
 
// --------------------------------------------------------------------------------------------------------------------
class XmlInputStream
extends InputStream
{
private InputStream origin;
private long pos = -1;
private long marked = 0;
private int markedRead = -1;
private int lastRead = -1;
private boolean useLast = false;
 
public XmlInputStream(InputStream origin)
{
this.origin = new BufferedInputStream(origin, 1024);
}
 
public long getPosition()
{
return pos;
}
 
public long getNextPosition()
{
return (useLast ? pos : pos+1);
}
 
public boolean eof()
{
return (pos > 0 && lastRead < 0);
}
 
public void back()
{
useLast = true;
}
 
public int read()
throws IOException
{
if(useLast) {
useLast = false;
}
else {
lastRead = origin.read();
++pos;
}
 
return lastRead;
}
 
public int read(byte[] b, int off, int len)
throws IOException
{
throw new IOException("not supported");
}
 
public long skip(long n)
throws IOException
{
long res = origin.skip(n);
 
if(res > 0) {
pos += res;
}
 
return res;
}
 
public int available()
throws IOException
{
return origin.available();
}
 
public void close()
throws IOException
{
origin.close();
}
 
public boolean markSupported()
{
return origin.markSupported();
}
 
public void mark()
{
mark(1024);
}
 
public void mark(int readlimit)
{
marked = pos;
if(useLast) {
markedRead = lastRead;
}
else {
markedRead = -1;
}
origin.mark(readlimit);
}
 
public void reset()
throws IOException
{
origin.reset();
pos = marked;
if(markedRead >= 0) {
useLast = true;
lastRead = markedRead;
}
else {
useLast = false;
}
}
}
 
// --------------------------------------------------------------------------------------------------------------------
class XmlDocument
{
private XmlInputStream in;
private long line;
private long linePos;
 
public void parse(InputStream in)
throws XmlException, IOException
{
this.in = new XmlInputStream(in);
this.line = 1;
this.linePos = 0;
if(parseProlog()) {
parseElement();
while(parseMisc());
/*
if(this.pos < this.len) throwException("trash at end of text");
*/
}
}
 
private void throwException(String message)
throws XmlException, IOException
{
throw new XmlException(message, line, /*linePos*/ in.getPosition());
}
 
private void log(String message)
{
System.out.println(message);
}
 
private String toString(XmlSelection sel)
throws IOException
{
//return new String(buf, (int)(sel.begin - bufOffset), (int)sel.getLength(), "UTF-8");
return (sel.begin + "," + (int)sel.getLength());
}
private void saveSelBegin(XmlSelection sel)
{
sel.begin = in.getNextPosition();
}
private void saveSelEnd(XmlSelection sel)
{
sel.end = in.getPosition();
}
private boolean parseProlog()
throws XmlException, IOException
{
parseDecl();
while(parseMisc());
if(parseDoctype()) {
while(parseMisc());
}
 
return true;
}
 
private boolean skipSpaces()
throws XmlException, IOException
{
//log("skipSpaces begin " + in.getPosition());
boolean found = false;
for(;;) {
int c = in.read();
if(c < 0) {
break;
}
else if(c == ' ' || c == '\t' || c == '\n' || c == '\r') {
found = true;
}
else {
in.back();
break;
}
}
//log("skipSpaces " + found + " " + in.getPosition());
return found;
}
 
private boolean parseDecl()
throws XmlException, IOException
{
//log("parseDecl begin " + in.getPosition());
// begin
if(!testChar('<') || !testChar('?') || !testChar('x')
|| !testChar('m') || !testChar('l'))
{
//log("parseDecl no 'xml' " + in.getPosition());
return false;
}
// attributes
XmlSelection selName = new XmlSelection();
XmlSelection selValue = new XmlSelection();
for(;;) {
if(!skipSpaces()) break;
if(!parseAttribute(selName, selValue)) break;
}
 
// end
if(!testChar('?') || !testChar('>')) {
throwException("end of XML declaration expected");
}
//log("parseDecl ok " + in.getPosition());
return true;
}
 
private boolean parseDoctype()
throws XmlException, IOException
{
// FIXME not implemented
return true;
}
private boolean parseName(XmlSelection sel)
throws XmlException, IOException
{
//log("parseName begin " + in.getPosition());
saveSelBegin(sel);
in.mark();
int c = in.read();
if(c < 0) {
return false;
}
else if(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || c == '_' || c == ':') {
}
else {
//log("parseName not a name " + in.getPosition());
in.reset();
return false;
}
for(;;) {
c = in.read();
if(c < 0) {
throwException("unexpected EoF");
}
else if(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9')
|| c == '.' || c == '-' || c == '_' || c == ':')
{
}
else {
in.back();
break;
}
}
saveSelEnd(sel);
//log("parseName ok " + in.getPosition());
return true;
}
 
private boolean testChar(char c)
throws XmlException, IOException
{
int cc = in.read();
 
if(cc < 0) {
//log("testChar eof " + in.getPosition() + " [" + c + "]");
return false;
}
else if(cc != c) {
//log("testChar false " + in.getPosition() + " [" + c + "] [" + (char)cc + "]");
in.back();
return false;
}
else {
//log("testChar true " + in.getPosition() + " [" + c + "] [" + (char)cc + "]");
return true;
}
}
 
private boolean parseAttribute(XmlSelection selName, XmlSelection selValue)
throws XmlException, IOException
{
//log("parseAttribute begin " + in.getPosition());
// name
if(!parseName(selName)) {
//log("parseAttribute no name " + in.getPosition());
return false;
}
// eq
skipSpaces();
if(!testChar('=')) {
throwException("equal sign expected");
}
skipSpaces();
// FIXME allow 'Reference' here
// value
if(!testChar('"')) {
throwException("quoted string expected");
}
saveSelBegin(selValue);
for(;;) {
int c = in.read();
if(c < 0) {
throwException("unexpected EoF");
}
else if(c == '<' || c == '&' || c == '"') {
in.back();
break;
}
}
saveSelEnd(selValue);
if(!testChar('"')) {
throwException("end of quoted string expected");
}
// FIXME check '[WFC: No External Entity References]'
//log("parseAttribute ok " + in.getPosition());
return true;
}
 
private boolean parseMisc()
throws XmlException, IOException
{
//log("parseMisc begin " + in.getPosition());
if(parseComment()) {
//log("parseMisc comment ok " + in.getPosition());
return true;
}
else if(parseProcessInstruction()) {
//log("parseMisc pi ok " + in.getPosition());
return true;
}
else if(skipSpaces()) {
//log("parseMisc spaces ok " + in.getPosition());
return true;
}
else {
//log("parseMisc false " + in.getPosition());
return false;
}
}
 
private boolean parseComment()
throws XmlException, IOException
{
//log("parseComment begin " + in.getPosition());
in.mark();
if(!testChar('<') || !testChar('!') || !testChar('-') || !testChar('-')) {
in.reset();
//log("parseComment no signature " + in.getPosition());
return false;
}
for(; !in.eof(); in.read()) {
if(testChar('-') && testChar('-')) {
if(testChar('>')) {
//log("parseComment ok " + in.getPosition());
return true;
}
else {
throwException("Sequence '--' is not allowed in comment");
}
}
}
 
throwException("unexpected EoF");
return false;
}
 
private boolean parseProcessInstruction()
throws XmlException, IOException
{
//log("parseProcessInstruction begin " + in.getPosition());
in.mark();
if(!testChar('<') || !testChar('?')) {
in.reset();
//log("parseProcessInstruction no signature " + in.getPosition());
return false;
}
for(; !in.eof(); in.read()) {
if(testChar('?') && testChar('>')) {
//log("parseProcessInstruction ok " + in.getPosition());
return true;
}
}
 
throwException("unexpected EoF");
return false;
}
 
private XmlElement parseElement()
throws XmlException, IOException
{
//log("parseElement begin " + in.getPosition());
 
XmlElement element = new XmlElement();
if(!parseStartTag(element)) {
//log("parseElement no start tag " + in.getPosition());
return null;
}
if(!element.isEmpty) {
if(!parseTagContent(element)) {
throwException("cannot parse tag content");
}
XmlSelection selEndName = new XmlSelection();
parseEndTag(selEndName);
//if(element.name != selEndName
// throwException("tag names do not match");
}
//log("parseElement ok " + in.getPosition());
return element;
}
 
private boolean parseStartTag(XmlElement element)
throws XmlException, IOException
{
//log("parseStartTag begin " + in.getPosition());
in.mark();
// begin
if(!testChar('<')) {
//log("parseStartTag no signature " + in.getPosition());
return false;
}
// name
XmlSelection sel = new XmlSelection();
if(!parseName(sel)) {
//log("parseStartTag no name " + in.getPosition());
in.reset();
return false;
}
//System.out.print("[" + toString(sel) + "]");
// attributes
XmlSelection selName = new XmlSelection();
XmlSelection selValue = new XmlSelection();
for(;;) {
if(!skipSpaces()) break;
if(!parseAttribute(selName, selValue)) break;
//System.out.print(" [" + toString(selName) + "]=[" + toString(selValue) + "]");
}
//System.out.println();
// end
element.isEmpty = testChar('/');
if(!testChar('>')) {
throwException("end of tag expected");
}
//log("parseStartTag ok " + in.getPosition());
return true;
}
 
private void parseEndTag(XmlSelection sel)
throws XmlException, IOException
{
//log("parseEndTag begin " + in.getPosition());
// begin
if(!testChar('<') || !testChar('/')) {
throwException("cannot find tag end");
}
// name
if(!parseName(sel)) {
throwException("tag name expected");
}
// spaces
skipSpaces();
// end
if(!testChar('>')) {
throwException("end of tag expected");
}
//log("parseEndTag ok " + in.getPosition());
}
 
private boolean parseTagContent(XmlElement element)
throws XmlException, IOException
{
//log("parseTagContent begin " + in.getPosition());
XmlSelection sel = new XmlSelection();
for(;;) {
if(in.eof()) {
throwException("unexpected EoF");
}
in.mark();
if(testChar('<') && testChar('/')) {
in.reset();
break;
}
in.reset();
if(parseElement() != null) {
}
else if(parseComment()) {
}
else if(parseCData(sel)) {
}
else if(parseProcessInstruction()) {
}
else if(parseCharData(sel)) {
}
// FIXME allow 'Reference' here
}
//log("parseTagContent ok " + in.getPosition());
return true;
}
 
// FIXME not fully conform the standard
private boolean parseCharData(XmlSelection sel)
throws XmlException, IOException
{
//log("parseCharData begin " + in.getPosition());
boolean found = false;
saveSelBegin(sel);
for(;;) {
int c = in.read();
if(c < 0) {
return false;
}
else if(c == '<') {
in.back();
break;
}
found = true;
}
//log("parseCharData " + found + " " + in.getPosition());
saveSelEnd(sel);
return found;
}
 
private boolean parseCData(XmlSelection sel)
throws XmlException, IOException
{
in.mark();
if(!testChar('<') || !testChar('!')
|| !testChar('[') || !testChar('C')
|| !testChar('D') || !testChar('A')
|| !testChar('T') || !testChar('A')
|| !testChar('['))
{
in.reset();
return false;
}
saveSelBegin(sel);
for(; !in.eof(); in.read()) {
if(testChar(']') && testChar(']') && testChar('>')) {
saveSelEnd(sel);
return true;
}
}
 
throwException("unexpected EoF");
return false;
}
}
/xmlparser_java/branches/002_input_stream/trunk
Property changes:
Added: svn:ignore
+classes