You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2012/06/13 00:10:52 UTC

svn commit: r1349562 - in /poi/trunk/src: documentation/content/xdocs/status.xml java/org/apache/poi/ss/usermodel/DataFormatter.java testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java

Author: nick
Date: Tue Jun 12 22:10:52 2012
New Revision: 1349562

URL: http://svn.apache.org/viewvc?rev=1349562&view=rev
Log:
Fix bug #53389 - Handle formatting General and @ formats even if a locale is prefixed to them

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1349562&r1=1349561&r2=1349562&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Tue Jun 12 22:10:52 2012
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.9-beta1" date="2012-??-??">
+          <action dev="poi-developers" type="fix">53389 - Handle formatting General and @ formats even if a locale is prefixed to them</action>
           <action dev="poi-developers" type="fix">53271 - Removed unconditional asserts in SXSSF</action>
           <action dev="poi-developers" type="add">53025 - Updatad documentation and example on using Data Validations  </action>
           <action dev="poi-developers" type="add">53227 - Corrected AddDimensionedImage.java to support XSSF/SXSSF  </action>

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java?rev=1349562&r1=1349561&r2=1349562&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/DataFormatter.java Tue Jun 12 22:10:52 2012
@@ -111,8 +111,11 @@ public class DataFormatter {
     /** Pattern to find "AM/PM" marker */
     private static final Pattern amPmPattern = Pattern.compile("((A|P)[M/P]*)", Pattern.CASE_INSENSITIVE);
 
-    /** A regex to find patterns like [$$-1009] and [$?-452]. */
-    private static final Pattern specialPatternGroup = Pattern.compile("(\\[\\$[^-\\]]*-[0-9A-Z]+\\])");
+    /** 
+     * A regex to find locale patterns like [$$-1009] and [$?-452].
+     * Note that we don't currently process these into locales 
+     */
+    private static final Pattern localePatternGroup = Pattern.compile("(\\[\\$[^-\\]]*-[0-9A-Z]+\\])");
 
     /**
      * A regex to match the colour formattings rules.
@@ -278,12 +281,16 @@ public class DataFormatter {
         if (format != null) {
             return format;
         }
+        
+        // Is it one of the special built in types, General or @?
         if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
             if (isWholeNumber(cellValue)) {
                 return generalWholeNumFormat;
             }
             return generalDecimalNumFormat;
         }
+        
+        // Build a formatter, and cache it
         format = createFormat(cellValue, formatIndex, formatStr);
         formats.put(formatStr, format);
         return format;
@@ -323,8 +330,8 @@ public class DataFormatter {
            colourM = colorPattern.matcher(formatStr);
         }
 
-        // try to extract special characters like currency
-        Matcher m = specialPatternGroup.matcher(formatStr);
+        // Strip off the locale information, we use an instance-wide locale for everything
+        Matcher m = localePatternGroup.matcher(formatStr);
         while(m.find()) {
             String match = m.group();
             String symbol = match.substring(match.indexOf('$') + 1, match.indexOf('-'));
@@ -336,12 +343,20 @@ public class DataFormatter {
                 symbol = sb.toString();
             }
             formatStr = m.replaceAll(symbol);
-            m = specialPatternGroup.matcher(formatStr);
+            m = localePatternGroup.matcher(formatStr);
         }
 
+        // Check for special cases
         if(formatStr == null || formatStr.trim().length() == 0) {
             return getDefaultFormat(cellValue);
         }
+        
+        if ("General".equalsIgnoreCase(formatStr) || "@".equals(formatStr)) {
+           if (isWholeNumber(cellValue)) {
+               return generalWholeNumFormat;
+           }
+           return generalDecimalNumFormat;
+        }
 
         if(DateUtil.isADateFormat(formatIndex,formatStr) &&
                 DateUtil.isValidExcelDate(cellValue)) {

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java?rev=1349562&r1=1349561&r2=1349562&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java Tue Jun 12 22:10:52 2012
@@ -47,6 +47,24 @@ public class TestDataFormatter extends T
     }
     
     /**
+     * At the moment, we don't decode the locale strings into
+     *  a specific locale, but we should format things as if
+     *  the locale (eg '[$-1010409]') isn't there
+     */
+    public void testLocaleBasedFormats() {
+       DataFormatter dfUS = new DataFormatter(Locale.US);
+
+       // Standard formats
+       assertEquals("63", dfUS.formatRawCellContents(63.0, -1, "[$-1010409]General"));
+       assertEquals("63", dfUS.formatRawCellContents(63.0, -1, "[$-1010409]@"));
+
+       // Regular numeric style formats
+       assertEquals("63", dfUS.formatRawCellContents(63.0, -1, "[$-1010409]##"));
+       assertEquals("63", dfUS.formatRawCellContents(63.0, -1, "[$-1010409]00"));        
+
+    }
+    
+    /**
      * Ensure that colours get correctly
      *  zapped from within the format strings
      */



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org