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     * parse space.
013     * S ::= (#x20 | #x9 | #xD | #xA)+
014     * <a href="http://www.fxis.co.jp/xmlcafe/tmp/rec-xml.html#NT-S">W3C REC-xml-980210</a>
015     * @author masaru
016     */
017    public class TextElementParser extends ParseElement {
018    
019        private String return_value_;
020    
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            StringBuffer sb = new StringBuffer();
028            int next_word_ = -1;
029            int state = 0;
030            while(c!=-1) {
031                if(state == 0) {
032                    if(allow(c)) {
033                        sb.append((char)c);
034                        state = 1;
035                    } else throw new XMLParseException("parse error: cannot read text :"+c);
036                } else if (state == 1) {
037                    if (allow(c)) {
038                        sb.append((char)c);
039                    }else {
040                        next_word_ = c;
041                        break;
042                    }
043                }
044                c = parser.get();
045            }
046            return_value_ = sb.toString();
047            return next_word_;
048        }
049        
050        @Override
051        public String getReturnValue() {
052            return return_value_;
053        }
054        
055        public boolean allow(int c) {
056            return ((c!='<') && (c!='>') && (c!=-1));
057        }
058    }