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: package gov.nist.javax.sdp.parser;
026:
027: import gov.nist.javax.sdp.fields.*;
028: import gov.nist.core.*;
029:
030: /**
031: * Parser for Connection Field.
032: *
033: * @version JAIN-SIP-1.1
034: *
035: *
036: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
037: *
038: */
039: public class ConnectionFieldParser extends SDPParser {
040:
041: /**
042: * Creates new ConnectionFieldParser.
043: * @param connectionField connection string to be parsed
044: */
045: public ConnectionFieldParser(String connectionField) {
046: this .lexer = new Lexer("charLexer", connectionField);
047: }
048:
049: /** Default constructor. */
050: protected ConnectionFieldParser() {
051: super ();
052: }
053:
054: /**
055: * Perform the connection address field parsing
056: * @param address the connection address string to parse
057: * @return the parsed connection address field
058: */
059: public ConnectionAddress connectionAddress(String address) {
060: ConnectionAddress connectionAddress = new ConnectionAddress();
061:
062: int begin = address.indexOf("/");
063:
064: if (begin != -1) {
065: connectionAddress.setAddress(new Host(address.substring(0,
066: begin)));
067:
068: int middle = address.indexOf("/", begin + 1);
069: if (middle != -1) {
070: String ttl = address.substring(begin + 1, middle);
071: connectionAddress.setTtl(Integer.parseInt(ttl.trim()));
072:
073: String addressNumber = address.substring(middle + 1);
074: connectionAddress.setPort(Integer
075: .parseInt(addressNumber.trim()));
076: } else {
077: String ttl = address.substring(begin + 1);
078: connectionAddress.setTtl(Integer.parseInt(ttl.trim()));
079: }
080: } else
081: connectionAddress.setAddress(new Host(address));
082:
083: return connectionAddress;
084: }
085:
086: /**
087: * Perform the connection address field parsing
088: * @return the parsed connection address field
089: * @exception ParseException if a parsing error occurs
090: */
091: public ConnectionField connectionField() throws ParseException {
092: try {
093: lexer.match('c');
094: lexer.SPorHT();
095: lexer.match('=');
096: lexer.SPorHT();
097:
098: ConnectionField connectionField = new ConnectionField();
099:
100: lexer.match(LexerCore.ID);
101: lexer.SPorHT();
102: Token token = lexer.getNextToken();
103: connectionField.setNettype(token.getTokenValue());
104:
105: lexer.match(LexerCore.ID);
106: lexer.SPorHT();
107: token = lexer.getNextToken();
108: connectionField.setAddressType(token.getTokenValue());
109: lexer.SPorHT();
110: String rest = lexer.getRest();
111: ConnectionAddress connectionAddress = connectionAddress(rest
112: .trim());
113:
114: connectionField.setAddress(connectionAddress);
115:
116: return connectionField;
117: } catch (SdpException e) {
118: throw new ParseException(e.getMessage(), lexer.getPtr());
119: }
120: }
121:
122: /**
123: * Perform the connection address field parsing
124: * @return the parsed connection address field
125: * @exception ParseException if a parsing error occurs
126: */
127: public SDPField parse() throws ParseException {
128: return this.connectionField();
129: }
130:
131: }
|