poi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cen...@apache.org
Subject svn commit: r1514632 - in /poi/trunk/src: java/org/apache/poi/ss/usermodel/DataFormatter.java testcases/org/apache/poi/ss/usermodel/TestDataFormatter.java
Date Fri, 16 Aug 2013 09:38:48 GMT
Author: centic
Date: Fri Aug 16 09:38:47 2013
New Revision: 1514632

URL: http://svn.apache.org/r1514632
Log:
Bug 54786: Fix missing quoting in date formatting, add a number of unit
tests which verify the new formatting options.

Modified:
    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/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=1514632&r1=1514631&r2=1514632&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 Fri Aug 16 09:38:47
2013
@@ -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);
         }

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=1514632&r1=1514631&r2=1514632&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 Fri Aug 16
09:38:47 2013
@@ -37,7 +37,9 @@ import org.apache.poi.hssf.usermodel.Tes
  *  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 T
        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"));
+		}
+	}
 }



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


Mime
View raw message