From: Andreas Beeker Date: Mon, 7 Sep 2015 22:34:21 +0000 (+0000) Subject: forbidden apis fixes X-Git-Tag: REL_3_13_FINAL~32 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e84b4d041c63936ab584bc3ca075e24035591f88;p=poi.git forbidden apis fixes git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1701710 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/java/org/apache/poi/ss/format/CellDateFormatter.java b/src/java/org/apache/poi/ss/format/CellDateFormatter.java index dcf702c329..4027d02765 100644 --- a/src/java/org/apache/poi/ss/format/CellDateFormatter.java +++ b/src/java/org/apache/poi/ss/format/CellDateFormatter.java @@ -38,13 +38,12 @@ public class CellDateFormatter extends CellFormatter { private final DateFormat dateFmt; private String sFmt; - private static final long EXCEL_EPOCH_TIME; - private static final Date EXCEL_EPOCH_DATE; + private final long EXCEL_EPOCH_TIME; + private final Date EXCEL_EPOCH_DATE; - private static final CellFormatter SIMPLE_DATE = new CellDateFormatter( - "mm/d/y"); + private static /* final */ CellDateFormatter SIMPLE_DATE = null; - static { + { Calendar c = LocaleUtil.getLocaleCalendar(1904, 0, 1, 0, 0, 0); EXCEL_EPOCH_DATE = c.getTime(); EXCEL_EPOCH_TIME = c.getTimeInMillis(); @@ -153,7 +152,7 @@ public class CellDateFormatter extends CellFormatter { // tweak the format pattern to pass tests on JDK 1.7, // See https://issues.apache.org/bugzilla/show_bug.cgi?id=53369 String ptrn = descBuf.toString().replaceAll("((y)(?!y))(?"mm/d/y". */ public void simpleValue(StringBuffer toAppendTo, Object value) { + synchronized (CellDateFormatter.class) { + if (SIMPLE_DATE == null || !SIMPLE_DATE.EXCEL_EPOCH_DATE.equals(EXCEL_EPOCH_DATE)) { + SIMPLE_DATE = new CellDateFormatter("mm/d/y"); + } + } SIMPLE_DATE.formatValue(toAppendTo, value); } } diff --git a/src/java/org/apache/poi/ss/format/CellFormatter.java b/src/java/org/apache/poi/ss/format/CellFormatter.java index 23cfbe541c..e529a90f16 100644 --- a/src/java/org/apache/poi/ss/format/CellFormatter.java +++ b/src/java/org/apache/poi/ss/format/CellFormatter.java @@ -16,7 +16,6 @@ ==================================================================== */ package org.apache.poi.ss.format; -import java.util.Locale; import java.util.logging.Logger; /** @@ -28,12 +27,6 @@ public abstract class CellFormatter { /** The original specified format. */ protected final String format; - /** - * This is the locale used to get a consistent format result from which to - * work. - */ - public static final Locale LOCALE = Locale.US; - /** * Creates a new formatter object, storing the format in {@link #format}. * diff --git a/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java b/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java index cdee8cf01b..b890e02176 100644 --- a/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java +++ b/src/java/org/apache/poi/ss/format/CellGeneralFormatter.java @@ -19,6 +19,8 @@ package org.apache.poi.ss.format; import java.util.Formatter; import java.util.Locale; +import org.apache.poi.util.LocaleUtil; + /** * A formatter for the default "General" cell format. * @@ -57,9 +59,9 @@ public class CellGeneralFormatter extends CellFormatter { stripZeros = false; } - Formatter formatter = new Formatter(toAppendTo, LOCALE); + Formatter formatter = new Formatter(toAppendTo, LocaleUtil.getUserLocale()); try { - formatter.format(LOCALE, fmt, value); + formatter.format(LocaleUtil.getUserLocale(), fmt, value); } finally { formatter.close(); } diff --git a/src/java/org/apache/poi/ss/format/CellNumberFormatter.java b/src/java/org/apache/poi/ss/format/CellNumberFormatter.java index 3addf46fe5..f38f78006f 100644 --- a/src/java/org/apache/poi/ss/format/CellNumberFormatter.java +++ b/src/java/org/apache/poi/ss/format/CellNumberFormatter.java @@ -597,9 +597,9 @@ public class CellNumberFormatter extends CellFormatter { writeFraction(value, null, fractional, output, mods); } else { StringBuffer result = new StringBuffer(); - Formatter f = new Formatter(result, LOCALE); + Formatter f = new Formatter(result, LocaleUtil.getUserLocale()); try { - f.format(LOCALE, printfFmt, value); + f.format(LocaleUtil.getUserLocale(), printfFmt, value); } finally { f.close(); } @@ -873,9 +873,9 @@ public class CellNumberFormatter extends CellFormatter { List numSpecials, Set mods) { StringBuffer sb = new StringBuffer(); - Formatter formatter = new Formatter(sb, LOCALE); + Formatter formatter = new Formatter(sb, LocaleUtil.getUserLocale()); try { - formatter.format(LOCALE, fmt, num); + formatter.format(LocaleUtil.getUserLocale(), fmt, num); } finally { formatter.close(); } diff --git a/src/java/org/apache/poi/util/LocaleUtil.java b/src/java/org/apache/poi/util/LocaleUtil.java index 4edd65d18d..d402be58c3 100644 --- a/src/java/org/apache/poi/util/LocaleUtil.java +++ b/src/java/org/apache/poi/util/LocaleUtil.java @@ -18,6 +18,7 @@ package org.apache.poi.util; +import java.nio.charset.Charset; import java.util.Calendar; import java.util.Locale; import java.util.TimeZone; @@ -39,6 +40,13 @@ public class LocaleUtil { * use UTC to perform calculations */ public static final TimeZone TIMEZONE_UTC = TimeZone.getTimeZone("UTC"); + + /** + * Default encoding for unknown byte encodings of native files + * (at least it's better than to rely on a platform dependent encoding + * for legacy stuff ...) + */ + public static final Charset CHARSET_1252 = Charset.forName("CP1252"); private static final ThreadLocal userTimeZone = new ThreadLocal() { @Override diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java index 0cbe5bacf6..a75fc84857 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/TOC.java @@ -19,6 +19,7 @@ package org.apache.poi.xwpf.usermodel; import java.math.BigInteger; import org.apache.poi.util.Internal; +import org.apache.poi.util.LocaleUtil; import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute.Space; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; @@ -67,8 +68,8 @@ public class TOC { rPr.addNewSzCs().setVal(new BigInteger("24")); CTSdtContentBlock content = block.addNewSdtContent(); CTP p = content.addNewP(); - p.setRsidR("00EF7E24".getBytes()); - p.setRsidRDefault("00EF7E24".getBytes()); + p.setRsidR("00EF7E24".getBytes(LocaleUtil.CHARSET_1252)); + p.setRsidRDefault("00EF7E24".getBytes(LocaleUtil.CHARSET_1252)); p.addNewPPr().addNewPStyle().setVal("TOCHeading"); p.addNewR().addNewT().setStringValue("Table of Contents"); } @@ -81,8 +82,8 @@ public class TOC { public void addRow(int level, String title, int page, String bookmarkRef) { CTSdtContentBlock contentBlock = this.block.getSdtContent(); CTP p = contentBlock.addNewP(); - p.setRsidR("00EF7E24".getBytes()); - p.setRsidRDefault("00EF7E24".getBytes()); + p.setRsidR("00EF7E24".getBytes(LocaleUtil.CHARSET_1252)); + p.setRsidRDefault("00EF7E24".getBytes(LocaleUtil.CHARSET_1252)); CTPPr pPr = p.addNewPPr(); pPr.addNewPStyle().setVal("TOC" + level); CTTabs tabs = pPr.addNewTabs(); diff --git a/src/scratchpad/src/org/apache/poi/hpbf/dev/HPBFDumper.java b/src/scratchpad/src/org/apache/poi/hpbf/dev/HPBFDumper.java index 3ea5e9a35a..7786950374 100644 --- a/src/scratchpad/src/org/apache/poi/hpbf/dev/HPBFDumper.java +++ b/src/scratchpad/src/org/apache/poi/hpbf/dev/HPBFDumper.java @@ -17,7 +17,7 @@ package org.apache.poi.hpbf.dev; -import java.io.FileInputStream; +import java.io.File; import java.io.IOException; import java.io.InputStream; @@ -27,6 +27,7 @@ import org.apache.poi.poifs.filesystem.DirectoryNode; import org.apache.poi.poifs.filesystem.DocumentEntry; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.StringUtil; /** @@ -39,7 +40,9 @@ public final class HPBFDumper { public HPBFDumper(NPOIFSFileSystem fs) { this.fs = fs; } - public HPBFDumper(InputStream inp) throws IOException { + + @SuppressWarnings("resource") + public HPBFDumper(InputStream inp) throws IOException { this(new NPOIFSFileSystem(inp)); } @@ -75,15 +78,14 @@ public final class HPBFDumper { return ret.toString(); } - public static void main(String[] args) throws Exception { + @SuppressWarnings("resource") + public static void main(String[] args) throws Exception { if(args.length < 1) { System.err.println("Use:"); System.err.println(" HPBFDumper "); System.exit(1); } - HPBFDumper dump = new HPBFDumper( - new FileInputStream(args[0]) - ); + HPBFDumper dump = new HPBFDumper(new NPOIFSFileSystem(new File(args[0]))); System.out.println("Dumping " + args[0]); dump.dumpContents(); @@ -201,7 +203,7 @@ public final class HPBFDumper { // then 4 bytes giving the length, then // 18 00 System.out.println( - new String(data, 0, 8) + + new String(data, 0, 8, LocaleUtil.CHARSET_1252) + dumpBytes(data, 8, 0x20-8) ); @@ -214,7 +216,7 @@ public final class HPBFDumper { ); pos += 2; } - String text = new String(data, pos, 4); + String text = new String(data, pos, 4, LocaleUtil.CHARSET_1252); int blen = 8; if(sixNotEight) blen = 6; @@ -265,11 +267,11 @@ public final class HPBFDumper { int offset = 0x20 + i*24; if(data[offset] == 0x18 && data[offset+1] == 0x00) { // Has data - startType[i] = new String(data, offset+2, 4); + startType[i] = new String(data, offset+2, 4, LocaleUtil.CHARSET_1252); optA[i] = LittleEndian.getUShort(data, offset+6); optB[i] = LittleEndian.getUShort(data, offset+8); optC[i] = LittleEndian.getUShort(data, offset+10); - endType[i] = new String(data, offset+12, 4); + endType[i] = new String(data, offset+12, 4, LocaleUtil.CHARSET_1252); from[i] = (int)LittleEndian.getUInt(data, offset+16); len[i] = (int)LittleEndian.getUInt(data, offset+20); } else { diff --git a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java index 2eefe6703b..78eb082a27 100644 --- a/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java +++ b/src/scratchpad/src/org/apache/poi/hssf/converter/ExcelToHtmlConverter.java @@ -17,7 +17,6 @@ package org.apache.poi.hssf.converter; import java.io.File; -import java.io.FileWriter; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -85,9 +84,8 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter { Document doc = ExcelToHtmlConverter.process( new File( args[0] ) ); - FileWriter out = new FileWriter( args[1] ); DOMSource domSource = new DOMSource( doc ); - StreamResult streamResult = new StreamResult( out ); + StreamResult streamResult = new StreamResult( new File(args[1]) ); TransformerFactory tf = TransformerFactory.newInstance(); Transformer serializer = tf.newTransformer(); @@ -96,7 +94,6 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter serializer.setOutputProperty( OutputKeys.INDENT, "no" ); serializer.setOutputProperty( OutputKeys.METHOD, "html" ); serializer.transform( domSource, streamResult ); - out.close(); } catch ( Exception e ) { @@ -118,7 +115,9 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter XMLHelper.getDocumentBuilderFactory().newDocumentBuilder() .newDocument() ); excelToHtmlConverter.processWorkbook( workbook ); - return excelToHtmlConverter.getDocument(); + Document doc = excelToHtmlConverter.getDocument(); + workbook.close(); + return doc; } private String cssClassContainerCell = null; @@ -368,8 +367,10 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter final short cellStyleIndex = cellStyle.getIndex(); if ( cellStyleIndex != 0 ) { + @SuppressWarnings("resource") HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook(); String mainCssClass = getStyleClassName( workbook, cellStyle ); + if ( wrapInDivs ) { tableCellElement.setAttribute( "class", mainCssClass + " "