001 /*
002 * TimeBase.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, 3:31 AM
025 */
026
027 package com.kitfox.svg.animation;
028
029 import java.util.regex.*;
030
031 /**
032 * SVG has a complicated way of specifying time. Potentially, a time could
033 * be represened as a summation of discrete times and times of other animation
034 * events. This provides a root for the many elements we will need to define
035 * time.
036 *
037 * @author Mark McKay
038 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
039 */
040 abstract public class TimeBase
041 {
042 static final Matcher matchIndefinite = Pattern.compile("\\s*indefinite\\s*").matcher("");
043 static final Matcher matchUnitTime = Pattern.compile("\\s*([-+]?((\\d*\\.\\d+)|(\\d+))([-+]?[eE]\\d+)?)\\s*(h|min|s|ms)?\\s*").matcher("");
044
045 /*
046 public static TimeBase parseTime(String text)
047 {
048 if (text == null) return null;
049
050 if (text.indexOf('+') == -1)
051 {
052 return parseTimeComponent(text);
053 }
054
055 return new TimeCompound(text);
056 }
057 */
058
059 protected static TimeBase parseTimeComponent(String text)
060 {
061 matchIndefinite.reset(text);
062 if (matchIndefinite.matches()) return new TimeIndefinite();
063
064 matchUnitTime.reset(text);
065 if (matchUnitTime.matches())
066 {
067 String val = matchUnitTime.group(1);
068 String units = matchUnitTime.group(6);
069
070 double time = 0;
071 try { time = Double.parseDouble(val); }
072 catch (Exception e) {}
073
074 if (units.equals("ms")) time *= .001;
075 else if (units.equals("min")) time *= 60;
076 else if (units.equals("h")) time *= 3600;
077
078 return new TimeDiscrete(time);
079 }
080
081 return null;
082 }
083
084 /**
085 * Calculates the (greater than or equal to 0) time in seconds this
086 * time represents. If the time cannot be determined, returns
087 * Double.NaN. If this represents an infinte amount of time, returns
088 * Double.POSITIVE_INFINITY.
089 */
090 abstract public double evalTime();
091
092 /**
093 * Some time elements need to refer to the animation element that contains
094 * them to evaluate correctly
095 */
096 public void setParentElement(AnimationElement ele)
097 {
098 }
099 }