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 the Zone 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 ZoneFieldParser extends SDPParser {
041:
042: /**
043: * Creates new ZoneFieldParser.
044: * @param zoneField the time zone field to be parsed
045: */
046: public ZoneFieldParser(String zoneField) {
047: lexer = new Lexer("charLexer", zoneField);
048: }
049:
050: /** Default constructor. */
051: protected ZoneFieldParser() {
052: super ();
053: }
054:
055: /**
056: * Gets the sign of the offset.
057: * @param tokenValue to set
058: * @return String
059: */
060: public String getSign(String tokenValue) {
061: if (tokenValue.startsWith("-"))
062: return "-";
063: else
064: return "+";
065: }
066:
067: /**
068: * Gets the typed time.
069: * @param tokenValue to set
070: * @return TypedTime
071: */
072: public TypedTime getTypedTime(String tokenValue) {
073: TypedTime typedTime = new TypedTime();
074: String offset = null;
075: if (tokenValue.startsWith("-"))
076: offset = tokenValue.replace('-', ' ');
077: else if (tokenValue.startsWith("+"))
078: offset = tokenValue.replace('+', ' ');
079: else
080: offset = tokenValue;
081:
082: if (offset.endsWith("d")) {
083: typedTime.setUnit("d");
084: String t = offset.replace('d', ' ');
085:
086: typedTime.setTime(Integer.parseInt(t.trim()));
087: } else if (offset.endsWith("h")) {
088: typedTime.setUnit("h");
089: String t = offset.replace('h', ' ');
090: typedTime.setTime(Integer.parseInt(t.trim()));
091: } else if (offset.endsWith("m")) {
092: typedTime.setUnit("m");
093: String t = offset.replace('m', ' ');
094: typedTime.setTime(Integer.parseInt(t.trim()));
095: } else {
096: typedTime.setUnit("s");
097: if (offset.endsWith("s")) {
098: String t = offset.replace('s', ' ');
099: typedTime.setTime(Integer.parseInt(t.trim()));
100: } else
101: typedTime.setTime(Integer.parseInt(offset.trim()));
102: }
103: return typedTime;
104: }
105:
106: /**
107: * Parses the Zone field string.
108: * @return ZoneField
109: * @exception ParseException if a parsing error occurs
110: */
111: public ZoneField zoneField() throws ParseException {
112: try {
113: ZoneField zoneField = new ZoneField();
114:
115: lexer.match('z');
116: lexer.SPorHT();
117: lexer.match('=');
118: lexer.SPorHT();
119:
120: // The zoneAdjustment list:
121: while (lexer.lookAhead(0) != '\n') {
122: ZoneAdjustment zoneAdjustment = new ZoneAdjustment();
123: lexer.match(LexerCore.ID);
124: Token time = lexer.getNextToken();
125: lexer.SPorHT();
126: zoneAdjustment.setTime(Long.parseLong(time
127: .getTokenValue()));
128:
129: lexer.match(LexerCore.ID);
130: Token offset = lexer.getNextToken();
131: lexer.SPorHT();
132: String sign = getSign(offset.getTokenValue());
133: TypedTime typedTime = getTypedTime(offset
134: .getTokenValue());
135: zoneAdjustment.setSign(sign);
136: zoneAdjustment.setOffset(typedTime);
137:
138: zoneField.addZoneAdjustment(zoneAdjustment);
139: }
140: return zoneField;
141: } catch (NumberFormatException e) {
142: throw lexer.createParseException();
143: }
144: }
145:
146: /**
147: * Parses the Zone field string.
148: * @return ZoneField
149: * @exception ParseException if a parsing error occurs
150: */
151: public SDPField parse() throws ParseException {
152: return this.zoneField();
153: }
154: }
|