Java URI Parse parseQuery(URI aURI, boolean shouldDecode)

Here you can find the source of parseQuery(URI aURI, boolean shouldDecode)

Description

Parse the query of a URI into a set of properties.

License

Open Source License

Parameter

Parameter Description
aURI the URI.
shouldDecode true if escaped octets should be decoded.

Return

a map of the Properties.

Declaration

public static Properties parseQuery(URI aURI, boolean shouldDecode) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright 2000, 2006 Visual Systems Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License version 2
 * which accompanies this distribution in a file named "COPYING".
 * // w w w.  j  a  v a  2  s  .c  om
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *******************************************************************************/

import java.net.*;
import java.util.*;

public class Main {
    /**
     * Parse the query of a URI into a set of properties. Escaped octets are decoded.
     *
     * @param aURI the URI.
     *
     * @return a map of the Properties.
     */
    public static Properties parseQuery(URI aURI) {
        return parseQuery(aURI, true);
    }

    /**
     * Parse the query of a URI into a set of properties.
     *
     * @param aURI the URI.
     * @param shouldDecode true if escaped octets should be decoded.
     *
     * @return a map of the Properties.
     */
    public static Properties parseQuery(URI aURI, boolean shouldDecode) {
        return parseQuery(aURI.getRawQuery(), shouldDecode, new Properties());
    }

    /**
     * Parse the query of a URI into a set of properties. Escaped octets are decoded.
     *
     * @param aQueryString the URI query String.
     *
     * @return a map of the Properties.
     */
    public static Properties parseQuery(String aQueryString) {
        return parseQuery(aQueryString, true, new Properties());
    }

    /**
     * Parse the query of a URI into a set of properties.
     *
     * @param aQueryString the URI query String.
     * @param shouldDecode true if escaped octets should be decoded.
     * @param someProperties a map of Properties that will be amended by this method.
     *
     * @return a map of the Properties.
     */
    public static Properties parseQuery(String aQueryString, boolean shouldDecode, Properties someProperties) {
        StringTokenizer tokenizer = new StringTokenizer(aQueryString, "&+");
        while (tokenizer.hasMoreTokens()) {
            String nv = tokenizer.nextToken();
            String name;
            String value = "";
            int idx = nv.indexOf('=');
            if (idx > 0) {
                name = nv.substring(0, idx);
                value = nv.substring(idx + 1);
            } else {
                name = nv;
            }

            someProperties.setProperty(name, shouldDecode ? unescape(value) : value);
        }

        return someProperties;
    }

    /**
     * Unescapes hex values in a URI query value.
     *
     * @param aValue the string to be unescaped. May be null.
     *
     * @return the unescaped string, or null if aValue was null.
     */
    public static String unescape(String aValue) {
        if (aValue == null) {
            return aValue;
        }

        char[] chars = aValue.toCharArray();
        StringBuffer buf = new StringBuffer(chars.length);
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] == '%') {
                ++i;
                buf.append((char) ((charToNybble(chars[i++]) << 4) | charToNybble(chars[i])));
            } else {
                buf.append(chars[i]);
            }
        }

        return buf.toString();
    }

    private static int charToNybble(char c) {
        if (Character.isDigit(c)) {
            return c - '0';
        }

        return (Character.toUpperCase(c) - 'A') + 10;
    }
}

Related

  1. parseParameters(URI uri)
  2. parseParentURI(final URI uri)
  3. parsePathElements(URI uri)
  4. parsePorts(URI connectionURI)
  5. parseQuery(String uri)
  6. parseQuery(URI uri)
  7. parseQuery(URI uri)
  8. parseQueryString(final URI uri, final M queryParams)
  9. parseQueryString(URI uri)