Author: ivaynberg
Date: Mon Oct 13 15:22:06 2008
New Revision: 704273

URL: http://svn.apache.org/viewvc?rev=704273&view=rev
Log:
reapplying WICKET-1868 to wicket-1.3.x since it doesnt seem to break this branch

Added:
    
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_en.txt
   (with props)
    
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_foo_bar.txt
   (with props)
Modified:
    
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/resource/locator/ResourceNameIterator.java
    
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/PackageResourceTest.java

Modified: 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/resource/locator/ResourceNameIterator.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/resource/locator/ResourceNameIterator.java?rev=704273&r1=704272&r2=704273&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/resource/locator/ResourceNameIterator.java
 (original)
+++ 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/main/java/org/apache/wicket/util/resource/locator/ResourceNameIterator.java
 Mon Oct 13 15:22:06 2008
@@ -16,8 +16,12 @@
  */
 package org.apache.wicket.util.resource.locator;
 
+import java.util.Arrays;
+import java.util.HashSet;
 import java.util.Iterator;
 import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.wicket.WicketRuntimeException;
 import org.apache.wicket.util.string.Strings;
@@ -50,6 +54,8 @@
  */
 public class ResourceNameIterator implements Iterator
 {
+       private static final Pattern LOCALE_PATTERN = 
Pattern.compile("_[a-zA-Z]{2}($|(?=_))");
+
        // The locale to search for the resource file
        private final Locale locale;
 
@@ -64,6 +70,10 @@
        // The latest exact Locale used
        private Locale currentLocale;
 
+       private final HashSet isoCountries = new 
HashSet(Arrays.asList(Locale.getISOCountries()));
+
+       private final HashSet isoLanguages = new 
HashSet(Arrays.asList(Locale.getISOLanguages()));
+
        /**
         * Construct.
         * 
@@ -77,7 +87,7 @@
         *            the filname's extensions (comma separated)
         */
        public ResourceNameIterator(String path, final String style, final 
Locale locale,
-                       final String extensions)
+               final String extensions)
        {
                this.locale = locale;
                if (extensions == null)
@@ -90,7 +100,63 @@
                        this.extensions = extensions;
                }
 
