Subversion Repositories general

Compare Revisions

Ignore whitespace Rev 1346 → Rev 1347

/xmlparser_java/branches/003_cycle_buffer/XmlParser.java
1,6 → 1,6
/*
* 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)
* 59.18s user 9.77s system 92% cpu 1:14.72 total; RES 9420K (empty java - 8200K)
*/
import java.io.*;
 
77,13 → 77,139
}
 
// --------------------------------------------------------------------------------------------------------------------
class XmlDocument
class XmlBuffer
{
private InputStream in;
private byte[] buf = new byte[2048]; // (buf.length % 16 == 0)
private int bufLen;
private int bufPos;
private long bufOffset;
private int len;
private int pos;
private long offset;
private int marked = -1;
 
public XmlBuffer(InputStream in)
{
if(buf.length % 16 != 0) {
throw new RuntimeException("wrong buffer size: " + buf.length);
}
this.in = in;
this.len = 0;
this.pos = 0;
this.offset = 0;
}
 
public byte cur()
throws XmlException, IOException
{
if(pos >= len) {
if(!ensureNext(1)) return 0;
}
 
return buf[pos];
}
 
public byte at(int n)
throws XmlException, IOException
{
if(pos + n >= len) {
if(!ensureNext(n+1)) return 0;
}
 
return buf[pos + n];
}
 
public void toNext()
{
++pos;
}
 
public boolean isEnd()
throws XmlException, IOException
{
return (pos >= len && !ensureNext(1));
}
 
public void skip(int n)
{
pos += n;
}
 
public long getOffset()
{
return offset;
}
 
public int getPosition()
{
return pos;
}
 
public long getAbsPosition()
{
return (offset + pos);
}
 
public void mark()
{
marked = pos;
}
 
public void reset()
{
if(marked < 0) {
throw new RuntimeException("no position saved");
}
else {
pos = marked;
marked = -1;
}
}
 
public boolean ensureNext(int count)
throws IOException
{
if(pos + count >= len) {
//log("ensureNext start " + pos + " " + count);
if(len == 0) {
// read full buffer at begin
len = in.read(buf);
pos = 0;
}
else if(len < 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);
len = buf.length / 16 + read;
pos -= buf.length / 16 * 15;
if(marked >= 0) {
marked -= buf.length / 16 * 15;
if(marked < 0) marked = -1;
}
offset += buf.length / 16 * 15;
}
return (pos + count < len);
}
else {
return true;
}
}
public String toString(long begin, int length)
throws IOException
{
return new String(buf, (int)(begin - offset), length, "UTF-8");
}
}
 
