[hibernate-commits] Hibernate SVN: r10318 - branches/Branch_3_2/Hibernate3/src/org/hibernate/util

hibernate-commits at lists.jboss.org hibernate-commits at lists.jboss.org
Wed Aug 23 09:36:51 EDT 2006


Author: steve.ebersole at jboss.com
Date: 2006-08-23 09:36:35 -0400 (Wed, 23 Aug 2006)
New Revision: 10318

Modified:
   branches/Branch_3_2/Hibernate3/src/org/hibernate/util/StringHelper.java
Log:
HHH-2022 : property names with leading underscores

Modified: branches/Branch_3_2/Hibernate3/src/org/hibernate/util/StringHelper.java
===================================================================
--- branches/Branch_3_2/Hibernate3/src/org/hibernate/util/StringHelper.java	2006-08-23 13:35:51 UTC (rev 10317)
+++ branches/Branch_3_2/Hibernate3/src/org/hibernate/util/StringHelper.java	2006-08-23 13:36:35 UTC (rev 10318)
@@ -305,6 +305,10 @@
 		}
 	}
 
+	public static String generateAlias(String description) {
+		return generateAliasRoot(description) + '_';
+	}
+
 	/**
 	 * Generate a nice alias for the given class name or collection role
 	 * name and unique integer. Subclasses of Loader do <em>not</em> have 
@@ -317,11 +321,20 @@
 			'_';
 	}
 
+	/**
+	 * Generates a root alias by truncating the "root name" defined by
+	 * the incoming decription and removing/modifying any non-valid
+	 * alias characters.
+	 *
+	 * @param description The root name from which to generate a root alias.
+	 * @return The generated root alias.
+	 */
 	private static String generateAliasRoot(String description) {
-		final String result = truncate( unqualifyEntityName(description), ALIAS_TRUNCATE_LENGTH )
+		String result = truncate( unqualifyEntityName(description), ALIAS_TRUNCATE_LENGTH )
 				.toLowerCase()
 		        .replace( '/', '_' ) // entityNames may now include slashes for the representations
 				.replace( '$', '_' ); //classname may be an inner class
+		result = cleanAlias( result );
 		if ( Character.isDigit( result.charAt(result.length()-1) ) ) {
 			return result + "x"; //ick!
 		}
@@ -330,6 +343,28 @@
 		}
 	}
 
+	/**
+	 * Clean the generated alias by removing any non-alpha characters from the
+	 * beginning.
+	 *
+	 * @param alias The generated alias to be cleaned.
+	 * @return The cleaned alias, stripped of any leading non-alpha characters.
+	 */
+	private static String cleanAlias(String alias) {
+		char[] chars = alias.toCharArray();
+		// short cut check...
+		if ( !Character.isLetter( chars[0] ) ) {
+			for ( int i = 1; i < chars.length; i++ ) {
+				// as soon as we encounter our first letter, return the substring
+				// from that position
+				if ( Character.isLetter( chars[i] ) ) {
+					return alias.substring( i );
+				}
+			}
+		}
+		return alias;
+	}
+
 	public static String unqualifyEntityName(String entityName) {
 		String result = unqualify(entityName);
 		int slashPos = result.indexOf( '/' );
@@ -339,10 +374,6 @@
 		return result;
 	}
 
-	public static String generateAlias(String description) {
-		return generateAliasRoot(description) + '_';
-	}
-	
 	public static String toUpperCase(String str) {
 		return str==null ? null : str.toUpperCase();
 	}




More information about the hibernate-commits mailing list