[jboss-svn-commits] JBoss Common SVN: r2749 - in common-core/trunk/src: main/java/org/jboss/util/propertyeditor and 1 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Mar 27 08:33:37 EDT 2008


Author: alesj
Date: 2008-03-27 08:33:37 -0400 (Thu, 27 Mar 2008)
New Revision: 2749

Added:
   common-core/trunk/src/main/java/org/jboss/util/CharacterChecker.java
   common-core/trunk/src/main/java/org/jboss/util/propertyeditor/LocaleEditor.java
Modified:
   common-core/trunk/src/main/java/org/jboss/util/Strings.java
   common-core/trunk/src/main/java/org/jboss/util/propertyeditor/DateEditor.java
   common-core/trunk/src/test/java/org/jboss/test/util/test/propertyeditor/PropertyEditorsUnitTestCase.java
Log:
Adding LocaleEditor and a way to set default locale to default date format.
Uncommenting doTests in PropertyEditor test, which were commented - meaning no tests actually run for JBoss editors.

Added: common-core/trunk/src/main/java/org/jboss/util/CharacterChecker.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/CharacterChecker.java	                        (rev 0)
+++ common-core/trunk/src/main/java/org/jboss/util/CharacterChecker.java	2008-03-27 12:33:37 UTC (rev 2749)
@@ -0,0 +1,48 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.util;
+
+/**
+ * Character checker interface.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public interface CharacterChecker
+{
+   public CharacterChecker WHITESPACE = new WhitespaceChecker();
+
+   /**
+    * Is character legal.
+    *
+    * @param character the char parameter
+    * @return true if legal, false otherwise
+    */
+   boolean isCharacterLegal(char character);
+
+   class WhitespaceChecker implements CharacterChecker
+   {
+      public boolean isCharacterLegal(char character)
+      {
+         return Character.isWhitespace(character);
+      }
+   }
+}

