forbidden apis fixes

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1701710 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2015-09-07 22:34:21 +00:00
parent a3d2eb57ff
commit e84b4d041c
8 changed files with 50 additions and 39 deletions

View File

@ -38,13 +38,12 @@ public class CellDateFormatter extends CellFormatter {
private final DateFormat dateFmt; private final DateFormat dateFmt;
private String sFmt; private String sFmt;
private static final long EXCEL_EPOCH_TIME; private final long EXCEL_EPOCH_TIME;
private static final Date EXCEL_EPOCH_DATE; private final Date EXCEL_EPOCH_DATE;
private static final CellFormatter SIMPLE_DATE = new CellDateFormatter( private static /* final */ CellDateFormatter SIMPLE_DATE = null;
"mm/d/y");
static { {
Calendar c = LocaleUtil.getLocaleCalendar(1904, 0, 1, 0, 0, 0); Calendar c = LocaleUtil.getLocaleCalendar(1904, 0, 1, 0, 0, 0);
EXCEL_EPOCH_DATE = c.getTime(); EXCEL_EPOCH_DATE = c.getTime();
EXCEL_EPOCH_TIME = c.getTimeInMillis(); 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, // tweak the format pattern to pass tests on JDK 1.7,
// See https://issues.apache.org/bugzilla/show_bug.cgi?id=53369 // See https://issues.apache.org/bugzilla/show_bug.cgi?id=53369
String ptrn = descBuf.toString().replaceAll("((y)(?!y))(?<!yy)", "yy"); String ptrn = descBuf.toString().replaceAll("((y)(?!y))(?<!yy)", "yy");
dateFmt = new SimpleDateFormat(ptrn, LOCALE); dateFmt = new SimpleDateFormat(ptrn, LocaleUtil.getUserLocale());
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -185,7 +184,7 @@ public class CellDateFormatter extends CellFormatter {
Formatter formatter = new Formatter(toAppendTo, Locale.ROOT); Formatter formatter = new Formatter(toAppendTo, Locale.ROOT);
try { try {
long msecs = dateObj.getTime() % 1000; long msecs = dateObj.getTime() % 1000;
formatter.format(LOCALE, sFmt, msecs / 1000.0); formatter.format(LocaleUtil.getUserLocale(), sFmt, msecs / 1000.0);
} finally { } finally {
formatter.close(); formatter.close();
} }
@ -219,6 +218,11 @@ public class CellDateFormatter extends CellFormatter {
* For a date, this is <tt>"mm/d/y"</tt>. * For a date, this is <tt>"mm/d/y"</tt>.
*/ */
public void simpleValue(StringBuffer toAppendTo, Object value) { 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); SIMPLE_DATE.formatValue(toAppendTo, value);
} }
} }

View File

@ -16,7 +16,6 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.ss.format; package org.apache.poi.ss.format;
import java.util.Locale;
import java.util.logging.Logger; import java.util.logging.Logger;
/** /**
@ -28,12 +27,6 @@ public abstract class CellFormatter {
/** The original specified format. */ /** The original specified format. */
protected final String 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}. * Creates a new formatter object, storing the format in {@link #format}.
* *

View File

@ -19,6 +19,8 @@ package org.apache.poi.ss.format;
import java.util.Formatter; import java.util.Formatter;
import java.util.Locale; import java.util.Locale;
import org.apache.poi.util.LocaleUtil;
/** /**
* A formatter for the default "General" cell format. * A formatter for the default "General" cell format.
* *
@ -57,9 +59,9 @@ public class CellGeneralFormatter extends CellFormatter {
stripZeros = false; stripZeros = false;
} }
Formatter formatter = new Formatter(toAppendTo, LOCALE); Formatter formatter = new Formatter(toAppendTo, LocaleUtil.getUserLocale());
try { try {
formatter.format(LOCALE, fmt, value); formatter.format(LocaleUtil.getUserLocale(), fmt, value);
} finally { } finally {
formatter.close(); formatter.close();
} }

View File

@ -597,9 +597,9 @@ public class CellNumberFormatter extends CellFormatter {
writeFraction(value, null, fractional, output, mods); writeFraction(value, null, fractional, output, mods);
} else { } else {
StringBuffer result = new StringBuffer(); StringBuffer result = new StringBuffer();
Formatter f = new Formatter(result, LOCALE); Formatter f = new Formatter(result, LocaleUtil.getUserLocale());
try { try {
f.format(LOCALE, printfFmt, value); f.format(LocaleUtil.getUserLocale(), printfFmt, value);
} finally { } finally {
f.close(); f.close();
} }
@ -873,9 +873,9 @@ public class CellNumberFormatter extends CellFormatter {
List<Special> numSpecials, Set<StringMod> mods) { List<Special> numSpecials, Set<StringMod> mods) {
StringBuffer sb = new StringBuffer(); StringBuffer sb = new StringBuffer();
Formatter formatter = new Formatter(sb, LOCALE); Formatter formatter = new Formatter(sb, LocaleUtil.getUserLocale());
try { try {
formatter.format(LOCALE, fmt, num); formatter.format(LocaleUtil.getUserLocale(), fmt, num);
} finally { } finally {
formatter.close(); formatter.close();
} }

View File

@ -18,6 +18,7 @@
package org.apache.poi.util; package org.apache.poi.util;
import java.nio.charset.Charset;
import java.util.Calendar; import java.util.Calendar;
import java.util.Locale; import java.util.Locale;
import java.util.TimeZone; import java.util.TimeZone;
@ -40,6 +41,13 @@ public class LocaleUtil {
*/ */
public static final TimeZone TIMEZONE_UTC = TimeZone.getTimeZone("UTC"); 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<TimeZone> userTimeZone = new ThreadLocal<TimeZone>() { private static final ThreadLocal<TimeZone> userTimeZone = new ThreadLocal<TimeZone>() {
@Override @Override
@SuppressForbidden @SuppressForbidden

View File

@ -19,6 +19,7 @@ package org.apache.poi.xwpf.usermodel;
import java.math.BigInteger; import java.math.BigInteger;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.LocaleUtil;
import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute.Space; import org.apache.xmlbeans.impl.xb.xmlschema.SpaceAttribute.Space;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDecimalNumber;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts;
@ -67,8 +68,8 @@ public class TOC {
rPr.addNewSzCs().setVal(new BigInteger("24")); rPr.addNewSzCs().setVal(new BigInteger("24"));
CTSdtContentBlock content = block.addNewSdtContent(); CTSdtContentBlock content = block.addNewSdtContent();
CTP p = content.addNewP(); CTP p = content.addNewP();
p.setRsidR("00EF7E24".getBytes()); p.setRsidR("00EF7E24".getBytes(LocaleUtil.CHARSET_1252));
p.setRsidRDefault("00EF7E24".getBytes()); p.setRsidRDefault("00EF7E24".getBytes(LocaleUtil.CHARSET_1252));
p.addNewPPr().addNewPStyle().setVal("TOCHeading"); p.addNewPPr().addNewPStyle().setVal("TOCHeading");
p.addNewR().addNewT().setStringValue("Table of Contents"); 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) { public void addRow(int level, String title, int page, String bookmarkRef) {
CTSdtContentBlock contentBlock = this.block.getSdtContent(); CTSdtContentBlock contentBlock = this.block.getSdtContent();
CTP p = contentBlock.addNewP(); CTP p = contentBlock.addNewP();
p.setRsidR("00EF7E24".getBytes()); p.setRsidR("00EF7E24".getBytes(LocaleUtil.CHARSET_1252));
p.setRsidRDefault("00EF7E24".getBytes()); p.setRsidRDefault("00EF7E24".getBytes(LocaleUtil.CHARSET_1252));
CTPPr pPr = p.addNewPPr(); CTPPr pPr = p.addNewPPr();
pPr.addNewPStyle().setVal("TOC" + level); pPr.addNewPStyle().setVal("TOC" + level);
CTTabs tabs = pPr.addNewTabs(); CTTabs tabs = pPr.addNewTabs();

View File

@ -17,7 +17,7 @@
package org.apache.poi.hpbf.dev; package org.apache.poi.hpbf.dev;
import java.io.FileInputStream; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; 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.DocumentEntry;
import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
/** /**
@ -39,6 +40,8 @@ public final class HPBFDumper {
public HPBFDumper(NPOIFSFileSystem fs) { public HPBFDumper(NPOIFSFileSystem fs) {
this.fs = fs; this.fs = fs;
} }
@SuppressWarnings("resource")
public HPBFDumper(InputStream inp) throws IOException { public HPBFDumper(InputStream inp) throws IOException {
this(new NPOIFSFileSystem(inp)); this(new NPOIFSFileSystem(inp));
} }
@ -75,15 +78,14 @@ public final class HPBFDumper {
return ret.toString(); return ret.toString();
} }
@SuppressWarnings("resource")
public static void main(String[] args) throws Exception { public static void main(String[] args) throws Exception {
if(args.length < 1) { if(args.length < 1) {
System.err.println("Use:"); System.err.println("Use:");
System.err.println(" HPBFDumper <filename>"); System.err.println(" HPBFDumper <filename>");
System.exit(1); System.exit(1);
} }
HPBFDumper dump = new HPBFDumper( HPBFDumper dump = new HPBFDumper(new NPOIFSFileSystem(new File(args[0])));
new FileInputStream(args[0])
);
System.out.println("Dumping " + args[0]); System.out.println("Dumping " + args[0]);
dump.dumpContents(); dump.dumpContents();
@ -201,7 +203,7 @@ public final class HPBFDumper {
// then 4 bytes giving the length, then // then 4 bytes giving the length, then
// 18 00 // 18 00
System.out.println( System.out.println(
new String(data, 0, 8) + new String(data, 0, 8, LocaleUtil.CHARSET_1252) +
dumpBytes(data, 8, 0x20-8) dumpBytes(data, 8, 0x20-8)
); );
@ -214,7 +216,7 @@ public final class HPBFDumper {
); );
pos += 2; pos += 2;
} }
String text = new String(data, pos, 4); String text = new String(data, pos, 4, LocaleUtil.CHARSET_1252);
int blen = 8; int blen = 8;
if(sixNotEight) if(sixNotEight)
blen = 6; blen = 6;
@ -265,11 +267,11 @@ public final class HPBFDumper {
int offset = 0x20 + i*24; int offset = 0x20 + i*24;
if(data[offset] == 0x18 && data[offset+1] == 0x00) { if(data[offset] == 0x18 && data[offset+1] == 0x00) {
// Has data // 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); optA[i] = LittleEndian.getUShort(data, offset+6);
optB[i] = LittleEndian.getUShort(data, offset+8); optB[i] = LittleEndian.getUShort(data, offset+8);
optC[i] = LittleEndian.getUShort(data, offset+10); 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); from[i] = (int)LittleEndian.getUInt(data, offset+16);
len[i] = (int)LittleEndian.getUInt(data, offset+20); len[i] = (int)LittleEndian.getUInt(data, offset+20);
} else { } else {

View File

@ -17,7 +17,6 @@
package org.apache.poi.hssf.converter; package org.apache.poi.hssf.converter;
import java.io.File; import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
@ -85,9 +84,8 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
{ {
Document doc = ExcelToHtmlConverter.process( new File( args[0] ) ); Document doc = ExcelToHtmlConverter.process( new File( args[0] ) );
FileWriter out = new FileWriter( args[1] );
DOMSource domSource = new DOMSource( doc ); DOMSource domSource = new DOMSource( doc );
StreamResult streamResult = new StreamResult( out ); StreamResult streamResult = new StreamResult( new File(args[1]) );
TransformerFactory tf = TransformerFactory.newInstance(); TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer(); Transformer serializer = tf.newTransformer();
@ -96,7 +94,6 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
serializer.setOutputProperty( OutputKeys.INDENT, "no" ); serializer.setOutputProperty( OutputKeys.INDENT, "no" );
serializer.setOutputProperty( OutputKeys.METHOD, "html" ); serializer.setOutputProperty( OutputKeys.METHOD, "html" );
serializer.transform( domSource, streamResult ); serializer.transform( domSource, streamResult );
out.close();
} }
catch ( Exception e ) catch ( Exception e )
{ {
@ -118,7 +115,9 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
XMLHelper.getDocumentBuilderFactory().newDocumentBuilder() XMLHelper.getDocumentBuilderFactory().newDocumentBuilder()
.newDocument() ); .newDocument() );
excelToHtmlConverter.processWorkbook( workbook ); excelToHtmlConverter.processWorkbook( workbook );
return excelToHtmlConverter.getDocument(); Document doc = excelToHtmlConverter.getDocument();
workbook.close();
return doc;
} }
private String cssClassContainerCell = null; private String cssClassContainerCell = null;
@ -368,8 +367,10 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
final short cellStyleIndex = cellStyle.getIndex(); final short cellStyleIndex = cellStyle.getIndex();
if ( cellStyleIndex != 0 ) if ( cellStyleIndex != 0 )
{ {
@SuppressWarnings("resource")
HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook(); HSSFWorkbook workbook = cell.getRow().getSheet().getWorkbook();
String mainCssClass = getStyleClassName( workbook, cellStyle ); String mainCssClass = getStyleClassName( workbook, cellStyle );
if ( wrapInDivs ) if ( wrapInDivs )
{ {
tableCellElement.setAttribute( "class", mainCssClass + " " tableCellElement.setAttribute( "class", mainCssClass + " "