1 17 18 package com.finalist.jaggenerator; 19 20 import java.text.StringCharacterIterator ; 21 22 26 public class Utils { 27 28 public static String toClassName(String unformatted) { 29 try { 30 if (unformatted.startsWith("T_")) { 31 unformatted = unformatted.substring(2); 32 } 33 String s = format(unformatted); 34 s = initCap(s); 35 return s; 36 } catch (Exception e) { 37 return unformatted; 38 } 39 } 40 41 44 public static String formatLowercase(String unformatted) { 45 try { 46 if (unformatted == null) { 47 return null; 48 } 49 StringBuffer sb = new StringBuffer (unformatted.toLowerCase()); 50 StringBuffer formattedString = new StringBuffer (); 51 for (int i = 0; i < sb.length(); i++) { 53 char c = sb.charAt(i); 54 if (c >= 'a' && c <= 'z') { 55 formattedString.append(c); 56 } 57 } 58 return formattedString.toString(); 59 } catch (Exception e) { 60 return unformatted; 61 } 62 } 63 64 68 public static String formatLowerAndUpperCase(String unformatted) { 69 try { 70 if (unformatted == null) { 71 return null; 72 } 73 StringBuffer sb = new StringBuffer (unformatted); 74 StringBuffer formattedString = new StringBuffer (); 75 for (int i = 0; i < sb.length(); i++) { 77 char c = sb.charAt(i); 78 if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){ 79 formattedString.append(c); 80 } 81 } 82 return firstToUpperCase(formattedString.toString()); 83 } catch (Exception e) { 84 return unformatted; 85 } 86 } 87 88 public static String format(String unformatted) { 89 try { 90 StringBuffer sb = new StringBuffer (unformatted.toLowerCase()); 91 int i = sb.toString().indexOf("_"); 93 while (i >= 0) { 94 if (i > -1) sb.replace(i, i + 2, sb.substring(i + 1, i + 2).toUpperCase()); 95 i = sb.toString().indexOf("_"); 96 } 97 i = sb.toString().indexOf("-"); 99 while (i >= 0) { 100 if (i > -1) sb.replace(i, i + 2, sb.substring(i + 1, i + 2).toUpperCase()); 101 i = sb.toString().indexOf("-"); 102 } 103 return sb.toString(); 104 } catch (Exception e) { 105 return unformatted; 106 } 107 } 108 109 116 public static String unformat(String formatted) { 117 StringBuffer result = new StringBuffer (); 118 for (int i = 0; i < formatted.length(); i++) { 119 if (Character.isUpperCase(formatted.charAt(i))) { 120 result.append('_'); 121 } 122 result.append(Character.toUpperCase(formatted.charAt(i))); 123 } 124 return result.toString(); 125 } 126 127 public static String initCap(String unformatted) { 128 StringBuffer sb = new StringBuffer (unformatted); 129 if ((unformatted != null) && (unformatted.length() > 0)) { 131 sb.replace(0, 1, sb.substring(0, 1).toUpperCase()); 132 return sb.toString(); 133 } else 134 return unformatted; 135 } 136 137 public static String formatFKName(String fkColumnName) { 138 return format(fkColumnName) + "FK"; 139 } 140 141 public static String firstToLowerCase(String text) { 142 return (text == null || text.length() == 0) ? "" : Character.toLowerCase(text.charAt(0)) + 143 (text.length() > 1 ? text.substring(1) : ""); 144 } 145 146 public static String firstToUpperCase(String text) { 147 return (text == null || text.length() == 0) ? "" : Character.toUpperCase(text.charAt(0)) + 148 (text.length() > 1 ? text.substring(1) : ""); 149 } 150 151 } 152 | Popular Tags |