replace Throwable.printStackTrace() with POILogger calls - Todo: other printStrackTrace(...) calls

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1723899 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-01-10 02:20:35 +00:00
parent 22a9bade84
commit 8d033e235c
24 changed files with 139 additions and 165 deletions

View File

@ -390,15 +390,9 @@ public final class BiffViewer {
* </table> * </table>
* *
*/ */
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException, CommandParseException {
// args = new String[] { "--out", "", }; // args = new String[] { "--out", "", };
CommandArgs cmdArgs; CommandArgs cmdArgs = CommandArgs.parse(args);
try {
cmdArgs = CommandArgs.parse(args);
} catch (CommandParseException e) {
e.printStackTrace();
return;
}
PrintWriter pw; PrintWriter pw;
if (cmdArgs.shouldOutputToFile()) { if (cmdArgs.shouldOutputToFile()) {

View File

@ -72,21 +72,14 @@ public class EFBiffViewer
this.file = file; this.file = file;
} }
public static void main(String [] args) public static void main(String [] args) throws IOException
{ {
if ((args.length == 1) && !args[ 0 ].equals("--help")) if ((args.length == 1) && !args[ 0 ].equals("--help"))
{ {
try EFBiffViewer viewer = new EFBiffViewer();
{
EFBiffViewer viewer = new EFBiffViewer();
viewer.setFile(args[ 0 ]); viewer.setFile(args[ 0 ]);
viewer.run(); viewer.run();
}
catch (IOException e)
{
e.printStackTrace();
}
} }
else else
{ {

View File

@ -18,6 +18,7 @@
package org.apache.poi.hssf.dev; package org.apache.poi.hssf.dev;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
@ -52,13 +53,14 @@ public class FormulaViewer
/** /**
* Method run * Method run
* @throws IOException
* *
* *
* @exception Exception * @exception Exception
* *
*/ */
public void run() throws Exception { public void run() throws IOException {
NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true); NPOIFSFileSystem fs = new NPOIFSFileSystem(new File(file), true);
InputStream is = BiffViewer.getPOIFSInputStream(fs); InputStream is = BiffViewer.getPOIFSInputStream(fs);
List<Record> records = RecordFactory.createRecords(is); List<Record> records = RecordFactory.createRecords(is);
@ -206,10 +208,12 @@ public class FormulaViewer
* pass me a filename and I'll try and parse the formulas from it * pass me a filename and I'll try and parse the formulas from it
* *
* @param args pass one argument with the filename or --help * @param args pass one argument with the filename or --help
* @throws IOException
* @throws Exception
* *
*/ */
public static void main(String args[]) public static void main(String args[]) throws IOException
{ {
if ((args == null) || (args.length >2 ) if ((args == null) || (args.length >2 )
|| args[ 0 ].equals("--help")) || args[ 0 ].equals("--help"))
@ -218,31 +222,17 @@ public class FormulaViewer
"FormulaViewer .8 proof that the devil lies in the details (or just in BIFF8 files in general)"); "FormulaViewer .8 proof that the devil lies in the details (or just in BIFF8 files in general)");
System.out.println("usage: Give me a big fat file name"); System.out.println("usage: Give me a big fat file name");
} else if (args[0].equals("--listFunctions")) { // undocumented attribute to research functions!~ } else if (args[0].equals("--listFunctions")) { // undocumented attribute to research functions!~
try { FormulaViewer viewer = new FormulaViewer();
FormulaViewer viewer = new FormulaViewer(); viewer.setFile(args[1]);
viewer.setFile(args[1]); viewer.setList(true);
viewer.setList(true); viewer.run();
viewer.run();
}
catch (Exception e) {
System.out.println("Whoops!");
e.printStackTrace();
}
} }
else else
{ {
try FormulaViewer viewer = new FormulaViewer();
{
FormulaViewer viewer = new FormulaViewer();
viewer.setFile(args[ 0 ]); viewer.setFile(args[ 0 ]);
viewer.run(); viewer.run();
}
catch (Exception e)
{
System.out.println("Whoops!");
e.printStackTrace();
}
} }
} }
} }

View File

@ -180,21 +180,14 @@ public class RecordLister
this.file = file; this.file = file;
} }
public static void main(String [] args) public static void main(String [] args) throws IOException
{ {
if ((args.length == 1) && !args[ 0 ].equals("--help")) if ((args.length == 1) && !args[ 0 ].equals("--help"))
{ {
try RecordLister viewer = new RecordLister();
{
RecordLister viewer = new RecordLister();
viewer.setFile(args[ 0 ]); viewer.setFile(args[ 0 ]);
viewer.run(); viewer.run();
}
catch (IOException e)
{
e.printStackTrace();
}
} }
else else
{ {

View File

@ -207,8 +207,9 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
/** /**
* Command line extractor. * Command line extractor.
* @throws IOException
*/ */
public static void main(String[] args) { public static void main(String[] args) throws IOException {
CommandArgs cmdArgs; CommandArgs cmdArgs;
try { try {
@ -225,27 +226,23 @@ public class ExcelExtractor extends POIOLE2TextExtractor implements org.apache.p
return; return;
} }
try { InputStream is;
InputStream is; if(cmdArgs.getInputFile() == null) {
if(cmdArgs.getInputFile() == null) { is = System.in;
is = System.in; } else {
} else { is = new FileInputStream(cmdArgs.getInputFile());
is = new FileInputStream(cmdArgs.getInputFile());
}
HSSFWorkbook wb = new HSSFWorkbook(is);
ExcelExtractor extractor = new ExcelExtractor(wb);
extractor.setIncludeSheetNames(cmdArgs.shouldShowSheetNames());
extractor.setFormulasNotResults(!cmdArgs.shouldEvaluateFormulas());
extractor.setIncludeCellComments(cmdArgs.shouldShowCellComments());
extractor.setIncludeBlankCells(cmdArgs.shouldShowBlankCells());
extractor.setIncludeHeadersFooters(cmdArgs.shouldIncludeHeadersFooters());
System.out.println(extractor.getText());
extractor.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
} }
HSSFWorkbook wb = new HSSFWorkbook(is);
is.close();
ExcelExtractor extractor = new ExcelExtractor(wb);
extractor.setIncludeSheetNames(cmdArgs.shouldShowSheetNames());
extractor.setFormulasNotResults(!cmdArgs.shouldEvaluateFormulas());
extractor.setIncludeCellComments(cmdArgs.shouldShowCellComments());
extractor.setIncludeBlankCells(cmdArgs.shouldShowBlankCells());
extractor.setIncludeHeadersFooters(cmdArgs.shouldIncludeHeadersFooters());
System.out.println(extractor.getText());
extractor.close();
} }
/** /**
* Should sheet names be included? Default is true * Should sheet names be included? Default is true

View File

@ -17,13 +17,24 @@
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
import org.apache.poi.ddf.*; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.poi.ddf.EscherBoolProperty;
import org.apache.poi.ddf.EscherChildAnchorRecord;
import org.apache.poi.ddf.EscherClientAnchorRecord;
import org.apache.poi.ddf.EscherContainerRecord;
import org.apache.poi.ddf.EscherOptRecord;
import org.apache.poi.ddf.EscherProperties;
import org.apache.poi.ddf.EscherProperty;
import org.apache.poi.ddf.EscherRGBProperty;
import org.apache.poi.ddf.EscherSimpleProperty;
import org.apache.poi.ddf.EscherSpRecord;
import org.apache.poi.hssf.record.CommonObjectDataSubRecord; import org.apache.poi.hssf.record.CommonObjectDataSubRecord;
import org.apache.poi.hssf.record.ObjRecord; import org.apache.poi.hssf.record.ObjRecord;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import java.io.ByteArrayOutputStream; import org.apache.poi.util.POILogger;
import java.io.IOException;
/** /**
* An abstract shape. * An abstract shape.
@ -34,6 +45,8 @@ import java.io.IOException;
* setFlipVertical() or setFlipHorizontally(). * setFlipVertical() or setFlipHorizontally().
*/ */
public abstract class HSSFShape { public abstract class HSSFShape {
private static final POILogger LOG = POILogFactory.getLogger(HSSFShape.class);
public static final int LINEWIDTH_ONE_PT = 12700; public static final int LINEWIDTH_ONE_PT = 12700;
public static final int LINEWIDTH_DEFAULT = 9525; public static final int LINEWIDTH_DEFAULT = 9525;
public static final int LINESTYLE__COLOR_DEFAULT = 0x08000040; public static final int LINESTYLE__COLOR_DEFAULT = 0x08000040;
@ -362,7 +375,7 @@ public abstract class HSSFShape {
LittleEndian.putInt(property.getPropertyValue(), bos); LittleEndian.putInt(property.getPropertyValue(), bos);
return LittleEndian.getShort(bos.toByteArray(), 2); return LittleEndian.getShort(bos.toByteArray(), 2);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); LOG.log(POILogger.ERROR, "can't determine rotation degree", e);
return 0; return 0;
} }
} }

View File

@ -57,6 +57,8 @@ import org.apache.poi.util.CloseIgnoringInputStream;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.LongField; import org.apache.poi.util.LongField;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/** /**
* <p>This is the main class of the POIFS system; it manages the entire * <p>This is the main class of the POIFS system; it manages the entire
@ -67,8 +69,7 @@ import org.apache.poi.util.LongField;
public class NPOIFSFileSystem extends BlockStore public class NPOIFSFileSystem extends BlockStore
implements POIFSViewable, Closeable implements POIFSViewable, Closeable
{ {
// private static final POILogger _logger = private static final POILogger LOG = POILogFactory.getLogger(NPOIFSFileSystem.class);
// POILogFactory.getLogger(NPOIFSFileSystem.class);
/** /**
* Convenience method for clients that want to avoid the auto-close behaviour of the constructor. * Convenience method for clients that want to avoid the auto-close behaviour of the constructor.
@ -346,7 +347,7 @@ public class NPOIFSFileSystem extends BlockStore
} }
// else not success? Try block did not complete normally // else not success? Try block did not complete normally
// just print stack trace and leave original ex to be thrown // just print stack trace and leave original ex to be thrown
e.printStackTrace(); LOG.log(POILogger.ERROR, "can't close input stream", e);
} }
} }

View File

@ -194,7 +194,7 @@ public class OPOIFSFileSystem
} }
// else not success? Try block did not complete normally // else not success? Try block did not complete normally
// just print stack trace and leave original ex to be thrown // just print stack trace and leave original ex to be thrown
e.printStackTrace(); _logger.log(POILogger.ERROR, "can't close input stream", e);
} }
} }

View File

@ -32,6 +32,8 @@ import java.util.regex.Matcher;
import org.apache.poi.ss.format.CellFormatPart.PartHandler; import org.apache.poi.ss.format.CellFormatPart.PartHandler;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/** /**
* This class implements printing out a value using a number format. * This class implements printing out a value using a number format.
@ -39,6 +41,8 @@ import org.apache.poi.util.LocaleUtil;
* @author Ken Arnold, Industrious Media LLC * @author Ken Arnold, Industrious Media LLC
*/ */
public class CellNumberFormatter extends CellFormatter { public class CellNumberFormatter extends CellFormatter {
private static final POILogger LOG = POILogFactory.getLogger(CellNumberFormatter.class);
private final String desc; private final String desc;
private String printfFmt; private String printfFmt;
private double scale; private double scale;
@ -843,7 +847,7 @@ public class CellNumberFormatter extends CellFormatter {
writeSingleInteger(denominatorFmt, d, output, denominatorSpecials, writeSingleInteger(denominatorFmt, d, output, denominatorSpecials,
mods); mods);
} catch (RuntimeException ignored) { } catch (RuntimeException ignored) {
ignored.printStackTrace(); LOG.log(POILogger.ERROR, "error while fraction evaluation", ignored);
} }
} }

View File

@ -332,7 +332,7 @@ public final class WorkbookEvaluator {
return new NotImplementedException(msg, inner); return new NotImplementedException(msg, inner);
} catch (Exception e) { } catch (Exception e) {
// avoid bombing out during exception handling // avoid bombing out during exception handling
e.printStackTrace(); LOG.log(POILogger.ERROR, "Can't add exception info", e);
return inner; // preserve original exception return inner; // preserve original exception
} }
} }

