001    /*
002     * Animate.java
003     *
004     *  The Salamander Project - 2D and 3D graphics libraries in Java
005     *  Copyright (C) 2004 Mark McKay
006     *
007     *  This library is free software; you can redistribute it and/or
008     *  modify it under the terms of the GNU Lesser General Public
009     *  License as published by the Free Software Foundation; either
010     *  version 2.1 of the License, or (at your option) any later version.
011     *
012     *  This library is distributed in the hope that it will be useful,
013     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015     *  Lesser General Public License for more details.
016     *
017     *  You should have received a copy of the GNU Lesser General Public
018     *  License along with this library; if not, write to the Free Software
019     *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020     *
021     *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
022     *  projects can be found at http://www.kitfox.com
023     *
024     * Created on August 15, 2004, 2:51 AM
025     */
026    
027    package com.kitfox.svg.animation;
028    
029    import com.kitfox.svg.SVGElement;
030    import com.kitfox.svg.SVGException;
031    import com.kitfox.svg.SVGLoaderHelper;
032    import com.kitfox.svg.animation.parser.AnimTimeParser;
033    import com.kitfox.svg.animation.parser.ParseException;
034    import com.kitfox.svg.xml.StyleAttribute;
035    import java.io.StringReader;
036    import org.xml.sax.Attributes;
037    import org.xml.sax.SAXException;
038    
039    /**
040     * @author Mark McKay
041     * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
042     */
043    abstract public class AnimateBase extends AnimationElement
044    {
045        protected double repeatCount = Double.NaN;
046        protected TimeBase repeatDur;
047        
048        /** Creates a new instance of Animate */
049        public AnimateBase()
050        {
051        }
052        
053        public void evalParametric(AnimationTimeEval state, double curTime)
054        {
055            evalParametric(state, curTime, repeatCount, repeatDur == null ? Double.NaN : repeatDur.evalTime());
056        }
057        
058        public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent) throws SAXException
059        {
060                    //Load style string
061            super.loaderStartElement(helper, attrs, parent);
062    
063            String repeatDurTime = attrs.getValue("repeatDur");
064    
065            try
066            {
067                if (repeatDurTime != null)
068                {
069                    helper.animTimeParser.ReInit(new StringReader(repeatDurTime));
070                    this.repeatDur = helper.animTimeParser.Expr();
071                    this.repeatDur.setParentElement(this);
072                }
073            }
074            catch (Exception e)
075            {
076                throw new SAXException(e);
077            }
078            
079            String strn = attrs.getValue("repeatCount");
080            if (strn == null)
081            {
082                repeatCount = 1;
083            }
084            else if ("indefinite".equals(strn))
085            {
086                repeatCount = Double.POSITIVE_INFINITY;
087            }
088            else
089            {
090                try { repeatCount = Double.parseDouble(strn); } 
091                catch (Exception e) { repeatCount = Double.NaN; }
092            }
093        }
094    
095        protected void rebuild(AnimTimeParser animTimeParser) throws SVGException
096        {
097            super.rebuild(animTimeParser);
098    
099            StyleAttribute sty = new StyleAttribute();
100    
101            if (getPres(sty.setName("repeatDur")))
102            {
103                String strn = sty.getStringValue();
104                if (strn != null)
105                {
106                    animTimeParser.ReInit(new StringReader(strn));
107                    try {
108                        this.repeatDur = animTimeParser.Expr();
109                    } catch (ParseException ex) {
110                        ex.printStackTrace();
111                    }
112                }
113            }
114    
115            if (getPres(sty.setName("repeatCount")))
116            {
117                String strn = sty.getStringValue();
118                if (strn == null)
119                {
120                    repeatCount = 1;
121                }
122                else if ("indefinite".equals(strn))
123                {
124                    repeatCount = Double.POSITIVE_INFINITY;
125                }
126                else
127                {
128                    try { repeatCount = Double.parseDouble(strn); }
129                    catch (Exception e) { repeatCount = Double.NaN; }
130                }
131            }
132        }
133    }