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: import java.util.*;
030:
031: /**
032: * Parser for Media field.
033: *
034: * @version JAIN-SDP-PUBLIC-RELEASE
035: *
036: *
037: * <a href="{@docRoot}/uncopyright.html">This code is in the public domain.</a>
038: *
039: */
040: public class MediaFieldParser extends SDPParser {
041:
042: /**
043: * Creates new MediaFieldParser.
044: * @param mediaField the media string to be parsed
045: */
046: public MediaFieldParser(String mediaField) {
047: lexer = new Lexer("charLexer", mediaField);
048: }
049:
050: /** Default constructor. */
051: protected MediaFieldParser() {
052: super ();
053: }
054:
055: /**
056: * Perform the media type field parsing
057: * @return the parsed media type field
058: * @exception ParseException if a parsing error occurs
059: */
060: public MediaField mediaField() throws ParseException {
061: if (ParserCore.debug)
062: dbg_enter("mediaField");
063: try {
064: MediaField mediaField = new MediaField();
065:
066: lexer.match('m');
067: lexer.SPorHT();
068: lexer.match('=');
069: lexer.SPorHT();
070:
071: lexer.match(Lexer.ID);
072: Token media = lexer.getNextToken();
073: mediaField.setMedia(media.getTokenValue());
074: lexer.SPorHT();
075:
076: lexer.match(Lexer.ID);
077: Token port = lexer.getNextToken();
078: mediaField.setPort(Integer.parseInt(port.getTokenValue()));
079:
080: lexer.SPorHT();
081:
082: // Some strange media formatting from Sun Ray systems with media
083: if (lexer.hasMoreChars() && lexer.lookAhead(1) == '\n')
084: return mediaField;
085: if (lexer.lookAhead(0) == '/') {
086: // The number of ports is present:
087: lexer.consume(1);
088: lexer.match(Lexer.ID);
089: Token portsNumber = lexer.getNextToken();
090: mediaField.setNports(Integer.parseInt(portsNumber
091: .getTokenValue()));
092: lexer.SPorHT();
093: }
094:
095: lexer.match(Lexer.ID);
096: Token token = lexer.getNextToken();
097: this .lexer.SPorHT();
098: String transport = token.getTokenValue();
099: if (lexer.lookAhead(0) == '/') {
100: lexer.consume(1);
101: lexer.match(Lexer.ID);
102: Token transportTemp = lexer.getNextToken();
103: transport = transport + "/"
104: + transportTemp.getTokenValue();
105: lexer.SPorHT();
106: }
107:
108: mediaField.setProto(transport);
109:
110: // The formats list:
111: Vector formatList = new Vector();
112: while (lexer.hasMoreChars()) {
113: if (lexer.lookAhead(0) == '\n'
114: || lexer.lookAhead(0) == '\r')
115: break;
116: lexer.SPorHT();
117: // while(lexer.lookAhead(0) == ' ') lexer.consume(1);
118: lexer.match(Lexer.ID);
119: Token tok = lexer.getNextToken();
120: lexer.SPorHT();
121: String format = tok.getTokenValue().trim();
122: if (!format.equals(""))
123: formatList.addElement(format);
124: }
125: mediaField.setFormats(formatList);
126:
127: return mediaField;
128: } catch (NumberFormatException e) {
129: throw new ParseException(lexer.getBuffer(), lexer.getPtr());
130: } finally {
131: dbg_leave("mediaField");
132: }
133: }
134:
135: /**
136: * Perform the media type field parsing
137: * @return the parsed media type field
138: * @exception ParseException if a parsing error occurs
139: */
140: public SDPField parse() throws ParseException {
141: return mediaField();
142: }
143: }
|