FileDocCategorySizeDatePackage
ClockParser.javaAPI DocphoneME MR2 API (J2ME)24565Wed May 02 18:00:36 BST 2007com.sun.perseus.parser

ClockParser

public class ClockParser extends AbstractParser
Parser for SVG Clock values, as originally defined in the SMIL spec:
Clock-val ::= Full-clock-val | Partial-clock-val
| Timecount-val
Full-clock-val ::= Hours ":" Minutes ":" Seconds ("." Fraction)?
Partial-clock-val ::= Minutes ":" Seconds ("." Fraction)?
Timecount-val ::= Timecount ("." Fraction)? (Metric)?
Metric ::= "h" | "min" | "s" | "ms"
Hours ::= DIGIT+; any positive number
Minutes ::= 2DIGIT; range from 00 to 59
Seconds ::= 2DIGIT; range from 00 to 59
Fraction ::= DIGIT+
Timecount ::= DIGIT+
2DIGIT ::= DIGIT DIGIT
DIGIT ::= [0-9]
author
Chris Campbell
version
$Id: ClockParser.java,v 1.3 2006/04/21 06:40:23 st125089 Exp $

Fields Summary
public static final int
MILLIS_PER_SECOND
Number of milliseconds in a second
public static final int
SECONDS_PER_MINUTE
Number of seconds in a minute
public static final int
MILLIS_PER_MINUTE
Number of milliseconds in a minute
public static final int
MINUTES_PER_HOUR
Number of minutes in an hour
public static final int
MILLIS_PER_HOUR
Number of milliseconds in an hour
private long
millis
Total number of milliseconds represented by this clock value.
Constructors Summary
Methods Summary
private voidaddHours(long hours)
Adds the given number of hours to the total clock value.

param
hours number of hours to add to the total clock value

        millis += (hours * MILLIS_PER_HOUR);
    
private voidaddMillis(long ms)
Adds the given number of milliseconds to the total clock value.

param
ms number of milliseconds to add to the total clock value

        millis += ms;
    
private voidaddMinutes(long minutes)
Adds the given number of minutes to the total clock value.

param
minutes number of minutes to add to the total clock value

        millis += (minutes * MILLIS_PER_MINUTE);
    
private voidaddSeconds(long seconds)
Adds the given number of seconds to the total clock value.

param
seconds number of seconds to add to the total clock value

        millis += (seconds * MILLIS_PER_SECOND);
    
public longparseClock(java.lang.String clockString)
Parses a clock value. This method throws an IllegalArgumentException if the input argument's syntax does not conform to that of a clock value, as defined by the SMIL specification.

param
clockString the value to convert to a long offset value.
return
long offset value corresponding to the input argument.


                                                           
         
        setString(clockString);
        current = read();
        return parseClock(true);
    
protected longparseClock(boolean eos)
Parses a clock value, beginning at the current character.

