Here you can find the source of escapeUnsafeCharacters(String anyURI, boolean escapePercent)
Parameter | Description |
---|---|
anyURI | a parameter |
escapePercent | - escapes '%' to it's hex value if true, ignores it otherwise. |
public static String escapeUnsafeCharacters(String anyURI, boolean escapePercent)
//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; } }