Home | History | Annotate | Download | only in parser
      1 /*
      2 * Conditions Of Use
      3 *
      4 * This software was developed by employees of the National Institute of
      5 * Standards and Technology (NIST), an agency of the Federal Government.
      6 * Pursuant to title 15 Untied States Code Section 105, works of NIST
      7 * employees are not subject to copyright protection in the United States
      8 * and are considered to be in the public domain.  As a result, a formal
      9 * license is not needed to use the software.
     10 *
     11 * This software is provided by NIST as a service and is expressly
     12 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
     13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
     14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
     15 * AND DATA ACCURACY.  NIST does not warrant or make any representations
     16 * regarding the use of the software or the results thereof, including but
     17 * not limited to the correctness, accuracy, reliability or usefulness of
     18 * the software.
     19 *
     20 * Permission to use this software is contingent upon your acceptance
     21 * of the terms of this agreement
     22 *
     23 * .
     24 *
     25 */
     26 package gov.nist.javax.sip.parser;
     27 
     28 import gov.nist.javax.sip.address.*;
     29 import java.text.ParseException;
     30 
     31 /** Parser for addresses.
     32  *
     33  * @version 1.2 $Revision: 1.11 $ $Date: 2009/10/22 10:26:27 $
     34  * @author M. Ranganathan
     35  *
     36  *
     37  */
     38 public class AddressParser extends Parser {
     39 
     40     public AddressParser(Lexer lexer) {
     41         this.lexer = lexer;
     42         this.lexer.selectLexer("charLexer");
     43     }
     44 
     45     public AddressParser(String address) {
     46         this.lexer = new Lexer("charLexer", address);
     47     }
     48 
     49     protected AddressImpl nameAddr() throws ParseException {
     50         if (debug)
     51             dbg_enter("nameAddr");
     52         try {
     53             if (this.lexer.lookAhead(0) == '<') {
     54                 this.lexer.consume(1);
     55                 this.lexer.selectLexer("sip_urlLexer");
     56                 this.lexer.SPorHT();
     57                 URLParser uriParser = new URLParser((Lexer) lexer);
     58                 GenericURI uri = uriParser.uriReference( true );
     59                 AddressImpl retval = new AddressImpl();
     60                 retval.setAddressType(AddressImpl.NAME_ADDR);
     61                 retval.setURI(uri);
     62                 this.lexer.SPorHT();
     63                 this.lexer.match('>');
     64                 return retval;
     65             } else {
     66                 AddressImpl addr = new AddressImpl();
     67                 addr.setAddressType(AddressImpl.NAME_ADDR);
     68                 String name = null;
     69                 if (this.lexer.lookAhead(0) == '\"') {
     70                     name = this.lexer.quotedString();
     71                     this.lexer.SPorHT();
     72                 } else
     73                     name = this.lexer.getNextToken('<');
     74                 addr.setDisplayName(name.trim());
     75                 this.lexer.match('<');
     76                 this.lexer.SPorHT();
     77                 URLParser uriParser = new URLParser((Lexer) lexer);
     78                 GenericURI uri = uriParser.uriReference( true );
     79                 AddressImpl retval = new AddressImpl();
     80                 addr.setAddressType(AddressImpl.NAME_ADDR);
     81                 addr.setURI(uri);
     82                 this.lexer.SPorHT();
     83                 this.lexer.match('>');
     84                 return addr;
     85             }
     86         } finally {
     87             if (debug)
     88                 dbg_leave("nameAddr");
     89         }
     90     }
     91 
     92     public AddressImpl address( boolean inclParams ) throws ParseException {
     93         if (debug)
     94             dbg_enter("address");
     95         AddressImpl retval = null;
     96         try {
     97             int k = 0;
     98             while (lexer.hasMoreChars()) {
     99                 char la = lexer.lookAhead(k);
    100                 if (la == '<'
    101                     || la == '\"'
    102                     || la == ':'
    103                     || la == '/')
    104                     break;
    105                 else if (la == '\0')
    106                     throw createParseException("unexpected EOL");
    107                 else
    108                     k++;
    109             }
    110             char la = lexer.lookAhead(k);
    111             if (la == '<' || la == '\"') {
    112                 retval = nameAddr();
    113             } else if (la == ':' || la == '/') {
    114                 retval = new AddressImpl();
    115                 URLParser uriParser = new URLParser((Lexer) lexer);
    116                 GenericURI uri = uriParser.uriReference( inclParams );
    117                 retval.setAddressType(AddressImpl.ADDRESS_SPEC);
    118                 retval.setURI(uri);
    119             } else {
    120                 throw createParseException("Bad address spec");
    121             }
    122             return retval;
    123         } finally {
    124             if (debug)
    125                 dbg_leave("address");
    126         }
    127 
    128     }
    129 
    130     /*
    131 
    132     */
    133 }
    134 /*
    135  * $Log: AddressParser.java,v $
    136  * Revision 1.11  2009/10/22 10:26:27  jbemmel
    137  * Fix for issue #230, restructured the code such that parsing for any address appearing without '<' '>'
    138  * stops at ';', then parameters are assigned to the header as expected
    139  *
    140  * Revision 1.10  2009/07/17 18:57:57  emcho
    141  * Converts indentation tabs to spaces so that we have a uniform indentation policy in the whole project.
    142  *
    143  * Revision 1.9  2007/02/12 15:19:26  belangery
    144  * Changed the encode() and encodeBody() methods of SIP headers and basic classes to make them use the same StringBuffer instance during the encoding phase.
    145  *
    146  * Revision 1.8  2007/02/06 16:40:02  belangery
    147  * Introduced simple code optimizations.
    148  *
    149  * Revision 1.7  2006/07/13 09:01:57  mranga
    150  * Issue number:
    151  * Obtained from:
    152  * Submitted by:  jeroen van bemmel
    153  * Reviewed by:   mranga
    154  * Moved some changes from jain-sip-1.2 to java.net
    155  *
    156  * CVS: ----------------------------------------------------------------------
    157  * CVS: Issue number:
    158  * CVS:   If this change addresses one or more issues,
    159  * CVS:   then enter the issue number(s) here.
    160  * CVS: Obtained from:
    161  * CVS:   If this change has been taken from another system,
    162  * CVS:   then name the system in this line, otherwise delete it.
    163  * CVS: Submitted by:
    164  * CVS:   If this code has been contributed to the project by someone else; i.e.,
    165  * CVS:   they sent us a patch or a set of diffs, then include their name/email
    166  * CVS:   address here. If this is your work then delete this line.
    167  * CVS: Reviewed by:
    168  * CVS:   If we are doing pre-commit code reviews and someone else has
    169  * CVS:   reviewed your changes, include their name(s) here.
    170  * CVS:   If you have not had it reviewed then delete this line.
    171  *
    172  * Revision 1.4  2006/06/19 06:47:27  mranga
    173  * javadoc fixups
    174  *
    175  * Revision 1.3  2006/06/16 15:26:28  mranga
    176  * Added NIST disclaimer to all public domain files. Clean up some javadoc. Fixed a leak
    177  *
    178  * Revision 1.2  2006/03/08 23:32:54  mranga
    179  * *** empty log message ***
    180  *
    181  * Revision 1.1.1.1  2005/10/04 17:12:35  mranga
    182  *
    183  * Import
    184  *
    185  *
    186  * Revision 1.5  2004/07/28 14:13:54  mranga
    187  * Submitted by:  mranga
    188  *
    189  * Move out the test code to a separate test/unit class.
    190  * Fixed some encode methods.
    191  *
    192  * Revision 1.4  2004/01/22 13:26:31  sverker
    193  * Issue number:
    194  * Obtained from:
    195  * Submitted by:  sverker
    196  * Reviewed by:   mranga
    197  *
    198  * Major reformat of code to conform with style guide. Resolved compiler and javadoc warnings. Added CVS tags.
    199  *
    200  * CVS: ----------------------------------------------------------------------
    201  * CVS: Issue number:
    202  * CVS:   If this change addresses one or more issues,
    203  * CVS:   then enter the issue number(s) here.
    204  * CVS: Obtained from:
    205  * CVS:   If this change has been taken from another system,
    206  * CVS:   then name the system in this line, otherwise delete it.
    207  * CVS: Submitted by:
    208  * CVS:   If this code has been contributed to the project by someone else; i.e.,
    209  * CVS:   they sent us a patch or a set of diffs, then include their name/email
    210  * CVS:   address here. If this is your work then delete this line.
    211  * CVS: Reviewed by:
    212  * CVS:   If we are doing pre-commit code reviews and someone else has
    213  * CVS:   reviewed your changes, include their name(s) here.
    214  * CVS:   If you have not had it reviewed then delete this line.
    215  *
    216  */
    217