Class ListParser<T>

java.lang.Object
org.simpleframework.common.parse.Parser
org.simpleframework.http.parse.ListParser<T>
Direct Known Subclasses:
LanguageParser, ValueParser

public abstract class ListParser<T> extends Parser
The ListParser is used to extract a comma separated list of HTTP header values. This will extract values without any leading or trailing spaces, which enables the values to be used. Listing the values that appear in the header also requires that the values are ordered. This orders the values using the values that appear with any quality parameter associated with it. The quality value is a special parameter that often found in a comma separated value list to specify the client preference.
 
    image/gif, image/jpeg, text/html
    image/gif;q=1.0, image/jpeg;q=0.8, image/png;  q=1.0,*;q=0.1
    gzip;q=1.0, identity; q=0.5, *;q=0

 
The above lists taken from RFC 2616 provides an example of the common form comma separated values take. The first illustrates a simple comma delimited list, here the ordering of values is determined from left to right. The second and third list have quality values associated with them, these are used to specify a preference and thus order.

Each value within a list has an implicit quality value of 1.0. If the value is explicitly set with a the "q" parameter, then the values can range from 1.0 to 0.001. This parser ensures that the order of values returned from the list method adheres to the optional quality parameters and ensures that the quality parameters a removed from the resulting text.

  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private class 
    The Entry object provides a comparable object to insert in to a priority queue.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
    This is used to determine whether to gather tokens.
    private List<T>
    Contains all the values extracted from the header(s).
    Provides a quick means of sorting the values extracted.
    private int
    Used to index into the write offset for the value.
    private long
    The quality associated with an individual value.
    private char[]
    This is used as a working space to parse the value.

    Fields inherited from class org.simpleframework.common.parse.Parser

    buf, count, off
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor for the ListParser.
    Constructor for the ListParser.
    Constructor for the ListParser.
  • Method Summary

    Modifier and Type
    Method
    Description
    private void
    This is used to remove the String tokens from the priority queue and place those tokens in an array.
    private void
    This is used to return the parser to a semi-initialized state.
    protected abstract T
    create(char[] text, int start, int len)
    This creates an value object using the range of characters that have been parsed as an item within the list of values.
    protected void
    Initializes the parser so that tokens can be extracted from the list.
    This will build an ordered list of values extracted from the comma separated header value.
    protected void
    This ensures that tokens are taken from the comma separated list as long as there bytes left to be examined within the source text.
    void
    parse(List<String> list)
    This allows multiple header values to be represented as one single comma separated list.
    private void
    This is used to extract the qvalue parameter from the header.
    private void
    This method will trim whitespace from the extracted token and store that token within the PriorityQueue.
    private void
    save(T value)
    This stores the string in the PriorityQueue.
    private void
    This method will extract a token from a comma separated list and write it to a buffer.

    Methods inherited from class org.simpleframework.common.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
  • Field Details

    • order

      private PriorityQueue<ListParser<T>.Entry> order
      Provides a quick means of sorting the values extracted.
    • list

      private List<T> list
      Contains all the values extracted from the header(s).
    • text

      private char[] text
      This is used as a working space to parse the value.
    • qvalue

      private long qvalue
      The quality associated with an individual value.
    • pos

      private int pos
      Used to index into the write offset for the value.
    • build

      private boolean build
      This is used to determine whether to gather tokens.
  • Constructor Details

    • ListParser

      public ListParser()
      Constructor for the ListParser. This creates a parser with no initial parse data, if there are headers to be parsed then the parse(String) method or parse(List) method can be used. This will parse a delimited list according so RFC 2616 section 4.2.
    • ListParser

      public ListParser(String text)
      Constructor for the ListParser. This creates a parser with the text supplied. This will parse the comma separated list according to RFC 2616 section 2.1 and 4.2. The tokens can be extracted using the list method, which will also sort and trim the tokens.
      Parameters:
      text - this is the comma separated list to be parsed
    • ListParser

      public ListParser(List<String> list)
      Constructor for the ListParser. This creates a parser with the text supplied. This will parse the comma separated list according to RFC 2616 section 2.1 and 4.2. The tokens can be extracted using the list method, which will also sort and trim the tokens.
      Parameters:
      list - a list of comma separated lists to be parsed
  • Method Details

    • parse

      public void parse(List<String> list)
      This allows multiple header values to be represented as one single comma separated list. RFC 2616 states that multiple message header fields with the same field name may be present in a message if and only if the entire field value for that header field is defined as a comma separated list. This means that if there are multiple header values with the same name they can be combined into a single comma separated list.
      Parameters:
      list - this is a list of header values to be combined
    • list

      public List<T> list()
      This will build an ordered list of values extracted from the comma separated header value. This enables the most preferred token, to be taken from the first index of the array and the least preferred token to be taken from the last index.
      Returns:
      tokens parsed from the list ordered by preference
    • build

      private void build()
      This is used to remove the String tokens from the priority queue and place those tokens in an array. The The String tokens are placed into the array in an ordered manner so that the most preferred token is inserted into the start of the list.
    • parse

      protected void parse()
      This ensures that tokens are taken from the comma separated list as long as there bytes left to be examined within the source text. This also makes sure that the implicit qvalue is decreased each time a token is extracted from the list.
      Specified by:
      parse in class Parser
    • init

      protected void init()
      Initializes the parser so that tokens can be extracted from the list. This creates a write buffer so that a if there is only one token as long as the source text, then that token can be accommodated, also this starts of the initial qvalue implicit to tokens within the list as the maximum long value.

      One thing that should be noted is that this will not empty the priority queue on each string parsed. This ensures that if there are multiple strings they can be parsed quickly and also contribute to the final result.

      Specified by:
      init in class Parser
    • clear

      private void clear()
      This is used to return the parser to a semi-initialized state. After extracting a token from the list the buffer will have accumulated bytes, this ensures that bytes previously written to the buffer do not interfere with the next token extracted.

      This also ensures the implicit qvalue is reset to the maximum long value, so that the next token parsed without a qvalue will have the highest priority and be placed at the top of the list. This ensures order is always maintained.

    • value

      private void value()
      This method will extract a token from a comma separated list and write it to a buffer. This performs the extraction in such a way that it can tolerate literals, parameters, and quality value parameters. The only alterations made to the token by this method is the removal of quality values, that is, qvalue parameters which have the name "q". Below is an example of some of the lists that this can parse.
      
          token; quantity=1;q=0.001, token; text="a, b, c, d";q=0
          image/gif, , image/jpeg, image/png;q=0.8, *
          token="\"a, b, c, d\", a, b, c, d", token="a";q=0.9,,
          
       
      This will only interpret a comma delimiter outside quotes of a literal. So if there are comma separated tokens that have quoted strings, then commas within those quoted strings will not upset the extraction of the token. Also escaped strings are tolerated according to RFC 2616 section 2.
    • save

      private void save()
      This method will trim whitespace from the extracted token and store that token within the PriorityQueue. This ensures that the tokens parsed from the comma separated list can be used. Trimming the whitespace is something that will be done to the tokens so that they can be examined, so this ensures that the overhead of the String.trim method is not required to remove trailing or leading spaces. This also ensures that empty tokens are not saved.
    • save

      private void save(T value)
      This stores the string in the PriorityQueue. If the qvalue extracted from the header value is less that 0.001 then this will not store the token. This ensures that client applications can specify tokens that are unacceptable to it.
      Parameters:
      value - this is the token to be enqueued into the queue
    • qvalue

      private void qvalue()
      This is used to extract the qvalue parameter from the header. The qvalue parameter is identified by a parameter with the name "q" and a numeric floating point number. The number can be in the range of 0.000 to 1.000. The qvalue is parsed byte bit shifting a byte in to a value in to a long, this may cause problems with varying accuracy.
    • create

      protected abstract T create(char[] text, int start, int len)
      This creates an value object using the range of characters that have been parsed as an item within the list of values. It is up to the implementation to create a value to insert in to the list. A null value will be ignored if returned.
      Parameters:
      text - this is the text buffer to acquire the value from
      start - the offset within the array to take characters
      len - this is the number of characters within the token