Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
  * Conditions Of Use
  *
  * This software was developed by employees of the National Institute of
  * Standards and Technology (NIST), an agency of the Federal Government.
  * Pursuant to title 15 Untied States Code Section 105, works of NIST
  * employees are not subject to copyright protection in the United States
  * and are considered to be in the public domain.  As a result, a formal
  * license is not needed to use the software.
 *
 * This software is provided by NIST as a service and is expressly
 * provided "AS IS."  NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED
 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT
 * AND DATA ACCURACY.  NIST does not warrant or make any representations
 * regarding the use of the software or the results thereof, including but
 * not limited to the correctness, accuracy, reliability or usefulness of
 * the software.
 *
 * Permission to use this software is contingent upon your acceptance
 * of the terms of this agreement
 *
 * .
 *
 */
 package gov.nist.javax.sip.parser;
 
Parser for UserAgent header.

Author(s):
Olivier Deruelle
M. Ranganathan
Version:
1.2 $Revision: 1.16 $ $Date: 2010-05-06 14:07:44 $
 
 public class UserAgentParser extends HeaderParser {

    
Constructor

Parameters:
userAgent - UserAgent header to parse
 
     public UserAgentParser(String userAgent) {
         super(userAgent);
     }

    
Constructor

Parameters:
lexer - the lexer to use.
 
     protected UserAgentParser(Lexer lexer) {
         super(lexer);
     }

    
parse the message. Note that we have losened up on the parsing quite a bit because user agents tend to be very bad about specifying the user agent according to RFC.

Returns:
SIPHeader (UserAgent object)
Throws:
SIPParseException if the message does not respect the spec.
 
     public SIPHeader parse() throws ParseException {
         if ()
             dbg_enter("UserAgentParser.parse");
         UserAgent userAgent = new UserAgent();
         try {
             headerName(.);
             if (this..lookAhead(0) == '\n')
                 throw createParseException("empty header");
 
             /*
              * BNF User-Agent = "User-Agent" HCOLON server-val *(LWS server-val)
              * server-val = product / comment product = token [SLASH
              * product-version] product-version = token
              */
             while (this..lookAhead(0) != '\n'
                     && this..lookAhead(0) != '\0') {
 
                 if (this..lookAhead(0) == '(') {
                     String comment = this..comment();
                     userAgent.addProductToken('(' + comment + ')');
                 } else {
                     // product = token [SLASHproduct-version]
                     // product-version = token
                     // The RFC Does NOT allow this space but we are generous in what we accept
 
                     this.getLexer().SPorHT();
 
 
                    String product = this..byteStringNoSlash();
                    if ( product == null ) throw createParseException("Expected product string");
                    StringBuilder productSb = new StringBuilder(product);
                    // do we possibily have the optional product-version?
                    if (this..peekNextToken().getTokenType() == .) {
                        // yes
                        this..match(.);
                        // product-version
                        // The RFC Does NOT allow this space but we are generous in what we accept
                        this.getLexer().SPorHT();
                        String productVersion = this..byteStringNoSlash();
                        if ( productVersion == null ) throw createParseException("Expected product version");
                        productSb.append("/");
                        productSb.append(productVersion);
                    }
                    userAgent.addProductToken(productSb.toString());
                }
                // LWS
                this..SPorHT();
            }
        } finally {
            if ()
                dbg_leave("UserAgentParser.parse");
        }
        return userAgent;
    }
      public static void main(String args[]) throws ParseException { String
      userAgent[] = { "User-Agent: Softphone/Beta1.5 \n""User-Agent:Nist/Beta1 (beta version) \n""User-Agent: Nist UA (beta version)\n",
      "User-Agent: Nist1.0/Beta2 Ubi/vers.1.0 (very cool) \n" ,
      "User-Agent: SJphone/1.60.299a/L (SJ Labs)\n",
      "User-Agent: sipXecs/3.5.11 sipXecs/sipxbridge (Linux)\n"};
      for (int i = 0; i < userAgent.lengthi++ ) { UserAgentParser parser =
      new UserAgentParser(userAgent[i]); UserAgent ua= (UserAgent)
      parser.parse(); ..println("encoded = " + ua.encode()); }
       }
New to GrepCode? Check out our FAQ X