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: * Origin field parser.
033: * @version 1.0
034: */
035: public class OriginFieldParser extends SDPParser {
036:
037: /**
038: * Creates new OriginFieldParser.
039: * @param originField origin field to be parsed
040: */
041: public OriginFieldParser(String originField) {
042: lexer = new Lexer("charLexer", originField);
043: }
044:
045: /** Default constructor. */
046: protected OriginFieldParser() {
047: super ();
048: }
049:
050: /**
051: * Perform the origin field parsing.
052: * @return the parsed origin field
053: * @exception ParseException if a parsing error occurs
054: */
055: public OriginField originField() throws ParseException {
056: try {
057: OriginField originField = new OriginField();
058:
059: lexer.match('o');
060: lexer.SPorHT();
061: lexer.match('=');
062: lexer.SPorHT();
063:
064: lexer.match(LexerCore.ID);
065: Token userName = lexer.getNextToken();
066: originField.setUsername(userName.getTokenValue());
067: this .lexer.SPorHT();
068:
069: lexer.match(LexerCore.ID);
070: Token sessionId = lexer.getNextToken();
071: originField.setSessId(Long.parseLong(sessionId
072: .getTokenValue()));
073: this .lexer.SPorHT();
074:
075: lexer.match(LexerCore.ID);
076: Token sessionVersion = lexer.getNextToken();
077: originField.setSessVersion(Long.parseLong(sessionVersion
078: .getTokenValue()));
079: this .lexer.SPorHT();
080:
081: lexer.match(LexerCore.ID);
082: Token networkType = lexer.getNextToken();
083: originField.setNettype(networkType.getTokenValue());
084: this .lexer.SPorHT();
085:
086: lexer.match(LexerCore.ID);
087: Token addressType = lexer.getNextToken();
088: originField.setAddrtype(addressType.getTokenValue());
089: this .lexer.SPorHT();
090:
091: String host = lexer.getRest();
092: Lexer lexer = new Lexer("charLexer", host);
093: HostNameParser hostNameParser = new HostNameParser(lexer);
094: Host h = hostNameParser.host();
095: originField.setAddress(h);
096:
097: return originField;
098: } catch (SdpException e) {
099: throw new ParseException(lexer.getBuffer(), lexer.getPtr());
100: } catch (NumberFormatException nfe) {
101: throw new ParseException(lexer.getBuffer(), lexer.getPtr());
102: }
103: }
104:
105: /**
106: * Perform the origin field parsing.
107: * @return the parsed origin field
108: * @exception ParseException if a parsing error occurs
109: */
110: public SDPField parse() throws ParseException {
111: return originField();
112: }
113:
114: /*
115: public static void main(String[] args) throws ParseException {
116: String origin[] = {
117: "o=4855 13760799956958020 13760799956958020 IN IP4 166.35.224.216\r\n",
118: "o=mhandley 2890844526 2890842807 IN IP4 126.16.64.4\n",
119: "o=UserB 2890844527 2890844527 IN IP4 everywhere.com\n",
120: "o=UserA 2890844526 2890844526 IN IP4 here.com\n",
121: "o=IFAXTERMINAL01 2890844527 2890844527 IN IP4 ift.here.com\n",
122: "o=GATEWAY1 2890844527 2890844527 IN IP4 gatewayone.wcom.com\n",
123: "o=- 2890844527 2890844527 IN IP4 gatewayone.wcom.com\n"
124: };
125:
126: for (int i = 0; i < origin.length; i++) {
127: OriginFieldParser originFieldParser =
128: new OriginFieldParser(origin[i]);
129: OriginField originField = originFieldParser.originField();
130: System.out.println("toParse :" + origin[i]);
131: System.out.println("encoded: " +originField.encode());
132: }
133:
134: }
135: */
136: }
|