You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xbean-scm@geronimo.apache.org by jl...@apache.org on 2007/08/22 10:08:12 UTC

svn commit: r568491 - in /geronimo/xbean/trunk/xbean-reflect: pom.xml src/main/java/org/apache/xbean/propertyeditor/DateEditor.java src/test/java/org/apache/xbean/propertyeditor/ src/test/java/org/apache/xbean/propertyeditor/DateEditorTest.java

Author: jlaskowski
Date: Wed Aug 22 01:08:10 2007
New Revision: 568491

URL: http://svn.apache.org/viewvc?rev=568491&view=rev
Log:
XBEAN-91 o.a.x.p.DateEditor.toObjectImpl throws exception when date format is in different locale than the default one

Added:
    geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/propertyeditor/
    geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/propertyeditor/DateEditorTest.java   (with props)
Modified:
    geronimo/xbean/trunk/xbean-reflect/pom.xml
    geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/DateEditor.java

Modified: geronimo/xbean/trunk/xbean-reflect/pom.xml
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/pom.xml?rev=568491&r1=568490&r2=568491&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/pom.xml (original)
+++ geronimo/xbean/trunk/xbean-reflect/pom.xml Wed Aug 22 01:08:10 2007
@@ -1,35 +1,62 @@
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
-
-<!--
-
-    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.
-
--->
-
-<!-- $Rev: 432962 $ $Date: 2006-08-20 03:47:39 -0700 (Sun, 20 Aug 2006) $ -->
-
-    <modelVersion>4.0.0</modelVersion>
-
-    <parent>
-        <artifactId>xbean</artifactId>
-        <groupId>org.apache.xbean</groupId>
-        <version>3.2-SNAPSHOT</version>
-    </parent>
-
-    <artifactId>xbean-reflect</artifactId>
-    <name>XBean :: Reflect</name>
-
-</project>
\ No newline at end of file
+<!--
+  
+  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.
+  
+-->
+
+<!-- $Rev: 432962 $ $Date: 2006-08-20 03:47:39 -0700 (Sun, 20 Aug 2006) $ -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+
+  <modelVersion>4.0.0</modelVersion>
+
+  <parent>
+    <artifactId>xbean</artifactId>
+    <groupId>org.apache.xbean</groupId>
+    <version>3.2-SNAPSHOT</version>
+  </parent>
+
+  <artifactId>xbean-reflect</artifactId>
+  <name>XBean :: Reflect</name>
+
+  <profiles>
+    <profile>
+      <activation>
+        <property>
+          <name>DEBUG</name>
+        </property>
+      </activation>
+      <build>
+        <plugins>
+          <plugin>
+            <groupId>org.apache.maven.plugins</groupId>
+            <artifactId>maven-surefire-plugin</artifactId>
+            <version>2.2</version>
+            <configuration>
+              <forkMode>pertest</forkMode>
+              <argLine>
+                -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005
+                -enableassertions
+              </argLine>
+              <workingDirectory>${basedir}/target</workingDirectory>
+            </configuration>
+          </plugin>
+        </plugins>
+      </build>
+    </profile>
+  </profiles>
+</project>

Modified: geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/DateEditor.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/DateEditor.java?rev=568491&r1=568490&r2=568491&view=diff
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/DateEditor.java (original)
+++ geronimo/xbean/trunk/xbean-reflect/src/main/java/org/apache/xbean/propertyeditor/DateEditor.java Wed Aug 22 01:08:10 2007
@@ -19,36 +19,72 @@
 import java.text.DateFormat;
 import java.text.ParseException;
 import java.util.Date;
