com.Ostermiller.util Java Utilities


com.Ostermiller.util
Class CGIParser

java.lang.Object
  extended by com.Ostermiller.util.CGIParser

public class CGIParser
extends Object

Parses query string data from a CGI request into name value pairs.

More information about this class is available from ostermiller.org.

Since:
ostermillerutils 1.00.00
Author:
Stephen Ostermiller http://ostermiller.org/contact.pl?regarding=Java+Utilities

Constructor Summary
CGIParser(InputStream in)
          Deprecated. This method does not decode URLEncoded values properly. Please use a method that specifies a character set.
CGIParser(InputStream in, String charset)
          Extract the name, value pairs from the given input stream and make them available for retrieval.
CGIParser(Reader in)
          Deprecated. This method does not decode URLEncoded values properly. Please use a method that specifies a character set.
CGIParser(Reader in, String charset)
          Extract the name, value pairs from the given reader and make them available for retrieval.
CGIParser(String s)
          Deprecated. This method does not decode URLEncoded values properly. Please use a method that specifies a character set.
CGIParser(String s, String charset)
          Extract the name, value pairs from the given string and make them available for retrieval.
 
Method Summary
 void addParameter(String name, String value)
          Set a name value pair as used in a URL.
 void addParameter(String name, String[] values)
          Set a name value pair as used in a URL.
 String getParameter(String name)
          Returns the value of a request parameter as a String, or null if the parameter does not exist.
 String[] getParameterNameList()
          Returns the names of the parameters contained in this request.
 Enumeration<String> getParameterNames()
          Returns an Enumeration of String objects containing the names of the parameters contained in this request.
 NameValuePair[] getParameters()
          Get the all the parameters in the order in which they were added.
 String[] getParameterValues(String name)
          Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.
 void setParameter(String name, String value)
          Set a name value pair as used in a URL.
 void setParameter(String name, String[] values)
          Set a name value pair as used in a URL.
 String toString()
          Returns the name value pairs properly escaped and written in URL format with UTF-8 URL encoding.
 String toString(String enc)
          Returns the name value pairs properly escaped and written in URL format.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CGIParser

@Deprecated
public CGIParser(InputStream in)
          throws IOException
Deprecated. This method does not decode URLEncoded values properly. Please use a method that specifies a character set.

Extract the name, value pairs from the given input stream and make them available for retrieval.

The stream is read until the stream contains no more bytes.

Byte to character conversion on the stream is done according the platforms default character encoding.

Parameters:
in - Stream containing CGI Encoded name value pairs.
Throws:
IOException - If an input error occurs
Since:
ostermillerutils 1.00.00

CGIParser

public CGIParser(InputStream in,
                 String charset)
          throws IOException,
                 UnsupportedEncodingException
Extract the name, value pairs from the given input stream and make them available for retrieval.

The stream is read until the stream contains no more bytes.

The character set is used both when converting the byte stream to a character stream and when decoding URL decoded parameters.

Parameters:
in - Stream containing CGI Encoded name value pairs.
charset - Character encoding to use when converting bytes to characters
Throws:
IOException - If an input error occurs
UnsupportedEncodingException - If the character set provided is not recognized
Since:
ostermillerutils 1.03.00

CGIParser

@Deprecated
public CGIParser(Reader in)
          throws IOException
Deprecated. This method does not decode URLEncoded values properly. Please use a method that specifies a character set.

Extract the name, value pairs from the given reader and make them available for retrieval.

The reader is read until the stream contains no more characters.

Parameters:
in - Reader containing CGI Encoded name value pairs.
Throws:
IOException - If an input error occurs
Since:
ostermillerutils 1.00.00

CGIParser

public CGIParser(Reader in,
                 String charset)
          throws IOException,
                 UnsupportedEncodingException
Extract the name, value pairs from the given reader and make them available for retrieval.

The reader is read until the stream contains no more characters.

Parameters:
in - Reader containing CGI Encoded name value pairs.
charset - Character encoding to use when converting bytes to characters
Throws:
IOException - If an input error occurs
UnsupportedEncodingException - If the character set provided is not recognized
Since:
ostermillerutils 1.03.00

CGIParser

@Deprecated
public CGIParser(String s)
Deprecated. This method does not decode URLEncoded values properly. Please use a method that specifies a character set.

