Subversion Repositories general

Compare Revisions

No changes between revisions

Ignore whitespace Rev 1345 → Rev 1346

/xmlparser_java/branches/003_cycle_buffer/XmlParser.java
0,0 → 1,606
/*
* input 2072160977 bytes; AMD Athlon 3100+, disk speed 38.0 MB/s
* 49.18s user 10.07s system 89% cpu 1:06.32 total; RES 9384K (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 XmlDocument
{
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;
 
public void parse(InputStream in)
throws XmlException, IOException
{
if(buf.length % 16 != 0) {
throwException("wrong buffer size: " + buf.length);
}
this.in = in;
this.bufLen = 0;
this.bufPos = 0;
this.bufOffset = 0;
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*/ bufPos);
}
 
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");
}
private void saveSelBegin(XmlSelection sel)
{
sel.begin = bufOffset + bufPos;
}
private void saveSelEnd(XmlSelection sel)
{
sel.end = bufOffset + bufPos;
}
private boolean ensureNext(int count)
throws XmlException, IOException
{
if(bufPos + count >= bufLen) {
//log("ensureNext start " + bufPos + " " + count);
if(bufLen == 0) {
// read full buffer at begin
bufLen = in.read(buf);
bufPos = 0;
}
else if(bufLen < buf.length) {
// we could not fill full buffer last time - no more data
return false;
}
else {
// move last 1/16 of data to begin, fill rest with new data
System.arraycopy(buf, buf.length / 16 * 15, buf, 0, buf.length / 16);
int read = in.read(buf, buf.length / 16, buf.length / 16 * 15);
bufLen = buf.length / 16 + read;
bufPos -= buf.length / 16 * 15;
bufOffset += buf.length / 16 * 15;
}
return (bufPos + count < bufLen);
}
else {
return true;
}
}
private boolean parseProlog()
throws XmlException, IOException
{
parseDecl();
while(parseMisc());
if(parseDoctype()) {
while(parseMisc());
}
 
return true;
}
 
private boolean skipSpaces()
throws XmlException, IOException
{
//log("skipSpaces begin " + bufPos);
boolean found = false;
for(;;) {
if(bufPos >= bufLen && !ensureNext(1)) break;
byte c = buf[bufPos];
if(c == ' ' || c == '\t' || c == '\n' || c == '\r') {
found = true;
++bufPos;
}
else {
break;
}
}
//log("skipSpaces " + found + " " + bufPos);
return found;
}
 
private boolean parseDecl()
throws XmlException, IOException
{
//log("parseDecl begin " + bufPos);
// begin
if(!testChar('<', bufPos) || !testChar('?', bufPos) || !testChar('x', bufPos)
|| !testChar('m', bufPos) || !testChar('l', bufPos))
{
//log("parseDecl no 'xml' " + bufPos);
return false;
}
// attributes
XmlSelection selName = new XmlSelection();
XmlSelection selValue = new XmlSelection();
for(;;) {
if(!skipSpaces()) break;
if(!parseAttribute(selName, selValue)) break;
}
 
// end
if(!testChar('?', bufPos) || !testChar('>', bufPos)) {
throwException("end of XML declaration expected");
}
//log("parseDecl ok " + bufPos);
return true;
}
 
private boolean parseDoctype()
throws XmlException, IOException
{
// FIXME not implemented
return true;
}
private boolean parseName(XmlSelection sel)
throws XmlException, IOException
{
//log("parseName begin " + bufPos);
saveSelBegin(sel);
int start = bufPos;
if(bufPos >= bufLen && !ensureNext(1)) {
return false;
}
byte c = buf[bufPos];
if(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || c == '_' || c == ':') {
++bufPos;
}
else {
//log("parseName not a name " + bufPos);
bufPos = start;
return false;
}
for(;;) {
if(bufPos >= bufLen && !ensureNext(1)) {
throwException("unexpected EoF");
}
c = buf[bufPos];
if(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9')
|| c == '.' || c == '-' || c == '_' || c == ':')
{
++bufPos;
}
else {
break;
}
}
saveSelEnd(sel);
//log("parseName ok " + bufPos);
return true;
}
 