+import java.util.Locale;
 
 /**
  * A property editor for Date typed properties.
- *
- * @version $Rev: 6680 $
+ * 
+ * @version $Rev$ $Date$
  */
 public class DateEditor extends AbstractConverter {
-    private final DateFormat formatter;
+    private DateFormat formatter;
 
     public DateEditor() {
         super(Date.class);
-
-        // Get a date formatter to parse this.
-        // This retrieves the formatter using the current execution locale,
-        // which could present an intererting problem when applied to deployment
-        // plans written in other locales.  Sort of a Catch-22 situation.
-        formatter = DateFormat.getDateInstance();
     }
 
     /**
      * Convert the text value of the property into a Date object instance.
-     *
+     * 
      * @return a Date object constructed from the property text value.
-     * @throws PropertyEditorException Unable to parse the string value into a Date.
+     * @throws PropertyEditorException
+     *                 Unable to parse the string value into a Date.
      */
     protected Object toObjectImpl(String text) {
         try {
+            // find out whether the first token is a locale id and style in that
+            // order
+            // if there's locale, style is mandatory
+            Locale locale = Locale.getDefault();
+            int style = DateFormat.MEDIUM;
+            int firstSpaceIndex = text.indexOf(' ');
+            if (firstSpaceIndex != -1) {
+                String token = text.substring(0, firstSpaceIndex).intern();
+                if (token.startsWith("locale")) {
+                    String localeStr = token.substring(token.indexOf('=') + 1);
+                    int underscoreIndex = localeStr.indexOf('_');
+                    if (underscoreIndex != -1) {
+                        String language = localeStr.substring(0, underscoreIndex);
+                        String country = localeStr.substring(underscoreIndex + 1);
+                        locale = new Locale(language, country);
+                    } else {
+                        locale = new Locale(localeStr);
+                    }
+                    // locale is followed by mandatory style
+                    int nextSpaceIndex = text.indexOf(' ', firstSpaceIndex + 1);
+                    token = text.substring(firstSpaceIndex + 1, nextSpaceIndex);
+                    String styleStr = token.substring(token.indexOf('=') + 1);
+                    if (styleStr.equalsIgnoreCase("SHORT")) {
+                        style = DateFormat.SHORT;
+                    } else if (styleStr.equalsIgnoreCase("MEDIUM")) {
+                        style = DateFormat.MEDIUM;
+                    } else if (styleStr.equalsIgnoreCase("LONG")) {
+                        style = DateFormat.LONG;
+                    } else if (styleStr.equalsIgnoreCase("FULL")) {
+                        style = DateFormat.FULL;
+                    } else {
+                        // unknown style name
+                        // throw exception or assume default?
+                        style = DateFormat.MEDIUM;
+                    }
+                    text = text.substring(nextSpaceIndex + 1);
+                }
+            }
+            formatter = DateFormat.getDateInstance(style, locale);
             return formatter.parse(text);
         } catch (ParseException e) {
-            // any format errors show up as a ParseException, which we turn into a PropertyEditorException.
+            // any format errors show up as a ParseException, which we turn into
+            // a PropertyEditorException.
             throw new PropertyEditorException(e);
         }
     }

Added: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/propertyeditor/DateEditorTest.java
URL: http://svn.apache.org/viewvc/geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/propertyeditor/DateEditorTest.java?rev=568491&view=auto
==============================================================================
--- geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/propertyeditor/DateEditorTest.java (added)
+++ geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/propertyeditor/DateEditorTest.java Wed Aug 22 01:08:10 2007
@@ -0,0 +1,35 @@
+/**
+ * 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.xbean.propertyeditor;
+
+import java.text.DateFormat;
+import java.util.Date;
+import java.util.Locale;
+
+import junit.framework.TestCase;
+
+/*
+ * @version $Rev$ $Date$
+ */
+public class DateEditorTest extends TestCase {
+    public void testToObjectImpl() throws Exception {
+        Date expected = DateFormat.getDateInstance(DateFormat.MEDIUM, Locale.US).parse("Mar 1, 1954");
+        Date actual = (Date) new DateEditor().toObjectImpl("locale=en_US format=MEDIUM Mar 1, 1954");
+        assertEquals(expected, actual);
+    }
+
+}

Propchange: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/propertyeditor/DateEditorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/propertyeditor/DateEditorTest.java
------------------------------------------------------------------------------
    svn:executable = *

Propchange: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/propertyeditor/DateEditorTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Revision

Propchange: geronimo/xbean/trunk/xbean-reflect/src/test/java/org/apache/xbean/propertyeditor/DateEditorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain