rewrite-impl-servlet 1.0.5.Final

org.ocpsoft.rewrite.servlet.util
Class QueryStringBuilder

java.lang.Object
  extended by org.ocpsoft.rewrite.servlet.util.QueryStringBuilder

public class QueryStringBuilder
extends Object

Utility for building URL query strings.

Author:
Lincoln Baxter, III

Constructor Summary
QueryStringBuilder()
           
 
Method Summary
 void addParameter(String name, String... values)
          Add a single parameter with the given values.
 QueryStringBuilder addParameterArrays(Map<String,String[]> params)
          Add parameters from the given map of name=value pairs.
 QueryStringBuilder addParameterLists(Map<String,List<String>> params)
          Add parameters from the given map of name=value pairs.
 QueryStringBuilder addParameters(String url)
          Add parameters from the given URL.
static QueryStringBuilder createFrom(String parameters)
          Build a query string from the given URL.
static QueryStringBuilder createFromArrays(Map<String,String[]> params)
          Build a query string from the given map of name=value pairs.
static QueryStringBuilder createFromLists(Map<String,List<String>> params)
          Build a query string from the given map of name=value pairs.
static QueryStringBuilder createNew()
          Return a new empty instance of QueryStringBuilder
 QueryStringBuilder decode()
          Return a new QueryStringBuilder instance having called URLDecoder.decode(String, String) on each name=value pair.
 QueryStringBuilder encode()
          Return a new QueryStringBuilder instance having called URLEncoder.encode(String, String) on each name=value pair.
 String getParameter(String name)
          Get the first value of given parameter name.
 Map<String,List<String>> getParameterMap()
          Get the name, values[] map representing this query string.
 Set<String> getParameterNames()
          Get set of parameter names currently stored in this query string.
 String[] getParameterValues(String name)
          Get the array of values for a given parameter name.
 boolean isEmpty()
          Return true if this query string currently contains no parameters.
 List<String> removeParameter(String string)
           
 String toQueryString()
          Convert the current parameters to a valid query string, including the leading '?' character.
 String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

QueryStringBuilder

public QueryStringBuilder()
Method Detail

createNew

public static QueryStringBuilder createNew()
Return a new empty instance of QueryStringBuilder


createFrom

public static QueryStringBuilder createFrom(String parameters)
Build a query string from the given URL. If a '?' character is encountered in the URL, the any characters up to and including the first '?' will be ignored.


createFromArrays

public static QueryStringBuilder createFromArrays(Map<String,String[]> params)
Build a query string from the given map of name=value pairs. For parameters with more than one value, each value will be appended using the same name.


createFromLists

public static QueryStringBuilder createFromLists(Map<String,List<String>> params)
Build a query string from the given map of name=value pairs. For parameters with more than one value, each value will be appended using the same name.


addParameters

public QueryStringBuilder addParameters(String url)
Add parameters from the given URL. If a '?' character is encountered in the URL, the any characters up to and including the first '?' will be ignored. If a parameter already exists, append new values to the existing list of values for that parameter.


addParameter

public void addParameter(String name,
                         String... values)
Add a single parameter with the given values.


addParameterArrays

public QueryStringBuilder addParameterArrays(Map<String,String[]> params)
Add parameters from the given map of name=value pairs. For parameters with more than one value, each value will be appended using the same name. If a parameter already exists, append new values to the existing list of values for that parameter.


addParameterLists

public QueryStringBuilder addParameterLists(Map<String,List<String>> params)
Add parameters from the given map of name=value pairs. For parameters with more than one value, each value will be appended using the same name. If a parameter already exists, append new values to the existing list of values for that parameter.


decode

public QueryStringBuilder decode()
Return a new QueryStringBuilder instance having called URLDecoder.decode(String, String) on each name=value pair.


encode

public QueryStringBuilder encode()
Return a new QueryStringBuilder instance having called URLEncoder.encode(String, String) on each name=value pair.


getParameter

public String getParameter(String name)
Get the first value of given parameter name.

Returns:
The value of the parameter, null if the parameter does not exist, or "" if the parameter exists but has no values.

getParameterMap

public Map<String,List<String>> getParameterMap()
Get the name, values[] map representing this query string.


getParameterNames

public Set<String> getParameterNames()
Get set of parameter names currently stored in this query string.


getParameterValues

public String[] getParameterValues(String name)
Get the array of values for a given parameter name.

Returns:
The values of the parameter, null if the parameter does not exist.

isEmpty

public boolean isEmpty()
Return true if this query string currently contains no parameters.


removeParameter

public List<String> removeParameter(String string)

toQueryString

public String toQueryString()
Convert the current parameters to a valid query string, including the leading '?' character.

For example, a QueryStringBuilder with the values [key=>value, name=>value1,value2,value3] will become:

 
 ?key=value&name=value1&name=value2&name=value3
 

Returns:
If parameters exist, return a valid query string with leading '?' character. If no parameters exist, return an empty string.

toString

public String toString()
Overrides:
toString in class Object

rewrite-impl-servlet 1.0.5.Final

Copyright © 2012 OCPsoft. All Rights Reserved.