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 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))(?<!yy)", "yy");
dateFmt = new SimpleDateFormat(ptrn, LOCALE);
dateFmt = new SimpleDateFormat(ptrn, LocaleUtil.getUserLocale());
}
/** {@inheritDoc} */
@ -185,7 +184,7 @@ public class CellDateFormatter extends CellFormatter {
Formatter formatter = new Formatter(toAppendTo, Locale.ROOT);
try {
long msecs = dateObj.getTime() % 1000;
formatter.format(LOCALE, sFmt, msecs / 1000.0);
formatter.format(LocaleUtil.getUserLocale(), sFmt, msecs / 1000.0);
} finally {
formatter.close();
}
@ -219,6 +218,11 @@ public class CellDateFormatter extends CellFormatter {
* For a date, this is <tt>"mm/d/y"</tt>.
*/
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);
}
}

View File

@ -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}.
*

View File

@ -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();
}

View File

@ -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<Special> numSpecials, Set<StringMod> 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();
}

View File

@ -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<TimeZone> userTimeZone = new ThreadLocal<TimeZone>() {
@Override

View File

@ -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();

View File

@ -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 <filename>");
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 {

View File

@ -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 + " "