Modified: common-core/trunk/src/main/java/org/jboss/util/Strings.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/Strings.java	2008-03-27 10:43:57 UTC (rev 2748)
+++ common-core/trunk/src/main/java/org/jboss/util/Strings.java	2008-03-27 12:33:37 UTC (rev 2749)
@@ -28,6 +28,10 @@
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.Map;
+import java.util.StringTokenizer;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Locale;
 
 /**
  * A collection of String utilities.
@@ -1138,4 +1142,135 @@
       }
       return retval;
    }
+
+   /**
+    * Tokenize the given String into a String array via a StringTokenizer.
+    *
+    * The given delimiters string is supposed to consist of any number of
+    * delimiter characters. Each of those characters can be used to separate
+    * tokens. A delimiter is always a single character; for multi-character
+    * delimiters, consider using delimitedListToStringArray
+    *
+    * @param str               the String to tokenize
+    * @param delimiters        the delimiter characters, assembled as String
+    *                          (each of those characters is individually considered as delimiter)
+    * @param trimTokens        trim the tokens via String's trim
+    * @param ignoreEmptyTokens omit empty tokens from the result array
+    *                          (only applies to tokens that are empty after trimming; StringTokenizer
+    *                          will not consider subsequent delimiters as token in the first place).
+    * @return an array of the tokens (null if the input String was null)
+    */
+   public static String[] tokenizeToStringArray(String str, String delimiters, boolean trimTokens, boolean ignoreEmptyTokens)
+   {
+      if (str == null)
+      {
+         return null;
+      }
+      StringTokenizer st = new StringTokenizer(str, delimiters);
+      List tokens = new ArrayList();
+      while (st.hasMoreTokens())
+      {
+         String token = st.nextToken();
+         if (trimTokens)
+         {
+            token = token.trim();
+         }
+         if (!ignoreEmptyTokens || token.length() > 0)
+         {
+            tokens.add(token);
+         }
+      }
+      return (String[])tokens.toArray(new String[tokens.size()]);
+   }
+
+   /**
+    * Trim leading whitespace from the given String.
+    *
+    * @param str the string to check
+    * @return the trimmed String
+    * @see java.lang.Character#isWhitespace
+    */
+   public static String trimLeadingWhitespace(String str)
+   {
+      return trimLeadingCharacter(str, CharacterChecker.WHITESPACE);
+   }
+
+   /**
+    * Trim all occurences of the supplied leading character from the given String.
+    *
+    * @param str the string to check
+    * @param leadingCharacter the leading character to be trimmed
+    * @return the trimmed String
+    */
+   public static String trimLeadingCharacter(String str, final char leadingCharacter)
+   {
+      return trimLeadingCharacter(str, new CharacterChecker()
+      {
+         public boolean isCharacterLegal(char character)
+         {
+            return character == leadingCharacter;
+         }
+      });
+   }
+
+   /**
+    * Trim all occurences of the supplied leading character from the given String.
+    *
+    * @param str the string to check
+    * @param checker the character checker
+    * @return the trimmed String
+    */
+   public static String trimLeadingCharacter(String str, CharacterChecker checker)
+   {
+      if (hasLength(str) == false)
+      {
+         return str;
+      }
+      StringBuffer buf = new StringBuffer(str);
+      while (buf.length() > 0 && checker.isCharacterLegal(buf.charAt(0)))
+      {
+         buf.deleteCharAt(0);
+      }
+      return buf.toString();
+   }
+
+   /**
+    * Check that the given string param is neither null nor of length 0.
+    *
+    * @param string the string
+    * @return true if the String is not null and has length
+    */
+   public static boolean hasLength(String string)
+   {
+      return (string != null && string.length() > 0);
+   }
+
+   /**
+    * Parse the given localeString into a {@link java.util.Locale}.
+    *
+    * This is the inverse operation of {@link java.util.Locale#toString Locale's toString}.
+    *
+    * @param localeString the locale string
+    * @return a corresponding Locale instance
+    */
+   public static Locale parseLocaleString(String localeString)
+   {
+      String[] parts = tokenizeToStringArray(localeString, "_ ", false, false);
+      String language = (parts.length > 0 ? parts[0] : "");
+      String country = (parts.length > 1 ? parts[1] : "");
+      String variant = "";
+      if (parts.length >= 2)
+      {
+         // There is definitely a variant, and it is everything after the country
+         // code sans the separator between the country code and the variant.
+         int endIndexOfCountryCode = localeString.indexOf(country) + country.length();
+         // Strip off any leading '_' and whitespace, what's left is the variant.
+         variant = trimLeadingWhitespace(localeString.substring(endIndexOfCountryCode));
+         if (variant.startsWith("_"))
+         {
+            variant = trimLeadingCharacter(variant, '_');
+         }
+      }
+      return (language.length() > 0 ? new Locale(language, country, variant) : null);
+   }
 }

Modified: common-core/trunk/src/main/java/org/jboss/util/propertyeditor/DateEditor.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/propertyeditor/DateEditor.java	2008-03-27 10:43:57 UTC (rev 2748)
+++ common-core/trunk/src/main/java/org/jboss/util/propertyeditor/DateEditor.java	2008-03-27 12:33:37 UTC (rev 2749)
@@ -30,6 +30,7 @@
 import java.util.Date;
 
 import org.jboss.util.NestedRuntimeException;
+import org.jboss.util.Strings;
 
 /**
  * A property editor for {@link java.util.Date}.
@@ -38,6 +39,7 @@
  * @author <a href="mailto:d_jencks at users.sourceforge.net">David Jencks</a>
  * @author <a href="mailto:scott.stark at jboss.org">Scott Stark</a>
  * @author <a href="mailto:adrian.brock at jboss.org">Adrian Brock</a>
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
  * @author <a href="mailto:dimitris at jboss.org">Dimitris Andreadis</a>
  * @version <tt>$Revision$</tt>
  */
