cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aadamc...@apache.org
Subject svn commit: r507653 - in /cayenne/main/branches/STABLE-1.2/cayenne: cayenne-java/src/cayenne/java/org/objectstyle/cayenne/util/ cayenne-java/src/tests/java/org/objectstyle/cayenne/util/ cayenne-other/release-notes/
Date Wed, 14 Feb 2007 18:51:06 GMT
Author: aadamchik
Date: Wed Feb 14 10:51:05 2007
New Revision: 507653

URL: http://svn.apache.org/viewvc?view=rev&rev=507653
Log:
CAY-751 AS400 datafields containing # in field names do not get mapped correctly (1.2)

Modified:
    cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/util/NameConverter.java
    cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/util/NameConverterTst.java
    cayenne/main/branches/STABLE-1.2/cayenne/cayenne-other/release-notes/RELEASE-NOTES-1.2.3.txt

Modified: cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/util/NameConverter.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/util/NameConverter.java?view=diff&rev=507653&r1=507652&r2=507653
==============================================================================
--- cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/util/NameConverter.java
(original)
+++ cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/cayenne/java/org/objectstyle/cayenne/util/NameConverter.java
Wed Feb 14 10:51:05 2007
@@ -55,6 +55,8 @@
  */
 package org.objectstyle.cayenne.util;
 
+import java.util.HashMap;
+import java.util.Map;
 import java.util.StringTokenizer;
 
 /** 
@@ -65,6 +67,12 @@
  */
 public class NameConverter {
 
+    private static final Map SPECIAL_CHAR_TO_JAVA_MAPPING = new HashMap();
+
+    static {
+        SPECIAL_CHAR_TO_JAVA_MAPPING.put("#", "pound");
+    }
+
     /**
      * Converts a String name to a String forllowing java convention for the 
      * static final variables. E.g. "abcXyz" will be converted to "ABC_XYZ".
@@ -76,6 +84,11 @@
             return null;
         }
 
+        // clear of non-java chars. While the method name implies that a passed identifier
+        // is pure Java, it is used to build pk columns names and such, so extra safety
+        // check is a good idea
+        name = specialCharsToJava(name);
+
         char charArray[] = name.toCharArray();
         StringBuffer buffer = new StringBuffer();
 
@@ -117,6 +130,9 @@
         while (st.hasMoreTokens()) {
             String token = st.nextToken();
 
+            // clear of non-java chars
+            token = specialCharsToJava(token);
+
             int len = token.length();
             if (len == 0) {
                 continue;
@@ -155,5 +171,30 @@
             }
         }
         return buf.toString();
+    }
+    
+    /**
+     * Replaces special chars with human-readable and Java-id-compatible symbols.
+     */
+    static String specialCharsToJava(String string) {
+        int len = string.length();
+        if (len == 0) {
+            return string;
+        }
+
+        StringBuffer buffer = new StringBuffer(len);
+        for (int i = 0; i < len; i++) {
+
+            char c = string.charAt(i);
+            if (Character.isJavaIdentifierPart(c)) {
+                buffer.append(c);
+            }
+            else {
+                Object word = SPECIAL_CHAR_TO_JAVA_MAPPING.get(String.valueOf(c));
+                buffer.append(word != null ? word : "_");
+            }
+        }
+
+        return buffer.toString();
     }
 }

Modified: cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/util/NameConverterTst.java
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/util/NameConverterTst.java?view=diff&rev=507653&r1=507652&r2=507653
==============================================================================
--- cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/util/NameConverterTst.java
(original)
+++ cayenne/main/branches/STABLE-1.2/cayenne/cayenne-java/src/tests/java/org/objectstyle/cayenne/util/NameConverterTst.java
Wed Feb 14 10:51:05 2007
@@ -61,16 +61,16 @@
 
     public void testUnderscoredToJava1() throws Exception {
         String expected = "ClassNameIdentifier";
-        assertEquals(
-            expected,
-            NameConverter.underscoredToJava("_CLASS_NAME_IDENTIFIER_", true));
+        assertEquals(expected, NameConverter.underscoredToJava(
+                "_CLASS_NAME_IDENTIFIER_",
+                true));
     }
 
     public void testUnderscoredToJava2() throws Exception {
         String expected = "propNameIdentifier123";
-        assertEquals(
-            expected,
-            NameConverter.underscoredToJava("_prop_name_Identifier_123", false));
+        assertEquals(expected, NameConverter.underscoredToJava(
+                "_prop_name_Identifier_123",
+                false));
     }
 
     public void testUnderscoredToJava3() throws Exception {
@@ -86,6 +86,10 @@
     public void testUnderscoredToJava5() throws Exception {
         String expected = "LastName";
         assertEquals(expected, NameConverter.underscoredToJava("LastName", true));
+    }
+
+    public void testUnderscoredToJavaSpecialChars() throws Exception {
+        assertEquals("ABCpoundXyz", NameConverter.underscoredToJava("ABC#_XYZ", true));
     }
 
     public void testJavaToUnderscored1() throws Exception {

Modified: cayenne/main/branches/STABLE-1.2/cayenne/cayenne-other/release-notes/RELEASE-NOTES-1.2.3.txt
URL: http://svn.apache.org/viewvc/cayenne/main/branches/STABLE-1.2/cayenne/cayenne-other/release-notes/RELEASE-NOTES-1.2.3.txt?view=diff&rev=507653&r1=507652&r2=507653
==============================================================================
--- cayenne/main/branches/STABLE-1.2/cayenne/cayenne-other/release-notes/RELEASE-NOTES-1.2.3.txt
(original)
+++ cayenne/main/branches/STABLE-1.2/cayenne/cayenne-other/release-notes/RELEASE-NOTES-1.2.3.txt
Wed Feb 14 10:51:05 2007
@@ -13,3 +13,4 @@
 CAY-741 Indexing entities with inheritance across datamaps
 CAY-743 Reordering mappings from domain in xml resulting in StackOverflowException
 CAY-750 Stored procedures without parameters fail on Postgres 8
+CAY-751 AS400 datafields containing # in field names do not get mapped correctly



Mime
View raw message