View File

@ -22,11 +22,15 @@ import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.OperandResolver; import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/** /**
* Implements the Excel Rate function * Implements the Excel Rate function
*/ */
public class Rate implements Function { public class Rate implements Function {
private static final POILogger LOG = POILogFactory.getLogger(Rate.class);
public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) { public ValueEval evaluate(ValueEval[] args, int srcRowIndex, int srcColumnIndex) {
if (args.length < 3) { //First 3 parameters are mandatory if (args.length < 3) { //First 3 parameters are mandatory
return ErrorEval.VALUE_INVALID; return ErrorEval.VALUE_INVALID;
@ -61,7 +65,7 @@ public class Rate implements Function {
checkValue(rate); checkValue(rate);
} catch (EvaluationException e) { } catch (EvaluationException e) {
e.printStackTrace() ; LOG.log(POILogger.ERROR, "Can't evaluate rate function", e);
return e.getErrorEval(); return e.getErrorEval();
} }

View File

@ -46,6 +46,8 @@ import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DateUtil; import org.apache.poi.ss.usermodel.DateUtil;
import org.apache.poi.util.DocumentHelper; import org.apache.poi.util.DocumentHelper;
import org.apache.poi.util.LocaleUtil; import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFMap; import org.apache.poi.xssf.usermodel.XSSFMap;
import org.apache.poi.xssf.usermodel.XSSFRow; import org.apache.poi.xssf.usermodel.XSSFRow;
@ -80,6 +82,7 @@ import org.xml.sax.SAXException;
* </ul> * </ul>
*/ */
public class XSSFExportToXml implements Comparator<String>{ public class XSSFExportToXml implements Comparator<String>{
private static final POILogger LOG = POILogFactory.getLogger(XSSFExportToXml.class);
private XSSFMap map; private XSSFMap map;
@ -265,7 +268,7 @@ public class XSSFExportToXml implements Comparator<String>{
//if no exceptions where raised, the document is valid //if no exceptions where raised, the document is valid
return true; return true;
} catch(IOException e) { } catch(IOException e) {
e.printStackTrace(); LOG.log(POILogger.ERROR, "document is not valid", e);
} }
return false; return false;

View File

@ -24,6 +24,8 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import org.apache.poi.POIXMLDocumentPart; import org.apache.poi.POIXMLDocumentPart;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFactory; import org.apache.poi.xwpf.usermodel.XWPFFactory;
import org.apache.poi.xwpf.usermodel.XWPFFooter; import org.apache.poi.xwpf.usermodel.XWPFFooter;
@ -66,6 +68,8 @@ import com.microsoft.schemas.vml.STTrueFalse;
* the right headers and footers for the document. * the right headers and footers for the document.
*/ */
public class XWPFHeaderFooterPolicy { public class XWPFHeaderFooterPolicy {
private static final POILogger LOG = POILogFactory.getLogger(XWPFHeaderFooterPolicy.class);
public static final Enum DEFAULT = STHdrFtr.DEFAULT; public static final Enum DEFAULT = STHdrFtr.DEFAULT;
public static final Enum EVEN = STHdrFtr.EVEN; public static final Enum EVEN = STHdrFtr.EVEN;
public static final Enum FIRST = STHdrFtr.FIRST; public static final Enum FIRST = STHdrFtr.FIRST;
@ -376,8 +380,7 @@ public class XWPFHeaderFooterPolicy {
pars[0] = getWatermarkParagraph(text, 3); pars[0] = getWatermarkParagraph(text, 3);
createHeader(EVEN, pars); createHeader(EVEN, pars);
} catch (IOException e) { } catch (IOException e) {
// TODO Auto-generated catch block LOG.log(POILogger.ERROR, "error while creating watermark", e);
e.printStackTrace();
} }
} }

View File

@ -52,6 +52,8 @@ import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.util.IOUtils; import org.apache.poi.util.IOUtils;
import org.apache.poi.util.IdentifierManager; import org.apache.poi.util.IdentifierManager;
import org.apache.poi.util.Internal; import org.apache.poi.util.Internal;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
import org.apache.poi.util.PackageHelper; import org.apache.poi.util.PackageHelper;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy; import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.xmlbeans.XmlCursor; import org.apache.xmlbeans.XmlCursor;
@ -88,6 +90,8 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.StylesDocument;
* at some point in your use.</p> * at some point in your use.</p>
*/ */
public class XWPFDocument extends POIXMLDocument implements Document, IBody { public class XWPFDocument extends POIXMLDocument implements Document, IBody {
private static final POILogger LOG = POILogFactory.getLogger(XWPFDocument.class);
protected List<XWPFFooter> footers = new ArrayList<XWPFFooter>(); protected List<XWPFFooter> footers = new ArrayList<XWPFFooter>();
protected List<XWPFHeader> headers = new ArrayList<XWPFHeader>(); protected List<XWPFHeader> headers = new ArrayList<XWPFHeader>();
protected List<XWPFComment> comments = new ArrayList<XWPFComment>(); protected List<XWPFComment> comments = new ArrayList<XWPFComment>();
@ -899,7 +903,7 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
int level = Integer.parseInt(parStyle.substring("Heading".length())); int level = Integer.parseInt(parStyle.substring("Heading".length()));
toc.addRow(level, par.getText(), 1, "112723803"); toc.addRow(level, par.getText(), 1, "112723803");
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
e.printStackTrace(); LOG.log(POILogger.ERROR, "can't format number in TOC heading", e);
} }
} }
} }

