Tuesday, January 10, 2012

Generate URL query string from key value parameters


Java class to generate URL query string from key value pairs. Source code below.




Source code:

/**
 * generates url from map
 * @author sergej.sizov
 */
public class UrlGenerator {

    private static String DEFAULT_ENCODING_FOR_URL = "UTF-8";

    private String address;
    private Map<String, String> map = new LinkedHashMap<String, String>();

    public UrlGenerator() {
        this.address = "";
    }

    public UrlGenerator(String address) {
        this.address = address;
    }


    public String getParameter(String param) {
        return map.get(param);
    }

    public void setParameter(String param, Object value) {
        map.put(param, value.toString());
    }

    public void unsetParameter(String param) {
        map.remove(param);
    }

    public String getUrl() {
        StringBuilder sb = new StringBuilder(address);

        List<String> listOfParams = new ArrayList<String>();
        for (String param : map.keySet()) {
            listOfParams.add(param + "=" + encodeString(map.get(param)));
        }

        if (!listOfParams.isEmpty()) {
            String query = StringUtils.join(listOfParams, "&");
            sb.append("?");
            sb.append(query);
        }

        return sb.toString();
    }

    public static String encodeString(String name) throws NullPointerException {
        String tmp = null;

        if (name == null)
            return null;

        try {
            tmp = java.net.URLEncoder.encode(name, DEFAULT_ENCODING_FOR_URL);
        } catch (Exception e) {}

        if (tmp == null)
            throw new NullPointerException();

        return tmp;
    }

}

No comments:

Post a Comment