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