aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/org/apache/poi/ss/format
diff options
context:
space:
mode:
authorAndreas Beeker <kiwiwings@apache.org>2015-09-07 20:19:50 +0000
committerAndreas Beeker <kiwiwings@apache.org>2015-09-07 20:19:50 +0000
commita3d2eb57ff4ac10ae215b9b7404260103eb6f538 (patch)
tree7b44ec32a7cdd0e0828779c793061ac6a00e4c00 /src/java/org/apache/poi/ss/format
parentf2e85496125f7fc68f7434244b9d9d5bb026933d (diff)
downloadpoi-a3d2eb57ff4ac10ae215b9b7404260103eb6f538.tar.gz
poi-a3d2eb57ff4ac10ae215b9b7404260103eb6f538.zip
Bug 58326 - Forbidden APIs patches - first set of changes for locale and timezone settings
also includes fixes for - name shadowing - unused deprecated method "getClipRect" in classes extending Graphics2d - HexDump - replaced intermediate String.format calls with custom padding - convert testcases to junit4 - closing resources also tested with an arbitary timezone (PST) and locale (ru) supresses forbidden apis check for - LocaleUtil (the only place where Locale.getDefault() and TimeZone.getDefault() should be called) - Classes using FontMetrics - without the actual text it's difficult to return something sane Some usage of UTC and Locale.ROOT might be still wrong, e.g. in MapiMessage we don't access the extended mapi properties, which might contain the timezone DataFormatter has now a Observable property which need to be observed when custom formats are used and the Locale changes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1701688 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/ss/format')
-rw-r--r--src/java/org/apache/poi/ss/format/CellDateFormatter.java12
-rw-r--r--src/java/org/apache/poi/ss/format/CellFormatPart.java4
-rw-r--r--src/java/org/apache/poi/ss/format/CellGeneralFormatter.java2
-rw-r--r--src/java/org/apache/poi/ss/format/CellNumberFormatter.java16
4 files changed, 18 insertions, 16 deletions
diff --git a/src/java/org/apache/poi/ss/format/CellDateFormatter.java b/src/java/org/apache/poi/ss/format/CellDateFormatter.java
index 0e6cfa15b4..dcf702c329 100644
--- a/src/java/org/apache/poi/ss/format/CellDateFormatter.java
+++ b/src/java/org/apache/poi/ss/format/CellDateFormatter.java
@@ -24,19 +24,14 @@ import java.util.Calendar;
import java.util.Date;
import java.util.Formatter;
import java.util.Locale;
-import java.util.TimeZone;
import java.util.regex.Matcher;
+import org.apache.poi.util.LocaleUtil;
+
/**
* Formats a date value.
*/
public class CellDateFormatter extends CellFormatter {
- /**
- * Excel doesn't store TimeZone information in the file, so if in doubt,
- * use UTC to perform calculations
- */
- private static final TimeZone DEFAULT_TIMEZONE = TimeZone.getTimeZone("UTC");
-
private boolean amPmUpper;
private boolean showM;
private boolean showAmPm;
@@ -50,8 +45,7 @@ public class CellDateFormatter extends CellFormatter {
"mm/d/y");
static {
- Calendar c = Calendar.getInstance(DEFAULT_TIMEZONE, Locale.ROOT);
- c.set(1904, 0, 1, 0, 0, 0);
+ Calendar c = LocaleUtil.getLocaleCalendar(1904, 0, 1, 0, 0, 0);
EXCEL_EPOCH_DATE = c.getTime();
EXCEL_EPOCH_TIME = c.getTimeInMillis();
}
diff --git a/src/java/org/apache/poi/ss/format/CellFormatPart.java b/src/java/org/apache/poi/ss/format/CellFormatPart.java
index 9a966e1529..40d8c0849c 100644
--- a/src/java/org/apache/poi/ss/format/CellFormatPart.java
+++ b/src/java/org/apache/poi/ss/format/CellFormatPart.java
@@ -524,4 +524,8 @@ public class CellFormatPart {
String str = m.group(g);
return (str == null ? "" : str);
}
+
+ public String toString() {
+ return format.format;
+ }
}
diff --git a/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java b/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java
index cb5d4a1bb1..cdee8cf01b 100644
--- a/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java
+++ b/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java
@@ -57,7 +57,7 @@ public class CellGeneralFormatter extends CellFormatter {
stripZeros = false;
}
- Formatter formatter = new Formatter(toAppendTo);
+ Formatter formatter = new Formatter(toAppendTo, LOCALE);
try {
formatter.format(LOCALE, fmt, value);
} finally {
diff --git a/src/java/org/apache/poi/ss/format/CellNumberFormatter.java b/src/java/org/apache/poi/ss/format/CellNumberFormatter.java
index 2ea72cc693..3addf46fe5 100644
--- a/src/java/org/apache/poi/ss/format/CellNumberFormatter.java
+++ b/src/java/org/apache/poi/ss/format/CellNumberFormatter.java
@@ -17,6 +17,7 @@
package org.apache.poi.ss.format;
import java.text.DecimalFormat;
+import java.text.DecimalFormatSymbols;
import java.text.FieldPosition;
import java.util.BitSet;
import java.util.Collections;
@@ -30,6 +31,7 @@ import java.util.TreeSet;
import java.util.regex.Matcher;
import org.apache.poi.ss.format.CellFormatPart.PartHandler;
+import org.apache.poi.util.LocaleUtil;
/**
* This class implements printing out a value using a number format.
@@ -182,8 +184,8 @@ public class CellNumberFormatter extends CellFormatter {
private char insertSignForExponent;
public String handlePart(Matcher m, String part, CellFormatType type,
- StringBuffer desc) {
- int pos = desc.length();
+ StringBuffer descBuf) {
+ int pos = descBuf.length();
char firstCh = part.charAt(0);
switch (firstCh) {
case 'e':
@@ -203,7 +205,7 @@ public class CellNumberFormatter extends CellFormatter {
case '#':
if (insertSignForExponent != '\0') {
specials.add(new Special(insertSignForExponent, pos));
- desc.append(insertSignForExponent);
+ descBuf.append(insertSignForExponent);
insertSignForExponent = '\0';
pos++;
}
@@ -354,7 +356,8 @@ public class CellNumberFormatter extends CellFormatter {
fmtBuf.append('E');
placeZeros(fmtBuf, exponentSpecials.subList(2,
exponentSpecials.size()));
- decimalFmt = new DecimalFormat(fmtBuf.toString());
+ DecimalFormatSymbols dfs = DecimalFormatSymbols.getInstance(LocaleUtil.getUserLocale());
+ decimalFmt = new DecimalFormat(fmtBuf.toString(), dfs);
}
if (exponent != null)
@@ -594,7 +597,7 @@ public class CellNumberFormatter extends CellFormatter {
writeFraction(value, null, fractional, output, mods);
} else {
StringBuffer result = new StringBuffer();
- Formatter f = new Formatter(result);
+ Formatter f = new Formatter(result, LOCALE);
try {
f.format(LOCALE, printfFmt, value);
} finally {
@@ -767,6 +770,7 @@ public class CellNumberFormatter extends CellFormatter {
writeInteger(exponentNum, output, exponentDigitSpecials, mods, false);
}
+ @SuppressWarnings("unchecked")
private void writeFraction(double value, StringBuffer result,
double fractional, StringBuffer output, Set<StringMod> mods) {
@@ -869,7 +873,7 @@ public class CellNumberFormatter extends CellFormatter {
List<Special> numSpecials, Set<StringMod> mods) {
StringBuffer sb = new StringBuffer();
- Formatter formatter = new Formatter(sb);
+ Formatter formatter = new Formatter(sb, LOCALE);
try {
formatter.format(LOCALE, fmt, num);
} finally {