001: /*
002: * Portions Copyright 2000-2007 Sun Microsystems, Inc. All Rights
003: * Reserved. Use is subject to license terms.
004: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License version
008: * 2 only, as published by the Free Software Foundation.
009: *
010: * This program is distributed in the hope that it will be useful, but
011: * WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * General Public License version 2 for more details (a copy is
014: * included at /legal/license.txt).
015: *
016: * You should have received a copy of the GNU General Public License
017: * version 2 along with this work; if not, write to the Free Software
018: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
019: * 02110-1301 USA
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
022: * Clara, CA 95054 or visit www.sun.com if you need additional
023: * information or have any questions.
024: */
025: /*
026: * TimeFieldParser.java
027: *
028: * Created on February 25, 2002, 9:58 AM
029: */
030:
031: package gov.nist.javax.sdp.parser;
032:
033: import gov.nist.javax.sdp.fields.*;
034: import gov.nist.core.*;
035:
036: /**
037: * Time field parser.
038: * @version 1.0
039: */
040: public class TimeFieldParser extends SDPParser {
041:
042: /**
043: * Creates new TimeFieldParser.
044: * @param timeField the time field to be parsed
045: */
046: public TimeFieldParser(String timeField) {
047: lexer = new Lexer("charLexer", timeField);
048: }
049:
050: /** Default constructor. */
051: protected TimeFieldParser() {
052: super ();
053: }
054:
055: /**
056: * Gets the typed time.
057: * @param tokenValue to set
058: * @return TypedTime
059: */
060: public TypedTime getTypedTime(String tokenValue) {
061: TypedTime typedTime = new TypedTime();
062:
063: if (tokenValue.endsWith("d")) {
064: typedTime.setUnit("d");
065: String t = tokenValue.replace('d', ' ');
066:
067: typedTime.setTime(Integer.parseInt(t.trim()));
068: } else if (tokenValue.endsWith("h")) {
069: typedTime.setUnit("h");
070: String t = tokenValue.replace('h', ' ');
071: typedTime.setTime(Integer.parseInt(t.trim()));
072: } else if (tokenValue.endsWith("m")) {
073: typedTime.setUnit("m");
074: String t = tokenValue.replace('m', ' ');
075: typedTime.setTime(Integer.parseInt(t.trim()));
076: } else {
077: typedTime.setUnit("s");
078: if (tokenValue.endsWith("s")) {
079: String t = tokenValue.replace('s', ' ');
080: typedTime.setTime(Integer.parseInt(t.trim()));
081: } else
082: typedTime.setTime(Integer.parseInt(tokenValue.trim()));
083: }
084: return typedTime;
085: }
086:
087: /**
088: * Gets the time value.
089: * @return the time value
090: * @exception if a parsing error occurs
091: */
092: private long getTime() throws ParseException {
093: try {
094: String startTime = this .lexer.number();
095: return Long.parseLong(startTime);
096: } catch (NumberFormatException ex) {
097: throw lexer.createParseException();
098: }
099:
100: }
101:
102: /**
103: * Parses the field string.
104: * @return TimeField
105: * @exception ParseException is a parsing error occurs
106: */
107: public TimeField timeField() throws ParseException {
108: lexer.match('t');
109: lexer.SPorHT();
110: lexer.match('=');
111: lexer.SPorHT();
112:
113: TimeField timeField = new TimeField();
114:
115: long st = this .getTime();
116: timeField.setStartTime(st);
117: lexer.SPorHT();
118:
119: st = this .getTime();
120: timeField.setStopTime(st);
121:
122: return timeField;
123: }
124:
125: /**
126: * Parses the field string.
127: * @return TimeField
128: * @exception ParseException is a parsing error occurs
129: */
130: public SDPField parse() throws ParseException {
131: return timeField();
132: }
133:
134: }
|