001 /* 002 * To change this template, choose Tools | Templates 003 * and open the template in the editor. 004 */ 005 006 package org.util.xml.parse; 007 008 import org.util.xml.parse.ElementParser; 009 010 /** 011 * 012 * @author masaru 013 */ 014 public class AttValueParser extends ParseElement { 015 016 String return_value_; 017 018 @Override 019 public boolean match(char c) { 020 return allow(c); 021 } 022 023 @Override 024 public int parse(int c, ElementParser parser) throws Exception { 025 StringBuffer sb = new StringBuffer(); 026 int next_word_ = -1; 027 int state = 0; 028 while(true) { 029 if(c==-1) throw new Exception("end of stream."); 030 if(state == 0) { 031 if(c=='\"') state = 1; 032 else if(c=='\'') state = 2; 033 else if(allow(c)) { 034 sb.append((char)c); 035 state = 3; 036 } else throw new Exception("parse error: cannot read value: this char does not allowed "+(char)c); 037 }else if(state == 1) { 038 if(c=='\"') state = 4; 039 else sb.append((char)c); 040 }else if(state == 2) { 041 if(c=='\'') state = 4; 042 else sb.append((char)c); 043 }else if(state == 3) { 044 if(allow(c)) sb.append((char)c); 045 else { 046 next_word_ = c; 047 break; 048 } 049 }else if(state == 4) { 050 next_word_ = c; 051 break; 052 } 053 c = parser.get(); 054 } 055 return_value_ = sb.toString(); 056 return next_word_; 057 } 058 059 @Override 060 public String getReturnValue() { 061 return return_value_; 062 } 063 064 public boolean allow(int c) { 065 return (!isSpace(c) && (c!='<') && (c!='>') && (c!='!') && (c!=-1)); 066 } 067 }