Java URL Query Parse parseQuery(String query)

Here you can find the source of parseQuery(String query)

Description

Utility method to parse the query part of a URL into parameters.

License

Open Source License

Declaration

public static Hashtable<String, String> parseQuery(String query) throws MalformedURLException 

Method Source Code

//package com.java2s;
/**//from w  w w .  j  av  a 2 s .c  o m
 *  '$RCSfile$'
 *    Purpose: A Class that implements utility methods for a metadata catalog
 *  Copyright: 2000 Regents of the University of California and the
 *             National Center for Ecological Analysis and Synthesis
 *    Authors: Matt Jones, Jivka Bojilova
 *
 *   '$Author: daigle $'
 *     '$Date: 2009-08-25 07:34:17 +1000 (Tue, 25 Aug 2009) $'
 * '$Revision: 5030 $'
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.net.MalformedURLException;
import java.util.Hashtable;

public class Main {
    /**
     * Utility method to parse the query part of a URL into parameters. This
     * method assumes the format of the query part of the url is an ampersand
     * separated list of name/value pairs, with equal signs separating the name
     * from the value (e.g., name=tom&zip=99801 ). Returns a has of the name
     * value pairs, hashed on name.
     */
    public static Hashtable<String, String> parseQuery(String query) throws MalformedURLException {
        String[][] params = new String[200][2];
        Hashtable<String, String> parameters = new Hashtable<String, String>();

        String temp = "";
        boolean ampflag = true;
        boolean poundflag = false;
        int arrcount = 0;

        if (query != null) {
            for (int i = 0; i < query.length(); i++) {

                // go throught the remainder of the query one character at a
                // time.
                if (query.charAt(i) == '=') {
                    // if the current char is a # then the preceding should be
                    // a name
                    if (!poundflag && ampflag) {
                        params[arrcount][0] = temp.trim();
                        temp = "";
                    } else {
                        //if there are two #s or &s in a row throw an
                        // exception.
                        throw new MalformedURLException(
                                "metacatURL: Two parameter names " + "not allowed in sequence");
                    }
                    poundflag = true;
                    ampflag = false;
                } else if (query.charAt(i) == '&' || i == query.length() - 1) {
                    //the text preceding the & should be the param value.
                    if (i == query.length() - 1) {
                        //if at the end of the string grab the last value and
                        // append it.
                        if (query.charAt(i) != '=') {
                            //ignore an extra & on the end of the string
                            temp += query.charAt(i);
                        }
                    }

                    if (!ampflag && poundflag) {
                        params[arrcount][1] = temp.trim();
                        parameters.put(params[arrcount][0], params[arrcount][1]);
                        temp = "";
                        arrcount++; //increment the array to the next row.
                    } else {
                        //if there are two =s or &s in a row through an
                        // exception
                        throw new MalformedURLException(
                                "metacatURL: Two parameter values " + "not allowed in sequence");
                    }
                    poundflag = false;
                    ampflag = true;
                } else {
                    //get the next character in the string
                    temp += query.charAt(i);
                }
            }
        }
        return parameters;
    }
}

Related

  1. parseInstructions(final String query)
  2. parseParameters( Optional queryString)
  3. parseParameters(String query)
  4. parseQuery(String query)
  5. parseQuery(String query)
  6. parseQuery(String query)
  7. parseQuery(String query)
  8. parseQuery(String queryString)
  9. parseQueryParameters( String query)