From c1b2bab50e888da5d780e0a91d2b066738568791 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Tue, 1 Sep 2015 21:18:33 +0000 Subject: [PATCH] Fix some Forbidden APIs errors git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1700672 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/dev/RecordGenerator.java | 6 ++++-- .../poi/ss/formula/atp/WorkdayCalculator.java | 15 ++++++++++----- .../apache/poi/ss/util/DateFormatConverter.java | 4 ++-- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/poi/dev/RecordGenerator.java b/src/java/org/apache/poi/dev/RecordGenerator.java index 588724d06f..b442b8e7ad 100644 --- a/src/java/org/apache/poi/dev/RecordGenerator.java +++ b/src/java/org/apache/poi/dev/RecordGenerator.java @@ -19,8 +19,9 @@ package org.apache.poi.dev; import java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; -import java.io.FileReader; +import java.io.InputStreamReader; import java.io.Reader; import java.util.Locale; import java.util.Properties; @@ -35,6 +36,7 @@ import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; +import org.apache.poi.util.StringUtil; import org.apache.poi.util.XMLHelper; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -128,7 +130,7 @@ public class RecordGenerator { private static void transform(final File in, final File out, final File xslt) throws FileNotFoundException, TransformerException { - final Reader r = new FileReader(xslt); + final Reader r = new InputStreamReader(new FileInputStream(xslt), StringUtil.UTF8); final StreamSource ss = new StreamSource(r); final TransformerFactory tf = TransformerFactory.newInstance(); final Transformer t; diff --git a/src/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java b/src/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java index 4b7ecf0fb4..ada303436e 100644 --- a/src/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java +++ b/src/java/org/apache/poi/ss/formula/atp/WorkdayCalculator.java @@ -19,15 +19,20 @@ package org.apache.poi.ss.formula.atp; import java.util.Calendar; import java.util.Date; +import java.util.Locale; +import java.util.TimeZone; import org.apache.poi.ss.usermodel.DateUtil; /** * A calculator for workdays, considering dates as excel representations. - * - * @author jfaenomoto@gmail.com */ public class WorkdayCalculator { + /** + * 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"); public static final WorkdayCalculator instance = new WorkdayCalculator(); @@ -64,7 +69,7 @@ public class WorkdayCalculator { public Date calculateWorkdays(double start, int workdays, double[] holidays) { Date startDate = DateUtil.getJavaDate(start); int direction = workdays < 0 ? -1 : 1; - Calendar endDate = Calendar.getInstance(); + Calendar endDate = Calendar.getInstance(DEFAULT_TIMEZONE, Locale.ROOT); endDate.setTime(startDate); double excelEndDate = DateUtil.getExcelDate(endDate.getTime()); while (workdays != 0) { @@ -92,7 +97,7 @@ public class WorkdayCalculator { int startDay = (int) Math.floor(start < end ? start : end); int endDay = (int) Math.floor(end > start ? end : start); for (; startDay <= endDay; startDay++) { - Calendar today = Calendar.getInstance(); + Calendar today = Calendar.getInstance(DEFAULT_TIMEZONE, Locale.ROOT); today.setTime(DateUtil.getJavaDate(startDay)); if (today.get(Calendar.DAY_OF_WEEK) == dayOfWeek) { pastDaysOfWeek++; @@ -128,7 +133,7 @@ public class WorkdayCalculator { * @return true if date is weekend, false otherwise. */ protected boolean isWeekend(double aDate) { - Calendar date = Calendar.getInstance(); + Calendar date = Calendar.getInstance(DEFAULT_TIMEZONE, Locale.ROOT); date.setTime(DateUtil.getJavaDate(aDate)); return date.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY || date.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY; } diff --git a/src/java/org/apache/poi/ss/util/DateFormatConverter.java b/src/java/org/apache/poi/ss/util/DateFormatConverter.java index 7d9248b164..aa343f3722 100644 --- a/src/java/org/apache/poi/ss/util/DateFormatConverter.java +++ b/src/java/org/apache/poi/ss/util/DateFormatConverter.java @@ -337,8 +337,8 @@ public class DateFormatConverter { result = localePrefixes.get( localeString.substring( 0, 2 ) ); if( result == null ) { Locale parentLocale = new Locale(localeString.substring( 0, 2 )); - logger.log( POILogger.ERROR, "Unable to find prefix for " + locale + "(" + locale.getDisplayName() + ") or " - + localeString.substring( 0, 2 ) + "(" + parentLocale.getDisplayName() + ")" ); + logger.log( POILogger.ERROR, "Unable to find prefix for " + locale + "(" + locale.getDisplayName(Locale.ROOT) + ") or " + + localeString.substring( 0, 2 ) + "(" + parentLocale.getDisplayName(Locale.ROOT) + ")" ); return ""; } }