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.element.Attribute;
009    import org.util.xml.parse.ElementParser;
010    import java.io.IOException;
011    
012    /**
013     *
014     * @author masaru
015     */
016    public class AttributeParser extends ParseElement {
017    
018        private NameParser name_parser_;
019        private EqParser eq_parser_;
020        private AttValueParser attvalue_parser_;
021        private SpaceParser space_parser_;
022        private Attribute attribute_;
023        
024        public AttributeParser() {
025            name_parser_ = new NameParser();
026            eq_parser_ = new EqParser();
027            attvalue_parser_ = new AttValueParser();
028            space_parser_ = new SpaceParser();
029        }
030        public boolean match(char c) {
031            return name_parser_.allow(c);
032        }
033    
034        public int parse(int next, ElementParser parser) throws XMLParseException, IOException {
035            String name = null, value = null;
036            
037            if(next==-1) throw new XMLParseException("end of line");
038            next = name_parser_.parse((char)next, parser);
039            name = name_parser_.getReturnValue();
040    
041            if(next==-1) throw new XMLParseException("end of line");
042            
043    //        next = eq_parser_.parse((char)next, parser);
044    
045    //        System.out.print("<"+name+":"+(char)next);
046            if(space_parser_.match((char)next))
047                next = space_parser_.parse(next, parser);
048    
049            if(next==-1) throw new XMLParseException("end of line");
050            
051            if(next=='=') {
052                next = parser.getChar();
053                if(space_parser_.match((char)next))
054                    next = space_parser_.parse(next, parser);
055                if(next==-1) throw new XMLParseException("end of line");
056                next = attvalue_parser_.parse(next, parser);
057                value = attvalue_parser_.getReturnValue();
058                attribute_ = new Attribute(name, value);
059            } else {
060                attribute_ = new Attribute(name, "");
061                attribute_.setNoValue(true);
062            }
063                
064    //        System.out.println((char)next+">");
065    
066            
067            return next;
068        }
069        public Attribute getAttribute() {
070            return attribute_;
071        }
072    }