private boolean testChar(char c, int rollback)
throws XmlException, IOException
{
if(bufPos >= bufLen && !ensureNext(1)) {
return false;
}
if(buf[bufPos] != c) {
bufPos = rollback;
return false;
}
else {
++bufPos;
return true;
}
}
 
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;
}
// eq
skipSpaces();
if(!testChar('=', bufPos)) {
throwException("equal sign expected");
}
skipSpaces();
// FIXME allow 'Reference' here
// value
if(!testChar('"', bufPos)) {
throwException("quoted string expected");
}
saveSelBegin(selValue);
for(;; ++bufPos) {
if(bufPos >= bufLen && !ensureNext(1)) {
throwException("unexpected EoF");
}
byte c = buf[bufPos];
if(c == '<' || c == '&' || c == '"') {
break;
}
}
saveSelEnd(selValue);
if(!testChar('"', bufPos)) {
throwException("end of quoted string expected");
}
// FIXME check '[WFC: No External Entity References]'
//log("parseAttribute ok " + bufPos);
return true;
}
 
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;
}
}
 
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;
}
bufPos += 4;
for(;; ++bufPos) {
if(bufPos+2 >= bufLen && !ensureNext(3)) {
throwException("unexpected EoF");
}
if(buf[bufPos] == '-' && buf[bufPos+1] == '-') {
if(buf[bufPos+2] == '>') {
bufPos += 3;
//log("parseComment ok " + bufPos);
return true;
}
else {
throwException("Sequence '--' is not allowed in comment");
}
}
}
}
 
private boolean parseProcessInstruction()
throws XmlException, IOException
{
if(bufPos+1 >= bufLen && !ensureNext(2)) {
return false;
}
if(buf[bufPos] != '<' || buf[bufPos+1] != '?') {
return false;
}
bufPos += 2;
for(;; ++bufPos) {
if(bufPos+1 >= bufLen && !ensureNext(2)) {
throwException("unexpected EoF");
}
 
if(buf[bufPos] == '?' && buf[bufPos+1] == '>') {
bufPos += 3;
return true;
}
}
}
 
private XmlElement parseElement()
throws XmlException, IOException
{
//log("parseElement begin " + bufPos);
 
XmlElement element = new XmlElement();
if(!parseStartTag(element)) {
//log("parseElement no start tag " + bufPos);
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 " + bufPos);
return element;
}
 
private boolean parseStartTag(XmlElement element)
throws XmlException, IOException
{
//log("parseStartTag begin " + bufPos);
int start = bufPos;
// begin
if(!testChar('<', bufPos)) {
//log("parseStartTag no signature " + bufPos);
return false;
}
// name
XmlSelection sel = new XmlSelection();
if(!parseName(sel)) {
//log("parseStartTag no name " + bufPos);
bufPos = start;
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('/', bufPos);
if(!testChar('>', bufPos)) {
throwException("end of tag expected");
}
//log("parseStartTag ok " + bufPos);
return true;
}
 
private void parseEndTag(XmlSelection sel)
throws XmlException, IOException
{
//log("parseEndTag begin " + bufPos);
// begin
if(!testChar('<', bufPos) || !testChar('/', bufPos)) {
throwException("cannot find tag end");
}
// name
if(!parseName(sel)) {
throwException("tag name expected");
}
// spaces
skipSpaces();
// end
if(!testChar('>', bufPos)) {
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)) {
throwException("unexpected EoF");
}
if(buf[bufPos] == '<' && buf[bufPos+1] == '/') break;
if(parseElement() != null) {
}
else if(parseComment()) {
}
else if(parseCData(sel)) {
}
else if(parseProcessInstruction()) {
}
else if(parseCharData(sel)) {
}
// 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);
boolean found = false;
saveSelBegin(sel);
for(;; ++bufPos) {
if(bufPos >= bufLen && !ensureNext(1)) {
return false;
}
byte c = buf[bufPos];
if(c == '<') {
break;
}
found = true;
}
//log("parseCharData " + found + " " + bufPos);
saveSelEnd(sel);
return found;
}
 
private boolean parseCData(XmlSelection sel)
throws XmlException, IOException
{
if(bufPos+8 >= bufLen && !ensureNext(9)) {
return false;
}
if(buf[bufPos] != '<' || buf[bufPos+1] != '!'
|| buf[bufPos+2] != '[' || buf[bufPos+3] != 'C'
|| buf[bufPos+4] != 'D' || buf[bufPos+5] != 'A'
|| buf[bufPos+6] != 'T' || buf[bufPos+7] != 'A'
|| buf[bufPos+8] != '[')
{
return false;
}
bufPos += 9;
saveSelBegin(sel);
for(;; ++bufPos) {
if(bufPos+2 >= bufLen && !ensureNext(3)) {
throwException("unexpected EoF");
}
 
if(buf[bufPos] == ']' && buf[bufPos+1] == ']' && buf[bufPos+2] == '>') {
saveSelEnd(sel);
bufPos += 3;
return true;
}
}
}
}
/xmlparser_java/branches/003_cycle_buffer
Property changes:
Added: svn:ignore
+classes