KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > cfg > ImprovedNamingStrategy


1 //$Id: ImprovedNamingStrategy.java,v 1.2 2005/06/01 19:39:56 oneovthafew Exp $
2
package org.hibernate.cfg;
3
4 import java.io.Serializable JavaDoc;
5
6 import org.hibernate.util.StringHelper;
7
8 /**
9  * An improved naming strategy that prefers embedded
10  * underscores to mixed case names
11  * @see DefaultNamingStrategy the default strategy
12  * @author Gavin King
13  */

14 public class ImprovedNamingStrategy implements NamingStrategy, Serializable JavaDoc {
15
16     /**
17      * The singleton instance
18      */

19     public static final NamingStrategy INSTANCE = new ImprovedNamingStrategy();
20
21     protected ImprovedNamingStrategy() {}
22
23     /**
24      * Return the unqualified class name, mixed case converted to
25      * underscores
26      */

27     public String JavaDoc classToTableName(String JavaDoc className) {
28         return addUnderscores( StringHelper.unqualify(className) );
29     }
30     /**
31      * Return the full property path with underscore seperators, mixed
32      * case converted to underscores
33      */

34     public String JavaDoc propertyToColumnName(String JavaDoc propertyName) {
35         return addUnderscores(propertyName);
36     }
37     /**
38      * Convert mixed case to underscores
39      */

40     public String JavaDoc tableName(String JavaDoc tableName) {
41         return addUnderscores(tableName);
42     }
43     /**
44      * Convert mixed case to underscores
45      */

46     public String JavaDoc columnName(String JavaDoc columnName) {
47         return addUnderscores(columnName);
48     }
49     /**
50      * Return the full property path prefixed by the unqualified class
51      * name, with underscore seperators, mixed case converted to underscores
52      */

53     public String JavaDoc propertyToTableName(String JavaDoc className, String JavaDoc propertyName) {
54         return classToTableName(className) + '_' + propertyToColumnName(propertyName);
55     }
56
57     private static String JavaDoc addUnderscores(String JavaDoc name) {
58         StringBuffer JavaDoc buf = new StringBuffer JavaDoc( name.replace('.', '_') );
59         for (int i=1; i<buf.length()-1; i++) {
60             if (
61                 Character.isLowerCase( buf.charAt(i-1) ) &&
62                 Character.isUpperCase( buf.charAt(i) ) &&
63                 Character.isLowerCase( buf.charAt(i+1) )
64             ) {
65                 buf.insert(i++, '_');
66             }
67         }
68         return buf.toString().toLowerCase();
69     }
70
71 }
72
Popular Tags