@@ -61,11 +63,21 @@
       {
          public Object run()
          {
-            String defaultFormat = System.getProperty("org.jboss.util.propertyeditor.DateEditor.format",
-                  "MMM d, yyyy");
+            String defaultFormat = System.getProperty("org.jboss.util.propertyeditor.DateEditor.format", "MMM d, yyyy");
+            String defaultLocale = System.getProperty("org.jboss.util.propertyeditor.DateEditor.locale");
+            DateFormat defaultDateFormat;
+            if (defaultLocale == null)
+            {
+               defaultDateFormat = new SimpleDateFormat(defaultFormat);
+            }
+            else
+            {
+               defaultDateFormat = new SimpleDateFormat(defaultFormat, Strings.parseLocaleString(defaultLocale));
+            }
+
             formats = new DateFormat[]
             {
-               new SimpleDateFormat(defaultFormat),
+               defaultDateFormat,
                // Tue Jan 04 00:00:00 PST 2005
                new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy"),
                // Wed, 4 Jul 2001 12:08:56 -0700
@@ -74,7 +86,7 @@
             return null;
          }
       };
-      AccessController.doPrivileged(action);      
+      AccessController.doPrivileged(action);
    }
 
    /** Keep the text version of the date */

Added: common-core/trunk/src/main/java/org/jboss/util/propertyeditor/LocaleEditor.java
===================================================================
--- common-core/trunk/src/main/java/org/jboss/util/propertyeditor/LocaleEditor.java	                        (rev 0)
+++ common-core/trunk/src/main/java/org/jboss/util/propertyeditor/LocaleEditor.java	2008-03-27 12:33:37 UTC (rev 2749)
@@ -0,0 +1,45 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.util.propertyeditor;
+
+import java.beans.PropertyEditorSupport;
+
+import org.jboss.util.Strings;
+
+/**
+ * Locale editor.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class LocaleEditor extends PropertyEditorSupport
+{
+   public void setAsText(String text)
+   {
+      setValue(Strings.parseLocaleString(text));
+   }
+
+   public String getAsText()
+   {
+      Object value = getValue();
+      return (value != null ? value.toString() : "");
+   }
+}

Modified: common-core/trunk/src/test/java/org/jboss/test/util/test/propertyeditor/PropertyEditorsUnitTestCase.java
===================================================================
--- common-core/trunk/src/test/java/org/jboss/test/util/test/propertyeditor/PropertyEditorsUnitTestCase.java	2008-03-27 10:43:57 UTC (rev 2748)
+++ common-core/trunk/src/test/java/org/jboss/test/util/test/propertyeditor/PropertyEditorsUnitTestCase.java	2008-03-27 12:33:37 UTC (rev 2749)
@@ -191,6 +191,7 @@
          int[].class,
          Date.class,
          java.util.Properties.class,
+         Locale.class,
       };
       // The input string data for each type
       String[][] inputData = {
@@ -216,6 +217,7 @@
          {"Jan 4, 2005", "Tue Jan  4 23:38:21 PST 2005", "Tue, 04 Jan 2005 23:38:48 -0800"},
          // java.util.Properties.class
          {"prop1=value1\nprop2=value2\nprop3=value3\nprop32=${prop3}\nprop4=${user.home}\nprop5=${some.win32.path}"},
+         {Locale.getDefault().toString(), "ja_JP"},
       };
       // The expected instance for each inputData value
       calendar.set(2005, 0, 4, 0, 0, 0);
@@ -247,6 +249,7 @@
          {new int[]{0, 0x123, -123}},
          {date1, date2, date3},
          {props},
+         {Locale.getDefault(), Locale.JAPAN},
       };
       // The expected string output from getAsText()
       String[][] expectedStringData = {
@@ -272,6 +275,7 @@
          {"Jan 4, 2005", "Tue Jan  4 23:38:21 PST 2005", "Tue, 04 Jan 2005 23:38:48 -0800"},            
          // java.util.Properties.class
          {props.toString()},
+         {Locale.getDefault().toString(), Locale.JAPAN.toString()},
       };
       // The Comparator for non-trival types
       Comparator[] comparators = {
@@ -286,9 +290,10 @@
          new IntArrayComparator(), // int[]
          null, // Date
          null, // Properties
+         null, // Locale
       };
 
-      //doTests(types, inputData, expectedData, expectedStringData, comparators);
+      doTests(types, inputData, expectedData, expectedStringData, comparators);
    }
    
    public void testDateEditor() throws Exception




More information about the jboss-svn-commits mailing list