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.io.IOException;
009    import org.util.xml.parse.ElementParser;
010    
011    /**
012     *
013     * @author masaru
014     */
015    public class AttValueParser extends ParseElement {
016    
017        String return_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 XMLParseException, IOException {
026            StringBuffer sb = new StringBuffer();
027            int next_word_ = -1;
028            int state = 0;
029            while(true) {
030                if(c==-1) throw new XMLParseException("end of stream.");
031                if(state == 0) {
032                    if(c=='\"') state = 1;
033                    else if(c=='\'') state = 2;
034                    else if(allow(c)) {
035                        sb.append((char)c);
036                        state = 3;
037                    } else throw new XMLParseException("parse error: cannot read value: this char does not allowed "+(char)c);
038                }else if(state == 1) {
039                    if(c=='\"') state = 4;
040                    else sb.append((char)c);
041                }else if(state == 2) {
042                    if(c=='\'') state = 4;
043                    else sb.append((char)c);
044                }else if(state == 3) {
045                    if(allow(c)) sb.append((char)c);
046                    else {
047                        next_word_ = c;
048                        break;
049                    }
050                }else if(state == 4) {
051                    next_word_ = c;
052                    break;
053                }
054                c = parser.get();
055            }
056            return_value_ = sb.toString();
057            return next_word_;
058        }
059    
060        @Override
061        public String getReturnValue() {
062            return return_value_;
063        }
064        
065        public boolean allow(int c) {
066            return (!isSpace(c) && (c!='<') && (c!='>') && (c!='!') && (c!=-1));
067        }
068    }