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 java.util.ArrayList; 009 import org.util.xml.parse.ElementParser; 010 011 /** 012 * 013 * @author masaru 014 */ 015 public class NameParser extends ParseElement { 016 017 private String result_value_; 018 019 @Override 020 public boolean match(char c) { 021 return allow(c); 022 } 023 024 @Override 025 public int parse(int c, ElementParser parser) throws Exception { 026 027 StringBuffer sb = new StringBuffer(); 028 int next_word_ = -1; 029 int state = 0; 030 while(true) { 031 if(state == 0) { 032 if(allow(c)) { 033 state = 1; 034 sb.append((char)c); 035 } else throw new Exception("parse error: cannot read name: this char is not allowd ("+(char)c+")"); 036 }else if(state == 1) { 037 if(allow(c)) sb.append((char)c); 038 else { 039 next_word_ = c; 040 break; 041 } 042 } 043 c = parser.get(); 044 } 045 result_value_ = sb.toString(); 046 return next_word_; 047 } 048 049 public boolean allow(int c) { 050 return (!isSpace(c) && (c!='<') && (c!='>') && (c!='/') && (c!='!') 051 && (c!='?') && (c!='=') && (c!='\"') && (c!='\'') && (c!=-1)); 052 } 053 054 @Override 055 public String getReturnValue() { 056 return result_value_.toString(); 057 } 058 }