param
eos if true, then there should be no more characters at the end of the string (excess characters will produce an IllegalArgumentException); if false, the parser will treat whitespace and ';' characters as if it marked the end of string.
return
a long offset value.

        millis = 0L;
        int[] wholeParts = new int[3];
        float fractionPart = 0.0f;
        int numWholeParts = 0;
        int tmp = 0;
        boolean isTimeCountVal = true;
        boolean isFirstDigitInferiorToSix = false;
        boolean hasFractionPart = false;
        
        // first digit (could be part of full, partial, or count)
        m1: switch (current) {
        default:
            throw new IllegalArgumentException();
        case '0": case '1": case '2": case '3": case '4": case '5":
            isFirstDigitInferiorToSix = true;
            break m1;
        case '6": case '7": case '8": case '9":
            break m1;
        }
        
        tmp = tmp * 10 + (current - '0");
        
        current = read();

        m2: switch (current) {
        default:
            throw new IllegalArgumentException();
        case 0x20: case 0x09: case 0x0D: case 0x0A: case ';":
            if (eos) {
                throw new IllegalArgumentException();
            }
            // FALLTHROUGH
        case -1:
            wholeParts[numWholeParts++] = tmp;
            break m2;
        case ':":
            if (isFirstDigitInferiorToSix) {
                isTimeCountVal = false;
                wholeParts[numWholeParts++] = tmp;
                tmp = 0;
                current = read();
                switch (current) {
                default:
                    throw new IllegalArgumentException();
                case '0": case '1": case '2": case '3": case '4": case '5":
                    tmp = tmp * 10 + (current - '0");
                    current = read();
                    switch (current) {
                    default:
                        throw new IllegalArgumentException();
                    case '0": case '1": case '2": case '3": case '4":
                    case '5": case '6": case '7": case '8": case '9":
                        tmp = tmp * 10 + (current - '0");
                        current = read();
                        switch (current) {
                        default:
                            throw new IllegalArgumentException();
                        case ':":
                            wholeParts[numWholeParts++] = tmp;
                            tmp = 0;
                            current = read();
                            switch (current) {
                            default:
                                throw new IllegalArgumentException();
                            case '0": case '1": case '2": 
                            case '3": case '4": case '5":
                                tmp = tmp * 10 + (current - '0");
                                current = read();
                                switch (current) {
                                default:
                                    throw new IllegalArgumentException();
                                case '0": case '1": case '2": case '3": 
                                case '4": case '5": case '6": case '7":
                                case '8": case '9":
                                    tmp = tmp * 10 + (current - '0");
                                    current = read();
                                    switch (current) {
                                    default:
                                        throw new IllegalArgumentException();
                                    case '.":
                                        break m2;
                                    case 0x20: case 0x09:
                                    case 0x0D: case 0x0A:
                                    case ';":
                                        if (eos) {
                                            throw
                                                new IllegalArgumentException();
                                        }
                                        // FALLTHROUGH
                                    case -1:
                                        wholeParts[numWholeParts++] = tmp;
                                        break m2;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            break m2;
        case '.": case 'h": case 'm": case 's":
            break m2;
        case '0": case '1": case '2": case '3": case '4":
        case '5": case '6": case '7": case '8": case '9":
            if (isFirstDigitInferiorToSix) {
                tmp = tmp * 10 + (current - '0");
                current = read();
                switch (current) {
                default:
                    throw new IllegalArgumentException();
                case 0x20: case 0x09: case 0x0D: case 0x0A: case ';":
                    if (eos) {
                        throw new IllegalArgumentException();
                    }
                    // FALLTHROUGH
                case -1:
                    wholeParts[numWholeParts++] = tmp;
                    break m2;
                case '.": case 'h": case 'm": case 's":
                case '0": case '1": case '2": case '3": case '4":
                case '5": case '6": case '7": case '8": case '9":
                    break m2;
                case ':":
                    isTimeCountVal = false;
                    wholeParts[numWholeParts++] = tmp;
                    tmp = 0;
                    current = read();
                    switch (current) {
                    default:
                        throw new IllegalArgumentException();
                    case '0": case '1": case '2": case '3": case '4": case '5":
                        tmp = tmp * 10 + (current - '0");
                        current = read();
                        switch (current) {
                        default:
                            throw new IllegalArgumentException();
                        case '0": case '1": case '2": case '3": case '4":
                        case '5": case '6": case '7": case '8": case '9":
                            tmp = tmp * 10 + (current - '0");
                            current = read();
                            switch (current) {
                            default:
                                throw new IllegalArgumentException();
                            case '.":
                                break m2;
                            case 0x20: case 0x09: case 0x0D: case 0x0A:
                            case ';":
                                if (eos) {
                                    throw new IllegalArgumentException();
                                }
                                // FALLTHROUGH
                            case -1:
                                wholeParts[numWholeParts++] = tmp;
                                break m2;
                            case ':":
                                wholeParts[numWholeParts++] = tmp;
                                tmp = 0;
                                current = read();
                                switch (current) {
                                default:
                                    throw new IllegalArgumentException();
                                case '0": case '1": case '2": 
                                case '3": case '4": case '5":
                                    tmp = tmp * 10 + (current - '0");
                                    current = read();
                                    switch (current) {
                                    default:
                                        throw new IllegalArgumentException();
                                    case '0": case '1": case '2": case '3": 
                                    case '4": case '5": case '6": case '7":
                                    case '8": case '9":
                                        tmp = tmp * 10 + (current - '0");
                                        current = read();
                                        switch (current) {
                                        default:
                                            throw
                                                new IllegalArgumentException();
                                        case '.":
                                            break m2;
                                        case 0x20: case 0x09:
                                        case 0x0D: case 0x0A:
                                        case ';":
                                            if (eos) {
                                                throw
                                                    new
                                                    IllegalArgumentException();
                                            }
                                            // FALLTHROUGH
                                        case -1:
                                            wholeParts[numWholeParts++] = tmp;
                                            break m2;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        m3: switch (current) {
        default:
            throw new IllegalArgumentException();
        case 0x20: case 0x09: case 0x0D: case 0x0A: case ';":
            if (eos) {
                throw new IllegalArgumentException();
            }
            // FALLTHROUGH
        case -1:
            break m3;
        case ':": case '.": case 'h": case 'm": case 's":
            break m3;
        case '0": case '1": case '2": case '3": case '4":
        case '5": case '6": case '7": case '8": case '9":
            // must be in the hours field; loop until we reach the colon
            for (;;) {
                tmp = tmp * 10 + (current - '0");
                current = read();
                switch (current) {
                default:
                    throw new IllegalArgumentException();
                case 0x20: case 0x09: case 0x0D: case 0x0A: case ';":
                    if (eos) {
                        throw new IllegalArgumentException();
                    }
                    // FALLTHROUGH
                case -1:
                    wholeParts[numWholeParts++] = tmp;
                    break m3;
                case ':": case '.": case 'h": case 'm": case 's":
                    break m3;
                case '0": case '1": case '2": case '3": case '4":
                case '5": case '6": case '7": case '8": case '9":
                }
            }
        }

        m4: switch (current) {
        default:
            throw new IllegalArgumentException();
        case 0x20: case 0x09: case 0x0D: case 0x0A: case ';":
            if (eos) {
                throw new IllegalArgumentException();
            }
            // FALLTHROUGH
        case -1:
            break m4;
        case '.": case 'h": case 'm": case 's":
            break m4;
        case ':":
            isTimeCountVal = false;
            wholeParts[numWholeParts++] = tmp;
            tmp = 0;
            current = read();
            switch (current) {
            default:
                throw new IllegalArgumentException();
            case '0": case '1": case '2": case '3": case '4": case '5":
                tmp = tmp * 10 + (current - '0");
                current = read();
                switch (current) {
                default:
                    throw new IllegalArgumentException();
                case '0": case '1": case '2": case '3": case '4":
                case '5": case '6": case '7": case '8": case '9":
                    tmp = tmp * 10 + (current - '0");
                    current = read();
                    switch (current) {
                    default:
                        throw new IllegalArgumentException();
                    case ':":
                        wholeParts[numWholeParts++] = tmp;
                        tmp = 0;
                        current = read();
                        switch (current) {
                        default:
                            throw new IllegalArgumentException();
                        case '0": case '1": case '2":case '3": 
                        case '4": case '5":
                            tmp = tmp * 10 + (current - '0");
                            current = read();
                            switch (current) {
                            default:
                                throw new IllegalArgumentException();
                            case '0": case '1": case '2": case '3": case '4":
                            case '5": case '6": case '7": case '8": case '9":
                                tmp = tmp * 10 + (current - '0");
                                current = read();
                                switch (current) {
                                default:
                                    throw new IllegalArgumentException();
                                case '.":
                                    break m4;
                                case 0x20: case 0x09: case 0x0D: case 0x0A:
                                case ';":
                                    if (eos) {
                                        throw new IllegalArgumentException();
                                    }
                                    // FALLTHROUGH
                                case -1:
                                    wholeParts[numWholeParts++] = tmp;
                                    break m4;
                                }
                            }
                        }
                    }
                }
            }
        }

        m5: switch (current) {
        default:
            throw new IllegalArgumentException();
        case '.":
            wholeParts[numWholeParts++] = tmp;
            hasFractionPart = true;
            String frac = "0.";
            current = read();
            if (isTimeCountVal) {
                switch (current) {
                default:
                    throw new IllegalArgumentException();
                case '0": case '1": case '2": case '3": case '4":
                case '5": case '6": case '7": case '8": case '9":
                    for (;;) {
                        frac = frac + (current - '0");
                        current = read();
                        switch (current) {
                        default:
                            break m5;
                        case 0x20: case 0x09: case 0x0D: case 0x0A: case ';":
                            if (eos) {
                                throw new IllegalArgumentException();
                            }
                            // FALLTHROUGH
                        case 'h": case 'm": case 's": case -1:
                            fractionPart = Float.parseFloat(frac);
                            break m5;
                        case '0": case '1": case '2": case '3": case '4":
                        case '5": case '6": case '7": case '8": case '9":
                        }
                    }
                }
            } else {
                switch (current) {
                default:
                    throw new IllegalArgumentException();
                case '0": case '1": case '2": case '3": case '4":
                case '5": case '6": case '7": case '8": case '9":
                    for (;;) {
                        frac = frac + (current - '0");
                        current = read();
                        switch (current) {
                        default:
                            throw new IllegalArgumentException();
                        case 0x20: case 0x09: case 0x0D: case 0x0A: case ';":
                            if (eos) {
                                throw new IllegalArgumentException();
                            }
                            // FALLTHROUGH
                        case -1:
                            fractionPart = Float.parseFloat(frac);
                            break m5;
                        case '0": case '1": case '2": case '3": case '4":
                        case '5": case '6": case '7": case '8": case '9":
                        }
                    }
                }
            }
        case 0x20: case 0x09: case 0x0D: case 0x0A: case ';":
            if (eos) {
                throw new IllegalArgumentException();
            }
            // FALLTHROUGH
        case 'h": case 'm": case 's": case -1:
            break m5;
        }

        switch (current) {
        default:
            throw new IllegalArgumentException();
        case 'h":
            numWholeParts = 0; // so we don't fall into the seconds case below
            addHours(tmp);
            addMillis((int) (fractionPart * MILLIS_PER_HOUR));
            current = read();
            break;
        case 'm":
            numWholeParts = 0; // so we don't fall into the seconds case below
            current = read();
            switch (current) {
            case 'i":
                current = read();
                switch (current) {
                case 'n":
                    addMinutes(tmp);
                    addMillis((int) (fractionPart * MILLIS_PER_MINUTE));
                    current = read();
                    break;
                default:
                    throw new IllegalArgumentException();
                }
                break;
            case 's":
                addMillis(tmp);
                current = read();
                break;
            default:
                throw new IllegalArgumentException();
            }
            break;
        case 's":
            // seconds case is handled in the time count case below
            if (!hasFractionPart) {
                wholeParts[numWholeParts++] = tmp;
            }
            current = read();
            break;
        case 0x20: case 0x09: case 0x0D: case 0x0A: case ';":
            if (eos) {
                throw new IllegalArgumentException();
            }
            // FALLTHROUGH
        case -1:
            break;
        }

        if (eos) {
            skipSpaces();
            if (current != -1) {
                throw new IllegalArgumentException();
            }
        }

        switch (numWholeParts) {
        case 0: // time count was already handled above, just break
            break;
        case 1: // time count (seconds)
            addSeconds(wholeParts[0]);
            addMillis((int) (fractionPart * MILLIS_PER_SECOND));
            break;
        case 2: // partial clock value
            addMinutes(wholeParts[0]);
            addSeconds(wholeParts[1]);
            addMillis((int) (fractionPart * MILLIS_PER_SECOND));
            break;
        case 3: // full clock value
            addHours(wholeParts[0]);
            addMinutes(wholeParts[1]);
            addSeconds(wholeParts[2]);
            addMillis((int) (fractionPart * MILLIS_PER_SECOND));
            break;
        default:
            throw new IllegalArgumentException("wrong number of whole parts");
        }

        return millis;