Here you can find the source of htmlEncode(String s, boolean encodeSpecialChars)
Parameter | Description |
---|---|
s | the String to escape. |
encodeSpecialChars | if true high characters will be encode other wise not. |
private static String htmlEncode(String s, boolean encodeSpecialChars)
//package com.java2s; /*// w w w . j av a 2 s . c om * Copyright (c) 2006-2013 by Public Library of Science * * http://plos.org * http://ambraproject.org * * 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. */ public class Main { /** * Escape html entity characters and high characters (eg "curvy" Word quotes). * Note this method can also be used to encode XML. * * @param s the String to escape. * @param encodeSpecialChars if true high characters will be encode other wise not. * @return the escaped string */ private static String htmlEncode(String s, boolean encodeSpecialChars) { s = noNull(s, ""); StringBuilder str = new StringBuilder(); for (int j = 0; j < s.length(); j++) { char c = s.charAt(j); // encode standard ASCII characters into HTML entities where needed if (c < '\200') { switch (c) { case '"': str.append("""); break; case '&': str.append("&"); break; case '<': str.append("<"); break; case '>': str.append(">"); break; default: str.append(c); } } // encode 'ugly' characters (ie Word "curvy" quotes etc) else if (encodeSpecialChars && (c < '\377')) { String hexChars = "0123456789ABCDEF"; int a = c % 16; int b = (c - a) / 16; str.append("&#x").append(hexChars.charAt(b)).append(hexChars.charAt(a)).append(';'); } //add other characters back in - to handle charactersets //other than ascii else { str.append(c); } } return str.toString(); } /** * Return <code>string</code>, or <code>defaultString</code> if * <code>string</code> is <code>null</code> or <code>""</code>. * Never returns <code>null</code>. * * <p>Examples:</p> * <pre> * // prints "hello" * String s=null; * System.out.println(TextUtils.noNull(s,"hello"); * * // prints "hello" * s=""; * System.out.println(TextUtils.noNull(s,"hello"); * * // prints "world" * s="world"; * System.out.println(TextUtils.noNull(s, "hello"); * </pre> * * @param string the String to check. * @param defaultString The default string to return if <code>string</code> is <code>null</code> or <code>""</code> * @return <code>string</code> if <code>string</code> is non-empty, and <code>defaultString</code> otherwise * @see #stringSet(String) */ private static String noNull(String string, String defaultString) { return (stringSet(string)) ? string : defaultString; } /** * Check whether <code>string</code> has been set to * something other than <code>""</code> or <code>null</code>. * @param string the <code>String</code> to check * @return a boolean indicating whether the string was non-empty (and non-null) */ private static boolean stringSet(String string) { return (string != null) && !"".equals(string); } }