package com.xml;
public class Lexer {
private StringBuffer s;
private int i;
private int e;
public Lexer(StringBuffer ss){
s=ss;
i=0;
e=s.length()-1;
}
public void reset(){
i=0;
}
public boolean eof(){
return i>=e;
}
public int pos(){
return i;
}
public char next(){
return s.charAt(i++);
}
public void back(){
i--;
}
private String spaceToken(){
int from=pos();
int len=0;
char ch;
while(!eof()){
ch=next();
switch(ch){
case ' ':
case '/t':
case '/r':
case '/n':
len++;
continue;
default:
back();
return s.substring(from,from+len);
}
}
return s.substring(from,from+len);
}
public String nextToken(){
boolean inQuote=false;
char quote='/0';
int from=pos();
int len=0;
char ch;
while(!eof()){
ch=next();
switch(ch){
case ' ':
case '/t':
case '/r':
case '/n':{
if(inQuote){
len++;
continue;
}else{
back();
if(len==0){
return spaceToken();
}else{
return s.substring(from,from+len);
}
}
}
case '/'':
case '"':{
if(len==0){
quote=ch;
inQuote=true;
len++;
continue;
}else if(inQuote){
len++;
if(quote==ch){
inQuote=false;
return s.substring(from,from+len);
}else{
continue;
}
}else{
//throw new ParseException("引号不匹配/n"+s.substring(0,pos())+"(错误在这里)");
//可能是英文的'号,这里作文本text处理
len++;
continue;
}
}
case '<':{
if(len==0){
len++;
continue;
}else{
back();
return s.substring(from,from+len);
}
}
case '>':{
if(inQuote){
len++;
continue;
}else{
len++;
return s.substring(from,from+len);
}
}
case '=':{
if(inQuote){
len++;
continue;
}else{
if(len==0){
return "=";
}else{
back();
return s.substring(from,from+len);
}
}
}
default:{
len++;
continue;
}
}
}
return s.substring(from,from+len);
}
}