Bug 54786: Fix missing quoting in date formatting, add a number of unit

tests which verify the new formatting options.

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1514632 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dominik Stadler 2013-08-16 09:38:47 +00:00
parent a62fcb2fbf
commit ed2e2a28bc
2 changed files with 50 additions and 2 deletions

View File

@ -423,6 +423,8 @@ public class DataFormatter {
formatStr = formatStr.replaceAll("\\\\/","/"); // weird: m\\/d\\/yyyy formatStr = formatStr.replaceAll("\\\\/","/"); // weird: m\\/d\\/yyyy
formatStr = formatStr.replaceAll(";@", ""); formatStr = formatStr.replaceAll(";@", "");
formatStr = formatStr.replaceAll("\"/\"", "/"); // "/" is escaped for no reason in: mm"/"dd"/"yyyy 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; boolean hasAmPm = false;
Matcher amPmMatcher = amPmPattern.matcher(formatStr); Matcher amPmMatcher = amPmPattern.matcher(formatStr);
@ -456,7 +458,21 @@ public class DataFormatter {
boolean isElapsed = false; boolean isElapsed = false;
for(int j=0; j<chars.length; j++) { for(int j=0; j<chars.length; j++) {
char c = chars[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; isElapsed = true;
mIsMonth = false; mIsMonth = false;
sb.append(c); sb.append(c);
@ -932,10 +948,12 @@ public class DataFormatter {
return sb.toString(); return sb.toString();
} }
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
return toAppendTo.append(format((Number)obj)); return toAppendTo.append(format((Number)obj));
} }
@Override
public Object parseObject(String source, ParsePosition pos) { public Object parseObject(String source, ParsePosition pos) {
return df.parseObject(source, pos); return df.parseObject(source, pos);
} }
@ -963,10 +981,12 @@ public class DataFormatter {
return sb.toString(); return sb.toString();
} }
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
return toAppendTo.append(format((Number)obj)); return toAppendTo.append(format((Number)obj));
} }
@Override
public Object parseObject(String source, ParsePosition pos) { public Object parseObject(String source, ParsePosition pos) {
return df.parseObject(source, pos); return df.parseObject(source, pos);
} }
@ -1009,10 +1029,12 @@ public class DataFormatter {
return sb.toString(); return sb.toString();
} }
@Override
public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) {
return toAppendTo.append(format((Number)obj)); return toAppendTo.append(format((Number)obj));
} }
@Override
public Object parseObject(String source, ParsePosition pos) { public Object parseObject(String source, ParsePosition pos) {
return df.parseObject(source, pos); return df.parseObject(source, pos);
} }

View File

@ -37,7 +37,9 @@ import org.apache.poi.hssf.usermodel.TestHSSFDataFormatter;
* more tests. * more tests.
*/ */
public class TestDataFormatter extends TestCase { public class TestDataFormatter extends TestCase {
/** private static final double _15_MINUTES = 0.041666667;
/**
* Test that we use the specified locale when deciding * Test that we use the specified locale when deciding
* how to format normal numbers * how to format normal numbers
*/ */
@ -538,4 +540,28 @@ public class TestDataFormatter extends TestCase {
DataFormatter dfUS = new DataFormatter(Locale.US, true); DataFormatter dfUS = new DataFormatter(Locale.US, true);
assertEquals("01.010", dfUS.formatRawCellContents(0.0000116898, -1, "ss.000")); 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"));
}
}
} }