Fixed locale-sensitive formatters in PackagePropertiesPart, see Bugzilla 49138

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@935896 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Yegor Kozlov 2010-04-20 12:57:27 +00:00
parent e3ea49bf61
commit fe048df54e
6 changed files with 78 additions and 13 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.7-SNAPSHOT" date="2010-??-??">
<action dev="POI-DEVELOPERS" type="fix">49138 - Fixed locale-sensitive formatters in PackagePropertiesPart</action>
<action dev="POI-DEVELOPERS" type="fix">49153 - Ensure that CTVectorVariant is included in poi-ooxml-schemas.jar</action>
<action dev="POI-DEVELOPERS" type="add">49146 - Added accessors to CoreProperties.Keywords </action>
<action dev="POI-DEVELOPERS" type="fix">48916 - Propagate parent to parent-aware records decoded from Escher</action>

View File

@ -22,6 +22,8 @@ import java.io.OutputStream;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.exceptions.InvalidOperationException;
@ -561,6 +563,7 @@ public final class PackagePropertiesPart extends PackagePart implements
}
SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date d = df.parse(s, new ParsePosition(0));
if (d == null) {
throw new InvalidFormatException("Date not well formated");
@ -582,6 +585,7 @@ public final class PackagePropertiesPart extends PackagePart implements
}
SimpleDateFormat df = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
return df.format(d.getValue());
}

View File

