Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java?rev=1561624&r1=1561623&r2=1561624&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/util/Utilities.java Mon Jan 27 10:06:12 2014 @@ -20,7 +20,6 @@ import java.util.Collections; import java.util.List; import java.util.Locale; import java.util.NoSuchElementException; -import java.util.StringTokenizer; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -29,98 +28,97 @@ import javax.activation.MimetypesFileTyp import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; -import org.apache.commons.lang.StringEscapeUtils; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.StringEscapeUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; -import org.apache.commons.codec.binary.Base64; import org.apache.roller.util.RegexUtil; - /** * General purpose utilities, not for use in templates. */ public class Utilities { /** The <code>Log</code> instance for this class. */ private static Log mLogger = LogFactory.getLog(Utilities.class); - + public static final String TAG_SPLIT_CHARS = " ,\n\r\f\t"; - - private static Pattern mLinkPattern = - Pattern.compile("<a href=.*?>", Pattern.CASE_INSENSITIVE); - private static final Pattern OPENING_B_TAG_PATTERN = - Pattern.compile("<b>", Pattern.CASE_INSENSITIVE); - private static final Pattern CLOSING_B_TAG_PATTERN = - Pattern.compile("</b>", Pattern.CASE_INSENSITIVE); - private static final Pattern OPENING_I_TAG_PATTERN = - Pattern.compile("<i>", Pattern.CASE_INSENSITIVE); - private static final Pattern CLOSING_I_TAG_PATTERN = - Pattern.compile("</i>", Pattern.CASE_INSENSITIVE); - private static final Pattern OPENING_BLOCKQUOTE_TAG_PATTERN = - Pattern.compile("<blockquote>", Pattern.CASE_INSENSITIVE); - private static final Pattern CLOSING_BLOCKQUOTE_TAG_PATTERN = - Pattern.compile("</blockquote>", Pattern.CASE_INSENSITIVE); - private static final Pattern BR_TAG_PATTERN = - Pattern.compile("<br */*>", Pattern.CASE_INSENSITIVE); - private static final Pattern OPENING_P_TAG_PATTERN = - Pattern.compile("<p>", Pattern.CASE_INSENSITIVE); - private static final Pattern CLOSING_P_TAG_PATTERN = - Pattern.compile("</p>", Pattern.CASE_INSENSITIVE); - private static final Pattern OPENING_PRE_TAG_PATTERN = - Pattern.compile("<pre>", Pattern.CASE_INSENSITIVE); - private static final Pattern CLOSING_PRE_TAG_PATTERN = - Pattern.compile("</pre>", Pattern.CASE_INSENSITIVE); - private static final Pattern OPENING_UL_TAG_PATTERN = - Pattern.compile("<ul>", Pattern.CASE_INSENSITIVE); - private static final Pattern CLOSING_UL_TAG_PATTERN = - Pattern.compile("</ul>", Pattern.CASE_INSENSITIVE); - private static final Pattern OPENING_OL_TAG_PATTERN = - Pattern.compile("<ol>", Pattern.CASE_INSENSITIVE); - private static final Pattern CLOSING_OL_TAG_PATTERN = - Pattern.compile("</ol>", Pattern.CASE_INSENSITIVE); - private static final Pattern OPENING_LI_TAG_PATTERN = - Pattern.compile("<li>", Pattern.CASE_INSENSITIVE); - private static final Pattern CLOSING_LI_TAG_PATTERN = - Pattern.compile("</li>", Pattern.CASE_INSENSITIVE); - private static final Pattern CLOSING_A_TAG_PATTERN = - Pattern.compile("</a>", Pattern.CASE_INSENSITIVE); - private static final Pattern OPENING_A_TAG_PATTERN = - Pattern.compile("<a href=.*?>", Pattern.CASE_INSENSITIVE); - private static final Pattern QUOTE_PATTERN = - Pattern.compile(""", Pattern.CASE_INSENSITIVE); - - - //------------------------------------------------------------------------ + + private static Pattern mLinkPattern = Pattern.compile("<a href=.*?>", + Pattern.CASE_INSENSITIVE); + private static final Pattern OPENING_B_TAG_PATTERN = Pattern.compile( + "<b>", Pattern.CASE_INSENSITIVE); + private static final Pattern CLOSING_B_TAG_PATTERN = Pattern.compile( + "</b>", Pattern.CASE_INSENSITIVE); + private static final Pattern OPENING_I_TAG_PATTERN = Pattern.compile( + "<i>", Pattern.CASE_INSENSITIVE); + private static final Pattern CLOSING_I_TAG_PATTERN = Pattern.compile( + "</i>", Pattern.CASE_INSENSITIVE); + private static final Pattern OPENING_BLOCKQUOTE_TAG_PATTERN = Pattern + .compile("<blockquote>", Pattern.CASE_INSENSITIVE); + private static final Pattern CLOSING_BLOCKQUOTE_TAG_PATTERN = Pattern + .compile("</blockquote>", Pattern.CASE_INSENSITIVE); + private static final Pattern BR_TAG_PATTERN = Pattern.compile( + "<br */*>", Pattern.CASE_INSENSITIVE); + private static final Pattern OPENING_P_TAG_PATTERN = Pattern.compile( + "<p>", Pattern.CASE_INSENSITIVE); + private static final Pattern CLOSING_P_TAG_PATTERN = Pattern.compile( + "</p>", Pattern.CASE_INSENSITIVE); + private static final Pattern OPENING_PRE_TAG_PATTERN = Pattern.compile( + "<pre>", Pattern.CASE_INSENSITIVE); + private static final Pattern CLOSING_PRE_TAG_PATTERN = Pattern.compile( + "</pre>", Pattern.CASE_INSENSITIVE); + private static final Pattern OPENING_UL_TAG_PATTERN = Pattern.compile( + "<ul>", Pattern.CASE_INSENSITIVE); + private static final Pattern CLOSING_UL_TAG_PATTERN = Pattern.compile( + "</ul>", Pattern.CASE_INSENSITIVE); + private static final Pattern OPENING_OL_TAG_PATTERN = Pattern.compile( + "<ol>", Pattern.CASE_INSENSITIVE); + private static final Pattern CLOSING_OL_TAG_PATTERN = Pattern.compile( + "</ol>", Pattern.CASE_INSENSITIVE); + private static final Pattern OPENING_LI_TAG_PATTERN = Pattern.compile( + "<li>", Pattern.CASE_INSENSITIVE); + private static final Pattern CLOSING_LI_TAG_PATTERN = Pattern.compile( + "</li>", Pattern.CASE_INSENSITIVE); + private static final Pattern CLOSING_A_TAG_PATTERN = Pattern.compile( + "</a>", Pattern.CASE_INSENSITIVE); + private static final Pattern OPENING_A_TAG_PATTERN = Pattern.compile( + "<a href=.*?>", Pattern.CASE_INSENSITIVE); + private static final Pattern QUOTE_PATTERN = Pattern.compile(""", + Pattern.CASE_INSENSITIVE); + + // ------------------------------------------------------------------------ /** Strip jsessionid off of a URL */ - public static String stripJsessionId( String url ) { + public static String stripJsessionId(String url) { // Strip off jsessionid found in referer URL int startPos = url.indexOf(";jsessionid="); - if ( startPos != -1 ) { - int endPos = url.indexOf('?',startPos); - if ( endPos == -1 ) { - url = url.substring(0,startPos); + if (startPos != -1) { + int endPos = url.indexOf('?', startPos); + if (endPos == -1) { + url = url.substring(0, startPos); } else { - url = url.substring(0,startPos) - + url.substring(endPos,url.length()); + url = url.substring(0, startPos) + + url.substring(endPos, url.length()); } } return url; } - - //------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ /** - * Escape, but do not replace HTML. - * The default behaviour is to escape ampersands. + * Escape, but do not replace HTML. The default behaviour is to escape + * ampersands. */ public static String escapeHTML(String s) { return escapeHTML(s, true); } - - //------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ /** * Escape, but do not replace HTML. - * @param escapeAmpersand Optionally escape - * ampersands (&). + * + * @param escapeAmpersand + * Optionally escape ampersands (&). */ public static String escapeHTML(String s, boolean escapeAmpersand) { // got to do amp's first so we don't double escape @@ -133,26 +131,24 @@ public class Utilities { s = StringUtils.replace(s, ">", ">"); return s; } - + public static String unescapeHTML(String str) { - return StringEscapeUtils.unescapeHtml(str); + return StringEscapeUtils.unescapeHtml4(str); } - - //------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ /** - * Remove occurrences of html, defined as any text - * between the characters "<" and ">". Replace - * any HTML tags with a space. + * Remove occurrences of html, defined as any text between the characters + * "<" and ">". Replace any HTML tags with a space. */ public static String removeHTML(String str) { return removeHTML(str, true); } - + /** - * Remove occurrences of html, defined as any text - * between the characters "<" and ">". - * Optionally replace HTML tags with a space. - * + * Remove occurrences of html, defined as any text between the characters + * "<" and ">". Optionally replace HTML tags with a space. + * * @param str * @param addSpace * @return @@ -172,14 +168,14 @@ public class Utilities { while (beginTag >= start) { if (beginTag > 0) { ret.append(str.substring(start, beginTag)); - + // replace each tag with a space (looks better) if (addSpace) { ret.append(" "); } } endTag = str.indexOf('>', beginTag); - + // if endTag found move "cursor" forward if (endTag > -1) { start = endTag + 1; @@ -197,32 +193,34 @@ public class Utilities { } return ret.toString().trim(); } - - //------------------------------------------------------------------------ - /** Run both removeHTML and escapeHTML on a string. - * @param s String to be run through removeHTML and escapeHTML. + + // ------------------------------------------------------------------------ + /** + * Run both removeHTML and escapeHTML on a string. + * + * @param s + * String to be run through removeHTML and escapeHTML. * @return String with HTML removed and HTML special characters escaped. */ - public static String removeAndEscapeHTML( String s ) { - if ( s==null ) { + public static String removeAndEscapeHTML(String s) { + if (s == null) { return ""; - } - else { - return Utilities.escapeHTML( Utilities.removeHTML(s) ); + } else { + return Utilities.escapeHTML(Utilities.removeHTML(s)); } } - - //------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ /** * Autoformat. */ public static String autoformat(String s) { return StringUtils.replace(s, "\n", "<br />"); } - - + /** - * Code (stolen from Pebble) to add rel="nofollow" string to all links in HTML. + * 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) { @@ -236,8 +234,8 @@ public class Utilities { String link = html.substring(start, end); buf.append(html.substring(0, start)); if (link.contains("rel=\"nofollow\"")) { - buf.append( - link.substring(0, link.length() - 1) + " rel=\"nofollow\">"); + buf.append(link.substring(0, link.length() - 1) + + " rel=\"nofollow\">"); } else { buf.append(link); } @@ -247,20 +245,18 @@ public class Utilities { buf.append(html); return buf.toString(); } - - - //------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ /** * Replaces occurences of non-alphanumeric characters with an underscore. */ public static String replaceNonAlphanumeric(String str) { return replaceNonAlphanumeric(str, '_'); } - - //------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ /** - * Replaces occurrences of non-alphanumeric characters with a - * supplied char. + * Replaces occurrences of non-alphanumeric characters with a supplied char. */ public static String replaceNonAlphanumeric(String str, char subst) { StringBuilder ret = new StringBuilder(str.length()); @@ -269,13 +265,13 @@ public class Utilities { if (Character.isLetterOrDigit(testChars[i])) { ret.append(testChars[i]); } else { - ret.append( subst ); + ret.append(subst); } } return ret.toString(); } - - //------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ /** * Remove occurences of non-alphanumeric characters. */ @@ -284,116 +280,99 @@ public class Utilities { char[] testChars = str.toCharArray(); for (int i = 0; i < testChars.length; i++) { // MR: Allow periods in page links - if (Character.isLetterOrDigit(testChars[i]) || - testChars[i] == '.') { + if (Character.isLetterOrDigit(testChars[i]) || testChars[i] == '.') { ret.append(testChars[i]); } } return ret.toString(); } - - //------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ /** Convert string array to string with delimeters. */ public static String stringArrayToString(String[] stringArray, String delim) { - String ret = ""; + StringBuilder bldr = new StringBuilder(); for (int i = 0; i < stringArray.length; i++) { - if (ret.length() > 0) { - ret = ret + delim + stringArray[i]; - } - else { - ret = stringArray[i]; + if (bldr.length() > 0) { + bldr.append(delim).append(stringArray[i]); + } else { + bldr.append(stringArray[i]); } } - return ret; + return bldr.toString(); } - - //------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ /** Convert string array to string with delimeters. */ - public static String stringListToString(List<String> stringList, String delim) { - String ret = ""; + public static String stringListToString(List<String> stringList, + String delim) { + StringBuilder bldr = new StringBuilder(); for (String s : stringList) { - if (ret.length() > 0) { - ret = ret + delim + s; - } - else { - ret = s; + if (bldr.length() > 0) { + bldr.append(delim).append(s); + } else { + bldr.append(s); } } - return ret; + return bldr.toString(); } - - //-------------------------------------------------------------------------- + + // -------------------------------------------------------------------------- /** Convert string with delimeters to string array. */ - public static String[] stringToStringArray(String instr, String delim) - throws NoSuchElementException, NumberFormatException { - StringTokenizer toker = new StringTokenizer(instr, delim); - String stringArray[] = new String[toker.countTokens()]; - int i = 0; - - while (toker.hasMoreTokens()) { - stringArray[i++] = toker.nextToken(); - } - return stringArray; + public static String[] stringToStringArray(String instr, String delim) { + return StringUtils.split(instr, delim); } - - //-------------------------------------------------------------------------- + + // -------------------------------------------------------------------------- /** Convert string with delimeters to string list. */ public static List<String> stringToStringList(String instr, String delim) - throws NoSuchElementException, NumberFormatException { - StringTokenizer toker = new StringTokenizer(instr, delim); + throws NoSuchElementException, NumberFormatException { List<String> stringList = new ArrayList<String>(); - while (toker.hasMoreTokens()) { - stringList.add(toker.nextToken()); + String[] str = StringUtils.split(instr, delim); + for (String string : str) { + stringList.add(string); } return stringList; } - - //-------------------------------------------------------------------------- + + // -------------------------------------------------------------------------- /** Convert string to integer array. */ - public static int[] stringToIntArray(String instr, String delim) - throws NoSuchElementException, NumberFormatException { - StringTokenizer toker = new StringTokenizer(instr, delim); - int intArray[] = new int[toker.countTokens()]; + public static int[] stringToIntArray(String instr, String delim) { + String[] str = StringUtils.split(instr, delim); + int intArray[] = new int[str.length]; int i = 0; - - while (toker.hasMoreTokens()) { - String sInt = toker.nextToken(); - int nInt = Integer.parseInt(sInt); - intArray[i++] = nInt; + for (String string : str) { + int nInt = Integer.parseInt(string); + intArray[i++] = new Integer(nInt).intValue(); } return intArray; } - - //------------------------------------------------------------------- + + // ------------------------------------------------------------------- /** Convert integer array to a string. */ public static String intArrayToString(int[] intArray) { - String ret = ""; - for (int i = 0; i < intArray.length; i++) { - if (ret.length() > 0) { - ret = ret + "," + Integer.toString(intArray[i]); - } - else { - ret = Integer.toString(intArray[i]); + StringBuilder bldr = new StringBuilder(); + for (int s : intArray) { + if (bldr.length() > 0) { + bldr.append(",").append(Integer.toString(s)); + } else { + bldr.append(Integer.toString(s)); } } - return ret; + return bldr.toString(); } - - //------------------------------------------------------------------------ + + // ------------------------------------------------------------------------ public static void copyFile(File from, File to) throws IOException { InputStream in = null; OutputStream out = null; - + try { in = new FileInputStream(from); } catch (IOException ex) { - throw new IOException( - "Utilities.copyFile: opening input stream '" - + from.getPath() - + "', " - + ex.getMessage()); + throw new IOException("Utilities.copyFile: opening input stream '" + + from.getPath() + "', " + ex.getMessage()); } - + try { out = new FileOutputStream(to); } catch (Exception ex) { @@ -401,16 +380,13 @@ public class Utilities { in.close(); } catch (IOException ex1) { } - throw new IOException( - "Utilities.copyFile: opening output stream '" - + to.getPath() - + "', " - + ex.getMessage()); + throw new IOException("Utilities.copyFile: opening output stream '" + + to.getPath() + "', " + ex.getMessage()); } - + copyInputToOutput(in, out, from.length()); } - + /** * Reads an inputstream into a string */ @@ -425,28 +401,25 @@ public class Utilities { return sb.toString(); } - //------------------------------------------------------------------------ + // ------------------------------------------------------------------------ /** - * Utility method to copy an input stream to an output stream. - * Wraps both streams in buffers. Ensures right numbers of bytes copied. + * Utility method to copy an input stream to an output stream. Wraps both + * streams in buffers. Ensures right numbers of bytes copied. */ - public static void copyInputToOutput( - InputStream input, - OutputStream output, - long byteCount) - throws IOException { + public static void copyInputToOutput(InputStream input, + OutputStream output, long byteCount) throws IOException { int bytes; long length; - + BufferedInputStream in = new BufferedInputStream(input); BufferedOutputStream out = new BufferedOutputStream(output); - + byte[] buffer; buffer = new byte[8192]; - + for (length = byteCount; length > 0;) { bytes = (int) (length > 8192 ? 8192 : length); - + try { bytes = in.read(buffer, 0, bytes); } catch (IOException ex) { @@ -455,16 +428,16 @@ public class Utilities { out.close(); } catch (IOException ex1) { } - throw new IOException( - "Reading input stream, " + ex.getMessage()); + throw new IOException("Reading input stream, " + + ex.getMessage()); } - + if (bytes < 0) { break; } - + length -= bytes; - + try { out.write(buffer, 0, bytes); } catch (IOException ex) { @@ -473,11 +446,11 @@ public class Utilities { out.close(); } catch (IOException ex1) { } - throw new IOException( - "Writing output stream, " + ex.getMessage()); + throw new IOException("Writing output stream, " + + ex.getMessage()); } } - + try { in.close(); out.close(); @@ -485,11 +458,9 @@ public class Utilities { throw new IOException("Closing file streams, " + ex.getMessage()); } } - - //------------------------------------------------------------------------ - public static void copyInputToOutput( - InputStream input, - OutputStream output) + + // ------------------------------------------------------------------------ + public static void copyInputToOutput(InputStream input, OutputStream output) throws IOException { BufferedInputStream in = new BufferedInputStream(input); BufferedOutputStream out = new BufferedOutputStream(output); @@ -500,7 +471,7 @@ public class Utilities { out.write(buffer, 0, count); } } - + try { in.close(); out.close(); @@ -508,23 +479,25 @@ public class Utilities { throw new IOException("Closing file streams, " + ex.getMessage()); } } - + /** * Encode a string using algorithm specified in web.xml and return the - * resulting encrypted password. If exception, the plain credentials - * string is returned - * - * @param password Password or other credentials to use in authenticating - * this username - * @param algorithm Algorithm used to do the digest - * + * resulting encrypted password. If exception, the plain credentials string + * is returned + * + * @param password + * Password or other credentials to use in authenticating this + * username + * @param algorithm + * Algorithm used to do the digest + * * @return encypted password based on the algorithm. */ public static String encodePassword(String password, String algorithm) { byte[] unencodedPassword = password.getBytes(); - + MessageDigest md = null; - + try { // first create an instance, given the provider md = MessageDigest.getInstance(algorithm); @@ -532,37 +505,38 @@ public class Utilities { mLogger.error("Exception: " + e); return password; } - + md.reset(); - + // call the update method one or more times // (useful when you don't know the size of your data, eg. stream) md.update(unencodedPassword); - + // now calculate the hash byte[] encodedPassword = md.digest(); - + StringBuilder buf = new StringBuilder(); - + for (int i = 0; i < encodedPassword.length; i++) { if ((encodedPassword[i] & 0xff) < 0x10) { buf.append("0"); } - + buf.append(Long.toString(encodedPassword[i] & 0xff, 16)); } - + return buf.toString(); } - + /** - * Encode a string using Base64 encoding. Used when storing passwords - * as cookies. - * - * This is weak encoding in that anyone can use the decodeString - * routine to reverse the encoding. - * - * @param str String to encode + * Encode a string using Base64 encoding. Used when storing passwords as + * cookies. + * + * This is weak encoding in that anyone can use the decodeString routine to + * reverse the encoding. + * + * @param str + * String to encode * @return String * @throws IOException */ @@ -570,11 +544,12 @@ public class Utilities { String encodedStr = new String(Base64.encodeBase64(str.getBytes())); return (encodedStr.trim()); } - + /** * Decode a string using Base64 encoding. - * - * @param str String to decode + * + * @param str + * String to decode * @return decoded string * @throws IOException */ @@ -582,78 +557,81 @@ public class Utilities { String value = new String(Base64.decodeBase64(str.getBytes())); return (value); } - + /** * Strips HTML and truncates. */ - public static String truncate( - String str, int lower, int upper, String appendToEnd) { + public static String truncate(String str, int lower, int upper, + String appendToEnd) { // strip markup from the string String str2 = removeHTML(str, false); - + // quickly adjust the upper if it is set lower than 'lower' if (upper < lower) { upper = lower; } - + // now determine if the string fits within the upper limit // if it does, go straight to return, do not pass 'go' and collect $200 - if(str2.length() > upper) { + if (str2.length() > upper) { // the magic location int int loc; - + // first we determine where the next space appears after lower loc = str2.lastIndexOf(' ', upper); - + // now we'll see if the location is greater than the lower limit - if(loc >= lower) { + 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); } - + // the string was truncated, so we append the appendToEnd String str2 = str2 + appendToEnd; } - + return str2; } - + /** * This method based on code from the String taglib at Apache Jakarta: - * http://cvs.apache.org/viewcvs/jakarta-taglibs/string/src/org/apache/taglibs/string/util/StringW.java?rev=1.16&content-type=text/vnd.viewcvs-markup - * Copyright (c) 1999 The Apache Software Foundation. - * Author: [email protected] - * + * http:/ + * /cvs.apache.org/viewcvs/jakarta-taglibs/string/src/org/apache/taglibs + * /string/util/StringW.java?rev=1.16&content-type=text/vnd.viewcvs-markup + * Copyright (c) 1999 The Apache Software Foundation. Author: + * [email protected] + * * @param str * @param lower * @param upper * @param appendToEnd * @return */ - public static String truncateNicely(String str, int lower, int upper, String appendToEnd) { + public static String truncateNicely(String str, int lower, int upper, + String appendToEnd) { // strip markup from the string String str2 = removeHTML(str, false); boolean diff = (str2.length() < str.length()); - + // quickly adjust the upper if it is set lower than 'lower' - if(upper < lower) { + if (upper < lower) { upper = lower; } - + // now determine if the string fits within the upper limit // if it does, go straight to return, do not pass 'go' and collect $200 - if(str2.length() > upper) { + if (str2.length() > upper) { // the magic location int int loc; - + // first we determine where the next space appears after lower loc = str2.lastIndexOf(' ', upper); - + // now we'll see if the location is greater than the lower limit - if(loc >= lower) { + if (loc >= lower) { // yes it was, so we'll cut it off here str2 = str2.substring(0, loc); } else { @@ -661,29 +639,30 @@ public class Utilities { str2 = str2.substring(0, upper); loc = upper; } - + // HTML was removed from original str if (diff) { - + // location of last space in truncated string loc = str2.lastIndexOf(' ', loc); - - // get last "word" in truncated string (add 1 to loc to eliminate space - String str3 = str2.substring(loc+1); - + + // get last "word" in truncated string (add 1 to loc to + // eliminate space + String str3 = str2.substring(loc + 1); + // find this fragment in original str, from 'loc' position loc = str.indexOf(str3, loc) + str3.length(); - + // get truncated string from original str, given new 'loc' str2 = str.substring(0, loc); - + // get all the HTML from original str after loc str3 = extractHTML(str.substring(loc)); - + // remove any tags which generate visible HTML // This call is unecessary, all HTML has already been stripped - //str3 = removeVisibleHTMLTags(str3); - + // str3 = removeVisibleHTMLTags(str3); + // append the appendToEnd String and // add extracted HTML back onto truncated string str = str2 + appendToEnd + str3; @@ -691,32 +670,33 @@ public class Utilities { // the string was truncated, so we append the appendToEnd String str = str2 + appendToEnd; } - + } - + return str; } - - public static String truncateText(String str, int lower, int upper, String appendToEnd) { + + public static String truncateText(String str, int lower, int upper, + String appendToEnd) { // strip markup from the string String str2 = removeHTML(str, false); // quickly adjust the upper if it is set lower than 'lower' - if(upper < lower) { + if (upper < lower) { upper = lower; } - + // now determine if the string fits within the upper limit // if it does, go straight to return, do not pass 'go' and collect $200 - if(str2.length() > upper) { + if (str2.length() > upper) { // the magic location int int loc; - + // first we determine where the next space appears after lower loc = str2.lastIndexOf(' ', upper); - + // now we'll see if the location is greater than the lower limit - if(loc >= lower) { + if (loc >= lower) { // yes it was, so we'll cut it off here str2 = str2.substring(0, loc); } else { @@ -728,7 +708,7 @@ public class Utilities { } return str; } - + /** * @param str * @return @@ -739,69 +719,83 @@ public class Utilities { str = str.replaceAll("<br/>", ""); str = str.replaceAll("<br />", ""); str = str.replaceAll("<p></p>", ""); - str = str.replaceAll("<p/>",""); - str = str.replaceAll("<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. - * + * 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 ([email protected]) + * + * @author Alexis Moussine-Pouchkine + * ([email protected]) * @author Lance Lavandowska - * @param str the String object to modify + * @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); StringBuilder result = new StringBuilder(str); StringBuilder lcresult = new StringBuilder(str.toLowerCase()); - + // <img should take care of smileys - String[] visibleTags = {"<img"}; // are there others to add? + 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() ); + 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 + // 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 + // 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 ); + 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. + + // 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 ? + 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]+">"; + String closeTag = "</" + openCloseTags[j] + ">"; int lastStringIndex = 0; - while ((stringIndex = lcresult.indexOf( "<"+openCloseTags[j], lastStringIndex)) > -1) { + while ((stringIndex = lcresult.indexOf("<" + openCloseTags[j], + lastStringIndex)) > -1) { lastStringIndex = stringIndex; - // Try to find the matching closing tag (ignores possible nesting!) + // 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()); + 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 />. + // 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)) { + 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); @@ -809,12 +803,13 @@ public class Utilities { } } } - + return result.toString(); } - + /** * Extract (keep) JUST the HTML from the String. + * * @param str * @return */ @@ -829,14 +824,14 @@ public class Utilities { 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) ); - + ret.append(str.substring(beginTag, endTag + 1)); + // move start forward and find another tag start = endTag + 1; beginTag = str.indexOf('<', start); @@ -848,54 +843,59 @@ public class Utilities { } 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 str!=null ? RegexUtil.encodeEmail(str) : null; + return str != null ? RegexUtil.encodeEmail(str) : null; } /** * URL encoding. - * @param s a string to be URL-encoded - * @return URL encoding of s using character encoding UTF-8; null if s is null. + * + * @param s + * a string to be URL-encoded + * @return URL encoding of s using character encoding UTF-8; null if s is + * null. */ public static final String encode(String s) { try { if (s != null) { return URLEncoder.encode(s, "UTF-8"); - } - else { + } else { return s; } } catch (UnsupportedEncodingException e) { - // Java Spec requires UTF-8 be in all Java environments, so this should not happen + // Java Spec requires UTF-8 be in all Java environments, so this + // should not happen return s; } } /** * URL decoding. - * @param s a URL-encoded string to be URL-decoded - * @return URL decoded value of s using character encoding UTF-8; null if s is null. + * + * @param s + * a URL-encoded string to be URL-decoded + * @return URL decoded value of s using character encoding UTF-8; null if s + * is null. */ public static final String decode(String s) { try { if (s != null) { return URLDecoder.decode(s, "UTF-8"); - } - else { + } else { return s; } } catch (UnsupportedEncodingException e) { - // Java Spec requires UTF-8 be in all Java environments, so this should not happen + // Java Spec requires UTF-8 be in all Java environments, so this + // should not happen return s; } } @@ -912,37 +912,38 @@ public class Utilities { } return 0; } - + /** * Convert a byte array into a Base64 string (as used in mime formats) */ public static String toBase64(byte[] aValue) { - - final String strBase64Chars = - "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - + + final String strBase64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + int byte1; int byte2; int byte3; int iByteLen = aValue.length; StringBuilder tt = new StringBuilder(); - + for (int i = 0; i < iByteLen; i += 3) { boolean bByte2 = (i + 1) < iByteLen; boolean bByte3 = (i + 2) < iByteLen; byte1 = aValue[i] & 0xFF; byte2 = (bByte2) ? (aValue[i + 1] & 0xFF) : 0; byte3 = (bByte3) ? (aValue[i + 2] & 0xFF) : 0; - + tt.append(strBase64Chars.charAt(byte1 / 4)); - tt.append(strBase64Chars.charAt((byte2 / 16) + ((byte1 & 0x3) * 16))); - tt.append(((bByte2) ? strBase64Chars.charAt((byte3 / 64) + ((byte2 & 0xF) * 4)) : '=')); + tt.append(strBase64Chars + .charAt((byte2 / 16) + ((byte1 & 0x3) * 16))); + tt.append(((bByte2) ? strBase64Chars.charAt((byte3 / 64) + + ((byte2 & 0xF) * 4)) : '=')); tt.append(((bByte3) ? strBase64Chars.charAt(byte3 & 0x3F) : '=')); } - + return tt.toString(); } - + /** * @param tag * @return @@ -971,39 +972,39 @@ public class Utilities { } return sb.toString(); } - + public static String normalizeTag(String tag, Locale locale) { tag = Utilities.stripInvalidTagCharacters(tag); - return locale == null ? tag.toLowerCase() : tag.toLowerCase(locale); + return locale == null ? tag.toLowerCase() : tag.toLowerCase(locale); } - + /** * @param tags * @return */ - public static List splitStringAsTags(String tags) { + public static List splitStringAsTags(String tags) { String[] tagsarr = StringUtils.split(tags, TAG_SPLIT_CHARS); - if(tagsarr == null) { + if (tagsarr == null) { return Collections.EMPTY_LIST; } return Arrays.asList(tagsarr); } - - + /** * Transforms the given String into a subset of HTML displayable on a web * page. The subset includes <b>, <i>, <p>, <br>, * <pre> and <a href> (and their corresponding end tags). - * - * @param s the String to transform - * @return the transformed String + * + * @param s + * the String to transform + * @return the transformed String */ public static String transformToHTMLSubset(String s) { - + if (s == null) { return null; } - + s = replace(s, OPENING_B_TAG_PATTERN, "<b>"); s = replace(s, CLOSING_B_TAG_PATTERN, "</b>"); s = replace(s, OPENING_I_TAG_PATTERN, "<i>"); @@ -1022,7 +1023,7 @@ public class Utilities { s = replace(s, OPENING_LI_TAG_PATTERN, "<li>"); s = replace(s, CLOSING_LI_TAG_PATTERN, "</li>"); s = replace(s, QUOTE_PATTERN, "\""); - + // HTTP links s = replace(s, CLOSING_A_TAG_PATTERN, "</a>"); Matcher m = OPENING_A_TAG_PATTERN.matcher(s); @@ -1034,17 +1035,17 @@ public class Utilities { s = s.substring(0, start) + link + s.substring(end, s.length()); m = OPENING_A_TAG_PATTERN.matcher(s); } - + // escaped angle brackets s = s.replaceAll("&lt;", "<"); s = s.replaceAll("&gt;", ">"); s = s.replaceAll("&#", "&#"); - + return s; } - - - private static String replace(String string, Pattern pattern, String replacement) { + + private static String replace(String string, Pattern pattern, + String replacement) { Matcher m = pattern.matcher(string); return m.replaceAll(replacement); } @@ -1052,17 +1053,18 @@ public class Utilities { public static String getContentTypeFromFileName(String fileName) { FileTypeMap map = FileTypeMap.getDefaultFileTypeMap(); - + // TODO: figure out why PNG is missing from Java MIME types if (map instanceof MimetypesFileTypeMap) { try { - ((MimetypesFileTypeMap)map).addMimeTypes("image/png png PNG"); - } catch (Exception ignored) {} + ((MimetypesFileTypeMap) map).addMimeTypes("image/png png PNG"); + } catch (Exception ignored) { + } } return map.getContentType(fileName); } - + /** * Validate the form of an email address. * @@ -1095,7 +1097,7 @@ public class Utilities { } return result; } - + /** * Checks for name and domain. * @@ -1108,5 +1110,5 @@ public class Utilities { return tokens.length == 2 && StringUtils.isNotEmpty(tokens[0]) && StringUtils.isNotEmpty(tokens[1]); } - + }
Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/adminprotocol/sdk/EntrySet.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/adminprotocol/sdk/EntrySet.java?rev=1561624&r1=1561623&r2=1561624&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/adminprotocol/sdk/EntrySet.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/adminprotocol/sdk/EntrySet.java Mon Jan 27 10:06:12 2014 @@ -20,7 +20,7 @@ package org.apache.roller.weblogger.webs import java.util.Arrays; import java.util.List; -import org.apache.commons.lang.builder.HashCodeBuilder; +import org.apache.commons.lang3.builder.HashCodeBuilder; import org.jdom.Document; import org.jdom.Element; Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/EntryCollection.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/EntryCollection.java?rev=1561624&r1=1561623&r2=1561624&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/EntryCollection.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/EntryCollection.java Mon Jan 27 10:06:12 2014 @@ -38,7 +38,7 @@ import java.util.List; import java.util.Set; import java.util.TreeSet; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.roller.weblogger.WebloggerException; Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/MediaCollection.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/MediaCollection.java?rev=1561624&r1=1561623&r2=1561624&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/MediaCollection.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/MediaCollection.java Mon Jan 27 10:06:12 2014 @@ -44,7 +44,7 @@ import java.util.SortedSet; import java.util.StringTokenizer; import java.util.TreeSet; import java.util.UUID; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.roller.weblogger.WebloggerException; Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java?rev=1561624&r1=1561623&r2=1561624&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomHandler.java Mon Jan 27 10:06:12 2014 @@ -41,7 +41,7 @@ import javax.servlet.http.HttpServletRes import net.oauth.OAuthAccessor; import net.oauth.OAuthMessage; import net.oauth.server.OAuthServlet; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.roller.weblogger.WebloggerException; import org.apache.roller.weblogger.business.OAuthManager; import org.apache.roller.weblogger.config.WebloggerConfig; Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomService.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomService.java?rev=1561624&r1=1561623&r2=1561624&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomService.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/atomprotocol/RollerAtomService.java Mon Jan 27 10:06:12 2014 @@ -22,7 +22,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.roller.weblogger.WebloggerException; import org.apache.roller.weblogger.business.MediaFileManager; import org.apache.roller.weblogger.business.Weblogger; Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/opensearch/OpenSearchServlet.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/opensearch/OpenSearchServlet.java?rev=1561624&r1=1561623&r2=1561624&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/opensearch/OpenSearchServlet.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/opensearch/OpenSearchServlet.java Mon Jan 27 10:06:12 2014 @@ -23,7 +23,7 @@ import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; -import org.apache.commons.lang.StringEscapeUtils; +import org.apache.commons.lang3.StringEscapeUtils; import org.apache.roller.weblogger.WebloggerException; import org.apache.roller.weblogger.business.URLStrategy; import org.apache.roller.weblogger.business.WebloggerFactory; Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/BloggerAPIHandler.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/BloggerAPIHandler.java?rev=1561624&r1=1561623&r2=1561624&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/BloggerAPIHandler.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/BloggerAPIHandler.java Mon Jan 27 10:06:12 2014 @@ -27,7 +27,7 @@ import java.util.StringTokenizer; import java.util.Vector; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.roller.weblogger.WebloggerException; Modified: roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/MetaWeblogAPIHandler.java URL: http://svn.apache.org/viewvc/roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/MetaWeblogAPIHandler.java?rev=1561624&r1=1561623&r2=1561624&view=diff ============================================================================== --- roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/MetaWeblogAPIHandler.java (original) +++ roller/trunk/app/src/main/java/org/apache/roller/weblogger/webservices/xmlrpc/MetaWeblogAPIHandler.java Mon Jan 27 10:06:12 2014 @@ -25,7 +25,7 @@ import java.util.Hashtable; import java.util.List; import java.util.Vector; -import org.apache.commons.lang.StringUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.roller.weblogger.business.MediaFileManager;
