001package bradleyross.library.helpers; 002import java.util.regex.Pattern; 003import java.io.PrintStream; 004import java.io.PrintWriter; 005import java.sql.ResultSet; 006import java.sql.SQLException; 007import java.util.regex.Matcher; 008import bradleyross.library.helpers.GenericPrinter; 009/** 010 * Classes for manipulating strings. 011 * @author Bradley Ross 012 * 013 */ 014public class StringHelpers 015{ 016 /** 017 * Pads a string with blanks to the right so that it has 018 * a specified size. 019 * 020 * @param value String to be padded 021 * @param size Desired size of padded string 022 * @return Padded string 023 */ 024 public static String padRight (String value, int size) 025 { 026 if (value == null) 027 { 028 StringBuffer local = new StringBuffer(); 029 for (int i = 0; i < size; i++) 030 { 031 local.append(" "); 032 } 033 return new String(local); 034 } 035 if (value.length() >= size) 036 { 037 return value; 038 } 039 StringBuffer working = new StringBuffer(value); 040 int padding = size - value.length(); 041 if (padding <= 0) { return value; } 042 for (int i = 0; i < padding; i++) 043 { 044 working.append(" "); 045 } 046 return new String(working); 047 } 048 /** 049 * Pads a string with blanks to the left so that it has 050 * a specified size. 051 * 052 * @param value String to be padded 053 * @param size Desired size of padded string 054 * @return Padded string 055 */ 056 public static String padLeft (String value, int size) 057 { 058 if (value == null) 059 { 060 StringBuffer local = new StringBuffer(); 061 for (int i = 0; i < size; i++) 062 { 063 local.append(" "); 064 } 065 return new String(local); 066 } 067 if (value.length() >= size) 068 { 069 return value; 070 } 071 StringBuffer working = new StringBuffer(); 072 int padding = size - value.length(); 073 if (padding <= 0) { return value; } 074 for (int i = 0; i < padding; i++) 075 { 076 working.append(" "); 077 } 078 working.append(value); 079 return new String(working); 080 } 081 /** 082 * Returns true if the string is only composed of numeric 083 * digits. 084 * @param value String to be evaluated 085 * @return True if string only has numeric digits 086 * as characters. 087 */ 088 public static boolean isDigitsOnly(String value) 089 { 090 return Pattern.matches("^[0-9]*$", value.trim()); 091 } 092 /** 093 * Returns true if and only if all of the characters in 094 * the string are alphanumeric. 095 * @param value String to be evaluated 096 * @return True if string has only alphanumeric characters 097 */ 098 public static boolean isAlphanumericOnly(String value) 099 { 100 return Pattern.matches("^[0-9a-zA-Z]*$", value.trim()); 101 } 102 public static void printJustified(String value) 103 { 104 printJustified(System.out, value, 72); 105 } 106 public static void printJustified(String value, int length) 107 { 108 printJustified(System.out, value, length); 109 } 110 public static void printJustified(PrintStream output, String value) 111 { 112 printJustified (output, value, 72); 113 } 114 public static void printJustified(PrintWriter output, String value) 115 { 116 printJustified (output, value, 72); 117 } 118 public static void printJustified(GenericPrinter output, String value) 119 { 120 printJustified(output, value, 72); 121 } 122 public static void printJustified(PrintStream output, String value, int length) 123 { 124 GenericPrinter writer = new GenericPrinter(output); 125 printJustified(writer, value, length); 126 } 127 public static void printJustified(PrintWriter output, String value, int length) 128 { 129 GenericPrinter writer = new GenericPrinter(output); 130 printJustified(writer, value, length); 131 } 132 /** 133 * Send a string to an output device, inserting line breaks at the 134 * first space after a specified number of characters on the line. 135 * @param output Output device 136 * @param value String to be sent to output 137 * @param length Number of characters on a line after which the next space 138 * is to be replaced by a line feed. 139 */ 140 protected static void printJustified(GenericPrinter output, String value, int length) 141 { 142 if (value == null) 143 { 144 output.println(); 145 return; 146 } 147 String working = value.trim(); 148 if (working.length() == 0) 149 { 150 output.println(); 151 return; 152 } 153 while (true) 154 { 155 working = working.trim(); 156 if (working.length() == 0) 157 { 158 return; 159 } 160 int newLocation = working.indexOf(" ", length); 161 if (newLocation < 0) 162 { 163 output.println(working); 164 return; 165 } 166 output.println(working.substring(0, newLocation).trim()); 167 working = working.substring(newLocation).trim(); 168 } 169 } 170 /** 171 * Replace less than signs, greater than signs, and ampersands in a 172 * string with their HTML representations. 173 * @param value String to be modified 174 * @return Modified string 175 */ 176 public static String escapeHTML(String value) 177 { 178 return escapeHTML(value, "NORMAL"); 179 } 180 /** 181 * Carry out the HTML escaping of a string twice. 182 * 183 * @param value String to be escaped 184 * @return Escaped string 185 */ 186 public static String doubleEscapeHTML(String value) 187 { 188 return escapeHTML(escapeHTML(value)); 189 } 190 /** 191 * Carry out the HTML escaping of a string twice. 192 * 193 * @param value String to be escaped 194 * @param modeValue Mode of escaping 195 * @return Escaped string 196 */ 197 public static String doubleEscapeHTML(String value, String modeValue) 198 { 199 return escapeHTML(escapeHTML(value, modeValue)); 200 } 201 /** 202 * Carry out the HTML escaping of a string twice. 203 * 204 * @param rs Result set containing value 205 * @param parameterName Name of column in result set 206 * @param modeValue Type of processing for escaping 207 * @return Escaped string 208 */ 209 public static String doubleEscapeHTML(ResultSet rs, String parameterName, String modeValue) 210 { 211 return escapeHTML(escapeHTML(rs, parameterName, modeValue)); 212 } 213 /** 214 * Replace less than signs, greater than signs, and ampersands in a 215 * string with their HTML representations. 216 * <p>If mode is TEXTAREA or TEXT, do not replace line breaks. When 217 * escaping the contents of a TEXTAREA tag, the new line and carriage return 218 * characters should remain unchanged so that they are properly 219 * represented in the form.</p> 220 * @param value String to be modified 221 * @param modeValue Mode of operation 222 * @return Modified string 223 */ 224 public static String escapeHTML(String value, String modeValue) 225 { 226 String mode = modeValue; 227 if (mode == null) 228 { 229 modeValue="NORMAL"; 230 } 231 if (value == null) 232 { 233 return " "; 234 } 235 else if (value.trim().length() == 0) 236 { 237 return " "; 238 } 239 String working = null; 240 Pattern pattern = Pattern.compile("\\&"); 241 Matcher matcher = pattern.matcher(value); 242 working = matcher.replaceAll("&"); 243 pattern = Pattern.compile("\\\""); 244 matcher = pattern.matcher(working); 245 working = matcher.replaceAll("""); 246 pattern = Pattern.compile("\\<"); 247 matcher = pattern.matcher(working); 248 working = matcher.replaceAll("<"); 249 pattern = Pattern.compile("\\>"); 250 matcher = pattern.matcher(working); 251 working = matcher.replaceAll(">"); 252 if (!(mode.equalsIgnoreCase("TEXTAREA") || mode.equalsIgnoreCase("TEXT"))) 253 { 254 pattern = Pattern.compile("\r\n"); 255 matcher = pattern.matcher(working); 256 working = matcher.replaceAll("<br />"); 257 pattern = Pattern.compile("\r"); 258 matcher = pattern.matcher(working); 259 working = matcher.replaceAll("<br />"); 260 pattern = Pattern.compile("\n"); 261 matcher = pattern.matcher(working); 262 working = matcher.replaceAll("<br />"); 263 } 264 return working; 265 } 266 /** 267 * Processes special characters in HTML code so that it will 268 * not be interpreted by browser as commands. 269 * 270 * <p>Uses @{link {@link #escapeHTML(String, String)}.</p> 271 * @param rs Result set containing value. 272 * @param parameterName Name of column containing value 273 * @param mode Mode of processing to be used 274 * @return Escaped string 275 */ 276 public static String escapeHTML(ResultSet rs, String parameterName, String mode) 277 { 278 try 279 { 280 String working = rs.getString(parameterName); 281 if (rs.wasNull()) 282 { working = new String(); } 283 return escapeHTML(working, mode); 284 } 285 catch (SQLException e) 286 { 287 return new String(); 288 } 289 } 290 /** 291 * Processes special characters in HTML code so that it will 292 * not be interpreted by browser as commands. 293 * 294 * <p>Uses @{link {@link #escapeHTML(String)}.</p> 295 * @param rs Result set containing value. 296 * @param parameterName Name of column containing value 297 * @return Escaped string 298 */ 299 public static String escapeHTML(ResultSet rs, String parameterName) 300 { 301 return escapeHTML(rs, parameterName, "NORMAL"); 302 } 303 /** 304 * Test driver. 305 * @param args Not used 306 */ 307 public static void main(String args[]) 308 { 309 System.out.println("Starting test driver at " + (new java.util.Date()).toString()); 310 System.out.println("***"); 311 System.out.println("*** Tests for isDigitsOnly"); 312 String list[] = { "123456", "-2", "a", "a1", "2" }; 313 for (int i = 0; i < list.length; i++) 314 { 315 System.out.println(Boolean.toString(isDigitsOnly(list[i])) + " " + list[i]); 316 } 317 System.out.println("***"); 318 System.out.println("*** Tests for escapeHTML"); 319 String list2[] = { "<abc&def>", "&&", "<<<>>>", "\"this\"", "aa\"\"bb", "a'b" }; 320 for (int i = 0; i < list2.length; i++) 321 { 322 System.out.println(list2[i] + " " + escapeHTML(list2[i])); 323 } 324 System.out.println("***"); 325 System.out.println("*** Tests for printJustified"); 326 String working = "abcdefghijklmnopqrstuvwxyz 01234567890123456789 " + 327 "Now is the time for all good men to come to the aid of their country. " + 328 "The quick brown fox jumped over the lazy dog."; 329 printJustified(working, 70); 330 System.out.println("***"); 331 System.out.println("*** Tests for isAlphanumericOnly"); 332 String list3[] = {"alpha23", "a b", "abc", "ABc", "a\"b9", "a&b" }; 333 for (int i = 0; i < list3.length; i++) 334 { 335 System.out.println(Boolean.toString(isAlphanumericOnly(list3[i])) + " " + list3[i]); 336 } 337 } 338}