001/*
002 * SVG Salamander
003 * Copyright (c) 2004, Mark McKay
004 * All rights reserved.
005 *
006 * Redistribution and use in source and binary forms, with or 
007 * without modification, are permitted provided that the following
008 * conditions are met:
009 *
010 *   - Redistributions of source code must retain the above 
011 *     copyright notice, this list of conditions and the following
012 *     disclaimer.
013 *   - Redistributions in binary form must reproduce the above
014 *     copyright notice, this list of conditions and the following
015 *     disclaimer in the documentation and/or other materials 
016 *     provided with the distribution.
017 *
018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
029 * OF THE POSSIBILITY OF SUCH DAMAGE. 
030 * 
031 * Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
032 * projects can be found at http://www.kitfox.com
033 *
034 * Created on August 15, 2004, 3:31 AM
035 */
036
037package com.kitfox.svg.animation;
038
039import java.util.regex.*;
040
041/**
042 * SVG has a complicated way of specifying time.  Potentially, a time could
043 * be represened as a summation of discrete times and times of other animation
044 * events.  This provides a root for the many elements we will need to define
045 * time.
046 *
047 * @author Mark McKay
048 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
049 */
050abstract public class TimeBase
051{
052    static final Matcher matchIndefinite = Pattern.compile("\\s*indefinite\\s*").matcher("");
053    static final Matcher matchUnitTime = Pattern.compile("\\s*([-+]?((\\d*\\.\\d+)|(\\d+))([-+]?[eE]\\d+)?)\\s*(h|min|s|ms)?\\s*").matcher("");
054    
055    /*
056    public static TimeBase parseTime(String text) 
057    { 
058        if (text == null) return null;
059        
060        if (text.indexOf('+') == -1)
061        {
062            return parseTimeComponent(text);
063        }
064        
065        return new TimeCompound(text);
066    }
067     */
068    
069    protected static TimeBase parseTimeComponent(String text)
070    {
071        matchIndefinite.reset(text);
072        if (matchIndefinite.matches()) return new TimeIndefinite();
073        
074        matchUnitTime.reset(text);
075        if (matchUnitTime.matches())
076        {
077            String val = matchUnitTime.group(1);
078            String units = matchUnitTime.group(6);
079            
080            double time = 0;
081            try { time = Double.parseDouble(val); }
082            catch (Exception e) {}
083            
084            if (units.equals("ms")) time *= .001;
085            else if (units.equals("min")) time *= 60;
086            else if (units.equals("h")) time *= 3600;
087            
088            return new TimeDiscrete(time);
089        }
090        
091        return null;
092    }
093    
094    /**
095     * Calculates the (greater than or equal to 0) time in seconds this 
096     * time represents.  If the time cannot be determined, returns 
097     * Double.NaN.  If this represents an infinte amount of time, returns 
098     * Double.POSITIVE_INFINITY.
099     */
100    abstract public double evalTime();
101    
102    /**
103     * Some time elements need to refer to the animation element that contains 
104     * them to evaluate correctly
105     */
106    public void setParentElement(AnimationElement ele)
107    {
108    }
109}