// --------------------------------------------------------------------------------------------------------------------
class XmlDocument
{
private XmlBuffer buf;
private long line;
private long linePos;
 
90,14 → 216,7
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.buf = new XmlBuffer(in);
this.line = 1;
this.linePos = 0;
114,7 → 233,7
private void throwException(String message)
throws XmlException, IOException
{
throw new XmlException(message, line, /*linePos*/ bufPos);
throw new XmlException(message, line, /*linePos*/ buf.getAbsPosition());
}
 
private void log(String message)
125,49 → 244,19
private String toString(XmlSelection sel)
throws IOException
{
return new String(buf, (int)(sel.begin - bufOffset), (int)sel.getLength(), "UTF-8");
return buf.toString(sel.begin, (int)sel.getLength());
}
private void saveSelBegin(XmlSelection sel)
{
sel.begin = bufOffset + bufPos;
sel.begin = buf.getAbsPosition();
}
private void saveSelEnd(XmlSelection sel)
{
sel.end = bufOffset + bufPos;
sel.end = buf.getAbsPosition();
}
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
{
187,12 → 276,13
boolean found = false;
for(;;) {
if(bufPos >= bufLen && !ensureNext(1)) break;
byte c = buf[bufPos];
if(c == ' ' || c == '\t' || c == '\n' || c == '\r') {
byte c = buf.cur();
if(c == 0) {
break;
}
else if(c == ' ' || c == '\t' || c == '\n' || c == '\r') {
found = true;
++bufPos;
buf.toNext();
}
else {
break;
209,8 → 299,8
//log("parseDecl begin " + bufPos);
// begin
if(!testChar('<', bufPos) || !testChar('?', bufPos) || !testChar('x', bufPos)
|| !testChar('m', bufPos) || !testChar('l', bufPos))
if(!testChar('<') || !testChar('?') || !testChar('x')
|| !testChar('m') || !testChar('l'))
{
//log("parseDecl no 'xml' " + bufPos);
return false;
225,7 → 315,7
}
 
// end
if(!testChar('?', bufPos) || !testChar('>', bufPos)) {
if(!testChar('?') || !testChar('>')) {
throwException("end of XML declaration expected");
}
246,32 → 336,26
{
//log("parseName begin " + bufPos);
saveSelBegin(sel);
int start = bufPos;
if(bufPos >= bufLen && !ensureNext(1)) {
return false;
}
byte c = buf[bufPos];
 
byte c = buf.cur();
if(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || c == '_' || c == ':') {
++bufPos;
buf.toNext();
}
else {
//log("parseName not a name " + bufPos);
bufPos = start;
return false;
}
for(;;) {
if(bufPos >= bufLen && !ensureNext(1)) {
if(buf.isEnd()) {
throwException("unexpected EoF");
}
c = buf[bufPos];
c = buf.cur();
if(('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9')
|| c == '.' || c == '-' || c == '_' || c == ':')
{
++bufPos;
buf.toNext();
}
else {
break;
283,19 → 367,14
return true;
}
 
private boolean testChar(char c, int rollback)
private boolean testChar(char c)
throws XmlException, IOException
{
if(bufPos >= bufLen && !ensureNext(1)) {
if(buf.cur() != c) {
return false;
}
if(buf[bufPos] != c) {
bufPos = rollback;
return false;
}
else {
++bufPos;
buf.toNext();
return true;
}
}
305,14 → 384,16
{
//log("parseAttribute begin " + bufPos);
// name
buf.mark();
if(!parseName(selName)) {
//log("parseAttribute no name " + bufPos);
buf.reset();
return false;
}
// eq
skipSpaces();
if(!testChar('=', bufPos)) {
if(!testChar('=')) {
throwException("equal sign expected");
}
skipSpaces();
320,17 → 401,17
// FIXME allow 'Reference' here
// value
if(!testChar('"', bufPos)) {
if(!testChar('"')) {
throwException("quoted string expected");
}
saveSelBegin(selValue);
for(;; ++bufPos) {
if(bufPos >= bufLen && !ensureNext(1)) {
for(;; buf.toNext()) {
if(buf.isEnd()) {
throwException("unexpected EoF");
}
byte c = buf[bufPos];
byte c = buf.cur();
if(c == '<' || c == '&' || c == '"') {
break;
}
337,7 → 418,7
}
saveSelEnd(selValue);
if(!testChar('"', bufPos)) {
if(!testChar('"')) {
throwException("end of quoted string expected");
}
373,24 → 454,20
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] != '-') {
if(buf.at(0) != '<' || buf.at(1) != '!' || buf.at(2) != '-' || buf.at(3) != '-') {
//log("parseComment no signature " + bufPos);
return false;
}
bufPos += 4;
for(;; ++bufPos) {
if(bufPos+2 >= bufLen && !ensureNext(3)) {
buf.skip(4);
for(;; buf.toNext()) {
if(buf.isEnd()) {
throwException("unexpected EoF");
}
if(buf[bufPos] == '-' && buf[bufPos+1] == '-') {
if(buf[bufPos+2] == '>') {
bufPos += 3;
if(buf.at(0) == '-' && buf.at(1) == '-') {
if(buf.at(2) == '>') {
buf.skip(3);
//log("parseComment ok " + bufPos);
return true;
}
404,21 → 481,18
private boolean parseProcessInstruction()
throws XmlException, IOException
{
if(bufPos+1 >= bufLen && !ensureNext(2)) {
if(buf.at(0) != '<' || buf.at(1) != '?') {
return false;
}
if(buf[bufPos] != '<' || buf[bufPos+1] != '?') {
return false;
}
bufPos += 2;
for(;; ++bufPos) {
if(bufPos+1 >= bufLen && !ensureNext(2)) {
buf.skip(2);
for(;; buf.toNext()) {
if(buf.isEnd()) {
throwException("unexpected EoF");
}
 
if(buf[bufPos] == '?' && buf[bufPos+1] == '>') {
bufPos += 3;
if(buf.at(0) == '?' && buf.at(1) == '>') {
buf.skip(2);
return true;
}
}
456,19 → 530,17
throws XmlException, IOException
{
//log("parseStartTag begin " + bufPos);
int start = bufPos;
// begin
if(!testChar('<', bufPos)) {
if(!testChar('<')) {
//log("parseStartTag no signature " + bufPos);
return false;
}
// name
buf.mark();
XmlSelection sel = new XmlSelection();
if(!parseName(sel)) {
//log("parseStartTag no name " + bufPos);
bufPos = start;
buf.reset();
return false;
}
//System.out.print("[" + toString(sel) + "]");
484,8 → 556,8
//System.out.println();
// end
element.isEmpty = testChar('/', bufPos);
if(!testChar('>', bufPos)) {
element.isEmpty = testChar('/');
if(!testChar('>')) {
throwException("end of tag expected");
}
498,7 → 570,7
{
//log("parseEndTag begin " + bufPos);
// begin
if(!testChar('<', bufPos) || !testChar('/', bufPos)) {
if(!testChar('<') || !testChar('/')) {
throwException("cannot find tag end");
}
511,7 → 583,7
skipSpaces();
// end
if(!testChar('>', bufPos)) {
if(!testChar('>')) {
throwException("end of tag expected");
}
524,10 → 596,10
//log("parseTagContent begin " + bufPos);
XmlSelection sel = new XmlSelection();
for(;;) {
if(bufPos+1 >= bufLen && !ensureNext(2)) {
if(buf.isEnd()) {
throwException("unexpected EoF");
}
if(buf[bufPos] == '<' && buf[bufPos+1] == '/') break;
if(buf.at(0) == '<' && buf.at(1) == '/') break;
if(parseElement() != null) {
}
555,13 → 627,9
boolean found = false;
saveSelBegin(sel);
for(;; ++bufPos) {
if(bufPos >= bufLen && !ensureNext(1)) {
return false;
}
byte c = buf[bufPos];
if(c == '<') {
for(;; buf.toNext()) {
byte c = buf.cur();
if(c == 0 || c == '<') {
break;
}
576,29 → 644,26
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] != '[')
if(buf.at(0) != '<' || buf.at(1) != '!'
|| buf.at(2) != '[' || buf.at(3) != 'C'
|| buf.at(4) != 'D' || buf.at(5) != 'A'
|| buf.at(6) != 'T' || buf.at(7) != 'A'
|| buf.at(8) != '[')
{
return false;
}
bufPos += 9;
buf.skip(9);
saveSelBegin(sel);
for(;; ++bufPos) {
if(bufPos+2 >= bufLen && !ensureNext(3)) {
for(;; buf.toNext()) {
if(buf.isEnd()) {
throwException("unexpected EoF");
}
 
if(buf[bufPos] == ']' && buf[bufPos+1] == ']' && buf[bufPos+2] == '>') {
if(buf.at(0) == ']' && buf.at(1) == ']' && buf.at(2) == '>') {
saveSelEnd(sel);
bufPos += 3;
buf.skip(3);
return true;
}
}