/* * (C) 2007-2012 Alibaba Group Holding Limited. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * Authors: * wuhua <wq163@163.com> , boyan <killme2008@gmail.com> */ package com.taobao.metamorphosis.utils; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLDecoder; import java.util.Collections; import java.util.HashMap; import java.util.Map; import java.util.Scanner; /** * * @author �޻� * @since 2011-6-22 ����04:04:00 */ public class URIUtils { private static final String PARAMETER_SEPARATOR = "&"; private static final String NAME_VALUE_SEPARATOR = "="; public static final String DEFAULT_CONTENT_CHARSET = "ISO_8859_1"; public static Map<String, String> parseParameters(URI uri, String encoding) { Map<String, String> result = Collections.emptyMap(); final String query = uri.getRawQuery(); if (query != null && query.length() > 0) { result = new HashMap<String, String>(); parse(result, new Scanner(query), encoding); } return result; } private static void parse(Map<String, String> result, Scanner scanner, String encoding) { scanner.useDelimiter(PARAMETER_SEPARATOR); while (scanner.hasNext()) { final String[] nameValue = scanner.next().split(NAME_VALUE_SEPARATOR); if (nameValue.length == 0 || nameValue.length > 2) throw new IllegalArgumentException("bad parameter"); final String name = decode(nameValue[0], encoding); String value = null; if (nameValue.length == 2) { value = decode(nameValue[1], encoding); } result.put(name, value); } } private static String decode(final String content, final String encoding) { try { return URLDecoder.decode(content, encoding != null ? encoding : DEFAULT_CONTENT_CHARSET); } catch (UnsupportedEncodingException e) { throw new IllegalArgumentException(e); } } }