-               this.styleIterator = new 
StyleAndVariationResourceNameIterator(path, style, null);
+               Matcher matcher = LOCALE_PATTERN.matcher(path);
+               if (matcher.find())
+               {
+                       String language = null;
+                       String country = null;
+                       String variant = null;
+                       int firstValidLocalePatternFragment = -1;
+                       do
+                       {
+                               String s = matcher.group().substring(1, 3);
+                               if (Character.isLowerCase(s.charAt(0)))
+                               {
+                                       if (isoLanguages.contains(s))
+                                       {
+                                               language = s;
+                                               firstValidLocalePatternFragment 
= matcher.start();
+                                               break;
+                                       }
+                               }
+                       }
+                       while (matcher.find());
+
+                       // did we find a language?
+                       if (language != null)
+                       {
+                               // check for country
+                               if (matcher.find())
+                               {
+                                       do
+                                       {
+                                               String s = 
matcher.group().substring(1, 3);
+                                               if 
(Character.isUpperCase(s.charAt(0)))
+                                               {
+                                                       if 
(isoCountries.contains(s))
+                                                       {
+                                                               country = s;
+                                                               break;
+                                                       }
+                                               }
+                                       }
+                                       while (matcher.find());
+                               }
+                               if (country != null)
+                               {
+                                       // country found... just get the rest 
of the string for any variant
+                                       if (matcher.find())
+                                       {
+                                               variant = 
path.substring(matcher.start());
+                                       }
+                               }
+                               path = path.substring(0, 
firstValidLocalePatternFragment);
+                               localeIterator = new 
LocaleResourceNameIterator(path, new Locale(language,
+                                       country != null ? country : "", variant 
!= null ? variant : ""));
+                       } // else skip the whole thing... probably user 
specific underscores used
+               }
+
+               styleIterator = new StyleAndVariationResourceNameIterator(path, 
style, null);
        }
 
        /**
@@ -100,7 +166,7 @@
         */
        public final Locale getLocale()
        {
-               return this.currentLocale;
+               return currentLocale;
        }
 
        /**
@@ -109,9 +175,9 @@
        public boolean hasNext()
        {
                // Most inner loop. Loop through all extensions provided
-               if (this.extenstionsIterator != null)
+               if (extenstionsIterator != null)
                {
-                       if (this.extenstionsIterator.hasNext() == true)
+                       if (extenstionsIterator.hasNext() == true)
                        {
                                return true;
                        }
@@ -123,38 +189,36 @@
                }
 
                // 2nd inner loop: Loop through all Locale combinations
-               if (this.localeIterator != null)
+               if (localeIterator != null)
                {
-                       while (this.localeIterator.hasNext())
+                       while (localeIterator.hasNext())
                        {
                                // Get the next Locale from the iterator and 
start the next
                                // inner iterator over again.
-                               String newPath = 
(String)this.localeIterator.next();
-                               this.currentLocale = 
this.localeIterator.getLocale();
-                               this.extenstionsIterator = new 
ExtensionResourceNameIterator(newPath,
-                                               this.extensions);
-                               if (this.extenstionsIterator.hasNext() == true)
+                               String newPath = (String)localeIterator.next();
+                               currentLocale = localeIterator.getLocale();
+                               extenstionsIterator = new 
ExtensionResourceNameIterator(newPath, extensions);
+                               if (extenstionsIterator.hasNext() == true)
                                {
                                        return true;
                                }
                        }
-                       this.localeIterator = null;
+                       localeIterator = null;
                }
 
                // Most outer loop: Loop through all combinations of styles and
                // variations
-               while (this.styleIterator.hasNext())
+               while (styleIterator.hasNext())
                {
-                       String newPath = (String)this.styleIterator.next();
+                       String newPath = (String)styleIterator.next();
 
-                       this.localeIterator = new 
LocaleResourceNameIterator(newPath, this.locale);
-                       while (this.localeIterator.hasNext())
+                       localeIterator = new 
LocaleResourceNameIterator(newPath, locale);
+                       while (localeIterator.hasNext())
                        {
-                               newPath = (String)this.localeIterator.next();
-                               this.currentLocale = 
this.localeIterator.getLocale();
-                               this.extenstionsIterator = new 
ExtensionResourceNameIterator(newPath,
-                                               this.extensions);
-                               if (this.extenstionsIterator.hasNext() == true)
+                               newPath = (String)localeIterator.next();
+                               currentLocale = localeIterator.getLocale();
+                               extenstionsIterator = new 
ExtensionResourceNameIterator(newPath, extensions);
+                               if (extenstionsIterator.hasNext() == true)
                                {
                                        return true;
                                }
@@ -175,7 +239,7 @@
                        return extenstionsIterator.next();
                }
                throw new WicketRuntimeException(
-                               "Illegal call of next(). Iterator not properly 
initialized");
+                       "Illegal call of next(). Iterator not properly 
initialized");
        }
 
        /**

Modified: 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/PackageResourceTest.java
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/PackageResourceTest.java?rev=704273&r1=704272&r2=704273&view=diff
==============================================================================
--- 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/PackageResourceTest.java
 (original)
+++ 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/PackageResourceTest.java
 Mon Oct 13 15:22:06 2008
@@ -65,9 +65,9 @@
                final SharedResources sharedResources = 
Application.get().getSharedResources();
                PackageResource.bind(application, PackageResourceTest.class, 
"packaged1.txt");
                assertNotNull("resource packaged1.txt should be available as a 
packaged resource",
-                               sharedResources.get(PackageResourceTest.class, 
"packaged1.txt", null, null, true));
+                       sharedResources.get(PackageResourceTest.class, 
"packaged1.txt", null, null, true));
                assertNull("resource packaged2.txt should NOT be available as a 
packaged resource",
-                               sharedResources.get(PackageResourceTest.class, 
"packaged2.txt", null, null, true));
+                       sharedResources.get(PackageResourceTest.class, 
"packaged2.txt", null, null, true));
        }
 
        /**
@@ -91,14 +91,43 @@
                assertFalse(guard.accept(PackageResourceTest.class, 
"Bar.java"));
        }
 
-       public void testInvalidPackageResource() throws Exception
+       /**
+        * Test lenient matching
+        * 
+        * @throws Exception
+        */
+       public void testLenientPackageResourceMatching() throws Exception
        {
                final SharedResources sharedResources = 
Application.get().getSharedResources();
                Resource invalidResource = new 
PackageResource(PackageResourceTest.class, "packaged3.txt",
-                               Locale.ENGLISH, null);
+                       Locale.ENGLISH, null);
                assertNotNull(
-                               "resource packaged3.txt SHOULD be available as 
a packaged resource even if it doesn't exist",
-                               invalidResource);
+                       "resource packaged3.txt SHOULD be available as a 
packaged resource even if it doesn't exist",
+                       invalidResource);
+
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1.txt", null, null));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1.txt", Locale.CHINA,
+                       null));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1.txt", Locale.CHINA,
+                       "foo"));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1.txt", null, "foo"));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1_en.txt", null, null));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1_en_US.txt", null,
+                       null));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1_en_US.txt", null,
+                       "foo"));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1_en_US.txt",
+                       Locale.US, null));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1_en_US.txt",
+                       Locale.CANADA, null));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1_en_US.txt",
+                       Locale.CHINA, null));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1_foo_bar_en.txt",
+                       null, null));
+               assertTrue(PackageResource.exists(PackageResourceTest.class, 
"packaged1_foo_bar_en_US.txt",
+                       null, null));
+               assertTrue(PackageResource.exists(PackageResourceTest.class,
+                       "packaged1_foo_bar_en_US_MAC.txt", null, null));
 
                try
                {

Added: 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_en.txt
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_en.txt?rev=704273&view=auto
==============================================================================
--- 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_en.txt
 (added)
+++ 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_en.txt
 Mon Oct 13 15:22:06 2008
@@ -0,0 +1 @@
+TEST
\ No newline at end of file

Propchange: 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_en.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_foo_bar.txt
URL: 
http://svn.apache.org/viewvc/wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_foo_bar.txt?rev=704273&view=auto
==============================================================================
--- 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_foo_bar.txt
 (added)
+++ 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_foo_bar.txt
 Mon Oct 13 15:22:06 2008
@@ -0,0 +1 @@
+TEST
\ No newline at end of file

Propchange: 
wicket/branches/wicket-1.3.x/jdk-1.4/wicket/src/test/java/org/apache/wicket/markup/html/packaged1_foo_bar.txt
------------------------------------------------------------------------------
    svn:mime-type = text/plain


Reply via email to