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.siplite.parser;
026:
027: import gov.nist.core.*;
028: import gov.nist.siplite.address.*;
029:
030: /**
031: * Parser for addresses.
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 AddressParser extends Parser {
040: /**
041: * Constructor with initial lexer engine.
042: * @param lexer initial lexer engine to use
043: */
044: protected AddressParser(Lexer lexer) {
045: this .lexer = lexer;
046: this .lexer.selectLexer("charLexer");
047: }
048:
049: /**
050: * Constructor with initial addres.
051: * @param address initial address
052: */
053: public AddressParser(String address) {
054: this .lexer = new Lexer("charLexer", address);
055: }
056:
057: /**
058: * Gets the address.
059: * @return the parsed address
060: */
061: public Address address() throws ParseException {
062: Address retval = new Address();
063:
064: lexer.SPorHT();
065: if (!lexer.hasMoreChars()) {
066: throw createParseException("Empty address");
067: }
068: char next = lexer.lookAhead(0);
069: // JSR 180: input string could be URL or display name <URL>
070: // RFC 3261, ABNF:
071: // display-name = *(token LWS)/ quoted-string
072: String displayName = null;
073: if (next == '*') { // "*" - stop parsing
074: retval.setAddressType(Address.WILD_CARD);
075: return retval;
076: }
077: if (next == '\"') { // quoted string
078: displayName = "\"" + lexer.quotedString() + "\"";
079: lexer.getString('<'); // skip till '<'
080: } else if (lexer.getRest().indexOf('<') > -1) { // unquoted display name
081: displayName = lexer.getString('<');
082: }
083: if (displayName != null) {
084: try {
085: retval.setDisplayName(displayName.trim());
086: } catch (IllegalArgumentException exc) {
087: throw createParseException("Wrong display name "
088: + displayName);
089: }
090: retval.setAddressType(Address.NAME_ADDR);
091: } else {
092: retval.setAddressType(Address.ADDRESS_SPEC);
093: }
094: // Parsing URL
095: lexer.SPorHT();
096: URLParser uriParser = new URLParser((Lexer) lexer);
097: URI uri = uriParser.uriReference();
098: lexer.SPorHT();
099: if (displayName != null) { // check for closing '>'
100: lexer.match('>');
101: }
102: retval.setURI(uri);
103: return retval;
104:
105: }
106: }
|