Regression in version 3.14-beta1: three or four-part formats with locale id cause exceptions when formatting instead of falling back to other formatting

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1724488 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2016-01-13 19:46:17 +00:00
parent 50d09bf36c
commit bdef6e1c52
5 changed files with 54 additions and 5 deletions

View File

@ -140,6 +140,9 @@ public class CellFormatPart {
String format = "(?:" + color + ")? # Text color\n" +
"(?:\\[" + condition + "\\])? # Condition\n" +
// see https://msdn.microsoft.com/en-ca/goglobal/bb964664.aspx and https://bz.apache.org/ooo/show_bug.cgi?id=70003
// we ignore these for now though
"(?:\\[\\$-[0-9a-fA-F]+\\])? # Optional locale id, ignored currently\n" +
"((?:" + part + ")+) # Format spec\n";
int flags = Pattern.COMMENTS | Pattern.CASE_INSENSITIVE;
@ -360,7 +363,7 @@ public class CellFormatPart {
}
// Something else inside [] which isn't supported!
throw new IllegalArgumentException("Unsupported [] format block '" +
repl + "' in '" + fdesc + "'");
repl + "' in '" + fdesc + "' with c2: " + c2);
case '#':
case '?':
return CellFormatType.NUMBER;

View File

@ -314,13 +314,15 @@ public class DataFormatter implements Observer {
CellFormat cfmt = CellFormat.getInstance(formatStr);
// CellFormat requires callers to identify date vs not, so do so
Object cellValueO = Double.valueOf(cellValue);
if (DateUtil.isADateFormat(formatIndex, formatStr)) {
if (DateUtil.isADateFormat(formatIndex, formatStr) &&
// don't try to handle Date value 0, let a 3 or 4-part format take care of it
((Double)cellValueO).doubleValue() != 0.0) {
cellValueO = DateUtil.getJavaDate(cellValue);
}
// Wrap and return (non-cachable - CellFormat does that)
return new CellFormatResultWrapper( cfmt.apply(cellValueO) );
} catch (Exception e) {
logger.log(POILogger.WARN, "Formatting failed as " + formatStr + ", falling back", e);
logger.log(POILogger.WARN, "Formatting failed for format " + formatStr + ", falling back", e);
}
}

View File

@ -96,7 +96,11 @@ public final class TestHSSFDataFormatter {
"[$-409]mmmmm;@",
"[$-409]mmmmm\\-yy;@",
"mmmm/d/yyyy;@",
"[$-409]d\\-mmm\\-yyyy;@"
"[$-409]d\\-mmm\\-yyyy;@",
"[$-409]d\\-mmm;[$-3]d\\-mmm;@", // international three-part
"[$-41f]d\\-mmm;[$-41f]d\\-mmm;@", // turkish international three-part
"[$-F40f]d\\-mmm;[$-F40f]d\\-mmm;@", // custom international three-part
"[$-F40f]d\\-mmm;[$-F40f]d\\-mmm;0;@" // custom international four-part
};
//valid time formats - all should have 11:23 in output
@ -120,12 +124,16 @@ public final class TestHSSFDataFormatter {
"$#,##0.00",
"[$-809]#,##0.00", // international format
"[$-2]#,##0.00", // international format
"[$-041f]#,##0.00", // international format
"0000.00000%",
"0.000E+00",
"0.00E+00",
"[BLACK]0.00;[COLOR 5]##.##",
"[>999999]#,,\"M\";[>999]#,\"K\";#", // num/K/M
"[>999999]#.000,,\"M\";[>999]#.000,\"K\";#.000", // with decimals
"[$-809]#,##0.00;[$-809]#,##0.00", // two-part international format
"[$-809]#,##0.00;[$-809]#,##0.00;0", // three-part international format
"[$-809]#,##0.00;[$-809]#,##0.00;0;@", // four-part international format
};
// invalid date formats -- will throw exception in DecimalFormat ctor

View File

@ -16,7 +16,7 @@
==================================================================== */
package org.apache.poi.ss.format;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.*;
import java.io.IOException;
import java.text.ParseException;
@ -975,4 +975,40 @@ public class TestCellFormat {
//assertEquals(" "+pound+" - ", cfUK.apply(Double.valueOf(0)).text);
//assertEquals(" - "+euro+" ", cfFR.apply(Double.valueOf(0)).text);
}
@Test
public void testThreePartComplexFormat1() {
// verify a rather complex format found e.g. in http://wahl.land-oberoesterreich.gv.at/Downloads/bp10.xls
CellFormatPart posPart = new CellFormatPart("[$-F400]h:mm:ss\\ AM/PM");
assertNotNull(posPart);
assertEquals("1:00:12 AM", posPart.apply(new Date(12345)).text);
CellFormatPart negPart = new CellFormatPart("[$-F40]h:mm:ss\\ AM/PM");
assertNotNull(negPart);
assertEquals("1:00:12 AM", posPart.apply(new Date(12345)).text);
//assertNotNull(new CellFormatPart("_-* \"\"??_-;_-@_-"));
CellFormat instance = CellFormat.getInstance("[$-F400]h:mm:ss\\ AM/PM;[$-F40]h:mm:ss\\ AM/PM;_-* \"\"??_-;_-@_-");
assertNotNull(instance);
assertEquals("1:00:12 AM", instance.apply(new Date(12345)).text);
}
@Test
public void testThreePartComplexFormat2() {
// verify a rather complex format found e.g. in http://wahl.land-oberoesterreich.gv.at/Downloads/bp10.xls
CellFormatPart posPart = new CellFormatPart("dd/mm/yyyy");
assertNotNull(posPart);
assertEquals("01/01/1970", posPart.apply(new Date(12345)).text);
CellFormatPart negPart = new CellFormatPart("dd/mm/yyyy");
assertNotNull(negPart);
assertEquals("01/01/1970", posPart.apply(new Date(12345)).text);
//assertNotNull(new CellFormatPart("_-* \"\"??_-;_-@_-"));
CellFormat instance = CellFormat.getInstance("dd/mm/yyyy;dd/mm/yyyy;_-* \"\"??_-;_-@_-");
assertNotNull(instance);
assertEquals("01/01/1970", instance.apply(new Date(12345)).text);
}
}