Java String Escape escapeUnsafeCharacters(String anyURI, boolean escapePercent)

Here you can find the source of escapeUnsafeCharacters(String anyURI, boolean escapePercent)

Description

Escapes the "delim" and "unwise" characters as specified by rfc2396.

License

Open Source License

Parameter

Parameter Description
anyURI a parameter
escapePercent - escapes '%' to it's hex value if true, ignores it otherwise.

Declaration

public static String escapeUnsafeCharacters(String anyURI, boolean escapePercent) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2005, 2012 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   www .ja  va  2 s  . c o m
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.util.HashMap;
import java.util.Iterator;

public class Main {
    public static final String PERCENT = "%";
    protected static HashMap fCharToEscaped = new HashMap(15);

    /**
     * Escapes the "delim" and "unwise" characters as specified by rfc2396.  Also escapes the tilde (~) as this
     * also seems to cause problems with the XSD validator.  The characters are escaped using the UTF-8 hex
     * notation, %HH.
     *
     * To do undo this operation, call convertUriToNamespace
     *
     * @param anyURI
     * @param escapePercent - escapes '%' to it's hex value if true, ignores it otherwise.
     * @return
     */
    public static String escapeUnsafeCharacters(String anyURI, boolean escapePercent) {
        if (anyURI == null)
            return null;

        // must escape % first since our escapes have % in them.
        if (escapePercent)
            anyURI = anyURI.replaceAll("\\" + PERCENT, (String) fCharToEscaped.get(PERCENT)); //$NON-NLS-1$

        String key = null;

        // escape all other characters except %
        for (Iterator i = fCharToEscaped.keySet().iterator(); i.hasNext();) {
            key = (String) i.next();
            if (key.equals(PERCENT))
                continue;

            anyURI = anyURI.replaceAll("\\" + key, (String) fCharToEscaped.get(key)); //$NON-NLS-1$
        }

        return anyURI;
    }
}

Related

  1. escapeSelected(String str, String chars)
  2. escapeSolr(String value)
  3. escapeToFileName(String name)
  4. escapeUnicode(String s)
  5. escapeUnicodeString(final String input, final boolean escapeAscii)
  6. escapeWiki(String s)
  7. escapeXML(String message)
  8. escapeXml(String str)
  9. escapeXML(String str)