org.apache.commons.fileupload
Class ParameterParser

java.lang.Object
  extended byorg.apache.commons.fileupload.ParameterParser

public class ParameterParser
extends java.lang.Object

A simple parser intended to parse sequences of name/value pairs. Parameter values are exptected to be enclosed in quotes if they contain unsafe characters, such as '=' characters or separators. Parameter values are optional and can be omitted.

param1 = value; param2 = "anything goes; really"; param3

Author:
Oleg Kalnichevski

Field Summary
private  char[] chars
          String to be parsed.
private  int i1
          Start of a token.
private  int i2
          End of a token.
private  int len
          Maximum position in the string.
private  boolean lowerCaseNames
          Whether names stored in the map should be converted to lower case.
private  int pos
          Current position in the string.
 
Constructor Summary
ParameterParser()
          Default ParameterParser constructor.
 
Method Summary
private  java.lang.String getToken(boolean quoted)
          A helper method to process the parsed token.
private  boolean hasChar()
          Are there any characters left to parse?
 boolean isLowerCaseNames()
          Returns true if parameter names are to be converted to lower case when name/value pairs are parsed.
private  boolean isOneOf(char ch, char[] charray)
          Tests if the given character is present in the array of characters.
 java.util.Map parse(char[] chars, char separator)
          Extracts a map of name/value pairs from the given array of characters.
 java.util.Map parse(char[] chars, int offset, int length, char separator)
          Extracts a map of name/value pairs from the given array of characters.
 java.util.Map parse(java.lang.String str, char separator)
          Extracts a map of name/value pairs from the given string.
private  java.lang.String parseQuotedToken(char[] terminators)
          Parses out a token until any of the given terminators is encountered outside the quotation marks.
private  java.lang.String parseToken(char[] terminators)
          Parses out a token until any of the given terminators is encountered.
 void setLowerCaseNames(boolean b)
          Sets the flag if parameter names are to be converted to lower case when name/value pairs are parsed.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

chars

private char[] chars
String to be parsed.


pos

private int pos
Current position in the string.


len

private int len
Maximum position in the string.


i1

private int i1
Start of a token.


i2

private int i2
End of a token.


lowerCaseNames

private boolean lowerCaseNames
Whether names stored in the map should be converted to lower case.

Constructor Detail

ParameterParser

public ParameterParser()
Default ParameterParser constructor.

Method Detail

hasChar

private boolean hasChar()
Are there any characters left to parse?

Returns:
true if there are unparsed characters, false otherwise.

getToken

private java.lang.String getToken(boolean quoted)
A helper method to process the parsed token. This method removes leading and trailing blanks as well as enclosing quotation marks, when necessary.

Parameters:
quoted - true if quotation marks are expected, false otherwise.
Returns:
the token

isOneOf

private boolean isOneOf(char ch,
                        char[] charray)
Tests if the given character is present in the array of characters.

Parameters:
ch - the character to test for presense in the array of characters
charray - the array of characters to test against
Returns:
true if the character is present in the array of characters, false otherwise.

parseToken

private java.lang.String parseToken(char[] terminators)
Parses out a token until any of the given terminators is encountered.

Parameters:
terminators - the array of terminating characters. Any of these characters when encountered signify the end of the token
Returns:
the token

parseQuotedToken

private java.lang.String parseQuotedToken(char[] terminators)
Parses out a token until any of the given terminators is encountered outside the quotation marks.

Parameters:
terminators - the array of terminating characters. Any of these characters when encountered outside the quotation marks signify the end of the token
Returns:
the token

isLowerCaseNames

public boolean isLowerCaseNames()
Returns true if parameter names are to be converted to lower case when name/value pairs are parsed.

Returns:
true if parameter names are to be converted to lower case when name/value pairs are parsed. Otherwise returns false

setLowerCaseNames

public void setLowerCaseNames(boolean b)
Sets the flag if parameter names are to be converted to lower case when name/value pairs are parsed.

Parameters:
b - true if parameter names are to be converted to lower case when name/value pairs are parsed. false otherwise.

parse

public java.util.Map parse(java.lang.String str,
                           char separator)
Extracts a map of name/value pairs from the given string. Names are expected to be unique.

Parameters:
str - the string that contains a sequence of name/value pairs
separator - the name/value pairs separator
Returns:
a map of name/value pairs

parse

public java.util.Map parse(char[] chars,
                           char separator)
Extracts a map of name/value pairs from the given array of characters. Names are expected to be unique.

Parameters:
chars - the array of characters that contains a sequence of name/value pairs
separator - the name/value pairs separator
Returns:
a map of name/value pairs

parse

public java.util.Map parse(char[] chars,
                           int offset,
                           int length,
                           char separator)
Extracts a map of name/value pairs from the given array of characters. Names are expected to be unique.

Parameters:
chars - the array of characters that contains a sequence of name/value pairs
offset - - the initial offset.
length - - the length.
separator - the name/value pairs separator
Returns:
a map of name/value pairs