📄 xmlparser.java
字号:
// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space
// Source File Name: XmlParser.java
package org.kxml.parser;
import java.io.IOException;
import java.io.Reader;
import java.util.Vector;
import org.kxml.Attribute;
import org.kxml.io.ParseException;
// Referenced classes of package org.kxml.parser:
// AbstractXmlParser, ParseEvent, Tag, StartTag
public class XmlParser extends AbstractXmlParser
{
class DefaultParserException extends ParseException
{
DefaultParserException(String msg, Exception chained)
{
super(msg, chained, line, column);
}
}
static final String UNEXPECTED_EOF = "Unexpected EOF";
char buf[];
boolean eof;
int bufPos;
int bufCount;
Reader reader;
boolean relaxed;
int line;
int column;
Vector qNames;
boolean immediateClose;
StartTag current;
protected ParseEvent next;
int peekChar()
throws IOException
{
if (eof)
return -1;
if (bufPos >= bufCount)
{
if (buf.length == 1)
{
int c = reader.read();
if (c == -1)
{
eof = true;
return -1;
}
bufCount = 1;
buf[0] = (char)c;
} else
{
bufCount = reader.read(buf, 0, buf.length);
if (bufCount == -1)
{
eof = true;
return -1;
}
}
bufPos = 0;
}
return buf[bufPos];
}
int readChar()
throws IOException
{
int p = peekChar();
bufPos++;
column++;
if (p == 10)
{
line++;
column = 1;
}
return p;
}
void skipWhitespace()
throws IOException
{
for (; !eof && peekChar() <= 32; readChar());
}
public String readName()
throws IOException
{
int c = readChar();
if (c < 128 && c != 95 && c != 58 && (c < 97 || c > 122) && (c < 65 || c > 90))
throw new DefaultParserException("name expected!", null);
StringBuffer buf = new StringBuffer();
buf.append((char)c);
while (!eof)
{
c = peekChar();
if (c < 128 && c != 95 && c != 45 && c != 58 && c != 46 && (c < 48 || c > 57) && (c < 97 || c > 122) && (c < 65 || c > 90))
break;
buf.append((char)readChar());
}
return buf.toString();
}
public StringBuffer readTo(char stopChar, StringBuffer buf)
throws IOException
{
for (; !eof && peekChar() != stopChar; buf.append((char)readChar()));
return buf;
}
public XmlParser(Reader reader)
throws IOException
{
this(reader, Runtime.getRuntime().freeMemory() < 0x100000L ? 1 : 8192);
}
public XmlParser(Reader reader, int bufSize)
throws IOException
{
line = 1;
column = 1;
qNames = new Vector();
immediateClose = false;
this.reader = reader;
buf = new char[bufSize];
}
public String resolveCharacterEntity(String name)
throws IOException
{
throw new DefaultParserException("Undefined: &" + name + ";", null);
}
ParseEvent parseComment()
throws IOException
{
StringBuffer buf = new StringBuffer();
if (readChar() != 45)
throw new DefaultParserException("'-' expected", null);
do
{
readTo('-', buf);
if (readChar() == -1)
throw new DefaultParserException("Unexpected EOF", null);
int cnt = 0;
int lst;
do
{
lst = readChar();
cnt++;
} while (lst == 45);
if (lst != 62 || cnt < 2)
{
while (cnt-- > 0)
buf.append('-');
buf.append((char)lst);
} else
{
while (cnt-- > 2)
buf.append('-');
return new ParseEvent(1, buf.toString());
}
} while (true);
}
ParseEvent parseDoctype()
throws IOException
{
StringBuffer buf = new StringBuffer();
int nesting = 1;
do
{
int i = readChar();
switch (i)
{
default:
break;
case -1:
throw new DefaultParserException("Unexpected EOF", null);
case 60: // '<'
nesting++;
break;
case 62: // '>'
if (--nesting == 0)
return new ParseEvent(2, buf.toString());
break;
}
buf.append((char)i);
} while (true);
}
ParseEvent parseCData()
throws IOException
{
StringBuffer buf = readTo('[', new StringBuffer());
if (!buf.toString().equals("CDATA"))
throw new DefaultParserException("Invalid CDATA start!", null);
buf.setLength(0);
readChar();
int c0 = readChar();
int c1 = readChar();
do
{
int c2 = readChar();
if (c2 == -1)
throw new DefaultParserException("Unexpected EOF", null);
if (c0 != 93 || c1 != 93 || c2 != 62)
{
buf.append((char)c0);
c0 = c1;
c1 = c2;
} else
{
return new ParseEvent(128, buf.toString());
}
} while (true);
}
ParseEvent parseEndTag()
throws IOException
{
skipWhitespace();
String name = readName();
skipWhitespace();
if (readChar() != 62)
throw new DefaultParserException("'>' expected", null);
int last = qNames.size();
do
{
if (last == 0)
if (relaxed)
return new ParseEvent(8, null);
else
throw new DefaultParserException("tagstack empty parsing </" + name + ">", null);
String qName = (String)qNames.elementAt(--last);
qNames.removeElementAt(last);
if (qName.equals(name))
break;
if (!relaxed)
throw new DefaultParserException("StartTag <" + qName + "> does not match end tag </" + name + ">", null);
if (qName.toLowerCase().equals(name.toLowerCase()))
break;
current = ((Tag) (current)).parent;
} while (true);
Tag result = new Tag(16, current, ((Tag) (current)).namespace, ((Tag) (current)).name);
current = ((Tag) (current)).parent;
return result;
}
ParseEvent parsePI()
throws IOException
{
StringBuffer buf = new StringBuffer();
readTo('?', buf);
readChar();
for (; peekChar() != 62; readChar())
{
buf.append('?');
int r = readChar();
if (r == -1)
throw new DefaultParserException("Unexpected EOF", null);
buf.append((char)r);
readTo('?', buf);
}
readChar();
return new ParseEvent(32, buf.toString());
}
StartTag parseStartTag()
throws IOException
{
String qname = readName();
Vector attributes = null;
immediateClose = false;
do
{
skipWhitespace();
int c = peekChar();
if (c == 47)
{
immediateClose = true;
readChar();
skipWhitespace();
if (readChar() != 62)
throw new DefaultParserException("illegal element termination", null);
break;
}
if (c == 62)
{
readChar();
break;
}
if (c == -1)
throw new DefaultParserException("Unexpected EOF", null);
String attrName = readName();
if (attrName.length() == 0)
throw new DefaultParserException("illegal char / attr", null);
skipWhitespace();
if (readChar() != 61)
throw new DefaultParserException("Attribute name " + attrName + "must be followed by '='!", null);
skipWhitespace();
int delimiter = readChar();
if (delimiter != 39 && delimiter != 34)
{
if (!relaxed)
throw new DefaultParserException("<" + qname + ">: invalid delimiter: " + (char)delimiter, null);
delimiter = 32;
}
StringBuffer buf = new StringBuffer();
readText(buf, (char)delimiter);
if (attributes == null)
attributes = new Vector();
attributes.addElement(new Attribute(null, attrName, buf.toString()));
if (delimiter != 32)
readChar();
} while (true);
try
{
current = new StartTag(current, "", qname, attributes, immediateClose, super.processNamespaces);
}
catch (Exception e)
{
throw new DefaultParserException(e.toString(), e);
}
if (!immediateClose)
qNames.addElement(qname);
return current;
}
int readText(StringBuffer buf, char delimiter)
throws IOException
{
int type = 256;
do
{
int nextChar = peekChar();
if (nextChar != -1 && nextChar != delimiter && (delimiter != ' ' || nextChar != 62 && nextChar >= 32))
{
readChar();
if (nextChar == 38)
{
String code = readTo(';', new StringBuffer()).toString();
readChar();
if (code.charAt(0) == '#')
{
nextChar = code.charAt(1) != 'x' ? Integer.parseInt(code.substring(1)) : Integer.parseInt(code.substring(2), 16);
if (nextChar > 32)
type = 128;
buf.append((char)nextChar);
} else
{
if (code.equals("lt"))
buf.append('<');
else
if (code.equals("gt"))
buf.append('>');
else
if (code.equals("apos"))
buf.append('\'');
else
if (code.equals("quot"))
buf.append('"');
else
if (code.equals("amp"))
buf.append('&');
else
buf.append(resolveCharacterEntity(code));
type = 128;
}
} else
{
if (nextChar > 32)
type = 128;
buf.append((char)nextChar);
}
} else
{
return type;
}
} while (true);
}
ParseEvent parseSpecial()
throws IOException
{
switch (peekChar())
{
case -1:
throw new DefaultParserException("Unexpected EOF", null);
case 33: // '!'
readChar();
switch (peekChar())
{
case 45: // '-'
readChar();
return parseComment();
case 91: // '['
readChar();
return parseCData();
}
return parseDoctype();
case 63: // '?'
readChar();
return parsePI();
case 47: // '/'
readChar();
return parseEndTag();
}
return parseStartTag();
}
public ParseEvent read()
throws IOException
{
if (next == null)
peek();
ParseEvent result = next;
next = null;
return result;
}
public ParseEvent peek()
throws IOException
{
if (next == null)
if (immediateClose)
{
next = new Tag(16, current, ((Tag) (current)).namespace, ((Tag) (current)).name);
current = current.getParent();
immediateClose = false;
} else
{
switch (peekChar())
{
case 60: // '<'
readChar();
next = parseSpecial();
break;
case -1:
if (current != null && !relaxed)
throw new DefaultParserException("End tag missing for: " + current, null);
next = new ParseEvent(8, null);
break;
default:
StringBuffer buf = new StringBuffer();
int type = readText(buf, '<');
next = new ParseEvent(type, buf.toString());
break;
}
}
return next;
}
public void setRelaxed(boolean relaxed)
{
this.relaxed = relaxed;
}
public int getLineNumber()
{
return line;
}
public int getColumnNumber()
{
return column;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -