Revision: 1489
          http://stripes.svn.sourceforge.net/stripes/?rev=1489&view=rev
Author:   bengunter
Date:     2012-05-17 17:22:07 +0000 (Thu, 17 May 2012)
Log Message:
-----------
Applied fix for STS-835 from 1.5.x branch.

Modified Paths:
--------------
    
trunk/stripes/src/net/sourceforge/stripes/localization/LocalizationUtility.java
    trunk/stripes/src/net/sourceforge/stripes/tag/InputOptionsCollectionTag.java
    
trunk/stripes/src/net/sourceforge/stripes/tag/InputOptionsEnumerationTag.java
    
trunk/tests/src/net/sourceforge/stripes/localization/LocalizationUtilityTest.java

Property Changed:
----------------
    trunk/


Property changes on: trunk
___________________________________________________________________
Modified: svn:mergeinfo
   - 
/branches/1.5.x:1463-1464,1466,1468-1469,1471,1473,1475,1477-1478,1482,1484,1486
   + 
/branches/1.5.x:1463-1464,1466,1468-1469,1471,1473,1475,1477-1478,1482,1484,1486,1488

Modified: 
trunk/stripes/src/net/sourceforge/stripes/localization/LocalizationUtility.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/localization/LocalizationUtility.java 
    2012-05-17 17:03:59 UTC (rev 1488)
+++ 
trunk/stripes/src/net/sourceforge/stripes/localization/LocalizationUtility.java 
    2012-05-17 17:22:07 UTC (rev 1489)
@@ -152,4 +152,26 @@
             return null;
         }
     }
+
+    /**
+     * Gets the simple name of a class for use as a key to look up a resource. 
This is usually the
+     * same as {@link Class#getSimpleName()}, but static inner classes are 
handled such that the
+     * simple name is {@code OuterClass.InnerClass}. Multiple layers of 
nesting are supported.
+     * 
+     * @param c The class whose simple name is requested.
+     * @return The simple name of the class.
+     */
+    public static String getSimpleName(Class<?> c) {
+        if (c.getEnclosingClass() == null)
+            return c.getSimpleName();
+        else
+            return prefixSimpleName(new StringBuilder(), c).toString();
+    }
+
+    /** A recursive method used by {@link #getSimpleName(Class)}. */
+    private static StringBuilder prefixSimpleName(StringBuilder s, Class<?> c) 
{
+        if (c.getEnclosingClass() != null)
+            prefixSimpleName(s, c.getEnclosingClass()).append('.');
+        return s.append(c.getSimpleName());
+    }
 }

Modified: 
trunk/stripes/src/net/sourceforge/stripes/tag/InputOptionsCollectionTag.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/tag/InputOptionsCollectionTag.java    
    2012-05-17 17:03:59 UTC (rev 1488)
+++ 
trunk/stripes/src/net/sourceforge/stripes/tag/InputOptionsCollectionTag.java    
    2012-05-17 17:22:07 UTC (rev 1489)
@@ -278,21 +278,22 @@
                 if (attemptToLocalizeLabels) {
                     // Try to localize the label
                     String packageName = clazz.getPackage() == null ? "" : 
clazz.getPackage().getName();
+                    String simpleName = 
LocalizationUtility.getSimpleName(clazz);
                     String localizedLabel = null;
                     if (label != null) {
                         localizedLabel = 
LocalizationUtility.getLocalizedFieldName
-                            (clazz.getSimpleName() + "."  + label, 
packageName, null, locale);
+                            (simpleName + "."  + label, packageName, null, 
locale);
                     }
                     if (localizedLabel == null && value != null) {
                         localizedLabel = 
LocalizationUtility.getLocalizedFieldName
-                            (clazz.getSimpleName() + "."  + value, 
packageName, null, locale);
+                            (simpleName + "."  + value, packageName, null, 
locale);
                     }
                     if (localizedLabel != null) label = localizedLabel;
 
                     // Try to localize the group
                     if (group != null) {
                         String localizedGroup = 
LocalizationUtility.getLocalizedFieldName(
-                            clazz.getSimpleName() + "." + group, packageName, 
null, locale);
+                            simpleName + "." + group, packageName, null, 
locale);
                         if (localizedGroup != null) group = localizedGroup;
                     }
                 }

Modified: 
trunk/stripes/src/net/sourceforge/stripes/tag/InputOptionsEnumerationTag.java
===================================================================
--- 
trunk/stripes/src/net/sourceforge/stripes/tag/InputOptionsEnumerationTag.java   
    2012-05-17 17:03:59 UTC (rev 1488)
+++ 
trunk/stripes/src/net/sourceforge/stripes/tag/InputOptionsEnumerationTag.java   
    2012-05-17 17:22:07 UTC (rev 1489)
@@ -122,12 +122,13 @@
 
             for (Enum item : enums) {
                 Object label = null;
+                String simpleName = LocalizationUtility.getSimpleName(clazz);
 
                 if (attemptToLocalizeLabels) {
                     String packageName = clazz.getPackage() == null ? "" : 
clazz.getPackage().getName();
 
                     // Check for a localized label using class.ENUM_VALUE and 
package.class.ENUM_VALUE
-                    label = 
LocalizationUtility.getLocalizedFieldName(clazz.getSimpleName() + "." + 
item.name(),
+                    label = 
LocalizationUtility.getLocalizedFieldName(simpleName + "." + item.name(),
                                                                       
packageName,
                                                                       null,
                                                                       locale);

Modified: 
trunk/tests/src/net/sourceforge/stripes/localization/LocalizationUtilityTest.java
===================================================================
--- 
trunk/tests/src/net/sourceforge/stripes/localization/LocalizationUtilityTest.java
   2012-05-17 17:03:59 UTC (rev 1488)
+++ 
trunk/tests/src/net/sourceforge/stripes/localization/LocalizationUtilityTest.java
   2012-05-17 17:22:07 UTC (rev 1489)
@@ -44,4 +44,24 @@
         String output = LocalizationUtility.makePseudoFriendlyName(input);
         Assert.assertEquals(output, "Bug Submitted By First Name");
     }
+
+    public static enum TestEnum {
+        A, B, C;
+    }
+
+    public static class A {
+        public static class B {
+            public static class C {
+            }
+        }
+    }
+
+    @Test(groups = "fast")
+    public void testSimpleClassName() throws Exception {
+        String output = LocalizationUtility.getSimpleName(TestEnum.class);
+        Assert.assertEquals(output, "LocalizationUtilityTest.TestEnum");
+
+        output = LocalizationUtility.getSimpleName(A.B.C.class);
+        Assert.assertEquals(output, "LocalizationUtilityTest.A.B.C");
+    }
 }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Live Security Virtual Conference
Exclusive live event will cover all the ways today's security and 
threat landscape has changed and how IT managers can respond. Discussions 
will include endpoint security, mobile security and the latest in malware 
threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
_______________________________________________
Stripes-development mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/stripes-development

Reply via email to