001: /*
002: *
003: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
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 com.sun.mmedia.protocol;
027:
028: /**
029: * Parses a given locator or encoding string into its constituent properties.
030: * Breaks it up into 3 main parts : the protocol, the device and the parameter
031: * list of the form protocol://device?param1=value1¶m2=value2...
032: */
033: public final class LocatorParser {
034:
035: /**
036: * Constructs a parser object and initializes its state.
037: * @param s The string to be parsed
038: */
039: public LocatorParser(String s) {
040: length = s.length();
041: locator = s;
042: }
043:
044: /**
045: * Parses and returns the protocol part of the locator, if any.
046: * @return The protocol string, or null if there isn't a protocol part
047: */
048: public String getProtocol() {
049: int colonIndex = locator.indexOf("://");
050: int oldIndex = index;
051: if (colonIndex < 1)
052: return null;
053: index = colonIndex + 3;
054: return locator.substring(oldIndex, colonIndex);
055: }
056:
057: /**
058: * Parses and returns the device name part of the locator, if any.
059: * @return The device name string, or null if there isn't one.
060: */
061: public String getDevice() {
062: if (index >= length)
063: return null;
064: int oldIndex = index;
065: // Get the index of the question mark
066: int queryIndex = locator.indexOf("?", index);
067: // If its not found, then the whole string is assumed to be the device
068: if (queryIndex < 0) {
069: index = length;
070: return locator.substring(oldIndex);
071: } else if (queryIndex == 0) {
072: return null;
073: }
074: // Otherwise move the index forward past the ?
075: index = queryIndex + 1;
076: // return the device name portion
077: return locator.substring(oldIndex, queryIndex);
078: }
079:
080: /**
081: * Parses and returns the next parameter key in a parameter
082: * "key=value" pair.
083: * @return The key part of the parameter "key=value" pair
084: */
085: public String getParameter() {
086: if (index >= length)
087: return null;
088: int ampIndex = locator.indexOf("&", index);
089: // Assume last parameter
090: if (ampIndex < 0)
091: ampIndex = length;
092:
093: // token = "abc=xyz"
094: String token = locator.substring(index, ampIndex);
095:
096: int eq = token.indexOf("=");
097: // If there's no = or nothing before the = or nothing after the =
098: if (eq < 1 || eq == token.length() - 1)
099: return null;
100: String key = token.substring(0, eq);
101: // What's after the = is the value. Store it for later query thru
102: // the getValue() method
103: value = token.substring(eq + 1);
104: index = ampIndex + 1;
105: return key;
106: }
107:
108: /**
109: * Returns the value corresponding to the most recently parsed parameter
110: * @return The value part of the "key=value" parameter pair.
111: */
112: public String getValue() {
113: return value;
114: }
115:
116: /**
117: * Checks if there are any more characters in the string that haven't been
118: * parsed yet.
119: * @return true if there are more characters to be parsed, false otherwise
120: */
121: public boolean hasMore() {
122: return index < length;
123: }
124:
125: public static final String S_PROTOCOL_CAPTURE = "capture";
126: //public static final String S_PROTOCOL_HTTP = "http";
127: //public static final String S_PROTOCOL_RTP = "rtp";
128:
129: public static final String S_DEVICE_AUDIO = "audio";
130: public static final String S_DEVICE_VIDEO = "video";
131: //public static final String S_DEVICE_AV = "audio_video";
132: public static final String S_DEVICE_RADIO = "radio";
133:
134: //AUDIO,VIDEO,IMAGE encoding parameter keys and values
135: public static final String S_ENCODING = "encoding";
136: public static final String S_ENCODING_JPEG = "jpeg";
137: public static final String S_ENCODING_PNG = "png";
138: public static final String S_ENCODING_PCM = "pcm";
139:
140: public static final String S_TYPE = "type";
141: public static final String S_TYPE_JFIF = "jfif";
142: //public static final String S_TYPE_EXIF = "exif";
143:
144: public static final String S_WIDTH = "width";
145:
146: public static final String S_HEIGHT = "height";
147:
148: public static final String S_QUALITY = "quality";
149:
150: public static final String S_PROGRESSIVE = "progressive";
151:
152: public static final String S_INTERLACED = "interlaced";
153:
154: public static final String S_FPS = "fps";
155:
156: public static final String S_COLORS = "colors";
157:
158: public static final String S_JPEG = "jpeg";
159:
160: public static final String S_PNG = "png";
161:
162: public static final String S_JFIF = "jfif";
163:
164: public static final String S_TRUE = "true";
165:
166: public static final String S_FALSE = "false";
167:
168: public static final String S_AUDIO = "audio";
169:
170: public static final String S_VIDEO = "video";
171:
172: public static final String S_RATE = "rate";
173: public static final String S_BITS = "bits";
174: public static final String S_CHANNELS = "channels";
175: public static final String S_ENDIAN = "endian";
176: public static final String S_ENDIAN_BIG = "big";
177: public static final String S_ENDIAN_LITTLE = "little";
178:
179: public static final String S_SIGN = "signed";
180: public static final String S_SIGN_SIGNED = "signed";
181: public static final String S_SIGN_UNSIGNED = "unsigned";
182:
183: //RADIO encoding parameter keys and values
184: public static final String S_RADIO_FREQ = "f";
185:
186: public static final String S_RADIO_MOD = "mod";
187: public static final String S_RADIO_MOD_FM = "fm";
188: public static final String S_RADIO_MOD_AM = "am";
189:
190: public static final String S_RADIO_ST = "st";
191: public static final String S_RADIO_ST_MONO = "mono";
192: public static final String S_RADIO_ST_STEREO = "stereo";
193: public static final String S_RADIO_ST_AUTO = "auto";
194:
195: public static final String S_RADIO_PRGID = "id";
196:
197: public static final String S_RADIO_PRESET = "preset";
198:
199: /** The locator to be parsed */
200: private String locator;
201:
202: /** The current pointer into the locator where parsing is to continue */
203: private int index;
204:
205: /** The length of the locator */
206: private int length;
207:
208: /** The latest value in a "key=value" pair */
209: private String value;
210:
211: // Usage:
212: // public static void main(String [] args) {
213: // LocatorParser lp = new LocatorParser(args[0]);
214: // System.err.println("Protocol = " + lp.getProtocol());
215: // System.err.println("Device = " + lp.getDevice());
216: // while (lp.hasMore()) {
217: // System.err.println(lp.getParameter() + " = " + lp.getValue());
218: // }
219: // }
220:
221: }
|