📄 stringhelper.java
字号:
sb.append(s.substring(start, end)); sb.append(replace); start = end + findLength; end = s.indexOf(find, start); } end = stringLength; sb.append(s.substring(start, end)); return (sb.toString()); } /** * Replaces characters that may be confused by a HTML * parser with their equivalent character entity references. * <p> * Any data that will appear as text on a web page should * be be escaped. This is especially important for data * that comes from untrusted sources such as Internet users. * A common mistake in CGI programming is to ask a user for * data and then put that data on a web page. For example:<pre> * Server: What is your name? * User: <b>Joe<b> * Server: Hello <b>Joe</b>, Welcome</pre> * If the name is put on the page without checking that it doesn't * contain HTML code or without sanitizing that HTML code, the user * could reformat the page, insert scripts, and control the the * content on your web server. * <p> * This method will replace HTML characters such as > with their * HTML entity reference (&gt;) so that the html parser will * be sure to interpret them as plain text rather than HTML or script. * <p> * This method should be used for both data to be displayed in text * in the html document, and data put in form elements. For example:<br> * <code><html><body><i>This in not a &lt;tag&gt; * in HTML</i></body></html></code><br> * and<br> * <code><form><input type="hidden" name="date" value="<i>This data could * be &quot;malicious&quot;</i>"></form></code><br> * In the second example, the form data would be properly be resubmitted * to your cgi script in the URLEncoded format:<br> * <code><i>This data could be %22malicious%22</i></code> * * @param s String to be escaped * @return escaped String * @throws NullPointerException if s is null. * * @since ostermillerutils 1.00.00 */ public static String escapeHTML(String s){ int length = s.length(); int newLength = length; boolean someCharacterEscaped = false; // first check for characters that might // be dangerous and calculate a length // of the string that has escapes. for (int i=0; i<length; i++){ char c = s.charAt(i); int cint = 0xffff & c; if (cint < 32){ switch(c){ case '\r': case '\n': case '\t': case '\f':{ } break; default: { newLength -= 1; someCharacterEscaped = true; } } } else { switch(c){ case '\"':{ newLength += 5; someCharacterEscaped = true; } break; case '&': case '\'':{ newLength += 4; someCharacterEscaped = true; } break; case '<': case '>':{ newLength += 3; someCharacterEscaped = true; } break; } } } if (!someCharacterEscaped){ // nothing to escape in the string return s; } StringBuffer sb = new StringBuffer(newLength); for (int i=0; i<length; i++){ char c = s.charAt(i); int cint = 0xffff & c; if (cint < 32){ switch(c){ case '\r': case '\n': case '\t': case '\f':{ sb.append(c); } break; default: { // Remove this character } } } else { switch(c){ case '\"':{ sb.append("""); } break; case '\'':{ sb.append("'"); } break; case '&':{ sb.append("&"); } break; case '<':{ sb.append("<"); } break; case '>':{ sb.append(">"); } break; default: { sb.append(c); } } } } return sb.toString(); } /** * Replaces characters that may be confused by an SQL * parser with their equivalent escape characters. * <p> * Any data that will be put in an SQL query should * be be escaped. This is especially important for data * that comes from untrusted sources such as Internet users. * <p> * For example if you had the following SQL query:<br> * <code>"SELECT * FROM addresses WHERE name='" + name + "' AND private='N'"</code><br> * Without this function a user could give <code>" OR 1=1 OR ''='"</code> * as their name causing the query to be:<br> * <code>"SELECT * FROM addresses WHERE name='' OR 1=1 OR ''='' AND private='N'"</code><br> * which will give all addresses, including private ones.<br> * Correct usage would be:<br> * <code>"SELECT * FROM addresses WHERE name='" + StringHelper.escapeSQL(name) + "' AND private='N'"</code><br> * <p> * Another way to avoid this problem is to use a PreparedStatement * with appropriate placeholders. * * @param s String to be escaped * @return escaped String * @throws NullPointerException if s is null. * * @since ostermillerutils 1.00.00 */ public static String escapeSQL(String s){ int length = s.length(); int newLength = length; // first check for characters that might // be dangerous and calculate a length // of the string that has escapes. for (int i=0; i<length; i++){ char c = s.charAt(i); switch(c){ case '\\': case '\"': case '\'': case '\0':{ newLength += 1; } break; } } if (length == newLength){ // nothing to escape in the string return s; } StringBuffer sb = new StringBuffer(newLength); for (int i=0; i<length; i++){ char c = s.charAt(i); switch(c){ case '\\':{ sb.append("\\\\"); } break; case '\"':{ sb.append("\\\""); } break; case '\'':{ sb.append("\\\'"); } break; case '\0':{ sb.append("\\0"); } break; default: { sb.append(c); } } } return sb.toString(); } /** * Replaces characters that are not allowed in a Java style * string literal with their escape characters. Specifically * quote ("), single quote ('), new line (\n), carriage return (\r), * and backslash (\), and tab (\t) are escaped. * * @param s String to be escaped * @return escaped String * @throws NullPointerException if s is null. * * @since ostermillerutils 1.00.00 */ public static String escapeJavaLiteral(String s){ int length = s.length(); int newLength = length; // first check for characters that might // be dangerous and calculate a length // of the string that has escapes. for (int i=0; i<length; i++){ char c = s.charAt(i); switch(c){ case '\"': case '\'': case '\n': case '\r': case '\t': case '\\':{ newLength += 1; } break; } } if (length == newLength){ // nothing to escape in the string return s; } StringBuffer sb = new StringBuffer(newLength); for (int i=0; i<length; i++){ char c = s.charAt(i); switch(c){ case '\"':{ sb.append("\\\""); } break; case '\'':{ sb.append("\\\'"); } break; case '\n':{ sb.append("\\n"); } break; case '\r':{ sb.append("\\r"); } break; case '\t':{ sb.append("\\t"); } break; case '\\':{ sb.append("\\\\"); } break; default: { sb.append(c); } } } return sb.toString(); } /** * Trim any of the characters contained in the second * string from the beginning and end of the first. * * @param s String to be trimmed. * @param c list of characters to trim from s. * @return trimmed String. * @throws NullPointerException if s is null. * * @since ostermillerutils 1.00.00 */ public static String trim(String s, String c){ int length = s.length(); if (c == null){ return s; } int cLength = c.length(); if (c.length() == 0){ return s; } int start = 0; int end = length; boolean found; // trim-able character found. int i; // Start from the beginning and find the // first non-trim-able character. found = false; for (i=0; !found && i<length; i++){ char ch = s.charAt(i); found = true; for (int j=0; found && j<cLength; j++){ if (c.charAt(j) == ch) found = false; } } // if all characters are trim-able. if (!found) return ""; start = i-1; // Start from the end and find the // last non-trim-able character. found = false; for (i=length-1; !found && i>=0; i--){ char ch = s.charAt(i); found = true; for (int j=0; found && j<cLength; j++){ if (c.charAt(j) == ch) found = false; } } end = i+2; return s.substring(start, end); } private static HashMap htmlEntities = new HashMap(); static { htmlEntities.put("nbsp", new Integer(160)); htmlEntities.put("iexcl", new Integer(161)); htmlEntities.put("cent", new Integer(162)); htmlEntities.put("pound", new Integer(163)); htmlEntities.put("curren", new Integer(164)); htmlEntities.put("yen", new Integer(165)); htmlEntities.put("brvbar", new Integer(166)); htmlEntities.put("sect", new Integer(167)); htmlEntities.put("uml", new Integer(168)); htmlEntities.put("copy", new Integer(169)); htmlEntities.put("ordf", new Integer(170)); htmlEntities.put("laquo", new Integer(171)); htmlEntities.put("not", new Integer(172)); htmlEntities.put("shy", new Integer(173)); htmlEntities.put("reg", new Integer(174)); htmlEntities.put("macr", new Integer(175)); htmlEntities.put("deg", new Integer(176)); htmlEntities.put("plusmn", new Integer(177)); htmlEntities.put("sup2", new Integer(178)); htmlEntities.put("sup3", new Integer(179)); htmlEntities.put("acute", new Integer(180)); htmlEntities.put("micro", new Integer(181)); htmlEntities.put("para", new Integer(182)); htmlEntities.put("middot", new Integer(183)); htmlEntities.put("cedil", new Integer(184)); htmlEntities.put("sup1", new Integer(185)); htmlEntities.put("ordm", new Integer(186)); htmlEntities.put("raquo", new Integer(187)); htmlEntities.put("frac14", new Integer(188)); htmlEntities.put("frac12", new Integer(189)); htmlEntities.put("frac34", new Integer(190)); htmlEntities.put("iquest", new Integer(191)); htmlEntities.put("Agrave", new Integer(192)); htmlEntities.put("Aacute", new Integer(193)); htmlEntities.put("Acirc", new Integer(194)); htmlEntities.put("Atilde", new Integer(195));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -