Java URI Create getURIParameterValue(URI uri, String name, String defVal)

Here you can find the source of getURIParameterValue(URI uri, String name, String defVal)

Description

get URI Parameter Value

License

Open Source License

Declaration

public static String getURIParameterValue(URI uri, String name,
            String defVal) 

Method Source Code

//package com.java2s;
/*//from   w w w  .j  a  va  2s  .  c  o m
 * JBoss, Home of Professional Open Source.
 * Copyright 2014, Red Hat, Inc., and individual contributors
 * as indicated by the @author tags. See the copyright.txt file in the
 * distribution for a full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.UnsupportedEncodingException;

import java.net.URI;

import java.net.URLDecoder;

public class Main {
    public static String getURIParameterValue(URI uri, String name,
            String defVal) {
        if (uri == null || name == null)
            return defVal;
        final String rawQuery = uri.getRawQuery();
        if (rawQuery == null)
            return defVal;
        String part;
        int s = 0;
        int e = rawQuery.indexOf('&');
        int i;
        String k, v;
        while (e != -1) {
            part = rawQuery.substring(s, e);
            i = part.indexOf('=');
            if (i == -1) {
                s = e + 1;
                e = rawQuery.indexOf('&', s);
                continue;
            }
            try {
                k = URLDecoder.decode(rawQuery.substring(s, i), "UTF-8");
                v = URLDecoder.decode(rawQuery.substring(s + i + 1, e),
                        "UTF-8");
            } catch (UnsupportedEncodingException e1) {
                s = e + 1;
                e = rawQuery.indexOf('&', s);
                continue;
            }
            if (k.equals(name)) {
                return v;
            }
        }
        part = rawQuery.substring(s);
        i = part.indexOf('=');
        if (i == -1) {
            return defVal;
        }
        try {
            k = URLDecoder.decode(rawQuery.substring(s, i), "UTF-8");
            v = URLDecoder.decode(rawQuery.substring(s + i + 1), "UTF-8");
        } catch (UnsupportedEncodingException e1) {
            return defVal;
        }
        if (k.equals(name)) {
            return v;
        }
        return defVal;
    }
}

Related

  1. getURIFromEncodedString(String unencoded)
  2. getURIFromPath(String fileOrURI)
  3. getURIFromPath(String path)
  4. getURIName(String forwardSlashPath)
  5. getURIName(URI uri)
  6. getURIs(final Object[] objs)
  7. getUriScheme(String address)
  8. getURIsPrettyPrint(final Collection uris)
  9. getURIStream(Object obj)