Java URI Encode encodeForURI(String uriPart)

Here you can find the source of encodeForURI(String uriPart)

Description

encode For URI

License

Open Source License

Declaration

public static String encodeForURI(String uriPart) 

Method Source Code


//package com.java2s;
/*/*w w w. j  a  v  a 2 s  .  c om*/
 *  eXist Open Source Native XML Database
 *  Copyright (C) 2001-05 The eXist Project
 *  http://exist-db.org
 *  
 *  This program 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
 *  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 Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *  
 *  $Id$
 */

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

public class Main {
    public static String encodeForURI(String uriPart) {
        String result = urlEncodeUtf8(uriPart);
        result = result.replaceAll("\\+", "%20");
        //result = result.replaceAll("%23", "#");      
        result = result.replaceAll("%2D", "-");
        result = result.replaceAll("%5F", "_");
        result = result.replaceAll("%2E", ".");
        //result = result.replaceAll("%21", "!");
        result = result.replaceAll("%7E", "~");
        result = result.replaceAll("\\*", "%2A");
        //result = result.replaceAll("%27", "'");
        //result = result.replaceAll("%28", "(");
        //result = result.replaceAll("%29", ")");
        return result;
    }

    /**
     * This method is a wrapper for {@link java.net.URLEncoder#encode(java.lang.String,java.lang.String)}
     * It calls this method, suppying the url parameter as
     * the first parameter, and "UTF-8" (the W3C recommended
     * encoding) as the second.  UnsupportedEncodingExceptions
     * are wrapped in a runtime exception.
     * 
     * IMPORTANT: the java.net.URLEncoder class encodes a space (" ")
     * as a "+".  The proper method of encoding spaces in the path of
     * a URI is with "%20", so this method will replace all instances of "+"
     * in the encoded string with "%20" before returning.  This means that
     * XmldbURIs constructed from java.net.URLEncoder#encoded strings
     * will not be String equivalents of XmldbURIs created with the result of
     * calls to this function.
     * 
     * @param uri The uri to encode
     * @return The UTF-8 encoded value of the supplied uri
     */
    public static String urlEncodeUtf8(String uri) {
        try {
            final String almostEncoded = URLEncoder.encode(uri, "UTF-8");
            return almostEncoded.replaceAll("\\+", "%20");
        } catch (final UnsupportedEncodingException e) {
            //wrap with a runtime Exception
            throw new RuntimeException(e);
        }
    }
}

Related

  1. encode(String str, boolean fullUri)
  2. encode(String uri)
  3. encodeIfNeeded(String uri)
  4. encodeUri(final String uri)
  5. encodeUri(final URI uri)
  6. encodeURI(String s)