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 /*
     27  * AcceptLanguageParser.java
     28  *
     29  * Created on June 10, 2002, 3:31 PM
     30  */
     31 
     32 package gov.nist.javax.sip.parser;
     33 import gov.nist.core.*;
     34 import gov.nist.javax.sip.header.*;
     35 import javax.sip.*;
     36 import java.text.ParseException;
     37 
     38 
     39 /**
     40  * Parser for Accept Language Headers.
     41  *
     42  * Accept Language body.
     43  * <pre>
     44  *
     45  * Accept-Language = "Accept-Language" ":"
     46  *                         1#( language-range [ ";" "q" "=" qvalue ] )
     47  *       language-range  = ( ( 1*8ALPHA *( "-" 1*8ALPHA ) ) | "*" )
     48  *
     49  * HTTP RFC 2616 Section 14.4
     50  * </pre>
     51  *
     52  *  Accept-Language: da, en-gb;q=0.8, en;q=0.7
     53  *
     54  * @see AcceptLanguageList
     55  * @version 1.2 $Revision: 1.8 $ $Date: 2009/07/17 18:57:56 $
     56  *
     57  * @author Olivier Deruelle
     58  *
     59  */
     60 public class AcceptLanguageParser extends HeaderParser {
     61 
     62     /**
     63      * Constructor
     64      * @param acceptLanguage AcceptLanguage message to parse
     65      */
     66     public AcceptLanguageParser(String acceptLanguage) {
     67         super(acceptLanguage);
     68     }
     69 
     70     /**
     71      * Constructor
     72      * @param lexer Lexer to set
     73      */
     74     protected AcceptLanguageParser(Lexer lexer) {
     75         super(lexer);
     76     }
     77 
     78     /**
     79      * parse the String message
     80      * @return SIPHeader (AcceptLanguage object)
     81      * @throws ParseException if the message does not respect the spec.
     82      */
     83     public SIPHeader parse() throws ParseException {
     84         AcceptLanguageList acceptLanguageList = new AcceptLanguageList();
     85         if (debug)
     86             dbg_enter("AcceptLanguageParser.parse");
     87 
     88         try {
     89             headerName(TokenTypes.ACCEPT_LANGUAGE);
     90 
     91             while (lexer.lookAhead(0) != '\n') {
     92                 AcceptLanguage acceptLanguage = new AcceptLanguage();
     93                 acceptLanguage.setHeaderName(SIPHeaderNames.ACCEPT_LANGUAGE);
     94                 if (lexer.lookAhead(0) != ';') {
     95                     // Content-Coding:
     96                     lexer.match(TokenTypes.ID);
     97                     Token value = lexer.getNextToken();
     98                     acceptLanguage.setLanguageRange(value.getTokenValue());
     99                 }
    100 
    101                 while (lexer.lookAhead(0) == ';') {
    102                     this.lexer.match(';');
    103                     this.lexer.SPorHT();
    104                     this.lexer.match('q');
    105                     this.lexer.SPorHT();
    106                     this.lexer.match('=');
    107                     this.lexer.SPorHT();
    108                     lexer.match(TokenTypes.ID);
    109                     Token value = lexer.getNextToken();
    110                     try {
    111                         float fl = Float.parseFloat(value.getTokenValue());
    112                         acceptLanguage.setQValue(fl);
    113                     } catch (NumberFormatException ex) {
    114                         throw createParseException(ex.getMessage());
    115                     } catch (InvalidArgumentException ex) {
    116                         throw createParseException(ex.getMessage());
    117                     }
    118                     this.lexer.SPorHT();
    119                 }
    120 
    121                 acceptLanguageList.add(acceptLanguage);
    122                 if (lexer.lookAhead(0) == ',') {
    123                     this.lexer.match(',');
    124                     this.lexer.SPorHT();
    125                 } else
    126                     this.lexer.SPorHT();
    127 
    128             }
    129         } finally {
    130             if (debug)
    131                 dbg_leave("AcceptLanguageParser.parse");
    132         }
    133 
    134         return acceptLanguageList;
    135     }
    136 }
    137 /*
    138  * $Log: AcceptLanguageParser.java,v $
    139  * Revision 1.8  2009/07/17 18:57:56  emcho
    140  * Converts indentation tabs to spaces so that we have a uniform indentation policy in the whole project.
    141  *
    142  * Revision 1.7  2006/07/13 09:02:11  mranga
    143  * Issue number:
    144  * Obtained from:
    145  * Submitted by:  jeroen van bemmel
    146  * Reviewed by:   mranga
    147  * Moved some changes from jain-sip-1.2 to java.net
    148  *
    149  * CVS: ----------------------------------------------------------------------
    150  * CVS: Issue number:
    151  * CVS:   If this change addresses one or more issues,
    152  * CVS:   then enter the issue number(s) here.
    153  * CVS: Obtained from:
    154  * CVS:   If this change has been taken from another system,
    155  * CVS:   then name the system in this line, otherwise delete it.
    156  * CVS: Submitted by:
    157  * CVS:   If this code has been contributed to the project by someone else; i.e.,
    158  * CVS:   they sent us a patch or a set of diffs, then include their name/email
    159  * CVS:   address here. If this is your work then delete this line.
    160  * CVS: Reviewed by:
    161  * CVS:   If we are doing pre-commit code reviews and someone else has
    162  * CVS:   reviewed your changes, include their name(s) here.
    163  * CVS:   If you have not had it reviewed then delete this line.
    164  *
    165  * Revision 1.3  2006/06/19 06:47:27  mranga
    166  * javadoc fixups
    167  *
    168  * Revision 1.2  2006/06/16 15:26:28  mranga
    169  * Added NIST disclaimer to all public domain files. Clean up some javadoc. Fixed a leak
    170  *
    171  * Revision 1.1.1.1  2005/10/04 17:12:35  mranga
    172  *
    173  * Import
    174  *
    175  *
    176  * Revision 1.5  2004/07/28 14:13:54  mranga
    177  * Submitted by:  mranga
    178  *
    179  * Move out the test code to a separate test/unit class.
    180  * Fixed some encode methods.
    181  *
    182  * Revision 1.4  2004/01/22 13:26:31  sverker
    183  * Issue number:
    184  * Obtained from:
    185  * Submitted by:  sverker
    186  * Reviewed by:   mranga
    187  *
    188  * Major reformat of code to conform with style guide. Resolved compiler and javadoc warnings. Added CVS tags.
    189  *
    190  * CVS: ----------------------------------------------------------------------
    191  * CVS: Issue number:
    192  * CVS:   If this change addresses one or more issues,
    193  * CVS:   then enter the issue number(s) here.
    194  * CVS: Obtained from:
    195  * CVS:   If this change has been taken from another system,
    196  * CVS:   then name the system in this line, otherwise delete it.
    197  * CVS: Submitted by:
    198  * CVS:   If this code has been contributed to the project by someone else; i.e.,
    199  * CVS:   they sent us a patch or a set of diffs, then include their name/email
    200  * CVS:   address here. If this is your work then delete this line.
    201  * CVS: Reviewed by:
    202  * CVS:   If we are doing pre-commit code reviews and someone else has
    203  * CVS:   reviewed your changes, include their name(s) here.
    204  * CVS:   If you have not had it reviewed then delete this line.
    205  *
    206  */
    207