]> source.dussan.org Git - poi.git/commitdiff
Bug 54786: Fix missing quoting in date formatting, add a number of unit
authorDominik Stadler <centic@apache.org>
Fri, 16 Aug 2013 09:38:47 +0000 (09:38 +0000)
committerDominik Stadler <centic@apache.org>
Fri, 16 Aug 2013 09:38:47 +0000 (09:38 +0000)
tests which verify the new formatting options.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1514632 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/ss/usermodel/DataFormatter.java
src/testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java

index 46740cc3816557c9a8e4878db295970a1c4b36ff..4e3718e956a3d6a961d852a0dd0588ead5f2570b 100644 (file)
@@ -423,6 +423,8 @@ public class DataFormatter {
         formatStr = formatStr.replaceAll("\\\\/","/"); // weird: m\\/d\\/yyyy 
         formatStr = formatStr.replaceAll(";@", "");
         formatStr = formatStr.replaceAll("\"/\"", "/"); // "/" is escaped for no reason in: mm"/"dd"/"yyyy
+        formatStr = formatStr.replace("\"\"", "'");    // replace Excel quoting with Java style quoting
+
 
         boolean hasAmPm = false;
         Matcher amPmMatcher = amPmPattern.matcher(formatStr);
@@ -456,7 +458,21 @@ public class DataFormatter {
         boolean isElapsed = false;
         for(int j=0; j<chars.length; j++) {
             char c = chars[j];
-            if (c == '[' && !isElapsed) {
+            if (c == '\'') {
+                sb.append(c);
+                j++;
+
+                // skip until the next quote
+                while(j<chars.length) {
+                    c = chars[j];
+                    sb.append(c);
+                    if(c == '\'') {
+                        break;
+                    }
+                    j++;
+                }
+            }
+            else if (c == '[' && !isElapsed) {
                 isElapsed = true;
                 mIsMonth = false;
                 sb.append(c);
@@ -932,10 +948,12 @@ public class DataFormatter {
             return sb.toString();
         }
 
+        @Override
         public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
             return toAppendTo.append(format((Number)obj));
         }
 
+        @Override
         public Object parseObject(String source, ParsePosition pos) {
             return df.parseObject(source, pos);
         }
@@ -963,10 +981,12 @@ public class DataFormatter {
             return sb.toString();
         }
 
+        @Override
         public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
             return toAppendTo.append(format((Number)obj));
         }
 
+        @Override
         public Object parseObject(String source, ParsePosition pos) {
             return df.parseObject(source, pos);
         }
@@ -1009,10 +1029,12 @@ public class DataFormatter {
             return sb.toString();
         }
 
+        @Override
         public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
             return toAppendTo.append(format((Number)obj));
         }
 
+        @Override
         public Object parseObject(String source, ParsePosition pos) {
             return df.parseObject(source, pos);
         }
index 2816a488f46bfb509faa129eda1db9fa6422891a..6ce19fb5e942707facc150028785d3347949985d 100644 (file)
@@ -37,7 +37,9 @@ import org.apache.poi.hssf.usermodel.TestHSSFDataFormatter;
  *  more tests.
  */
 public class TestDataFormatter extends TestCase {
-    /**
+    private static final double _15_MINUTES = 0.041666667;
+
+       /**
      * Test that we use the specified locale when deciding
      *   how to format normal numbers
      */
@@ -538,4 +540,28 @@ public class TestDataFormatter extends TestCase {
        DataFormatter dfUS = new DataFormatter(Locale.US, true);
        assertEquals("01.010", dfUS.formatRawCellContents(0.0000116898, -1, "ss.000"));
     }
+
+       public void testBug54786() {
+               DataFormatter formatter = new DataFormatter();
+               String format = "[h]\"\"h\"\" m\"\"m\"\"";
+               assertTrue(DateUtil.isADateFormat(-1,format));
+               assertTrue(DateUtil.isValidExcelDate(_15_MINUTES));
+               
+               assertEquals("1h 0m", formatter.formatRawCellContents(_15_MINUTES, -1, format, false));
+               assertEquals("0.041666667", formatter.formatRawCellContents(_15_MINUTES, -1, "[h]'h'", false));
+               assertEquals("1h 0m\"", formatter.formatRawCellContents(_15_MINUTES, -1, "[h]\"\"h\"\" m\"\"m\"\"\"", false));
+               assertEquals("1h", formatter.formatRawCellContents(_15_MINUTES, -1, "[h]\"\"h\"\"", false));
+               assertEquals("h1", formatter.formatRawCellContents(_15_MINUTES, -1, "\"\"h\"\"[h]", false));
+               assertEquals("h1", formatter.formatRawCellContents(_15_MINUTES, -1, "\"\"h\"\"h", false));
+               assertEquals(" 60", formatter.formatRawCellContents(_15_MINUTES, -1, " [m]", false));
+               assertEquals("h60", formatter.formatRawCellContents(_15_MINUTES, -1, "\"\"h\"\"[m]", false));
+               assertEquals("m1", formatter.formatRawCellContents(_15_MINUTES, -1, "\"\"m\"\"h", false));
+               
+               try {
+                       assertEquals("1h 0m\"", formatter.formatRawCellContents(_15_MINUTES, -1, "[h]\"\"h\"\" m\"\"m\"\"\"\"", false));
+                       fail("Catches exception because of invalid format, i.e. trailing quoting");
+               } catch (IllegalArgumentException e) {
+                       assertTrue(e.getMessage().contains("Cannot format given Object as a Number"));
+               }
+       }
 }