com.ocpsoft.pretty.faces.url.QueryString Maven / Gradle / Ivy
/*
* PrettyFaces is an OpenSource JSF library to create bookmarkable URLs.
* Copyright (C) 2010 - Lincoln Baxter, III This program
* is free software: you can redistribute it and/or modify it under the terms of
* the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later
* version. This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details. You should have received a copy of the GNU Lesser General
* Public License along with this program. If not, see the file COPYING.LESSER
* or visit the GNU website at .
*/
package com.ocpsoft.pretty.faces.url;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;
import com.ocpsoft.pretty.PrettyException;
import com.ocpsoft.pretty.faces.config.mapping.RequestParameter;
/**
* @author Lincoln Baxter, III
*/
public class QueryString
{
private final Map> parameters = new HashMap>();
/**
* 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.
*/
public static QueryString build(final Map params)
{
QueryString queryString = new QueryString();
queryString.addParameters(params);
return queryString;
}
/**
* Build a query string from the given list of {@link RequestParameter}
* objects.
*/
public static QueryString build(final List params)
{
QueryString queryString = new QueryString();
queryString.addParameters(params);
return queryString;
}
/**
* 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.
*/
public static QueryString build(final String url)
{
QueryString queryString = new QueryString();
queryString.addParameters(url);
return queryString;
}
/*
* Utility Methods
*/
/**
* Return true if this query string currently contains no parameters.
*/
public boolean isEmpty()
{
return parameters.isEmpty();
}
/*
* Work methods
*/
/**
* Get the first value of given parameter name.
*
* @return The value of the parameter, null if the parameter does not exist,
* or "" if the parameter exists but has no values.
*/
public String getParameter(final String name)
{
List values = parameters.get(name);
if (values == null)
{
return null;
}
if (values.size() == 0)
{
return "";
}
return values.get(0);
}
/**
* Get the array of values for a given parameter name.
*
* @return The values of the parameter, null if the parameter does not exist.
*/
public String[] getParameterValues(final String name)
{
List values = parameters.get(name);
if (values == null)
{
return null;
}
return values.toArray(new String[values.size()]);
}
/**
* Get the list of parameter names currently stored in this query string..
*/
public Enumeration getParameterNames()
{
return Collections.enumeration(parameters.keySet());
}
/**
* Get the name, values[] map representing this query string.
*/
public Map getParameterMap()
{
Map map = new TreeMap();
for (Map.Entry> entry : parameters.entrySet())
{
List list = entry.getValue();
String[] values;
if (list == null)
{
values = null;
}
else
{
values = list.toArray(new String[list.size()]);
}
map.put(entry.getKey(), values);
}
return map;
}
/*
* Add Parameters
*/
/**
* Add query parameters from the given list of {@link RequestParameter}
* objects. If a parameter already exists, append new values to the existing
* list of values for that parameter.
*/
public void addParameters(final List params)
{
for (RequestParameter rp : params)
{
String value = rp.getValue();
String name = rp.getName();
if (!parameters.containsKey(name))
{
ArrayList values = new ArrayList();
if (value != null)
{
values.add(value);
}
parameters.put(name, values);
}
else
{
parameters.get(name).add(value);
}
}
}
/**
* 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.
*/
public void addParameters(final Map params)
{
if (params != null)
{
for (Entry entry : params.entrySet())
{
List values = null;
if (entry.getValue() != null)
{
values = Arrays.asList(entry.getValue());
}
parameters.put(entry.getKey(), values);
}
}
}
/**
* 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.
*/
public void addParameters(String url)
{
if ((url != null) && !"".equals(url))
{
url = url.trim();
if (url.length() > 1)
{
if (url.contains("?"))
{
url = url.substring(url.indexOf('?') + 1);
}
String pairs[] = url.split("&");
for (String pair : pairs)
{
String name;
String value;
int pos = pair.indexOf('=');
// for "n=", the value is "", for "n", the value is null
if (pos == -1)
{
name = pair;
value = null;
}
else
{
try
{
// FIXME This probably shouldn't be happening here.
name = URLDecoder.decode(pair.substring(0, pos), "UTF-8");
value = URLDecoder.decode(pair.substring(pos + 1, pair.length()), "UTF-8");
}
catch (UnsupportedEncodingException e)
{
throw new PrettyException("UTF-8 encoding not supported. Something is seriously wrong with your environment.");
}
}
List list = parameters.get(name);
if (list == null)
{
list = new ArrayList();
parameters.put(name, list);
}
list.add(value);
}
}
}
return;
}
/**
* Convert the current parameters to a valid query string, including the
* leading '?' character.
*
*
* For example, a {@link QueryString} with the values [key=>value,
* name=>value1,value2,value3] will become:
*
*
*
* ?key=value&name=value1&name=value2&name=value3
*
*
* @return If parameters exist, return a valid query string with leading '?'
* character. If no parameters exist, return an empty string.
*/
public String toQueryString()
{
try
{
StringBuffer result = new StringBuffer();
if ((null != parameters) && !parameters.isEmpty())
{
result.append("?");
Iterator>> iterator = parameters.entrySet().iterator();
while (iterator.hasNext())
{
Entry> entry = iterator.next();
String key = entry.getKey();
List values = entry.getValue();
if ((key != null) && !"".equals(key))
{
key = URLEncoder.encode(key, "UTF-8");
result.append(key);
if ((values != null) && !values.isEmpty())
{
for (int i = 0; i < values.size(); i++)
{
String value = values.get(i);
if ((value != null) && !"".equals(value))
{
value = URLEncoder.encode(value, "UTF-8");
result.append("=" + value);
}
else if ((value != null) && "".equals(value))
{
result.append("=");
}
if (i < values.size() - 1)
{
result.append("&" + key);
}
}
}
}
if (iterator.hasNext())
{
result.append("&");
}
}
}
return result.toString();
}
catch (UnsupportedEncodingException e)
{
throw new PrettyException("Error building query string.", e);
}
}
@Override
public String toString()
{
return toQueryString();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy