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