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: package gov.nist.javax.sdp.parser;
027:
028: import gov.nist.javax.sdp.fields.*;
029: import gov.nist.core.*;
030:
031: /**
032: * Parser for Repeat field.
033: *
034: * @version JAIN-SIP-1.1
035: *
036: *
037: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
038: *
039: */
040: public class RepeatFieldParser extends SDPParser {
041:
042: /**
043: * Creates new RepeatFieldsParser.
044: * @param repeatField the repeat times filed to be parsed
045: */
046: public RepeatFieldParser(String repeatField) {
047: lexer = new Lexer("charLexer", repeatField);
048: }
049:
050: /** Default constructor. */
051: protected RepeatFieldParser() {
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: * Parses the field string.
089: * @return RepeatFields
090: * @exception ParseException if a parsing error occurs
091: */
092: public RepeatField repeatField() throws ParseException {
093: lexer.match('r');
094: lexer.SPorHT();
095: lexer.match('=');
096: lexer.SPorHT();
097:
098: RepeatField repeatField = new RepeatField();
099:
100: lexer.match(LexerCore.ID);
101: Token repeatInterval = lexer.getNextToken();
102: this .lexer.SPorHT();
103: TypedTime typedTime = getTypedTime(repeatInterval
104: .getTokenValue());
105: repeatField.setRepeatInterval(typedTime);
106:
107: lexer.match(LexerCore.ID);
108: Token activeDuration = lexer.getNextToken();
109: this .lexer.SPorHT();
110: typedTime = getTypedTime(activeDuration.getTokenValue());
111: repeatField.setActiveDuration(typedTime);
112:
113: // The offsets list:
114: while (lexer.lookAhead(0) != '\n') {
115: lexer.match(LexerCore.ID);
116: Token offsets = lexer.getNextToken();
117: this .lexer.SPorHT();
118: typedTime = getTypedTime(offsets.getTokenValue());
119: repeatField.addOffset(typedTime);
120: }
121:
122: return repeatField;
123: }
124:
125: /**
126: * Parses the field string.
127: * @return RepeatFields
128: * @exception ParseException if a parsing error occurs
129: */
130: public SDPField parse() throws ParseException {
131: return this.repeatField();
132: }
133:
134: }
|