View Javadoc

1   //$Id: StringUtilities.java,v 1.8 2007/07/13 13:18:29 kameleono Exp $
2   
3   package net.sourceforge.sql2java;
4   
5   import java.util.Random;
6   
7   /**
8    * @author Kelvin Nishikawa
9    *
10   * This utility class handles various SQL -> Java String conversions for code in various templates.
11   * Use sharedInstance() to get the singleton instance.
12   *
13   * Most of these methods were excised to here from the CodeWriter class for good housekeeping.
14   *
15   */
16  public final class StringUtilities {
17  	static private StringUtilities singleton = new StringUtilities();
18  
19  	// TODO: convert into velocity macros
20  	static public final String PREFIX = "";
21  
22  	private StringUtilities () { }
23  
24  	/** This is the default method for obtaining a StringUtilities instance.
25  	 * @return The shared instance
26  	 */
27  	static public synchronized StringUtilities getInstance() {
28  		return singleton;
29  	}
30  
31      public String getPackageAsPath(String pkg){
32          if (pkg == null)
33              return "";
34          return pkg.replace('.', '/');
35      }
36  
37  	// TODO: convert into velocity macros
38  	public static String convertClass(String table, String type)
39      {
40          String suffix = "";
41          String postfix = "";
42          if ("".equalsIgnoreCase(PREFIX) == false)
43              suffix = suffix + "_";
44          if ("".equalsIgnoreCase(type) == false)
45              postfix =  "_" + type;
46          return convertName(suffix + table + postfix, false);
47      }
48  
49  	public static String convertClass(Table table, String type)
50      {
51          return convertClass(table.getName(), type);
52      }
53  
54      /**
55       * Converts name into a more Java-ish style name.
56       * @author netkernel
57       * <br>
58       * Basically it looks for underscores, removes them, and makes the
59       * letter after the underscore a capital letter. If wimpyCaps is true,
60       * then the first letter of the name will be lowercase, otherwise it
61       * will be uppercase. Here are some examples:
62       * <p>
63       * member_profile   becomes   MemberProfile
64       * <br>
65       * firstname&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp becomes&nbsp Firstname
66       */
67      public static String convertName(String name, boolean wimpyCaps) {
68          StringBuffer buffer = new StringBuffer(name.length());
69          char list[] = name.toLowerCase().toCharArray();
70          for(int i = 0; i < list.length; i++) {
71              if(i == 0 && !wimpyCaps) {
72                  buffer.append(Character.toUpperCase(list[i]));
73              } else if(list[i] == '_' && (i+1) < list.length && i != 0) {
74                  buffer.append(Character.toUpperCase(list[++i]));
75              } else buffer.append(list[i]);
76          }
77          return buffer.toString();
78      }
79  
80      public static String escape(String s) {
81          return isReserved(s) ? ("my_"+s) : s;
82      }
83  
84      public static String escape(Column s) {
85          return isReserved(s.getName()) ? ("my_"+s.getName()) : s.getName();
86      }
87  
88      public static boolean isReserved(String s) {
89          for(int i=0; i<reserved_words.length; i++) {
90              if (s.compareToIgnoreCase(reserved_words[i]) == 0) {
91                  return true;
92              }
93          }
94          return false;
95      }
96  
97      static String [] reserved_words = new String[] {
98          "null",
99          "true",
100         "false",
101         "abstract",
102         "double",
103         "int",
104         "strictfp",
105         "boolean",
106         "else",
107         "interface",
108         "super",
109         "break",
110         "extends",
111         "long",
112         "switch",
113         "byte",
114         "final",
115         "native",
116         "synchronized",
117         "case",
118         "finally",
119         "new",
120         "this",
121         "catch",
122         "float",
123         "package",
124         "throw",
125         "char",
126         "for",
127         "private",
128         "throws",
129         "class",
130         "goto",
131         "protected",
132         "transient",
133         "const",
134         "if",
135         "public",
136         "try",
137         "continue",
138         "implements",
139         "return",
140         "void",
141         "default",
142         "import",
143         "short",
144         "volatile",
145         "do",
146         "instanceof",
147         "static",
148         "while",
149         "assert",
150         "enum"
151     };
152 
153     static String latin[] = {
154         "Ac",
155         "Aethiopia",
156         "Africae",
157         "Anazarbus",
158         "Argonautarum",
159         "Ciliciam",
160         "Cydno",
161         "Danaes,",
162         "Etenim",
163         "Iovis",
164         "Mopsi,",
165         "Mopsuestia",
166         "Perseus",
167         "Quibus",
168         "Sandan",
169         "Tarsus",
170         "a",
171         "abstractum",
172         "ad",
173         "alacriter",
174         "alia",
175         "amni",
176         "anceps",
177         "artes,",
178         "auctoris",
179         "aureo",
180         "aut",
181         "bene",
182         "certamen",
183         "certe",
184         "cespite",
185         "cognatione",
186         "commune",
187         "concitat",
188         "condidisse",
189         "conmilitio",
190         "consumpsit,",
191         "consurgentem",
192         "continentur.",
193         "cum",
194         "cunctorum.",
195         "dediti",
196         "delatumque",
197         "dicendi",
198         "dici",
199         "direpto",
200         "disciplina,",
201         "distarent,",
202         "dolorem",
203         "dolorum",
204         "domicilium",
205         "ductores",
206         "eius",
207         "eo",
208         "errore",
209         "et",
210         "eum",
211         "ex",
212         "explicatis",
213         "exultat,",
214         "facultas",
215         "feriens",
216         "filius",
217         "forte",
218         "fuimus.",
219         "gestu",
220         "habent",
221         "habitus",
222         "haec",
223         "hanc",
224         "hastisque",
225         "haut",
226         "heroici",
227         "hoc",
228         "huic",
229         "humanitatem",
230         "iam",
231         "illius",
232         "in",
233         "ingeni,",
234         "intempestivum",
235         "inter",
236         "iram",
237         "ita",
238         "litus",
239         "locari",
240         "longe",
241         "manes",
242         "medentur",
243         "memoratur,",
244         "miles",
245         "miretur,",
246         "mors",
247         "muri",
248         "ne",
249         "neque",
250         "nobilis",
251         "nobilitat,",
252         "nobis",
253         "nomine",
254         "nos",
255         "occurrere",
256         "omnes",
257         "opulentus",
258         "ordinibus",
259         "parans",
260         "penitus",
261         "perspicabilis",
262         "pertinax",
263         "pertinent,",
264         "plerumque",
265         "poterat",
266         "profectus",
267         "proximos",
268         "pugnantium",
269         "punico",
270         "quadam",
271         "quae",
272         "quaedam",
273         "quasi",
274         "quem",
275         "qui",
276         "quidam",
277         "quidem",
278         "quis",
279         "quod",
280         "quoddam",
281         "quorum",
282         "rati",
283         "ratio",
284         "redirent,",
285         "referens,",
286         "repentina",
287         "revocavere",
288         "scuta",
289         "se",
290         "securitas",
291         "sed",
292         "sit",
293         "solido",
294         "sospitales.",
295         "studio",
296         "subire",
297         "tecti",
298         "terrebat",
299         "tutela",
300         "umquam",
301         "uni",
302         "urbs",
303         "varietati",
304         "vatis",
305         "vel",
306         "vellere",
307         "vero,",
308         "vinculum,",
309         "vir",
310         "vocabulum"
311     };
312 
313     private static Random rand = new Random();
314     public static String getSampleString(int size)
315     {
316         StringBuffer s = new StringBuffer(size);
317         while (s.length() < size - 5)
318         {
319             s.append(latin[rand.nextInt(latin.length)]).append(" ");
320         }
321         return s.toString();
322     }
323 }