@ -17,8 +17,7 @@
package org.apache.poi;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.*;
import junit.framework.TestCase;
@ -27,7 +26,7 @@ import org.apache.poi.xssf.XSSFTestDataSamples;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.XWPFTestDataSamples;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.openxmlformats.schemas.officeDocument.x2006.customProperties.CTProperties;
import org.apache.poi.openxml4j.util.Nullable;
/**
* Test setting extended and custom OOXML properties
@ -143,8 +142,8 @@ public final class TestPOIXMLProperties extends TestCase {
_coreProperties.setContentStatus(contentStatus);
assertEquals("Draft", contentStatus);
Date created = _coreProperties.getCreated();
SimpleDateFormat formatter = new SimpleDateFormat("EEE, MMM d, ''yy");
assertEquals("Mon, Jul 20, '09", formatter.format(created));
// the original file contains a following value: 2009-07-20T13:12:00Z
assertTrue(dateTimeEqualToUTCString(created, "2009-07-20T13:12:00Z"));
String creator = _coreProperties.getCreator();
assertEquals("Paolo Mottadelli", creator);
String subject = _coreProperties.getSubject();
@ -153,6 +152,21 @@ public final class TestPOIXMLProperties extends TestCase {
assertEquals("Hello World", title);
}
public void testTransitiveSetters() {
XWPFDocument doc = new XWPFDocument();
CoreProperties cp = doc.getProperties().getCoreProperties();
Date dateCreated = new GregorianCalendar(2010, 6, 15, 10, 0, 0).getTime();
cp.setCreated(new Nullable<Date>(dateCreated));
assertEquals(dateCreated.toString(), cp.getCreated().toString());
doc = XWPFTestDataSamples.writeOutAndReadBack(doc);
cp = doc.getProperties().getCoreProperties();
Date dt3 = cp.getCreated();
assertEquals(dateCreated.toString(), dt3.toString());
}
public void testGetSetRevision() {
String revision = _coreProperties.getRevision();
assertTrue("Revision number is 1", Integer.parseInt(revision) > 1);
@ -161,4 +175,26 @@ public final class TestPOIXMLProperties extends TestCase {
_coreProperties.setRevision("20xx");
assertEquals("20", _coreProperties.getRevision());
}
public static boolean dateTimeEqualToUTCString(Date dateTime, String utcString) {
Calendar utcCalendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"), Locale.UK);
utcCalendar.setTimeInMillis(dateTime.getTime());
String dateTimeUtcString = utcCalendar.get(Calendar.YEAR) + "-" +
zeroPad((utcCalendar.get(Calendar.MONTH)+1)) + "-" +
zeroPad(utcCalendar.get(Calendar.DAY_OF_MONTH)) + "T" +
zeroPad(utcCalendar.get(Calendar.HOUR_OF_DAY)) + ":" +
zeroPad(utcCalendar.get(Calendar.MINUTE)) + ":" +
zeroPad(utcCalendar.get(Calendar.SECOND)) + "Z";
return utcString.equals(dateTimeUtcString);
}
private static String zeroPad(long i) {
if (i >= 0 && i <=9) {
return "0" + i;
} else {
return String.valueOf(i);
}
}
}

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import junit.framework.TestCase;
@ -64,6 +65,7 @@ public final class TestPackageCoreProperties extends TestCase {
OPCPackage p = OPCPackage.open(inputPath, PackageAccess.READ_WRITE);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date dateToInsert = df.parse("2007-05-12T08:00:00Z", new ParsePosition(
0));
@ -96,6 +98,7 @@ public final class TestPackageCoreProperties extends TestCase {
private void compareProperties(OPCPackage p) throws InvalidFormatException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
df.setTimeZone(TimeZone.getTimeZone("UTC"));
Date expectedDate = df.parse("2007-05-12T08:00:00Z", new ParsePosition(
0));

View File

@ -19,6 +19,9 @@ package org.apache.poi.hsmf.extractor;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import junit.framework.TestCase;
@ -57,7 +60,10 @@ public final class TestOutlookTextExtractor extends TestCase {
assertEquals(-1, text.indexOf("CC:"));
assertEquals(-1, text.indexOf("BCC:"));
assertContains(text, "Subject: Test the content transformer\n");
assertContains(text, "Date: Thu, 14 Jun 2007 09:42:55\n");
Calendar cal = new GregorianCalendar(2007, 5, 14, 9, 42, 55);
SimpleDateFormat f = new SimpleDateFormat("E, d MMM yyyy HH:mm:ss");
String dateText = f.format(cal.getTime());
assertContains(text, "Date: " + dateText + "\n");
assertContains(text, "The quick brown fox jumps over the lazy dog");
}

View File

@ -17,9 +17,14 @@
package org.apache.poi.hssf.usermodel;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.Locale;
import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.ss.usermodel.Cell;
@ -175,13 +180,19 @@ public final class TestHSSFDataFormatter extends TestCase {
log("==== VALID DATE FORMATS ====");
while (it.hasNext()) {
Cell cell = it.next();
log(formatter.formatCellValue(cell));
String fmtval = formatter.formatCellValue(cell);
log(fmtval);
// should not be equal to "555.555"
assertTrue( ! "555.555".equals(formatter.formatCellValue(cell)));
assertTrue( ! "555.555".equals(fmtval));
// should contain "Jul" in the String
assertTrue( formatter.formatCellValue(cell).indexOf("Jul") > -1);
String fmt = cell.getCellStyle().getDataFormatString();
//assert the correct month form, as in the original Excel format
String monthPtrn = fmt.indexOf("mmmm") != -1 ? "MMMM" : "MMM";
// this line is intended to compute how "July" would look like in the current locale
String jul = new SimpleDateFormat(monthPtrn).format(new GregorianCalendar(2010,6,15).getTime());
assertTrue( fmtval.indexOf(jul) > -1);
}
// test number formats
@ -203,8 +214,10 @@ public final class TestHSSFDataFormatter extends TestCase {
while (it.hasNext()) {
HSSFCell cell = (HSSFCell) it.next();
log(formatter.formatCellValue(cell));
// should be equal to "1234567890.12345"
assertEquals("1234567890.12345", formatter.formatCellValue(cell));
// should be equal to "1234567890.12345"
// in some locales the the decimal delimiter is a comma, not a dot
char decimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
assertEquals("1234567890" + decimalSeparator + "12345", formatter.formatCellValue(cell));
}
// test Zip+4 format
@ -248,7 +261,9 @@ public final class TestHSSFDataFormatter extends TestCase {
// now with a formula evaluator
HSSFFormulaEvaluator evaluator = new HSSFFormulaEvaluator(wb);
log(formatter.formatCellValue(cell, evaluator) + "\t\t\t (with evaluator)");
assertEquals("24.50%", formatter.formatCellValue(cell,evaluator));
char decimalSeparator = new DecimalFormatSymbols().getDecimalSeparator();
assertEquals("24" + decimalSeparator + "50%", formatter.formatCellValue(cell,evaluator));
}
/**