diff --git a/src/examples/src/org/apache/poi/ss/examples/formula/SettingExternalFunction.java b/src/examples/src/org/apache/poi/ss/examples/formula/SettingExternalFunction.java index bf4018f3d..b9e1f2d37 100644 --- a/src/examples/src/org/apache/poi/ss/examples/formula/SettingExternalFunction.java +++ b/src/examples/src/org/apache/poi/ss/examples/formula/SettingExternalFunction.java @@ -19,6 +19,12 @@ package org.apache.poi.ss.examples.formula; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; + import org.apache.poi.ss.formula.OperationEvaluationContext; import org.apache.poi.ss.formula.eval.ErrorEval; import org.apache.poi.ss.formula.eval.ValueEval; @@ -29,19 +35,11 @@ import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; -import java.io.FileOutputStream; -import java.io.IOException; -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; - /** * Demonstrates how to use functions provided by third-party add-ins, e.g. Bloomberg Excel Add-in. * * There can be situations when you are not interested in formula evaluation, * you just need to set the formula and the workbook will be evaluation by the client. - * - * @author Yegor Kozlov */ public class SettingExternalFunction { @@ -90,7 +88,8 @@ public class SettingExternalFunction { FileOutputStream out = new FileOutputStream("bloomberg-demo.xlsx"); wb.write(out); out.close(); - + + wb.close(); } } diff --git a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java index af9d62b85..669931f9e 100644 --- a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIDecryptor.java @@ -194,7 +194,7 @@ public class CryptoAPIDecryptor extends Decryptor implements Cloneable { assert(entry.streamName.length() == nameSize); } - fsOut = new POIFSFileSystem(); + fsOut = new POIFSFileSystem(); // NOSONAR for (StreamDescriptorEntry entry : entries) { sbis.seek(entry.streamOffset); sbis.setBlock(entry.block); diff --git a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java index d04096c39..5740380d0 100644 --- a/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java +++ b/src/java/org/apache/poi/poifs/crypt/cryptoapi/CryptoAPIEncryptor.java @@ -126,7 +126,7 @@ public class CryptoAPIEncryptor extends Encryptor implements Cloneable { */ public OutputStream getSummaryEntries(DirectoryNode dir) throws IOException, GeneralSecurityException { - CryptoAPIDocumentOutputStream bos = new CryptoAPIDocumentOutputStream(this); + CryptoAPIDocumentOutputStream bos = new CryptoAPIDocumentOutputStream(this); // NOSONAR byte buf[] = new byte[8]; bos.write(buf, 0, 8); // skip header diff --git a/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java b/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java index 80151146a..b82e61a93 100644 --- a/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java +++ b/src/java/org/apache/poi/poifs/eventfilesystem/POIFSReader.java @@ -19,9 +19,10 @@ package org.apache.poi.poifs.eventfilesystem; -import java.io.*; - -import java.util.*; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Iterator; import org.apache.poi.poifs.filesystem.DocumentInputStream; import org.apache.poi.poifs.filesystem.OPOIFSDocument; @@ -34,6 +35,7 @@ import org.apache.poi.poifs.storage.BlockList; import org.apache.poi.poifs.storage.HeaderBlock; import org.apache.poi.poifs.storage.RawDataBlockList; import org.apache.poi.poifs.storage.SmallBlockTableReader; +import org.apache.poi.util.IOUtils; /** * An event-driven reader for POIFS file systems. Users of this class @@ -42,8 +44,6 @@ import org.apache.poi.poifs.storage.SmallBlockTableReader; * documents. Once all the listeners have been registered, the read() * method is called, which results in the listeners being notified as * their documents are read. - * - * @author Marc Johnson (mjohnson at apache dot org) */ public class POIFSReader @@ -190,20 +190,19 @@ public class POIFSReader { if (args.length == 0) { - System.err - .println("at least one argument required: input filename(s)"); + System.err.println("at least one argument required: input filename(s)"); System.exit(1); } // register for all - for (int j = 0; j < args.length; j++) + for (String arg : args) { POIFSReader reader = new POIFSReader(); POIFSReaderListener listener = new SampleListener(); reader.registerListener(listener); - System.out.println("reading " + args[ j ]); - FileInputStream istream = new FileInputStream(args[ j ]); + System.out.println("reading " + arg); + FileInputStream istream = new FileInputStream(arg); reader.read(istream); istream.close(); @@ -300,31 +299,25 @@ public class POIFSReader * @param event */ - public void processPOIFSReaderEvent(final POIFSReaderEvent event) - { - @SuppressWarnings("resource") + @Override + public void processPOIFSReaderEvent(final POIFSReaderEvent event) { DocumentInputStream istream = event.getStream(); POIFSDocumentPath path = event.getPath(); String name = event.getName(); - try - { - byte[] data = new byte[ istream.available() ]; - - istream.read(data); + try { + byte[] data = IOUtils.toByteArray(istream); int pathLength = path.length(); - for (int k = 0; k < pathLength; k++) - { + for (int k = 0; k < pathLength; k++) { System.out.print("/" + path.getComponent(k)); } - System.out.println("/" + name + ": " + data.length - + " bytes read"); - } - catch (IOException ignored) - { + System.out.println("/" + name + ": " + data.length + " bytes read"); + } catch (IOException ignored) { + } finally { + IOUtils.closeQuietly(istream); } } - } // end private class SampleListener -} // end public class POIFSReader + } +} diff --git a/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java b/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java index cfd6cf991..ef0cc0847 100644 --- a/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java +++ b/src/java/org/apache/poi/sl/draw/DrawSimpleShape.java @@ -25,7 +25,6 @@ import java.awt.geom.AffineTransform; import java.awt.geom.Ellipse2D; import java.awt.geom.Path2D; import java.awt.geom.Rectangle2D; -import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.Collection; @@ -53,6 +52,7 @@ import org.apache.poi.sl.usermodel.LineDecoration.DecorationSize; import org.apache.poi.sl.usermodel.PaintStyle.SolidPaint; import org.apache.poi.sl.usermodel.Shadow; import org.apache.poi.sl.usermodel.SimpleShape; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.Units; @@ -110,15 +110,21 @@ public class DrawSimpleShape extends DrawShape { } protected void drawDecoration(Graphics2D graphics, Paint line, BasicStroke stroke) { - if(line == null) return; + if(line == null) { + return; + } graphics.setPaint(line); List lst = new ArrayList(); LineDecoration deco = getShape().getLineDecoration(); Outline head = getHeadDecoration(graphics, deco, stroke); - if (head != null) lst.add(head); + if (head != null) { + lst.add(head); + } Outline tail = getTailDecoration(graphics, deco, stroke); - if (tail != null) lst.add(tail); + if (tail != null) { + lst.add(tail); + } for(Outline o : lst){ @@ -126,8 +132,12 @@ public class DrawSimpleShape extends DrawShape { Path p = o.getPath(); graphics.setRenderingHint(Drawable.GRADIENT_SHAPE, s); - if(p.isFilled()) graphics.fill(s); - if(p.isStroked()) graphics.draw(s); + if(p.isFilled()) { + graphics.fill(s); + } + if(p.isStroked()) { + graphics.draw(s); + } } } @@ -221,8 +231,7 @@ public class DrawSimpleShape extends DrawShape { double lineWidth = Math.max(2.5, stroke.getLineWidth()); Rectangle2D anchor = getAnchor(graphics, getShape()); - double x1 = anchor.getX(), - y1 = anchor.getY(); + double x1 = anchor.getX(), y1 = anchor.getY(); double alpha = Math.atan(anchor.getHeight() / anchor.getWidth()); @@ -283,13 +292,15 @@ public class DrawSimpleShape extends DrawShape { } protected void drawShadow( - Graphics2D graphics - , Collection outlines - , Paint fill - , Paint line + Graphics2D graphics + , Collection outlines + , Paint fill + , Paint line ) { Shadow shadow = getShape().getShadow(); - if (shadow == null || (fill == null && line == null)) return; + if (shadow == null || (fill == null && line == null)) { + return; + } SolidPaint shadowPaint = shadow.getFillStyle(); Color shadowColor = DrawPaint.applyColorTransform(shadowPaint.getSolidColor()); @@ -373,11 +384,7 @@ public class DrawSimpleShape extends DrawShape { } catch (Exception e) { throw new RuntimeException("Unable to load preset geometries.", e); } finally { - try { - presetIS.close(); - } catch (IOException e) { - throw new RuntimeException("Unable to load preset geometries.", e); - } + IOUtils.closeQuietly(presetIS); } } diff --git a/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java b/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java index db23e1de0..b61d55daf 100644 --- a/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java +++ b/src/java/org/apache/poi/sl/usermodel/SlideShowFactory.java @@ -16,16 +16,20 @@ ==================================================================== */ package org.apache.poi.sl.usermodel; -import java.io.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.PushbackInputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import org.apache.poi.EncryptedDocumentException; import org.apache.poi.OldFileFormatException; -import org.apache.poi.poifs.filesystem.DocumentFactoryHelper; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.filesystem.DirectoryNode; +import org.apache.poi.poifs.filesystem.DocumentFactoryHelper; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.util.IOUtils; @@ -66,7 +70,7 @@ public class SlideShowFactory { return createXSLFSlideShow(stream); } finally { - if (stream != null) stream.close(); + IOUtils.closeQuietly(stream); } } @@ -211,14 +215,10 @@ public class SlideShowFactory { fs = new NPOIFSFileSystem(file, readOnly); return create(fs, password); } catch(OfficeXmlFileException e) { - if(fs != null) { - fs.close(); - } + IOUtils.closeQuietly(fs); return createXSLFSlideShow(file, readOnly); } catch(RuntimeException e) { - if(fs != null) { - fs.close(); - } + IOUtils.closeQuietly(fs); throw e; } } diff --git a/src/java/org/apache/poi/ss/formula/FormulaUsedBlankCellSet.java b/src/java/org/apache/poi/ss/formula/FormulaUsedBlankCellSet.java index e054f125d..a07cb9c70 100644 --- a/src/java/org/apache/poi/ss/formula/FormulaUsedBlankCellSet.java +++ b/src/java/org/apache/poi/ss/formula/FormulaUsedBlankCellSet.java @@ -25,9 +25,7 @@ import java.util.Map; import org.apache.poi.ss.util.CellReference; /** - * Optimisation - compacts many blank cell references used by a single formula. - * - * @author Josh Micich + * Optimization - compacts many blank cell references used by a single formula. */ final class FormulaUsedBlankCellSet { public static final class BookSheetKey { @@ -39,11 +37,15 @@ final class FormulaUsedBlankCellSet { _bookIndex = bookIndex; _sheetIndex = sheetIndex; } - public int hashCode() { + @Override + public int hashCode() { return _bookIndex * 17 + _sheetIndex; } - public boolean equals(Object obj) { - assert obj instanceof BookSheetKey : "these private cache key instances are only compared to themselves"; + @Override + public boolean equals(Object obj) { + if (!(obj instanceof BookSheetKey)) { + return false; + } BookSheetKey other = (BookSheetKey) obj; return _bookIndex == other._bookIndex && _sheetIndex == other._sheetIndex; } @@ -148,7 +150,8 @@ final class FormulaUsedBlankCellSet { _lastRowIndex = rowIndex; return true; } - public String toString() { + @Override + public String toString() { StringBuffer sb = new StringBuffer(64); CellReference crA = new CellReference(_firstRowIndex, _firstColumnIndex, false, false); CellReference crB = new CellReference(_lastRowIndex, _lastColumnIndex, false, false); diff --git a/src/java/org/apache/poi/ss/formula/PlainCellCache.java b/src/java/org/apache/poi/ss/formula/PlainCellCache.java index f422b9604..2514c0711 100644 --- a/src/java/org/apache/poi/ss/formula/PlainCellCache.java +++ b/src/java/org/apache/poi/ss/formula/PlainCellCache.java @@ -20,10 +20,6 @@ package org.apache.poi.ss.formula; import java.util.HashMap; import java.util.Map; -/** - * - * @author Josh Micich - */ final class PlainCellCache { public static final class Loc { @@ -48,12 +44,16 @@ final class PlainCellCache { _rowIndex = rowIndex; } - public int hashCode() { + @Override + public int hashCode() { return (int)(_bookSheetColumn ^ (_bookSheetColumn >>> 32)) + 17 * _rowIndex; } - public boolean equals(Object obj) { - assert obj instanceof Loc : "these package-private cache key instances are only compared to themselves"; + @Override + public boolean equals(Object obj) { + if (!(obj instanceof Loc)) { + return false; + } Loc other = (Loc) obj; return _bookSheetColumn == other._bookSheetColumn && _rowIndex == other._rowIndex; } diff --git a/src/java/org/apache/poi/ss/formula/atp/YearFracCalculator.java b/src/java/org/apache/poi/ss/formula/atp/YearFracCalculator.java index 42bd9aed1..473c22536 100644 --- a/src/java/org/apache/poi/ss/formula/atp/YearFracCalculator.java +++ b/src/java/org/apache/poi/ss/formula/atp/YearFracCalculator.java @@ -29,8 +29,6 @@ import org.apache.poi.util.LocaleUtil; * Internal calculation methods for Excel 'Analysis ToolPak' function YEARFRAC()
* * Algorithm inspired by www.dwheeler.com/yearfrac - * - * @author Josh Micich */ final class YearFracCalculator { private static final int MS_PER_HOUR = 60 * 60 * 1000; @@ -212,25 +210,21 @@ final class YearFracCalculator { * @return true if dates both within a leap year, or span a period including Feb 29 */ private static boolean shouldCountFeb29(SimpleDate start, SimpleDate end) { - boolean startIsLeapYear = isLeapYear(start.year); - if (startIsLeapYear && start.year == end.year) { - // note - dates may not actually span Feb-29, but it gets counted anyway in this case - return true; - } + if (isLeapYear(start.year)) { + if (start.year == end.year) { + // note - dates may not actually span Feb-29, but it gets counted anyway in this case + return true; + } - boolean endIsLeapYear = isLeapYear(end.year); - if (!startIsLeapYear && !endIsLeapYear) { - return false; - } - if (startIsLeapYear) { - switch (start.month) { + switch (start.month) { case SimpleDate.JANUARY: case SimpleDate.FEBRUARY: return true; } return false; } - if (endIsLeapYear) { + + if (isLeapYear(end.year)) { switch (end.month) { case SimpleDate.JANUARY: return false; diff --git a/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationSheet.java b/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationSheet.java index 072a777e1..abd3b9925 100644 --- a/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationSheet.java +++ b/src/java/org/apache/poi/ss/formula/eval/forked/ForkedEvaluationSheet.java @@ -37,8 +37,6 @@ import org.apache.poi.util.Internal; * will be used in all subsequent evaluations.
* * For POI internal use only - * - * @author Josh Micich */ @Internal final class ForkedEvaluationSheet implements EvaluationSheet { @@ -128,7 +126,9 @@ final class ForkedEvaluationSheet implements EvaluationSheet { } @Override public boolean equals(Object obj) { - assert obj instanceof RowColKey : "these private cache key instances are only compared to themselves"; + if (!(obj instanceof RowColKey)) { + return false; + } RowColKey other = (RowColKey) obj; return _rowIndex == other._rowIndex && _columnIndex == other._columnIndex; } @@ -136,6 +136,7 @@ final class ForkedEvaluationSheet implements EvaluationSheet { public int hashCode() { return _rowIndex ^ _columnIndex; } + @Override public int compareTo(RowColKey o) { int cmp = _rowIndex - o._rowIndex; if (cmp != 0) { diff --git a/src/java/org/apache/poi/ss/formula/functions/Countif.java b/src/java/org/apache/poi/ss/formula/functions/Countif.java index f2a7ebe3a..04328d28d 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Countif.java +++ b/src/java/org/apache/poi/ss/formula/functions/Countif.java @@ -136,6 +136,7 @@ public final class Countif extends Fixed2ArgFunction { throw new RuntimeException("Cannot call boolean evaluate on non-equality operator '" + _representation + "'"); } + @Override public String toString() { StringBuffer sb = new StringBuffer(64); sb.append(getClass().getName()); @@ -187,6 +188,7 @@ public final class Countif extends Fixed2ArgFunction { return String.valueOf(_value); } + @Override public boolean matches(ValueEval x) { double testValue; if(x instanceof StringEval) { @@ -246,22 +248,21 @@ public final class Countif extends Fixed2ArgFunction { return value ? 1 : 0; } + @Override public boolean matches(ValueEval x) { int testValue; if(x instanceof StringEval) { - if (true) { // change to false to observe more intuitive behaviour - // Note - Unlike with numbers, it seems that COUNTIF never matches - // boolean values when the target(x) is a string - return false; - } - @SuppressWarnings("unused") - StringEval se = (StringEval)x; - Boolean val = parseBoolean(se.getStringValue()); - if(val == null) { - // x is text that is not a boolean - return false; - } - testValue = boolToInt(val.booleanValue()); + // Note - Unlike with numbers, it seems that COUNTIF never matches + // boolean values when the target(x) is a string + return false; + // uncomment to observe more intuitive behaviour + // StringEval se = (StringEval)x; + // Boolean val = parseBoolean(se.getStringValue()); + // if(val == null) { + // // x is text that is not a boolean + // return false; + // } + // testValue = boolToInt(val.booleanValue()); } else if((x instanceof BoolEval)) { BoolEval be = (BoolEval) x; testValue = boolToInt(be.getBooleanValue()); @@ -300,6 +301,7 @@ public final class Countif extends Fixed2ArgFunction { return FormulaError.forInt(_value).getString(); } + @Override public boolean matches(ValueEval x) { if(x instanceof ErrorEval) { int testValue = ((ErrorEval)x).getErrorCode(); @@ -339,6 +341,7 @@ public final class Countif extends Fixed2ArgFunction { return _pattern.pattern(); } + @Override public boolean matches(ValueEval x) { if (x instanceof BlankEval) { switch(getCode()) { @@ -433,6 +436,7 @@ public final class Countif extends Fixed2ArgFunction { } } + @Override public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { I_MatchPredicate mp = createCriteriaPredicate(arg1, srcRowIndex, srcColumnIndex); @@ -525,13 +529,27 @@ public final class Countif extends Fixed2ArgFunction { if (value.length() < 4 || value.charAt(0) != '#') { return null; } - if (value.equals("#NULL!")) return ErrorEval.NULL_INTERSECTION; - if (value.equals("#DIV/0!")) return ErrorEval.DIV_ZERO; - if (value.equals("#VALUE!")) return ErrorEval.VALUE_INVALID; - if (value.equals("#REF!")) return ErrorEval.REF_INVALID; - if (value.equals("#NAME?")) return ErrorEval.NAME_INVALID; - if (value.equals("#NUM!")) return ErrorEval.NUM_ERROR; - if (value.equals("#N/A")) return ErrorEval.NA; + if (value.equals("#NULL!")) { + return ErrorEval.NULL_INTERSECTION; + } + if (value.equals("#DIV/0!")) { + return ErrorEval.DIV_ZERO; + } + if (value.equals("#VALUE!")) { + return ErrorEval.VALUE_INVALID; + } + if (value.equals("#REF!")) { + return ErrorEval.REF_INVALID; + } + if (value.equals("#NAME?")) { + return ErrorEval.NAME_INVALID; + } + if (value.equals("#NUM!")) { + return ErrorEval.NUM_ERROR; + } + if (value.equals("#N/A")) { + return ErrorEval.NA; + } return null; } diff --git a/src/java/org/apache/poi/ss/formula/ptg/Ptg.java b/src/java/org/apache/poi/ss/formula/ptg/Ptg.java index bb6fb64fb..73885703b 100644 --- a/src/java/org/apache/poi/ss/formula/ptg/Ptg.java +++ b/src/java/org/apache/poi/ss/formula/ptg/Ptg.java @@ -35,10 +35,6 @@ import org.apache.poi.util.LittleEndianOutput; * parsed formula. However, in BIFF files Ptgs are written/read in * Reverse-Polish Notation order. The RPN ordering also simplifies formula * evaluation logic, so POI mostly accesses Ptgs in the same way. - * - * @author andy - * @author avik - * @author Jason Height (jheight at chariot dot net dot au) */ public abstract class Ptg { public static final Ptg[] EMPTY_PTG_ARRAY = { }; @@ -201,8 +197,8 @@ public abstract class Ptg { * @return number of bytes written */ public static int serializePtgs(Ptg[] ptgs, byte[] array, int offset) { - LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(array, offset); - + LittleEndianByteArrayOutputStream out = new LittleEndianByteArrayOutputStream(array, offset); // NOSONAR + List arrayPtgs = null; for (Ptg ptg : ptgs) { diff --git a/src/java/org/apache/poi/ss/util/CellReference.java b/src/java/org/apache/poi/ss/util/CellReference.java index c07ee5bf9..f530a3783 100644 --- a/src/java/org/apache/poi/ss/util/CellReference.java +++ b/src/java/org/apache/poi/ss/util/CellReference.java @@ -323,6 +323,7 @@ public class CellReference { /** * @deprecated 3.15 beta 2. Use {@link #isColumnWithinRange}. */ + @Deprecated public static boolean isColumnWithnRange(String colStr, SpreadsheetVersion ssVersion) { return isColumnWithinRange(colStr, ssVersion); } @@ -352,6 +353,7 @@ public class CellReference { /** * @deprecated 3.15 beta 2. Use {@link #isRowWithinRange} */ + @Deprecated public static boolean isRowWithnRange(String rowStr, SpreadsheetVersion ssVersion) { return isRowWithinRange(rowStr, ssVersion); } @@ -388,7 +390,9 @@ public class CellReference { final String sheetName = parseSheetName(reference, plingPos); String cell = reference.substring(plingPos+1).toUpperCase(Locale.ROOT); Matcher matcher = CELL_REF_PATTERN.matcher(cell); - if (!matcher.matches()) throw new IllegalArgumentException("Invalid CellReference: " + reference); + if (!matcher.matches()) { + throw new IllegalArgumentException("Invalid CellReference: " + reference); + } String col = matcher.group(1); String row = matcher.group(2); @@ -430,13 +434,11 @@ public class CellReference { sb.append(ch); continue; } - if(i < lastQuotePos) { - if(reference.charAt(i+1) == SPECIAL_NAME_DELIMITER) { - // two consecutive quotes is the escape sequence for a single one - i++; // skip this and keep parsing the special name - sb.append(ch); - continue; - } + if(i+1 < lastQuotePos && reference.charAt(i+1) == SPECIAL_NAME_DELIMITER) { + // two consecutive quotes is the escape sequence for a single one + i++; // skip this and keep parsing the special name + sb.append(ch); + continue; } throw new IllegalArgumentException("Bad sheet name quote escaping: (" + reference + ")"); } diff --git a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/facets/XAdESXLSignatureFacet.java b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/facets/XAdESXLSignatureFacet.java index d25bd7d1b..783b16cfd 100644 --- a/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/facets/XAdESXLSignatureFacet.java +++ b/src/ooxml/java/org/apache/poi/poifs/crypt/dsig/facets/XAdESXLSignatureFacet.java @@ -48,6 +48,7 @@ import java.util.UUID; import javax.xml.crypto.MarshalException; import org.apache.poi.poifs.crypt.dsig.services.RevocationData; +import org.apache.poi.util.IOUtils; import org.apache.poi.util.POILogFactory; import org.apache.poi.util.POILogger; import org.apache.xml.security.c14n.Canonicalizer; @@ -62,28 +63,7 @@ import org.bouncycastle.asn1.x509.Extension; import org.bouncycastle.cert.ocsp.BasicOCSPResp; import org.bouncycastle.cert.ocsp.OCSPResp; import org.bouncycastle.cert.ocsp.RespID; -import org.etsi.uri.x01903.v13.CRLIdentifierType; -import org.etsi.uri.x01903.v13.CRLRefType; -import org.etsi.uri.x01903.v13.CRLRefsType; -import org.etsi.uri.x01903.v13.CRLValuesType; -import org.etsi.uri.x01903.v13.CertIDListType; -import org.etsi.uri.x01903.v13.CertIDType; -import org.etsi.uri.x01903.v13.CertificateValuesType; -import org.etsi.uri.x01903.v13.CompleteCertificateRefsType; -import org.etsi.uri.x01903.v13.CompleteRevocationRefsType; -import org.etsi.uri.x01903.v13.DigestAlgAndValueType; -import org.etsi.uri.x01903.v13.EncapsulatedPKIDataType; -import org.etsi.uri.x01903.v13.OCSPIdentifierType; -import org.etsi.uri.x01903.v13.OCSPRefType; -import org.etsi.uri.x01903.v13.OCSPRefsType; -import org.etsi.uri.x01903.v13.OCSPValuesType; -import org.etsi.uri.x01903.v13.QualifyingPropertiesDocument; -import org.etsi.uri.x01903.v13.QualifyingPropertiesType; -import org.etsi.uri.x01903.v13.ResponderIDType; -import org.etsi.uri.x01903.v13.RevocationValuesType; -import org.etsi.uri.x01903.v13.UnsignedPropertiesType; -import org.etsi.uri.x01903.v13.UnsignedSignaturePropertiesType; -import org.etsi.uri.x01903.v13.XAdESTimeStampType; +import org.etsi.uri.x01903.v13.*; import org.etsi.uri.x01903.v14.ValidationDataType; import org.w3.x2000.x09.xmldsig.CanonicalizationMethodType; import org.w3c.dom.Document; @@ -341,8 +321,8 @@ public class XAdESXLSignatureFacet extends SignatureFacet { ASN1Integer integer = (ASN1Integer)asn1IS2.readObject(); return integer.getPositiveValue(); } finally { - asn1IS2.close(); - asn1IS1.close(); + IOUtils.closeQuietly(asn1IS2); + IOUtils.closeQuietly(asn1IS1); } } catch (IOException e) { throw new RuntimeException("I/O error: " + e.getMessage(), e); diff --git a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java index 621e71d8d..83b1e8609 100644 --- a/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java +++ b/src/ooxml/java/org/apache/poi/ss/usermodel/WorkbookFactory.java @@ -16,11 +16,14 @@ ==================================================================== */ package org.apache.poi.ss.usermodel; -import java.io.*; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.PushbackInputStream; import org.apache.poi.EmptyFileException; import org.apache.poi.EncryptedDocumentException; -import org.apache.poi.poifs.filesystem.DocumentFactoryHelper; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; @@ -28,6 +31,7 @@ import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.filesystem.DirectoryNode; +import org.apache.poi.poifs.filesystem.DocumentFactoryHelper; import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; import org.apache.poi.poifs.filesystem.OfficeXmlFileException; import org.apache.poi.poifs.filesystem.POIFSFileSystem; @@ -252,8 +256,7 @@ public class WorkbookFactory { return create(fs, password); } catch (RuntimeException e) { // ensure that the file-handle is closed again - fs.close(); - + IOUtils.closeQuietly(fs); throw e; } } catch(OfficeXmlFileException e) { @@ -261,20 +264,18 @@ public class WorkbookFactory { OPCPackage pkg = OPCPackage.open(file, readOnly ? PackageAccess.READ : PackageAccess.READ_WRITE); try { return new XSSFWorkbook(pkg); - } catch (IOException ioe) { - // ensure that file handles are closed (use revert() to not re-write the file) + } catch (Exception ioe) { + // ensure that file handles are closed - use revert() to not re-write the file pkg.revert(); - //pkg.close(); + // do not pkg.close(); - // rethrow exception - throw ioe; - } catch (RuntimeException ioe) { - // ensure that file handles are closed (use revert() to not re-write the file) - pkg.revert(); - //pkg.close(); - - // rethrow exception - throw ioe; + if (ioe instanceof IOException) { + throw (IOException)ioe; + } else if (ioe instanceof RuntimeException) { + throw (RuntimeException)ioe; + } else { + throw new IOException(ioe); + } } } }