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 the Phone 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 PhoneFieldParser extends SDPParser {
040:
041: /**
042: * Creates new PhoneFieldParser.
043: * @param phoneField the phone field to be parsed
044: */
045: public PhoneFieldParser(String phoneField) {
046: lexer = new Lexer("charLexer", phoneField);
047: }
048:
049: /** Default constructor. */
050: protected PhoneFieldParser() {
051: super ();
052: }
053:
054: /**
055: * Gets the user friendly display name.
056: * @param rest the remaining string to be parsed
057: * @return the display name
058: */
059: public String getDisplayName(String rest) {
060: String retval = null;
061:
062: int begin = rest.indexOf("(");
063: int end = rest.indexOf(")");
064:
065: if (begin != -1 && end != -1 && end > begin) {
066: // p=+44-171-380-7777 (Mark Handley)
067: retval = rest.substring(begin + 1, end);
068: } else {
069: // The alternative RFC822 name quoting convention is
070: // also allowed for
071: // email addresses. ex: p=Mark Handley <+44-171-380-7777>
072: int ind = rest.indexOf("<");
073: if (ind != -1) {
074: retval = rest.substring(0, ind);
075: } else {
076: // There is no display name !!!
077: }
078: }
079:
080: return retval;
081: }
082:
083: /**
084: * Gets the phone number.
085: * @param rest the remaining string to be parsed
086: * @return the phone number
087: */
088: public String getPhoneNumber(String rest) {
089: String phoneNumber = null;
090:
091: int begin = rest.indexOf("(");
092:
093: if (begin != -1) {
094: // p=+44-171-380-7777 (Mark Handley)
095: phoneNumber = rest.substring(0, begin).trim();
096: } else {
097: // The alternative RFC822 name quoting convention is
098: // also allowed for email addresses. ex: p=Mark
099: // Handley <+44-171-380-7777>
100: int ind = rest.indexOf("<");
101: int end = rest.indexOf(">");
102:
103: if (ind != -1 && end != -1 && end > ind) {
104: phoneNumber = rest.substring(ind + 1, end);
105: } else {
106: // p=+44-171-380-7777
107: phoneNumber = rest.trim();
108: }
109: }
110: return phoneNumber;
111: }
112:
113: /**
114: * Perform the phone number field parsing
115: * @return the parsed phone number field
116: * @exception ParseException if a parsing error occurs
117: */
118: public PhoneField phoneField() throws ParseException {
119: lexer.match('p');
120: lexer.SPorHT();
121: lexer.match('=');
122: lexer.SPorHT();
123:
124: PhoneField phoneField = new PhoneField();
125: String rest = lexer.getRest();
126:
127: String displayName = getDisplayName(rest.trim());
128: phoneField.setName(displayName);
129: String phoneNumber = getPhoneNumber(rest);
130: phoneField.setPhoneNumber(phoneNumber);
131:
132: return phoneField;
133: }
134:
135: /**
136: * Perform the phone number field parsing
137: * @return the parsed phone number field
138: * @exception ParseException if a parsing error occurs
139: */
140: public SDPField parse() throws ParseException {
141: return phoneField();
142: }
143:
144: }
|