Extract the name, value pairs from the given string and make them available for retrieval.

Parameters:
s - CGI Encoded name value pairs.
Since:
ostermillerutils 1.00.00

CGIParser

public CGIParser(String s,
                 String charset)
          throws UnsupportedEncodingException
Extract the name, value pairs from the given string and make them available for retrieval.

Parameters:
s - CGI Encoded name value pairs.
charset - Character encoding to use when converting bytes to characters
Throws:
UnsupportedEncodingException - If the character set provided is not recognized
Since:
ostermillerutils 1.03.00
Method Detail

getParameterValues

public String[] getParameterValues(String name)
Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.

If the parameter has a single value, the array has a length of 1.

Parameters:
name - a String containing the name of the parameter whose value is requested
Returns:
an array of String objects containing the parameter's values
Since:
ostermillerutils 1.00.00

setParameter

public void setParameter(String name,
                         String value)
Set a name value pair as used in a URL. This method will replace any previously defined values with the single value specified. If the value is null, the name is removed.

Parameters:
name - a String specifying the name of the parameter.
value - a String specifying the value of the single parameter, or null to remove.
Since:
ostermillerutils 1.02.15

setParameter

public void setParameter(String name,
                         String[] values)
Set a name value pair as used in a URL. This method will replace any previously defined values with the single value specified. If values is null or empty, the name is removed.

Parameters:
name - a String specifying the name of the parameter.
values - a String array specifying the values for the parameter, or null to remove.
Throws:
NullPointerException - if any of the values is null.
Since:
ostermillerutils 1.02.15

addParameter

public void addParameter(String name,
                         String value)
Set a name value pair as used in a URL. This method will add to any previously defined values the values specified. If value is null, this method has no effect.

Parameters:
name - a String specifying the name of the parameter.
value - a String specifying the value of the single parameter, or null to remove.
Since:
ostermillerutils 1.02.15

addParameter

public void addParameter(String name,
                         String[] values)
Set a name value pair as used in a URL. This method will add to any previously defined values the values specified. If values is null, this method has no effect.

Parameters:
name - a String specifying the name of the parameter.
values - a String array specifying the values of the parameter, or null to remove.
Throws:
NullPointerException - if any of the values is null.
Since:
ostermillerutils 1.02.15

getParameter

public String getParameter(String name)
Returns the value of a request parameter as a String, or null if the parameter does not exist. Request parameters are extra information sent with the request.

You should only use this method when you are sure the parameter has only one value. If the parameter might have more than one value, use getParameterValues(java.lang.String).

If you use this method with a multiple valued parameter, the value returned is equal to the first value in the array returned by getParameterValues.

Parameters:
name - a String specifying the name of the parameter
Returns:
a String representing the single value of the parameter
Since:
ostermillerutils 1.00.00

getParameterNames

public Enumeration<String> getParameterNames()
Returns an Enumeration of String objects containing the names of the parameters contained in this request. If the request has no parameters, the method returns an empty Enumeration.

Returns:
an Enumeration of String objects, each String containing the name of a request parameter; or an empty Enumeration if the request has no parameters
Since:
ostermillerutils 1.00.00

getParameterNameList

public String[] getParameterNameList()
Returns the names of the parameters contained in this request. If the request has no parameters, the method returns an empty String array. Each name will appear only once, even if it was contained in the request multiple times. The order of the names may not match the order from the request.

Returns:
An array of Strings, each of which is the name of a request parameter; or an array of length zero if the request has no parameters
Since:
ostermillerutils 1.03.00

getParameters

public NameValuePair[] getParameters()
Get the all the parameters in the order in which they were added.

Returns:
array of all name value pairs.

toString

public String toString(String enc)
                throws UnsupportedEncodingException
Returns the name value pairs properly escaped and written in URL format.

Parameters:
enc - Character encoding to use when escaping characters.
Returns:
URLEncoded name value pairs.
Throws:
UnsupportedEncodingException - If the named encoding is not supported.
Since:
ostermillerutils 1.00.00

toString

public String toString()
Returns the name value pairs properly escaped and written in URL format with UTF-8 URL encoding.

Overrides:
toString in class Object
Returns:
URLEncoded name value pairs.
Since:
ostermillerutils 1.03.00

com.Ostermiller.util Java Utilities


Copyright © 2001-2012 by Stephen Ostermiller