tomcat-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jboy...@apache.org
Subject svn commit: r1441843 - in /tomcat/taglibs/standard/trunk: impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/ impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ impl/src/test/java/org/apache/taglibs/standard/tag/common/fmt/ jstlel/sr...
Date Sun, 03 Feb 2013 00:43:10 GMT
Author: jboynes
Date: Sun Feb  3 00:43:10 2013
New Revision: 1441843

URL: http://svn.apache.org/viewvc?rev=1441843&view=rev
Log:
refactor parsing of attributes used to specify a locale

Added:
    tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtil.java
  (with props)
Modified:
    tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/SetLocaleSupport.java
    tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ParseDateTag.java
    tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ParseNumberTag.java
    tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtilTest.java
  (contents, props changed)
    tomcat/taglibs/standard/trunk/jstlel/src/main/java/org/apache/taglibs/standard/tag/el/fmt/ParseDateTag.java
    tomcat/taglibs/standard/trunk/jstlel/src/main/java/org/apache/taglibs/standard/tag/el/fmt/ParseNumberTag.java

Added: tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtil.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtil.java?rev=1441843&view=auto
==============================================================================
--- tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtil.java
(added)
+++ tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtil.java
Sun Feb  3 00:43:10 2013
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.taglibs.standard.tag.common.fmt;
+
+import org.apache.taglibs.standard.resources.Resources;
+
+import java.util.Locale;
+
+/**
+ * Utility functions for working with Locales.
+ *
+ * This implementation sticks with the Locale parsing rules defined by the JSTL Specification.
+ * When permitted, this should be updated to use the richer Locale parsing rules provided
+ * in Java 7 and later.
+ *
+ */
+public class LocaleUtil {
+    private static final char HYPHEN = '-';
+    private static final char UNDERSCORE = '_';
+
+    /**
+     * Handles Locales that can be passed to tags as instances of String or Locale.
+     * If the parameter is an instance of Locale, it is simply returned.
+     * If the parameter is a String and is not empty, then it is parsed to a Locale
+     * using {@link LocaleUtil#parseLocale(String)}.
+     * Otherwise null will be returned.
+     *
+     * @param stringOrLocale locale represented as an instance of Locale or as a String
+     * @return the locale represented by the parameter, or null if the parameter is undefined
+     */
+    public static Locale parseLocale(Object stringOrLocale) {
+        if (stringOrLocale instanceof Locale) {
+            return (Locale) stringOrLocale;
+        } else if (stringOrLocale instanceof String) {
+            String string = (String) stringOrLocale;
+            if (string.length() == 0) {
+                return null;
+            } else {
+                return parseLocale(string.trim());
+            }
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * See parseLocale(String, String) for details.
+     */
+    public static Locale parseLocale(String locale) {
+        return parseLocale(locale, null);
+    }
+
+    /**
+     * Parses the given locale string into its language and (optionally)
+     * country components, and returns the corresponding {@link Locale} object.
+     *
+     * @param locale  the locale string to parse; must not be null or empty
+     * @param variant the variant
+     * @return the specified Locale
+     * @throws IllegalArgumentException if the given locale does not have a
+     *                                  language component or has an empty country component
+     */
+    public static Locale parseLocale(String locale, String variant) {
+
+        String language;
+        String country;
+        int index;
+
+        if (((index = locale.indexOf(HYPHEN)) > -1) || ((index = locale.indexOf(UNDERSCORE))
> -1)) {
+            if (index == 0) {
+                throw new IllegalArgumentException(Resources.getMessage("LOCALE_NO_LANGUAGE"));
+            } else if (index == locale.length() - 1) {
+                throw new IllegalArgumentException(Resources.getMessage("LOCALE_EMPTY_COUNTRY"));
+            }
+            language = locale.substring(0, index);
+            country = locale.substring(index + 1);
+        } else {
+            language = locale;
+            country = "";
+        }
+
+        if (variant != null) {
+            return new Locale(language, country, variant);
+        } else {
+            return new Locale(language, country);
+        }
+    }
+}

Propchange: tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtil.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/SetLocaleSupport.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/SetLocaleSupport.java?rev=1441843&r1=1441842&r2=1441843&view=diff
==============================================================================
--- tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/SetLocaleSupport.java
(original)
+++ tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/common/fmt/SetLocaleSupport.java
Sun Feb  3 00:43:10 2013
@@ -33,7 +33,6 @@ import javax.servlet.jsp.jstl.fmt.Locali
 import javax.servlet.jsp.tagext.Tag;
 import javax.servlet.jsp.tagext.TagSupport;
 
-import org.apache.taglibs.standard.resources.Resources;
 import org.apache.taglibs.standard.tag.common.core.Util;
 
 /**
@@ -49,9 +48,6 @@ public abstract class SetLocaleSupport e
     //*********************************************************************
     // Private constants
 
-    private static final char HYPHEN = '-';
-    private static final char UNDERSCORE = '_';
-
 
     //*********************************************************************
     // Protected state
@@ -95,16 +91,12 @@ public abstract class SetLocaleSupport e
     public int doEndTag() throws JspException {
         Locale locale;
 
-        if (value == null) {
-            locale = Locale.getDefault();
-        } else if (value instanceof String) {
-            if (((String) value).trim().equals("")) {
-                locale = Locale.getDefault();
-            } else {
-                locale = parseLocale((String) value, variant);
-            }
-        } else {
+        if (value instanceof Locale) {
             locale = (Locale) value;
+        } else if (value instanceof String && !"".equals(((String)value).trim()))
{
+            locale = LocaleUtil.parseLocale((String) value, variant);
+        } else {
+            locale = Locale.getDefault();
         }
 
         Config.set(pageContext, Config.FMT_LOCALE, locale, scope);
@@ -124,66 +116,6 @@ public abstract class SetLocaleSupport e
     //*********************************************************************
     // Public utility methods
 
-    /**
-     * See parseLocale(String, String) for details.
-     */
-    public static Locale parseLocale(String locale) {
-        return parseLocale(locale, null);
-    }
-
-    /**
-     * Parses the given locale string into its language and (optionally)
-     * country components, and returns the corresponding
-     * <tt>java.util.Locale</tt> object.
-     * <p>If the given locale string is null or empty, the runtime's default
-     * locale is returned.
-     *
-     * @param locale  the locale string to parse
-     * @param variant the variant
-     * @return <tt>java.util.Locale</tt> object corresponding to the given
-     *         locale string, or the runtime's default locale if the locale string is
-     *         null or empty
-     * @throws IllegalArgumentException if the given locale does not have a
-     *                                  language component or has an empty country component
-     */
-    public static Locale parseLocale(String locale, String variant) {
-
-        Locale ret;
-        String language = locale;
-        String country = null;
-        int index;
-
-        if (((index = locale.indexOf(HYPHEN)) > -1)
-                || ((index = locale.indexOf(UNDERSCORE)) > -1)) {
-            language = locale.substring(0, index);
-            country = locale.substring(index + 1);
-        }
-
-        if ((language == null) || (language.length() == 0)) {
-            throw new IllegalArgumentException(
-                    Resources.getMessage("LOCALE_NO_LANGUAGE"));
-        }
-
-        if (country == null) {
-            if (variant != null) {
-                ret = new Locale(language, "", variant);
-            } else {
-                ret = new Locale(language, "");
-            }
-        } else if (country.length() > 0) {
-            if (variant != null) {
-                ret = new Locale(language, country, variant);
-            } else {
-                ret = new Locale(language, country);
-            }
-        } else {
-            throw new IllegalArgumentException(
-                    Resources.getMessage("LOCALE_EMPTY_COUNTRY"));
-        }
-
-        return ret;
-    }
-
 
     //*********************************************************************
     // Package-scoped utility methods
@@ -381,7 +313,7 @@ public abstract class SetLocaleSupport e
             if (obj instanceof Locale) {
                 loc = (Locale) obj;
             } else {
-                loc = parseLocale((String) obj);
+                loc = LocaleUtil.parseLocale((String) obj);
             }
         }
 

Modified: tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ParseDateTag.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ParseDateTag.java?rev=1441843&r1=1441842&r2=1441843&view=diff
==============================================================================
--- tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ParseDateTag.java
(original)
+++ tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ParseDateTag.java
Sun Feb  3 00:43:10 2013
@@ -21,8 +21,8 @@ import java.util.Locale;
 
 import javax.servlet.jsp.JspTagException;
 
+import org.apache.taglibs.standard.tag.common.fmt.LocaleUtil;
 import org.apache.taglibs.standard.tag.common.fmt.ParseDateSupport;
-import org.apache.taglibs.standard.tag.common.fmt.SetLocaleSupport;
 
 /**
  * <p>A handler for &lt;parseDate&gt; that supports rtexprvalue-based
@@ -76,15 +76,9 @@ public class ParseDateTag extends ParseD
     // 'parseLocale' attribute
 
     public void setParseLocale(Object loc) throws JspTagException {
+        Locale locale = LocaleUtil.parseLocale(loc);
         if (loc != null) {
-            if (loc instanceof Locale) {
-                this.parseLocale = (Locale) loc;
-            } else {
-                if (!"".equals((String) loc)) {
-                    this.parseLocale = SetLocaleSupport.parseLocale((String)
-                            loc);
-                }
-            }
+            this.parseLocale = locale;
         }
     }
 }

Modified: tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ParseNumberTag.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ParseNumberTag.java?rev=1441843&r1=1441842&r2=1441843&view=diff
==============================================================================
--- tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ParseNumberTag.java
(original)
+++ tomcat/taglibs/standard/trunk/impl/src/main/java/org/apache/taglibs/standard/tag/rt/fmt/ParseNumberTag.java
Sun Feb  3 00:43:10 2013
@@ -21,8 +21,8 @@ import java.util.Locale;
 
 import javax.servlet.jsp.JspTagException;
 
+import org.apache.taglibs.standard.tag.common.fmt.LocaleUtil;
 import org.apache.taglibs.standard.tag.common.fmt.ParseNumberSupport;
-import org.apache.taglibs.standard.tag.common.fmt.SetLocaleSupport;
 
 /**
  * <p>A handler for &lt;parseNumber&gt; that supports rtexprvalue-based
@@ -58,15 +58,9 @@ public class ParseNumberTag extends Pars
     // 'parseLocale' attribute
 
     public void setParseLocale(Object loc) throws JspTagException {
+        Locale locale = LocaleUtil.parseLocale(loc);
         if (loc != null) {
-            if (loc instanceof Locale) {
-                this.parseLocale = (Locale) loc;
-            } else {
-                if (!"".equals((String) loc)) {
-                    this.parseLocale = SetLocaleSupport.parseLocale((String)
-                            loc);
-                }
-            }
+            this.parseLocale = locale;
         }
     }
 

Modified: tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtilTest.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtilTest.java?rev=1441843&r1=1441842&r2=1441843&view=diff
==============================================================================
--- tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtilTest.java
(original)
+++ tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtilTest.java
Sun Feb  3 00:43:10 2013
@@ -17,7 +17,6 @@
 package org.apache.taglibs.standard.tag.common.fmt;
 
 import junit.framework.Assert;
-import org.junit.Ignore;
 import org.junit.Test;
 
 import java.util.Locale;
@@ -27,49 +26,38 @@ public class LocaleUtilTest {
     private static final Locale DEFAULT = Locale.getDefault();
 
     @Test
-    @Ignore("original causes NPE")
-    public void testUsesDefaultIfNull() {
-        Assert.assertEquals(DEFAULT, SetLocaleSupport.parseLocale(null, null));
-    }
-
-    @Test
-    @Ignore("original results in IllegalArgumentException")
-    public void testUsesDefaultIfEmpty() {
-        Assert.assertEquals(DEFAULT, SetLocaleSupport.parseLocale("", null));
-    }
-
-    @Test
     public void testLocaleWithLanguage() {
-        Assert.assertEquals(new Locale("fr"), SetLocaleSupport.parseLocale("fr", null));
+        Assert.assertEquals(new Locale("fr"), LocaleUtil.parseLocale("fr", null));
     }
 
     @Test
     public void testLocaleUsingHyphen() {
-        Assert.assertEquals(new Locale("en", "DE"), SetLocaleSupport.parseLocale("en-DE",
null));
+        Assert.assertEquals(new Locale("en", "DE"), LocaleUtil.parseLocale("en-DE", null));
     }
 
     @Test
     public void testLocaleUsingUnderscore() {
-        Assert.assertEquals(new Locale("en", "IE"), SetLocaleSupport.parseLocale("en_IE",
null));
+        Assert.assertEquals(new Locale("en", "IE"), LocaleUtil.parseLocale("en_IE", null));
+        Assert.assertEquals(new Locale("en", "IE"), LocaleUtil.parseLocale("en_IE"));
     }
 
     @Test
     public void testLocaleWithLanguageVariant() {
-        Assert.assertEquals(new Locale("fr", "", "xxx"), SetLocaleSupport.parseLocale("fr",
"xxx"));
+        Assert.assertEquals(new Locale("fr", "", "xxx"), LocaleUtil.parseLocale("fr", "xxx"));
     }
 
     @Test
     public void testLocaleWithLanguageCountryVariant() {
-        Assert.assertEquals(new Locale("en", "GB", "cockney"), SetLocaleSupport.parseLocale("en-GB",
"cockney"));
+        Assert.assertEquals(new Locale("en", "GB", "cockney"), LocaleUtil.parseLocale("en-GB",
"cockney"));
     }
 
     @Test(expected = IllegalArgumentException.class)
     public void testEmptyLanguageIsInvalid() {
-        SetLocaleSupport.parseLocale("-GB", null);
+        LocaleUtil.parseLocale("-GB", null);
     }
 
     @Test(expected = IllegalArgumentException.class)
     public void testEmptyCountryIsInvalid() {
-        SetLocaleSupport.parseLocale("en-", null);
+        LocaleUtil.parseLocale("en-", null);
     }
 }

Propchange: tomcat/taglibs/standard/trunk/impl/src/test/java/org/apache/taglibs/standard/tag/common/fmt/LocaleUtilTest.java
------------------------------------------------------------------------------
    svn:keywords = Author Date Id Revision

Modified: tomcat/taglibs/standard/trunk/jstlel/src/main/java/org/apache/taglibs/standard/tag/el/fmt/ParseDateTag.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/jstlel/src/main/java/org/apache/taglibs/standard/tag/el/fmt/ParseDateTag.java?rev=1441843&r1=1441842&r2=1441843&view=diff
==============================================================================
--- tomcat/taglibs/standard/trunk/jstlel/src/main/java/org/apache/taglibs/standard/tag/el/fmt/ParseDateTag.java
(original)
+++ tomcat/taglibs/standard/trunk/jstlel/src/main/java/org/apache/taglibs/standard/tag/el/fmt/ParseDateTag.java
Sun Feb  3 00:43:10 2013
@@ -22,8 +22,8 @@ import java.util.Locale;
 import javax.servlet.jsp.JspException;
 
 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
+import org.apache.taglibs.standard.tag.common.fmt.LocaleUtil;
 import org.apache.taglibs.standard.tag.common.fmt.ParseDateSupport;
-import org.apache.taglibs.standard.tag.common.fmt.SetLocaleSupport;
 
 /**
  * <p>A handler for &lt;parseDate&gt; that accepts attributes as Strings
@@ -191,15 +191,9 @@ public class ParseDateTag extends ParseD
         if (parseLocale_ != null) {
             Object obj = ExpressionEvaluatorManager.evaluate(
                     "parseLocale", parseLocale_, Object.class, this, pageContext);
-            if (obj != null) {
-                if (obj instanceof Locale) {
-                    parseLocale = (Locale) obj;
-                } else {
-                    String localeStr = (String) obj;
-                    if (!"".equals(localeStr)) {
-                        parseLocale = SetLocaleSupport.parseLocale(localeStr);
-                    }
-                }
+            Locale locale = LocaleUtil.parseLocale(obj);
+            if (locale != null) {
+                this.parseLocale = locale;
             }
         }
     }

Modified: tomcat/taglibs/standard/trunk/jstlel/src/main/java/org/apache/taglibs/standard/tag/el/fmt/ParseNumberTag.java
URL: http://svn.apache.org/viewvc/tomcat/taglibs/standard/trunk/jstlel/src/main/java/org/apache/taglibs/standard/tag/el/fmt/ParseNumberTag.java?rev=1441843&r1=1441842&r2=1441843&view=diff
==============================================================================
--- tomcat/taglibs/standard/trunk/jstlel/src/main/java/org/apache/taglibs/standard/tag/el/fmt/ParseNumberTag.java
(original)
+++ tomcat/taglibs/standard/trunk/jstlel/src/main/java/org/apache/taglibs/standard/tag/el/fmt/ParseNumberTag.java
Sun Feb  3 00:43:10 2013
@@ -22,8 +22,8 @@ import java.util.Locale;
 import javax.servlet.jsp.JspException;
 
 import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
+import org.apache.taglibs.standard.tag.common.fmt.LocaleUtil;
 import org.apache.taglibs.standard.tag.common.fmt.ParseNumberSupport;
-import org.apache.taglibs.standard.tag.common.fmt.SetLocaleSupport;
 
 /**
  * <p>A handler for &lt;parseNumber&gt; that accepts attributes as Strings
@@ -161,15 +161,9 @@ public class ParseNumberTag extends Pars
         if (parseLocale_ != null) {
             obj = ExpressionEvaluatorManager.evaluate(
                     "parseLocale", parseLocale_, Object.class, this, pageContext);
-            if (obj != null) {
-                if (obj instanceof Locale) {
-                    parseLocale = (Locale) obj;
-                } else {
-                    String localeStr = (String) obj;
-                    if (!"".equals(localeStr)) {
-                        parseLocale = SetLocaleSupport.parseLocale(localeStr);
-                    }
-                }
+            Locale locale = LocaleUtil.parseLocale(obj);
+            if (locale != null) {
+                this.parseLocale = locale;
             }
         }
 



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


Mime
View raw message