View File

@ -25,5 +25,6 @@
#java.lang.Runtime#exit(int) #java.lang.Runtime#exit(int)
#java.lang.Runtime#halt(int) #java.lang.Runtime#halt(int)
@defaultMessage Please do not try to stop the world
java.lang.System#gc() java.lang.System#gc() @ Please do not try to stop the world
java.lang.Throwable#printStackTrace() @ Please use POILogger for exceptions

View File

@ -29,6 +29,8 @@ import org.apache.poi.hslf.exceptions.HSLFException;
import org.apache.poi.hslf.record.StyleTextPropAtom; import org.apache.poi.hslf.record.StyleTextPropAtom;
import org.apache.poi.util.HexDump; import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
/** /**
* For a given run of characters, holds the properties (which could * For a given run of characters, holds the properties (which could
@ -37,6 +39,8 @@ import org.apache.poi.util.LittleEndian;
* properties, and the indent level if required. * properties, and the indent level if required.
*/ */
public class TextPropCollection { public class TextPropCollection {
private static final POILogger LOG = POILogFactory.getLogger(TextPropCollection.class);
/** All the different kinds of paragraph properties we might handle */ /** All the different kinds of paragraph properties we might handle */
private static final TextProp[] paragraphTextPropTypes = { private static final TextProp[] paragraphTextPropTypes = {
// TextProp order is according to 2.9.20 TextPFException, // TextProp order is according to 2.9.20 TextPFException,
@ -376,8 +380,8 @@ public class TextPropCollection {
writeOut(baos); writeOut(baos);
byte[] b = baos.toByteArray(); byte[] b = baos.toByteArray();
out.append(HexDump.dump(b, 0, 0)); out.append(HexDump.dump(b, 0, 0));
} catch (Exception e ) { } catch (IOException e ) {
e.printStackTrace(); LOG.log(POILogger.ERROR, "can't dump TextPropCollection", e);
} }
return out.toString(); return out.toString();

View File

@ -77,8 +77,7 @@ public final class MasterTextPropAtom extends RecordAtom {
try { try {
read(); read();
} catch (Exception e){ } catch (Exception e){
logger.log(POILogger.ERROR, "Failed to parse MasterTextPropAtom: " + e.getMessage()); logger.log(POILogger.ERROR, "Failed to parse MasterTextPropAtom", e);
e.printStackTrace();
} }
} }

View File

@ -25,11 +25,8 @@ import org.apache.poi.util.POILogger;
/** /**
* Ruler of a text as it differs from the style's ruler settings. * Ruler of a text as it differs from the style's ruler settings.
*
* @author Yegor Kozlov
*/ */
public final class TextRulerAtom extends RecordAtom { public final class TextRulerAtom extends RecordAtom {
/** /**
* Record header. * Record header.
*/ */
@ -79,7 +76,6 @@ public final class TextRulerAtom extends RecordAtom {
read(); read();
} catch (Exception e){ } catch (Exception e){
logger.log(POILogger.ERROR, "Failed to parse TextRulerAtom: " + e.getMessage()); logger.log(POILogger.ERROR, "Failed to parse TextRulerAtom: " + e.getMessage());
e.printStackTrace();
} }
} }

View File

@ -48,6 +48,8 @@ import org.apache.poi.util.POILogger;
* @author Yegor Kozlov * @author Yegor Kozlov
*/ */
public final class TxMasterStyleAtom extends RecordAtom { public final class TxMasterStyleAtom extends RecordAtom {
private static final POILogger LOG = POILogFactory.getLogger(TxMasterStyleAtom.class);
/** /**
* Maximum number of indentation levels allowed in PowerPoint documents * Maximum number of indentation levels allowed in PowerPoint documents
*/ */
@ -71,7 +73,7 @@ public final class TxMasterStyleAtom extends RecordAtom {
try { try {
init(); init();
} catch (Exception e){ } catch (Exception e){
POILogFactory.getLogger(TxMasterStyleAtom.class).log(POILogger.WARN, "Exception when reading available styles", e); LOG.log(POILogger.WARN, "Exception when reading available styles", e);
} }
} }

View File

@ -17,13 +17,16 @@
package org.apache.poi.hssf.converter; package org.apache.poi.hssf.converter;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys; import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer; import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory; import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource; import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamResult;
@ -68,8 +71,11 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
* </p> * </p>
* Where infile is an input .xls file ( Word 97-2007) which will be rendered * Where infile is an input .xls file ( Word 97-2007) which will be rendered
* as HTML into outfile * as HTML into outfile
* @throws TransformerException
* @throws Exception
*/ */
public static void main( String[] args ) public static void main( String[] args )
throws IOException, ParserConfigurationException, TransformerException
{ {
if ( args.length < 2 ) if ( args.length < 2 )
{ {
@ -80,25 +86,19 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
System.out.println( "Converting " + args[0] ); System.out.println( "Converting " + args[0] );
System.out.println( "Saving output to " + args[1] ); System.out.println( "Saving output to " + args[1] );
try
{
Document doc = ExcelToHtmlConverter.process( new File( args[0] ) );
DOMSource domSource = new DOMSource( doc ); Document doc = ExcelToHtmlConverter.process( new File( args[0] ) );
StreamResult streamResult = new StreamResult( new File(args[1]) );
TransformerFactory tf = TransformerFactory.newInstance(); DOMSource domSource = new DOMSource( doc );
Transformer serializer = tf.newTransformer(); StreamResult streamResult = new StreamResult( new File(args[1]) );
// TODO set encoding from a command argument
serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" ); TransformerFactory tf = TransformerFactory.newInstance();
serializer.setOutputProperty( OutputKeys.INDENT, "no" ); Transformer serializer = tf.newTransformer();
serializer.setOutputProperty( OutputKeys.METHOD, "html" ); // TODO set encoding from a command argument
serializer.transform( domSource, streamResult ); serializer.setOutputProperty( OutputKeys.ENCODING, "UTF-8" );
} serializer.setOutputProperty( OutputKeys.INDENT, "no" );
catch ( Exception e ) serializer.setOutputProperty( OutputKeys.METHOD, "html" );
{ serializer.transform( domSource, streamResult );
e.printStackTrace();
}
} }
/** /**
@ -107,8 +107,10 @@ public class ExcelToHtmlConverter extends AbstractExcelConverter
* @param xlsFile * @param xlsFile
* file to process * file to process
* @return DOM representation of result HTML * @return DOM representation of result HTML
* @throws IOException
* @throws ParserConfigurationException
*/ */
public static Document process( File xlsFile ) throws Exception public static Document process( File xlsFile ) throws IOException, ParserConfigurationException
{ {
final HSSFWorkbook workbook = ExcelToHtmlUtils.loadXls( xlsFile ); final HSSFWorkbook workbook = ExcelToHtmlUtils.loadXls( xlsFile );
ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter( ExcelToHtmlConverter excelToHtmlConverter = new ExcelToHtmlConverter(

View File

@ -18,6 +18,8 @@
package org.apache.poi.hwpf; package org.apache.poi.hwpf;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import org.apache.poi.hwpf.usermodel.CharacterRun; import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph; import org.apache.poi.hwpf.usermodel.Paragraph;
@ -30,10 +32,8 @@ public final class QuickTest
{ {
} }
public static void main(String[] args) public static void main(String[] args) throws IOException
{ {
try
{
HWPFDocument doc = new HWPFDocument (new FileInputStream (args[0])); HWPFDocument doc = new HWPFDocument (new FileInputStream (args[0]));
Range r = doc.getRange(); Range r = doc.getRange();
@ -81,12 +81,6 @@ public final class QuickTest
// System.out.println(); // System.out.println();
// } // }
// } // }
}
catch (Exception t)
{
t.printStackTrace();
}
} }
} }

View File

@ -156,16 +156,7 @@ public class CHPBinTable
if ( hasChp ) if ( hasChp )
{ {
SprmBuffer newSprmBuffer; SprmBuffer newSprmBuffer = sprmBuffer.clone();
try
{
newSprmBuffer = (SprmBuffer) sprmBuffer.clone();
}
catch ( CloneNotSupportedException e )
{
// shall not happen
throw new Error( e );
}
CHPX chpx = new CHPX( textPiece.getStart(), CHPX chpx = new CHPX( textPiece.getStart(),
textPiece.getEnd(), newSprmBuffer ); textPiece.getEnd(), newSprmBuffer );

View File

@ -277,18 +277,9 @@ public class PAPBinTable
if ( papx.getGrpprl() == null || papx.getGrpprl().length == 0 ) if ( papx.getGrpprl() == null || papx.getGrpprl().length == 0 )
continue; continue;
if ( sprmBuffer == null ) if ( sprmBuffer == null ) {
try sprmBuffer = papx.getSprmBuf().clone();
{ } else {
sprmBuffer = (SprmBuffer) papx.getSprmBuf().clone();
}
catch ( CloneNotSupportedException e )
{
// can't happen
throw new Error( e );
}
else
{
sprmBuffer.append( papx.getGrpprl(), 2 ); sprmBuffer.append( papx.getGrpprl(), 2 );
} }
} }
@ -325,15 +316,7 @@ public class PAPBinTable
PAPX currentPap = _paragraphs.get(listIndex); PAPX currentPap = _paragraphs.get(listIndex);
if (currentPap != null && currentPap.getStart() < cpStart) if (currentPap != null && currentPap.getStart() < cpStart)
{ {
SprmBuffer clonedBuf = null; SprmBuffer clonedBuf = currentPap.getSprmBuf().clone();
try
{
clonedBuf = (SprmBuffer)currentPap.getSprmBuf().clone();
}
catch (CloneNotSupportedException exc)
{
exc.printStackTrace();
}
// Copy the properties of the one before to afterwards // Copy the properties of the one before to afterwards
// Will go: // Will go:

View File

@ -128,13 +128,16 @@ public final class SprmBuffer implements Cloneable
System.arraycopy( grpprl, offset, _buf, _offset, grpprl.length - offset ); System.arraycopy( grpprl, offset, _buf, _offset, grpprl.length - offset );
_offset += grpprl.length - offset; _offset += grpprl.length - offset;
} }
public Object clone() public SprmBuffer clone()
throws CloneNotSupportedException
{ {
SprmBuffer retVal = (SprmBuffer)super.clone(); try {
retVal._buf = new byte[_buf.length]; SprmBuffer retVal = (SprmBuffer)super.clone();
System.arraycopy(_buf, 0, retVal._buf, 0, _buf.length); retVal._buf = new byte[_buf.length];
return retVal; System.arraycopy(_buf, 0, retVal._buf, 0, _buf.length);
return retVal;
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
} }
private void ensureCapacity( int addition ) private void ensureCapacity( int addition )
{ {