KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > bull > eclipse > jonas > utils > DBToJavaNameTransformUtil


1 /*
2  * Created on 13 févr. 2005
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package com.bull.eclipse.jonas.utils;
8
9 /**
10  * @author riase
11  *
12  * TODO To change the template for this generated type comment go to
13  * Window - Preferences - Java - Code Style - Code Templates
14  */

15 public class DBToJavaNameTransformUtil {
16        public static String JavaDoc dbNameToVariableName(String JavaDoc s) {
17           if ("".equals(s)) {
18              return s;
19           }
20           StringBuffer JavaDoc result = new StringBuffer JavaDoc();
21           boolean capitalize = true;
22           boolean lastCapital = false;
23           boolean lastDecapitalized = false;
24           for (int i = 0; i < s.length(); i++) {
25              String JavaDoc c = s.substring(i, i + 1);
26              if ("_".equals(c) || " ".equals(c)) {
27                 capitalize = true;
28                 continue;
29              }
30
31              if (c.toUpperCase().equals(c)) {
32                 if (lastDecapitalized && !lastCapital) {
33                    capitalize = true;
34                 }
35                 lastCapital = true;
36              }
37              else {
38                 lastCapital = false;
39              }
40
41              if (capitalize) {
42                 result.append(c.toUpperCase());
43                 capitalize = false;
44              }
45              else {
46                 result.append(c.toLowerCase());
47                 lastDecapitalized = true;
48              }
49
50           }
51           String JavaDoc r = result.toString();
52           return r;
53        }
54
55
56         public static String JavaDoc singularise(String JavaDoc name) {
57             String JavaDoc result = name;
58             if (seemsPluralised(name)) {
59                 String JavaDoc lower = name.toLowerCase();
60                 if (lower.endsWith("ies")) {
61                     // cities --> city
62
result = name.substring(0, name.length() - 3) + "y";
63                 } else if (lower.endsWith("ches") || lower.endsWith("ses")) {
64                     // switches --> switch or buses --> bus
65
result = name.substring(0, name.length() - 2);
66                 } else if (lower.endsWith("s")) {
67                     // customers --> customer
68
result = name.substring(0, name.length() - 1);
69                 }
70             }
71             return result;
72         }
73
74         private static boolean seemsPluralised(String JavaDoc name) {
75             name = name.toLowerCase();
76             boolean pluralised = false;
77             pluralised |= name.endsWith("es");
78             pluralised |= name.endsWith("s");
79             pluralised &= !(name.endsWith("ss") || name.endsWith("us"));
80             return pluralised;
81         }
82
83         public static String JavaDoc capitalise(String JavaDoc s) {
84             if (s.equals("")) {
85                return "";
86             }
87             if (s.length() == 1) {
88                return s.toUpperCase();
89             }
90             else {
91                String JavaDoc caps = s.substring(0, 1).toUpperCase();
92                String JavaDoc rest = s.substring(1);
93                return caps + rest;
94             }
95          }
96
97         
98 }
99
Popular Tags