/** * @param queryString a query string of the form n1=v1&n2=v2&... to decode. May be null. * @param acceptAmp -> "&" if true, "&" if false * @return a Map of String[] indexed by name, an empty Map if the query string was null */ public static Map<String, String[]> decodeQueryString( final CharSequence queryString, final boolean acceptAmp) { final Map<String, String[]> result = new TreeMap<String, String[]>(); if (queryString != null) { final Matcher matcher = acceptAmp ? PATTERN_AMP.matcher(queryString) : PATTERN_NO_AMP.matcher(queryString); int matcherEnd = 0; while (matcher.find()) { matcherEnd = matcher.end(); try { // Group 0 is the whole match, e.g. a=b, while group 1 is the first group // denoted ( with parens ) in the expression. Hence we start with group 1. final String name = URLDecoder.decode(matcher.group(1), NetUtils.STANDARD_PARAMETER_ENCODING); final String value = URLDecoder.decode(matcher.group(2), NetUtils.STANDARD_PARAMETER_ENCODING); StringUtils.addValueToStringArrayMap(result, name, value); } catch (UnsupportedEncodingException e) { // Should not happen as we are using a required encoding throw new OXFException(e); } } if (queryString.length() != matcherEnd) { // There was garbage at the end of the query. throw new OXFException("Malformed URL: " + queryString); } } return result; }