📄 utilities.java
字号:
// now we'll see if the location is greater than the lower limit if(loc >= lower) { // yes it was, so we'll cut it off here str2 = str2.substring(0, loc); } else { // no it wasnt, so we'll cut it off at the upper limit str2 = str2.substring(0, upper); loc = upper; } // the string was truncated, so we append the appendToEnd String str = str2 + appendToEnd; } return str; } /** * @param str * @return */ private static String stripLineBreaks(String str) { // TODO: use a string buffer, ignore case ! str = str.replaceAll("<br>", ""); str = str.replaceAll("<br/>", ""); str = str.replaceAll("<br />", ""); str = str.replaceAll("<p></p>", ""); str = str.replaceAll("<p/>",""); str = str.replaceAll("<p />",""); return str; } /** * Need need to get rid of any user-visible HTML tags once all text has been * removed such as <BR>. This sounds like a better approach than removing * all HTML tags and taking the chance to leave some tags un-closed. * * WARNING: this method has serious performance problems a * * @author Alexis Moussine-Pouchkine <alexis.moussine-pouchkine@france.sun.com> * @author Lance Lavandowska * @param str the String object to modify * @return the new String object without the HTML "visible" tags */ private static String removeVisibleHTMLTags(String str) { str = stripLineBreaks(str); StringBuffer result = new StringBuffer(str); StringBuffer lcresult = new StringBuffer(str.toLowerCase()); // <img should take care of smileys String[] visibleTags = {"<img"}; // are there others to add? int stringIndex; for ( int j = 0 ; j < visibleTags.length ; j++ ) { while ( (stringIndex = lcresult.indexOf(visibleTags[j])) != -1 ) { if ( visibleTags[j].endsWith(">") ) { result.delete(stringIndex, stringIndex+visibleTags[j].length() ); lcresult.delete(stringIndex, stringIndex+visibleTags[j].length() ); } else { // need to delete everything up until next closing '>', for <img for instance int endIndex = result.indexOf(">", stringIndex); if (endIndex > -1) { // only delete it if we find the end! If we don't the HTML may be messed up, but we // can't safely delete anything. result.delete(stringIndex, endIndex + 1 ); lcresult.delete(stringIndex, endIndex + 1 ); } } } } // TODO: This code is buggy by nature. It doesn't deal with nesting of tags properly. // remove certain elements with open & close tags String[] openCloseTags = {"li", "a", "div", "h1", "h2", "h3", "h4"}; // more ? for (int j = 0; j < openCloseTags.length; j++) { // could this be better done with a regular expression? String closeTag = "</"+openCloseTags[j]+">"; int lastStringIndex = 0; while ( (stringIndex = lcresult.indexOf( "<"+openCloseTags[j], lastStringIndex)) > -1) { lastStringIndex = stringIndex; // Try to find the matching closing tag (ignores possible nesting!) int endIndex = lcresult.indexOf(closeTag, stringIndex); if (endIndex > -1) { // If we found it delete it. result.delete(stringIndex, endIndex+closeTag.length()); lcresult.delete(stringIndex, endIndex+closeTag.length()); } else { // Try to see if it is a self-closed empty content tag, i.e. closed with />. endIndex = lcresult.indexOf(">", stringIndex); int nextStart = lcresult.indexOf("<", stringIndex+1); if (endIndex > stringIndex && lcresult.charAt(endIndex-1) == '/' && (endIndex < nextStart || nextStart == -1)) { // Looks like it, so remove it. result.delete(stringIndex, endIndex + 1); lcresult.delete(stringIndex, endIndex + 1); } } } } return result.toString(); } /** * Extract (keep) JUST the HTML from the String. * @param str * @return */ public static String extractHTML(String str) { if (str == null) return ""; StringBuffer ret = new StringBuffer(str.length()); int start = 0; int beginTag = str.indexOf("<"); int endTag = 0; if (beginTag == -1) return str; while (beginTag >= start) { endTag = str.indexOf(">", beginTag); // if endTag found, keep tag if (endTag > -1) { ret.append( str.substring(beginTag, endTag+1) ); // move start forward and find another tag start = endTag + 1; beginTag = str.indexOf("<", start); } // if no endTag found, break else { break; } } return ret.toString(); } public static String hexEncode(String str) { if (StringUtils.isEmpty(str)) return str; return RegexUtil.encode(str); } public static String encodeEmail(String str) { return RegexUtil.encodeEmail(str); } /** * Converts a character to HTML or XML entity. * * @param ch The character to convert. * @param xml Convert the character to XML if set to true. * @author Erik C. Thauvin * * @return The converted string. */ public static final String charToHTML(char ch, boolean xml) { int c; // Convert left bracket if (ch == '<') { return ("<"); } // Convert left bracket else if (ch == '>') { return (">"); } // Convert ampersand else if (ch == '&') { return ("&"); } // Commented out to eliminate redundant numeric character codes (ROL-507) // High-ASCII character //else if (ch >= 128) //{ //c = ch; //return ("&#" + c + ';'); //} // Convert double quote else if (xml && (ch == '"')) { return ("""); } // Convert single quote else if (xml && (ch == '\'')) { return ("'"); } // No conversion else { // Return character as string return (String.valueOf(ch)); } } /** * Converts a text string to HTML or XML entities. * * @author Erik C. Thauvin * @param text The string to convert. * @param xml Convert the string to XML if set to true. * * @return The converted string. */ public static final String textToHTML(String text, boolean xml) { final StringBuffer html = new StringBuffer(); // Loop thru each characters of the text for (int i = 0; i < text.length(); i++) { // Convert character to HTML/XML html.append(charToHTML(text.charAt(i), xml)); } // Return HTML/XML string return html.toString(); } /** * Converts a text string to HTML or XML entities. * * @param text The string to convert. * @author Erik C. Thauvin * @return The converted string. */ public static final String textToHTML(String text) { return textToHTML(text, false); } /** * Converts a text string to XML entities. * * @param text The string to convert. * @author Erik C. Thauvin * @return The converted string. */ public static final String textToXML(String text) { return textToHTML(text, true); } /** * Converts a text string to HTML or XML entities. * @param text The string to convert. * @return The converted string. */ public static final String textToCDATA(String text) { final StringBuffer html = new StringBuffer(); // Loop thru each characters of the text for (int i = 0; i < text.length(); i++) { // Convert character to HTML/XML html.append(charToCDATA(text.charAt(i))); } // Return HTML/XML string return html.toString(); } /** * Converts a character to CDATA character. * @param ch The character to convert. * @return The converted string. */ public static final String charToCDATA(char ch) { int c; if (ch >= 128) { c = ch; return ("&#" + c + ';'); } // No conversion else { // Return character as string return (String.valueOf(ch)); } } public static final String encode(String s) { try { return URLEncoder.encode(s, "utf-8"); } catch (UnsupportedEncodingException e) { return s; } } /** * @param string * @return */ public static int stringToInt(String string) { try { return Integer.valueOf(string).intValue(); } catch (NumberFormatException e) { mLogger.debug("Invalid Integer:" + string); } return 0; } /** * Code (stolen from Pebble) to add rel="nofollow" string to all links in HTML. */ public static String addNofollow(String html) { if (html == null || html.length() == 0) { return html; } Matcher m = mLinkPattern.matcher(html); StringBuffer buf = new StringBuffer(); while (m.find()) { int start = m.start(); int end = m.end(); String link = html.substring(start, end); buf.append(html.substring(0, start)); if (link.indexOf("rel=\"nofollow\"") == -1) { buf.append( link.substring(0, link.length() - 1) + " rel=\"nofollow\">"); } else { buf.append(link); } html = html.substring(end, html.length()); m = mLinkPattern.matcher(html); } buf.append(html); return buf.toString(); } public static String unescapeHTML(String str) { return StringEscapeUtils.unescapeHtml(str); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -