1 16 package com.blandware.atleap.common.util; 17 18 import org.apache.commons.logging.Log; 19 import org.apache.commons.logging.LogFactory; 20 import sun.misc.BASE64Decoder; 21 import sun.misc.BASE64Encoder; 22 23 import java.io.IOException ; 24 import java.io.PrintWriter ; 25 import java.io.StringWriter ; 26 import java.security.MessageDigest ; 27 import java.util.ArrayList ; 28 import java.util.Map ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 32 33 46 public class StringUtil { 47 49 51 61 public static String encodePassword(String password, String algorithm) { 62 63 if ( password == null ) { 64 return null; 65 } 66 67 Log log = LogFactory.getLog(StringUtil.class); 68 byte[] unencodedPassword = password.getBytes(); 69 70 MessageDigest md = null; 71 72 try { 73 md = MessageDigest.getInstance(algorithm); 75 } catch ( Exception e ) { 76 StringWriter sw = new StringWriter (); 77 e.printStackTrace(new PrintWriter (sw)); 78 if ( log.isErrorEnabled() ) { 79 log.error(sw.toString()); 80 } 81 return password; 82 } 83 84 md.reset(); 85 86 md.update(unencodedPassword); 89 90 byte[] encodedPassword = md.digest(); 92 93 StringBuffer buf = new StringBuffer (); 94 95 for ( int i = 0; i < encodedPassword.length; i++ ) { 96 if ( (encodedPassword[i] & 0xff) < 0x10 ) { 97 buf.append("0"); 98 } 99 100 buf.append(Long.toString(encodedPassword[i] & 0xff, 16)); 101 } 102 103 return buf.toString(); 104 } 105 106 115 public static String encodeString(String str) { 116 BASE64Encoder encoder = new BASE64Encoder(); 117 return encoder.encodeBuffer(str.getBytes()).trim(); 118 } 119 120 126 public static String decodeString(String str) { 127 BASE64Decoder dec = new BASE64Decoder(); 128 try { 129 return new String (dec.decodeBuffer(str)); 130 } catch ( IOException io ) { 131 throw new RuntimeException (io.getMessage(), io.getCause()); 132 } 133 } 134 135 141 public static String escape(String inputString) { 142 if (inputString == null) 143 return null; 144 145 StringBuffer buf = new StringBuffer ((int) (inputString.length() * 1.1)); 146 int stringLength = inputString.length(); 147 148 for ( int i = 0; i < stringLength; ++i ) { 149 char c = inputString.charAt(i); 150 151 switch ( c ) { 152 case 0: 153 buf.append('\\'); 154 buf.append('0'); 155 156 break; 157 158 case '\n': 159 buf.append('\\'); 160 buf.append('n'); 161 162 break; 163 164 case '\r': 165 buf.append('\\'); 166 buf.append('r'); 167 168 break; 169 170 case '\\': 171 buf.append('\\'); 172 buf.append('\\'); 173 174 break; 175 176 case '\'': 177 buf.append('\\'); 178 buf.append('\''); 179 180 break; 181 182 case '"': 183 buf.append('\\'); 184 buf.append('"'); 185 186 break; 187 188 case '\032': 189 buf.append('\\'); 190 buf.append('Z'); 191 192 break; 193 194 default: 195 buf.append(c); 196 } 197 } 198 return buf.toString(); 199 } 200 201 206 public static String htmlEncode(String html) { 207 if ( html == null ) { 208 return null; 209 } 210 StringBuffer buf = new StringBuffer ((int) (html.length() * 1.1)); 211 int stringLength = html.length(); 212 213 for ( int i = 0; i < stringLength; ++i ) { 214 char c = html.charAt(i); 215 216 switch ( c ) { 217 case '\'': 218 buf.append("'"); 219 220 break; 221 222 case '"': 223 buf.append("""); 224 225 break; 226 227 case '<': 228 buf.append("<"); 229 230 break; 231 232 case '>': 233 buf.append(">"); 234 235 break; 236 237 case '&': 238 buf.append("&"); 239 240 break; 241 default: 242 buf.append(c); 243 } 244 } 245 return buf.toString(); 246 } 247 248 253 public static Map htmlEncodeModel(Map model) { 254 Map result = new HashMap (model.size()); 255 for ( Iterator i = model.entrySet().iterator(); i.hasNext(); ) { 256 Map.Entry entry = (Map.Entry ) i.next(); 257 String key = (String ) entry.getKey(); 258 Object value = (Object ) entry.getValue(); 259 if ( value instanceof String ) { 260 result.put(key, htmlEncode((String ) value)); 261 } else { 262 result.put(key, value); 263 } 264 } 265 return result; 266 } 267 268 274 public static String elToBr(String html) { 275 if ( html == null ) { 276 return null; 277 } 278 StringBuffer buf = new StringBuffer ((int) (html.length() * 1.1)); 279 int stringLength = html.length(); 280 281 for ( int i = 0; i < stringLength; ++i ) { 282 char c = html.charAt(i); 283 284 switch ( c ) { 285 case '\n': 286 buf.append("<br />"); 287 288 break; 289 default: 290 buf.append(c); 291 } 292 } 293 return buf.toString(); 294 } 295 296 301 public static String capitalize(String s) { 302 if ( s == null || s.length() == 0 ) { 303 return s; 304 } 305 char[] chars = s.toCharArray(); 306 chars[0] = Character.toUpperCase(chars[0]); 307 return String.valueOf(chars); 308 } 309 310 315 public static String [] splitTemplateName(String templateName) { 316 ArrayList result = new ArrayList (); 317 int k = templateName.lastIndexOf("_"); 318 if ( k != -1 ) { 319 String locale = templateName.substring(k + 1); 320 templateName = templateName.substring(0, k); 321 k = templateName.lastIndexOf("_"); 322 if ( k != -1 ) { 323 String field = templateName.substring(k + 1); 324 templateName = templateName.substring(0, k); 325 result.add(templateName); 326 result.add(field); 327 result.add(locale); 328 } 329 } 330 return (String []) result.toArray(new String [0]); 331 } 332 333 340 public static String createTemplateName(String identifier, String field, String locale) { 341 StringBuffer result = new StringBuffer (identifier); 342 result.append("_"); 343 result.append(field); 344 result.append("_"); 345 result.append(locale); 346 return result.toString(); 347 } 348 349 355 public static String shortenString(String s, int requiredLength) { 356 if ( s != null && s.length() > requiredLength ) { 357 s = s.substring(0, requiredLength + 1); 358 int space = s.lastIndexOf(" "); 359 int lineFeed = s.lastIndexOf("\n"); 360 int tab = s.lastIndexOf("\t"); 361 if ( space > 0 || lineFeed > 0 || tab > 0 ) { 362 int cut = space > lineFeed ? ( space > tab ? space : tab ) : ( lineFeed > tab ? lineFeed : tab ); 363 s = s.substring(0, cut); 364 } 365 s += "..."; 366 } 367 return s; 368 } 369 370 378 public static String completeNumber(int number, int signs) { 379 380 if ( signs == 0 ) { 382 return ""; 383 } 384 385 int radix = (int) Math.pow(10, signs - 1); 387 388 StringBuffer result = new StringBuffer (); 389 while ( (number / radix == 0) && (number % radix > 0) ) { 390 result.append("0"); 391 radix = radix / 10; 392 } 393 394 result.append(number); 396 397 return result.toString(); 398 } 399 400 } 401 | Popular Tags |