simple.util.parse
Class LanguageParser

java.lang.Object
  extended by simple.util.parse.Parser
      extended by simple.util.parse.LanguageParser
All Implemented Interfaces:
java.io.Serializable

public class LanguageParser
extends Parser

LanguageParser is used to parse the HTTP Accept-Language header. This takes in an Accept-Language header and parses it according the RFC 2616 BNF for the Accept-Language header. This also has the ability to sequence the language tokens in terms of the most preferred and the least preferred.

This uses the qvalues outlined by RFC 2616 to order the language tokens by preference. Typically the language tokens will not have qvalues with the language. However when a language tag has the qvalue parameter then this tag will be ordered based on the value of that parameter. A language tag without the qvalue parameter is considered to have a qvalue of 1 and is ordered accordingly.

Author:
Niall Gallagher
See Also:
Serialized Form

Field Summary
 
Fields inherited from class simple.util.parse.Parser
buf, count, off
 
Constructor Summary
LanguageParser()
          Used to create a language parser for the Accept-Language HTTP header value.
LanguageParser(java.lang.String value)
          This is used to create a language parser for the Accept-Language HTTP header value.
 
Method Summary
 java.util.Locale getLocale()
          This will return the preferred Locale for this header.
 java.util.Locale[] getLocales()
          This will order the tokens based on the preference of the language.
protected  void init()
          This will initialize the parser when it is ready to parse a new String.
protected  void parse()
          This will iteratively take language tokens from the HTTP header list.
 
Methods inherited from class simple.util.parse.Parser
digit, ensureCapacity, parse, skip, space, toLower
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LanguageParser

public LanguageParser()
Used to create a language parser for the Accept-Language HTTP header value. This will parse a set of language tokens and there parameters. The languages will be ordered on preference.


LanguageParser

public LanguageParser(java.lang.String value)
This is used to create a language parser for the Accept-Language HTTP header value. This will parse a set of language tokens and there parameters. The languages will be ordered on preference. This constructor will parse the value given using parse(String).

Parameters:
value - value of a Accept-Language header
Method Detail

getLocale

public java.util.Locale getLocale()
This will return the preferred Locale for this header. The preferred Locale may be any of the Locales given in the header with a qvalue parameter value of 1.

Returns:
this returns the users preferred Locale

getLocales

public java.util.Locale[] getLocales()
This will order the tokens based on the preference of the language. The Locale[] objects extracted from the Accept-Language header. The array returned is sorted using the qvalue preferences within the Accept-Language header.

Returns:
a sorted array of Locale objects

parse

protected void parse()
This will iteratively take language tokens from the HTTP header list. This places each language token parsed from the buffer into the priority queue based on its value.

Specified by:
parse in class Parser

init

protected void init()
This will initialize the parser when it is ready to parse a new String. This will reset the parser to a ready state. The init method is invoked by the parser when the parse method is invoked.

Specified by:
init in class Parser