From 928e6937f40754450a42ad3c5a106908bd083105 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Alain=20B=C3=A9arez?= Date: Tue, 21 May 2019 00:14:12 +0000 Subject: [PATCH] fix result of multiplication cast to wider type git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1859595 13f79535-47bb-0310-9956-ffa450edef68 --- .../hssf/usermodel/examples/BigExample.java | 6 +- .../usermodel/examples/HSSFReadWrite.java | 4 +- .../poi/poifs/filesystem/POIFSFileSystem.java | 6 +- .../ss/formula/atp/YearFracCalculator.java | 22 ++--- .../poi/ss/formula/functions/Days360.java | 28 ++++--- .../org/apache/poi/ss/usermodel/DateUtil.java | 81 ++++++++++--------- .../ss/usermodel/ExcelStyleDateFormatter.java | 26 +++--- .../org/apache/poi/ss/util/ImageUtils.java | 42 ++++++---- .../apache/poi/ss/util/cellwalk/CellWalk.java | 7 +- src/java/org/apache/poi/util/StringUtil.java | 2 +- .../poi/hemf/record/emf/HemfComment.java | 10 ++- .../poi/hpbf/model/qcbits/QCTextBit.java | 2 +- .../poi/hslf/record/CurrentUserAtom.java | 36 ++++----- .../poi/hslf/record/MasterTextPropAtom.java | 12 +-- .../apache/poi/hslf/record/TextCharsAtom.java | 2 +- .../poi/hslf/record/TextSpecInfoRun.java | 59 ++++++++------ .../org/apache/poi/hwmf/record/HwmfDraw.java | 11 ++- .../hwpf/converter/WordToHtmlConverter.java | 20 ++--- .../org/apache/poi/hwpf/sprm/SprmUtils.java | 4 +- .../poi/hwpf/sprm/TableSprmCompressor.java | 4 +- 20 files changed, 216 insertions(+), 168 deletions(-) diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/BigExample.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/BigExample.java index d00bd9c87c..a1e84efba5 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/BigExample.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/BigExample.java @@ -102,9 +102,9 @@ public class BigExample { // create a numeric cell c = r.createCell(cellnum); // do some goofy math to demonstrate decimals - c.setCellValue(rownum * 10000 + cellnum - + (((double) rownum / 1000) - + ((double) cellnum / 10000))); + c.setCellValue((rownum * 10000.0) + cellnum + + (rownum / 1000.0) + + (cellnum / 10000.0)); // on every other row if ((rownum % 2) == 0) { diff --git a/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java b/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java index 6e6fc5b369..0484d6feef 100644 --- a/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java +++ b/src/examples/src/org/apache/poi/hssf/usermodel/examples/HSSFReadWrite.java @@ -87,8 +87,8 @@ public final class HSSFReadWrite { for (int cellnum = 0; cellnum < 50; cellnum += 2) { HSSFCell c = r.createCell(cellnum); - c.setCellValue(rownum * 10000 + cellnum - + (((double) rownum / 1000) + ((double) cellnum / 10000))); + c.setCellValue((rownum * 10000.0) + cellnum + + (rownum / 1000.0) + (cellnum / 10000.0)); if ((rownum % 2) == 0) { c.setCellStyle(cs); } diff --git a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java index e68f5d9686..12e408ee63 100644 --- a/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java +++ b/src/java/org/apache/poi/poifs/filesystem/POIFSFileSystem.java @@ -759,6 +759,7 @@ public class POIFSFileSystem extends BlockStore * and buffers. After this, you will be unable to read or * write from the FileSystem. */ + @Override public void close() throws IOException { _data.close(); } @@ -834,6 +835,7 @@ public class POIFSFileSystem extends BlockStore * * @return an array of Object; may not be null, but may be empty */ + @Override public Object[] getViewableArray() { if (preferArray()) { return getRoot().getViewableArray(); @@ -850,6 +852,7 @@ public class POIFSFileSystem extends BlockStore * back end store */ + @Override public Iterator getViewableIterator() { if (!preferArray()) { return getRoot().getViewableIterator(); @@ -866,6 +869,7 @@ public class POIFSFileSystem extends BlockStore * a viewer should call getViewableIterator */ + @Override public boolean preferArray() { return getRoot().preferArray(); } @@ -877,6 +881,7 @@ public class POIFSFileSystem extends BlockStore * @return short description */ + @Override public String getShortDescription() { return "POIFS FileSystem"; } @@ -949,4 +954,3 @@ public class POIFSFileSystem extends BlockStore } } - 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 52cdbd7e0b..7a31656aed 100644 --- a/src/java/org/apache/poi/ss/formula/atp/YearFracCalculator.java +++ b/src/java/org/apache/poi/ss/formula/atp/YearFracCalculator.java @@ -27,7 +27,7 @@ import org.apache.poi.util.LocaleUtil; /** * Internal calculation methods for Excel 'Analysis ToolPak' function YEARFRAC()
- * + * * Algorithm inspired by www.dwheeler.com/yearfrac */ final class YearFracCalculator { @@ -61,7 +61,7 @@ final class YearFracCalculator { int startDateVal = (int) Math.floor(pStartDateVal); int endDateVal = (int) Math.floor(pEndDateVal); if (startDateVal == endDateVal) { - // when dates are equal, result is zero + // when dates are equal, result is zero return 0; } // swap start and end if out of order @@ -92,7 +92,7 @@ final class YearFracCalculator { int date1day = startDate.day; int date2day = endDate.day; - // basis zero has funny adjustments to the day-of-month fields when at end-of-month + // basis zero has funny adjustments to the day-of-month fields when at end-of-month if (date1day == LONG_MONTH_LEN && date2day == LONG_MONTH_LEN) { date1day = SHORT_MONTH_LEN; date2day = SHORT_MONTH_LEN; @@ -155,7 +155,7 @@ final class YearFracCalculator { int date2day = endDate.day; - // basis four has funny adjustments to the day-of-month fields when at end-of-month + // basis four has funny adjustments to the day-of-month fields when at end-of-month if (date1day == LONG_MONTH_LEN) { date1day = SHORT_MONTH_LEN; } @@ -169,10 +169,10 @@ final class YearFracCalculator { private static double calculateAdjusted(SimpleDate startDate, SimpleDate endDate, int date1day, int date2day) { - double dayCount - = (endDate.year - startDate.year) * 360 - + (endDate.month - startDate.month) * SHORT_MONTH_LEN - + (date2day - date1day) * 1; + double dayCount + = (endDate.year - startDate.year) * 360.0 + + (endDate.month - startDate.month) * (double)SHORT_MONTH_LEN + + (date2day - date1day) * 1.0; return dayCount / 360; } @@ -223,7 +223,7 @@ final class YearFracCalculator { } return false; } - + if (isLeapYear(end.year)) { switch (end.month) { case SimpleDate.JANUARY: @@ -245,14 +245,14 @@ final class YearFracCalculator { private static int dateDiff(long startDateMS, long endDateMS) { long msDiff = endDateMS - startDateMS; - // some extra checks to make sure we don't hide some other bug with the rounding + // some extra checks to make sure we don't hide some other bug with the rounding int remainderHours = (int) ((msDiff % MS_PER_DAY) / MS_PER_HOUR); switch (remainderHours) { case 0: // normal case break; case 1: // transition from normal time to daylight savings adjusted case 23: // transition from daylight savings adjusted to normal time - // Unexpected since we are using UTC_TIME_ZONE + // Unexpected since we are using UTC_TIME_ZONE default: throw new RuntimeException("Unexpected date diff between " + startDateMS + " and " + endDateMS); diff --git a/src/java/org/apache/poi/ss/formula/functions/Days360.java b/src/java/org/apache/poi/ss/formula/functions/Days360.java index 6a5dabcf49..e86b23be89 100644 --- a/src/java/org/apache/poi/ss/formula/functions/Days360.java +++ b/src/java/org/apache/poi/ss/formula/functions/Days360.java @@ -30,17 +30,17 @@ import org.apache.poi.util.LocaleUtil; * (twelve 30-day months), which is used in some accounting calculations. Use * this function to help compute payments if your accounting system is based on * twelve 30-day months.

- * + * * {@code DAYS360(start_date,end_date,[method])} - * + * *

- * + * * @see DAYS360 Function Produces Different Values Depending on the Version of Excel */ public class Days360 extends Var2or3ArgFunction { + @Override public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1) { try { double d0 = NumericFunction.singleOperandEvaluate(arg0, srcRowIndex, srcColumnIndex); @@ -76,6 +77,7 @@ public class Days360 extends Var2or3ArgFunction { } } + @Override public ValueEval evaluate(int srcRowIndex, int srcColumnIndex, ValueEval arg0, ValueEval arg1, ValueEval arg2) { try { @@ -95,8 +97,8 @@ public class Days360 extends Var2or3ArgFunction { int[] startingDate = getStartingDate(realStart, method); int[] endingDate = getEndingDate(realEnd, startingDate, method); return - (endingDate[0]*360+endingDate[1]*30+endingDate[2])- - (startingDate[0]*360+startingDate[1]*30+startingDate[2]); + (endingDate[0]*360.0+endingDate[1]*30.0+endingDate[2])- + (startingDate[0]*360.0+startingDate[1]*30.0+startingDate[2]); } private static Calendar getDate(double date) { @@ -109,9 +111,11 @@ public class Days360 extends Var2or3ArgFunction { int yyyy = realStart.get(Calendar.YEAR); int mm = realStart.get(Calendar.MONTH); int dd = Math.min(30, realStart.get(Calendar.DAY_OF_MONTH)); - - if (!method && isLastDayOfMonth(realStart)) dd = 30; - + + if (!method && isLastDayOfMonth(realStart)) { + dd = 30; + } + return new int[]{yyyy,mm,dd}; } @@ -134,7 +138,7 @@ public class Days360 extends Var2or3ArgFunction { return new int[]{yyyy,mm,dd}; } - + private static boolean isLastDayOfMonth(Calendar date) { int dayOfMonth = date.get(Calendar.DAY_OF_MONTH); int lastDayOfMonth = date.getActualMaximum(Calendar.DAY_OF_MONTH); diff --git a/src/java/org/apache/poi/ss/usermodel/DateUtil.java b/src/java/org/apache/poi/ss/usermodel/DateUtil.java index a46a94c684..fbf34b7ae7 100644 --- a/src/java/org/apache/poi/ss/usermodel/DateUtil.java +++ b/src/java/org/apache/poi/ss/usermodel/DateUtil.java @@ -62,7 +62,7 @@ public class DateUtil { private static final Pattern date_ptrn3b = Pattern.compile("^[\\[\\]yYmMdDhHsS\\-T/\u5e74\u6708\u65e5,. :\"\\\\]+0*[ampAMP/]*$"); // elapsed time patterns: [h],[m] and [s] private static final Pattern date_ptrn4 = Pattern.compile("^\\[([hH]+|[mM]+|[sS]+)\\]"); - + // for format which start with "[DBNum1]" or "[DBNum2]" or "[DBNum3]" could be a Chinese date private static final Pattern date_ptrn5 = Pattern.compile("^\\[DBNum(1|2|3)\\]"); @@ -124,11 +124,11 @@ public class DateUtil { // be 4 hours. // E.g. 2004-03-28 04:00 CEST - 2004-03-28 00:00 CET is 3 hours // and 2004-10-31 04:00 CET - 2004-10-31 00:00 CEST is 5 hours - double fraction = (((date.get(Calendar.HOUR_OF_DAY) * 60 + double fraction = (((date.get(Calendar.HOUR_OF_DAY) * 60.0 + date.get(Calendar.MINUTE) - ) * 60 + date.get(Calendar.SECOND) - ) * 1000 + date.get(Calendar.MILLISECOND) - ) / ( double ) DAY_MILLISECONDS; + ) * 60.0 + date.get(Calendar.SECOND) + ) * 1000.0 + date.get(Calendar.MILLISECOND) + ) / DAY_MILLISECONDS; Calendar calStart = dayStart(date); double value = fraction + absoluteDay(calStart, use1904windowing); @@ -145,12 +145,12 @@ public class DateUtil { /** * Given an Excel date with using 1900 date windowing, and * converts it to a java.util.Date. - * - * Excel Dates and Times are stored without any timezone - * information. If you know (through other means) that your file + * + * Excel Dates and Times are stored without any timezone + * information. If you know (through other means) that your file * uses a different TimeZone to the system default, you can use * this version of the getJavaDate() method to handle it. - * + * * @param date The Excel date. * @param tz The TimeZone to evaluate the date in * @return Java representation of the date, or null if date is not a valid Excel date @@ -182,12 +182,12 @@ public class DateUtil { /** * Given an Excel date with either 1900 or 1904 date windowing, * converts it to a java.util.Date. - * - * Excel Dates and Times are stored without any timezone - * information. If you know (through other means) that your file + * + * Excel Dates and Times are stored without any timezone + * information. If you know (through other means) that your file * uses a different TimeZone to the system default, you can use * this version of the getJavaDate() method to handle it. - * + * * @param date The Excel date. * @param tz The TimeZone to evaluate the date in * @param use1904windowing true if date uses 1904 windowing, @@ -197,16 +197,16 @@ public class DateUtil { public static Date getJavaDate(double date, boolean use1904windowing, TimeZone tz) { return getJavaDate(date, use1904windowing, tz, false); } - + /** * Given an Excel date with either 1900 or 1904 date windowing, * converts it to a java.util.Date. - * - * Excel Dates and Times are stored without any timezone - * information. If you know (through other means) that your file + * + * Excel Dates and Times are stored without any timezone + * information. If you know (through other means) that your file * uses a different TimeZone to the system default, you can use * this version of the getJavaDate() method to handle it. - * + * * @param date The Excel date. * @param tz The TimeZone to evaluate the date in * @param use1904windowing true if date uses 1904 windowing, @@ -218,7 +218,7 @@ public class DateUtil { Calendar calendar = getJavaCalendar(date, use1904windowing, tz, roundSeconds); return calendar == null ? null : calendar.getTime(); } - + /** * Given an Excel date with either 1900 or 1904 date windowing, * converts it to a java.util.Date. @@ -314,7 +314,7 @@ public class DateUtil { public static Calendar getJavaCalendar(double date, boolean use1904windowing, TimeZone timeZone) { return getJavaCalendar(date, use1904windowing, timeZone, false); } - + /** * Get EXCEL date as Java Calendar with given time zone. * @param date The Excel date. @@ -345,13 +345,14 @@ public class DateUtil { // string represents a date format if the same string is passed multiple times. // see https://issues.apache.org/bugzilla/show_bug.cgi?id=55611 private static ThreadLocal lastFormatIndex = new ThreadLocal() { + @Override protected Integer initialValue() { return -1; } }; private static ThreadLocal lastFormatString = new ThreadLocal<>(); private static ThreadLocal lastCachedResult = new ThreadLocal<>(); - + private static boolean isCached(String formatString, int formatIndex) { String cachedFormatString = lastFormatString.get(); return cachedFormatString != null && formatIndex == lastFormatIndex.get() @@ -378,12 +379,14 @@ public class DateUtil { * @see #isInternalDateFormat(int) */ public static boolean isADateFormat(ExcelNumberFormat numFmt) { - - if (numFmt == null) return false; - + + if (numFmt == null) { + return false; + } + return isADateFormat(numFmt.getIdx(), numFmt.getFormat()); } - + /** * Given a format ID and its format String, will check to see if the * format represents a date format or not. @@ -399,7 +402,7 @@ public class DateUtil { * @see #isInternalDateFormat(int) */ public static boolean isADateFormat(int formatIndex, String formatString) { - + // First up, is this an internal date format? if(isInternalDateFormat(formatIndex)) { cache(formatString, formatIndex, true); @@ -469,7 +472,7 @@ public class DateUtil { return true; } // If it starts with [DBNum1] or [DBNum2] or [DBNum3] - // then it could be a Chinese date + // then it could be a Chinese date fs = date_ptrn5.matcher(fs).replaceAll(""); // If it starts with [$-...], then could be a date, but // who knows what that starting bit is all about @@ -490,7 +493,7 @@ public class DateUtil { if (! date_ptrn3a.matcher(fs).find()) { return false; } - + // If we get here, check it's only made up, in any case, of: // y m d h s - \ / , . : [ ] T // optionally followed by AM/PM @@ -530,7 +533,7 @@ public class DateUtil { * Check if a cell contains a date * Since dates are stored internally in Excel as double values * we infer it is a date if it is formatted as such. - * @param cell + * @param cell * @return true if it looks like a date * @see #isADateFormat(int, String) * @see #isInternalDateFormat(int) @@ -538,32 +541,36 @@ public class DateUtil { public static boolean isCellDateFormatted(Cell cell) { return isCellDateFormatted(cell, null); } - + /** * Check if a cell contains a date * Since dates are stored internally in Excel as double values * we infer it is a date if it is formatted as such. * Format is determined from applicable conditional formatting, if * any, or cell style. - * @param cell + * @param cell * @param cfEvaluator if available, or null * @return true if it looks like a date * @see #isADateFormat(int, String) * @see #isInternalDateFormat(int) */ public static boolean isCellDateFormatted(Cell cell, ConditionalFormattingEvaluator cfEvaluator) { - if (cell == null) return false; + if (cell == null) { + return false; + } boolean bDate = false; double d = cell.getNumericCellValue(); if ( DateUtil.isValidExcelDate(d) ) { ExcelNumberFormat nf = ExcelNumberFormat.from(cell, cfEvaluator); - if(nf==null) return false; + if(nf==null) { + return false; + } bDate = isADateFormat(nf); } return bDate; } - + /** * Check if a cell contains a date, checking only for internal * excel date formats. @@ -573,7 +580,9 @@ public class DateUtil { * @see #isInternalDateFormat(int) */ public static boolean isCellInternalDateFormatted(Cell cell) { - if (cell == null) return false; + if (cell == null) { + return false; + } boolean bDate = false; double d = cell.getNumericCellValue(); @@ -691,7 +700,7 @@ public class DateUtil { int minutes = parseInt(minStr, "minute", MINUTES_PER_HOUR); int seconds = parseInt(secStr, "second", SECONDS_PER_MINUTE); - double totalSeconds = seconds + (minutes + (hours) * 60) * 60; + double totalSeconds = seconds + (minutes + (hours * 60.0)) * 60.0; return totalSeconds / (SECONDS_PER_DAY); } /** diff --git a/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java b/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java index 82fef17e24..c5684b1c4d 100644 --- a/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java +++ b/src/java/org/apache/poi/ss/usermodel/ExcelStyleDateFormatter.java @@ -16,13 +16,17 @@ ==================================================================== */ package org.apache.poi.ss.usermodel; -import java.util.*; +import java.math.RoundingMode; +import java.text.DateFormatSymbols; +import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; +import java.text.FieldPosition; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.Locale; import org.apache.poi.util.LocaleUtil; -import java.math.RoundingMode; -import java.text.*; - /** * A wrapper around a {@link SimpleDateFormat} instance, * which handles a few Excel-style extensions that @@ -60,7 +64,7 @@ public class ExcelStyleDateFormatter extends SimpleDateFormat { DataFormatter.setExcelStyleRoundingMode(format3digit); DataFormatter.setExcelStyleRoundingMode(format4digits); } - + { setTimeZone(LocaleUtil.getUserTimeZone()); } @@ -152,7 +156,7 @@ public class ExcelStyleDateFormatter extends SimpleDateFormat { } if (s.indexOf(S_BRACKET_SYMBOL) != -1 || s.indexOf(SS_BRACKET_SYMBOL) != -1) { - float seconds = (float) (dateToBeFormatted * 24.0 * 60.0 * 60.0); + float seconds = (float) (dateToBeFormatted * 24 * 60 * 60); s = s.replaceAll( String.valueOf(S_BRACKET_SYMBOL), format1digit.format(seconds) @@ -165,15 +169,15 @@ public class ExcelStyleDateFormatter extends SimpleDateFormat { if (s.indexOf(L_BRACKET_SYMBOL) != -1 || s.indexOf(LL_BRACKET_SYMBOL) != -1) { - float millisTemp = (float) ((dateToBeFormatted - Math.floor(dateToBeFormatted)) * 24.0 * 60.0 * 60.0); + float millisTemp = (float) ((dateToBeFormatted - Math.floor(dateToBeFormatted)) * 24 * 60 * 60); float millis = (millisTemp - (int) millisTemp); s = s.replaceAll( String.valueOf(L_BRACKET_SYMBOL), - format3digit.format(millis * 10) + format3digit.format(millis * 10.0) ); s = s.replaceAll( String.valueOf(LL_BRACKET_SYMBOL), - format4digits.format(millis * 100) + format4digits.format(millis * 100.0) ); } @@ -185,11 +189,11 @@ public class ExcelStyleDateFormatter extends SimpleDateFormat { if (!(o instanceof ExcelStyleDateFormatter)) { return false; } - + ExcelStyleDateFormatter other = (ExcelStyleDateFormatter) o; return dateToBeFormatted == other.dateToBeFormatted; } - + @Override public int hashCode() { return Double.valueOf(dateToBeFormatted).hashCode(); diff --git a/src/java/org/apache/poi/ss/util/ImageUtils.java b/src/java/org/apache/poi/ss/util/ImageUtils.java index 9a2b1778c7..3a3b6efd5b 100644 --- a/src/java/org/apache/poi/ss/util/ImageUtils.java +++ b/src/java/org/apache/poi/ss/util/ImageUtils.java @@ -122,10 +122,14 @@ public class ImageUtils { NodeList lst; Element node = (Element)r.getImageMetadata(0).getAsTree("javax_imageio_1.0"); lst = node.getElementsByTagName("HorizontalPixelSize"); - if(lst != null && lst.getLength() == 1) hdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value"))); + if(lst != null && lst.getLength() == 1) { + hdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value"))); + } lst = node.getElementsByTagName("VerticalPixelSize"); - if(lst != null && lst.getLength() == 1) vdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value"))); + if(lst != null && lst.getLength() == 1) { + vdpi = (int)(mm2inch/Float.parseFloat(((Element)lst.item(0)).getAttribute("value"))); + } return new int[]{hdpi, vdpi}; } @@ -142,7 +146,7 @@ public class ImageUtils { boolean isHSSF = (anchor instanceof HSSFClientAnchor); PictureData data = picture.getPictureData(); Sheet sheet = picture.getSheet(); - + // in pixel Dimension imgSize = getImageDimension(new ByteArrayInputStream(data.getData()), data.getPictureType()); // in emus @@ -163,11 +167,11 @@ public class ImageUtils { } else { w -= anchor.getDx1()/(double)EMU_PER_PIXEL; } - + while(w < scaledWidth){ w += sheet.getColumnWidthInPixels(col2++); } - + if(w > scaledWidth) { //calculate dx2, offset in the rightmost cell double cw = sheet.getColumnWidthInPixels(--col2); @@ -177,7 +181,9 @@ public class ImageUtils { } else { dx2 = (int)((cw-delta)*EMU_PER_PIXEL); } - if (dx2 < 0) dx2 = 0; + if (dx2 < 0) { + dx2 = 0; + } } anchor.setCol2(col2); anchor.setDx2(dx2); @@ -185,7 +191,7 @@ public class ImageUtils { double h = 0; int row2 = anchor.getRow1(); int dy2 = 0; - + h = getRowHeightInPixels(sheet,row2++); if (isHSSF) { h *= 1 - anchor.getDy1()/256d; @@ -196,7 +202,7 @@ public class ImageUtils { while(h < scaledHeight){ h += getRowHeightInPixels(sheet,row2++); } - + if(h > scaledHeight) { double ch = getRowHeightInPixels(sheet,--row2); double delta = h - scaledHeight; @@ -205,7 +211,9 @@ public class ImageUtils { } else { dy2 = (int)((ch-delta)*EMU_PER_PIXEL); } - if (dy2 < 0) dy2 = 0; + if (dy2 < 0) { + dy2 = 0; + } } anchor.setRow2(row2); @@ -238,20 +246,20 @@ public class ImageUtils { } else { w -= anchor.getDx1()/(double)EMU_PER_PIXEL; } - + while(col2 < anchor.getCol2()){ w += sheet.getColumnWidthInPixels(col2++); } - + if (isHSSF) { - w += sheet.getColumnWidthInPixels(col2) * anchor.getDx2()/1024d; + w += anchor.getDx2()/1024d * sheet.getColumnWidthInPixels(col2); } else { w += anchor.getDx2()/(double)EMU_PER_PIXEL; } double h = 0; int row2 = anchor.getRow1(); - + h = getRowHeightInPixels(sheet,row2++); if (isHSSF) { h *= 1 - anchor.getDy1()/256d; @@ -262,7 +270,7 @@ public class ImageUtils { while(row2 < anchor.getRow2()){ h += getRowHeightInPixels(sheet,row2++); } - + if (isHSSF) { h += getRowHeightInPixels(sheet,row2) * anchor.getDy2()/256; } else { @@ -271,11 +279,11 @@ public class ImageUtils { w *= EMU_PER_PIXEL; h *= EMU_PER_PIXEL; - + return new Dimension((int)Math.rint(w), (int)Math.rint(h)); } - - + + public static double getRowHeightInPixels(Sheet sheet, int rowNum) { Row r = sheet.getRow(rowNum); double points = (r == null) ? sheet.getDefaultRowHeightInPoints() : r.getHeightInPoints(); diff --git a/src/java/org/apache/poi/ss/util/cellwalk/CellWalk.java b/src/java/org/apache/poi/ss/util/cellwalk/CellWalk.java index 9ab0e530fc..2826cc3323 100644 --- a/src/java/org/apache/poi/ss/util/cellwalk/CellWalk.java +++ b/src/java/org/apache/poi/ss/util/cellwalk/CellWalk.java @@ -5,9 +5,9 @@ The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - + http://www.apache.org/licenses/LICENSE-2.0 - + Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -116,14 +116,17 @@ public class CellWalk { public int rowNumber; public int colNumber; + @Override public long getOrdinalNumber() { return ordinalNumber; } + @Override public int getRowNumber() { return rowNumber; } + @Override public int getColumnNumber() { return colNumber; } diff --git a/src/java/org/apache/poi/util/StringUtil.java b/src/java/org/apache/poi/util/StringUtil.java index d8b4837931..fb69fbd538 100644 --- a/src/java/org/apache/poi/util/StringUtil.java +++ b/src/java/org/apache/poi/util/StringUtil.java @@ -263,7 +263,7 @@ public class StringUtil { } public static String readUnicodeLE(LittleEndianInput in, int nChars) { - byte[] bytes = IOUtils.safelyAllocate(nChars * 2, MAX_RECORD_LENGTH); + byte[] bytes = IOUtils.safelyAllocate(nChars * 2L, MAX_RECORD_LENGTH); in.readFully(bytes); return new String(bytes, UTF16LE); } diff --git a/src/scratchpad/src/org/apache/poi/hemf/record/emf/HemfComment.java b/src/scratchpad/src/org/apache/poi/hemf/record/emf/HemfComment.java index eee11cfc9f..a2cc75886c 100644 --- a/src/scratchpad/src/org/apache/poi/hemf/record/emf/HemfComment.java +++ b/src/scratchpad/src/org/apache/poi/hemf/record/emf/HemfComment.java @@ -69,7 +69,9 @@ public class HemfComment { public static HemfCommentRecordType getById(long id, boolean isEmfPublic) { for (HemfCommentRecordType wrt : values()) { - if (wrt.id == id && wrt.isEmfPublic == isEmfPublic) return wrt; + if (wrt.id == id && wrt.isEmfPublic == isEmfPublic) { + return wrt; + } } return emfGeneric; } @@ -277,7 +279,7 @@ public class HemfComment { // The number of Unicode characters in the optional description string that follows. int nDescription = (int)leis.readUInt(); - byte[] buf = IOUtils.safelyAllocate(nDescription*2, MAX_RECORD_LENGTH); + byte[] buf = IOUtils.safelyAllocate(nDescription * 2L, MAX_RECORD_LENGTH); leis.readFully(buf); description = new String(buf, StandardCharsets.UTF_16LE); @@ -373,7 +375,9 @@ public class HemfComment { public static EmfFormatSignature getById(int id) { for (EmfFormatSignature wrt : values()) { - if (wrt.id == id) return wrt; + if (wrt.id == id) { + return wrt; + } } return null; } diff --git a/src/scratchpad/src/org/apache/poi/hpbf/model/qcbits/QCTextBit.java b/src/scratchpad/src/org/apache/poi/hpbf/model/qcbits/QCTextBit.java index 330c948fb2..cc153708bb 100644 --- a/src/scratchpad/src/org/apache/poi/hpbf/model/qcbits/QCTextBit.java +++ b/src/scratchpad/src/org/apache/poi/hpbf/model/qcbits/QCTextBit.java @@ -41,7 +41,7 @@ public final class QCTextBit extends QCBit { } public void setText(String text) { - byte[] data = IOUtils.safelyAllocate(text.length() * 2, MAX_RECORD_LENGTH); + byte[] data = IOUtils.safelyAllocate(text.length() * 2L, MAX_RECORD_LENGTH); StringUtil.putUnicodeLE(text, data, 0); setData(data); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/CurrentUserAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/CurrentUserAtom.java index 26ba59d2c6..2c673a22e8 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/CurrentUserAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/CurrentUserAtom.java @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - + package org.apache.poi.hslf.record; @@ -73,7 +73,7 @@ public class CurrentUserAtom /** Only correct after reading in or writing out */ private byte[] _contents; - + /** Flag for encryption state of the whole file */ private boolean isEncrypted; @@ -96,7 +96,7 @@ public class CurrentUserAtom public boolean isEncrypted() { return isEncrypted; } public void setEncrypted(boolean isEncrypted) { this.isEncrypted = isEncrypted; } - + /* ********************* real code follows *************************** */ @@ -117,19 +117,19 @@ public class CurrentUserAtom } - /** + /** * Find the Current User in the filesystem, and create from that */ public CurrentUserAtom(DirectoryNode dir) throws IOException { // Decide how big it is DocumentEntry docProps = (DocumentEntry)dir.getEntry("Current User"); - + // If it's clearly junk, bail out if(docProps.getSize() > 131072) { throw new CorruptPowerPointFileException("The Current User stream is implausably long. It's normally 28-200 bytes long, but was " + docProps.getSize() + " bytes"); } - + // Grab the contents int len = docProps.getSize(); _contents = IOUtils.safelyAllocate(len, MAX_RECORD_LENGTH); @@ -140,8 +140,8 @@ public class CurrentUserAtom if (len != readLen) { throw new IOException("Current User input stream ended prematurely - expected "+len+" bytes - received "+readLen+" bytes"); } - - + + // See how long it is. If it's under 28 bytes long, we can't // read it if(_contents.length < 28) { @@ -169,9 +169,9 @@ public class CurrentUserAtom private void init() { // First up is the size, in 4 bytes, which is fixed // Then is the header - - isEncrypted = (LittleEndian.getInt(encHeaderToken) == LittleEndian.getInt(_contents,12)); - + + isEncrypted = (LittleEndian.getInt(encHeaderToken) == LittleEndian.getInt(_contents,12)); + // Grab the edit offset currentEditOffset = LittleEndian.getUInt(_contents,16); @@ -188,7 +188,7 @@ public class CurrentUserAtom usernameLen = 0; } - // Now we know the length of the username, + // Now we know the length of the username, // use this to grab the revision if(_contents.length >= 28+(int)usernameLen + 4) { releaseVersion = LittleEndian.getUInt(_contents,28+(int)usernameLen); @@ -227,7 +227,7 @@ public class CurrentUserAtom _contents = IOUtils.safelyAllocate(size, MAX_RECORD_LENGTH); // First we have a 8 byte atom header - System.arraycopy(atomHeader,0,_contents,0,4); + System.arraycopy(atomHeader,0,_contents,0,4); // Size is 20+user len + revision len(4) int atomSize = 20+4+lastEditUser.length(); LittleEndian.putInt(_contents,4,atomSize); @@ -241,14 +241,14 @@ public class CurrentUserAtom // Now the current edit offset LittleEndian.putInt(_contents,16,(int)currentEditOffset); - // The username gets stored twice, once as US + // The username gets stored twice, once as US // ascii, and again as unicode laster on byte[] asciiUN = IOUtils.safelyAllocate(lastEditUser.length(), MAX_RECORD_LENGTH); StringUtil.putCompressedUnicode(lastEditUser,asciiUN,0); - + // Now we're able to do the length of the last edited user LittleEndian.putShort(_contents,20,(short)asciiUN.length); - + // Now the file versions, 2+1+1 LittleEndian.putShort(_contents,22,(short)docFinalVersion); _contents[24] = docMajorNo; @@ -265,7 +265,7 @@ public class CurrentUserAtom LittleEndian.putInt(_contents,28+asciiUN.length,(int)releaseVersion); // username in unicode - byte [] ucUN = IOUtils.safelyAllocate(lastEditUser.length()*2, MAX_RECORD_LENGTH); + byte [] ucUN = IOUtils.safelyAllocate(lastEditUser.length() * 2L, MAX_RECORD_LENGTH); StringUtil.putUnicodeLE(lastEditUser,ucUN,0); System.arraycopy(ucUN,0,_contents,28+asciiUN.length+4,ucUN.length); @@ -280,7 +280,7 @@ public class CurrentUserAtom // Grab contents ByteArrayOutputStream baos = new ByteArrayOutputStream(); writeOut(baos); - ByteArrayInputStream bais = + ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); // Write out diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/MasterTextPropAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/MasterTextPropAtom.java index d754a10b3c..9d76556e86 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/MasterTextPropAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/MasterTextPropAtom.java @@ -59,7 +59,7 @@ public final class MasterTextPropAtom extends RecordAtom { LittleEndian.putShort(_header, 2, (short)getRecordType()); LittleEndian.putInt(_header, 4, _data.length); - + indents = new ArrayList<>(); } @@ -92,6 +92,7 @@ public final class MasterTextPropAtom extends RecordAtom { * * @return the record type. */ + @Override public long getRecordType() { return RecordTypes.MasterTextPropAtom.typeID; } @@ -103,12 +104,13 @@ public final class MasterTextPropAtom extends RecordAtom { * @param out the output stream to write to. * @throws java.io.IOException if an error occurs. */ + @Override public void writeOut(OutputStream out) throws IOException { write(); out.write(_header); out.write(_data); } - + /** * Write the internal variables to the record bytes */ @@ -129,7 +131,7 @@ public final class MasterTextPropAtom extends RecordAtom { private void read() { int pos = 0; indents = new ArrayList<>(_data.length / 6); - + while (pos <= _data.length - 6) { int count = LittleEndian.getInt(_data, pos); short indent = LittleEndian.getShort(_data, pos+4); @@ -137,7 +139,7 @@ public final class MasterTextPropAtom extends RecordAtom { pos += 6; } } - + /** * Returns the indent that applies at the given text offset */ @@ -151,7 +153,7 @@ public final class MasterTextPropAtom extends RecordAtom { } return -1; } - + public List getIndents() { return Collections.unmodifiableList(indents); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/TextCharsAtom.java b/src/scratchpad/src/org/apache/poi/hslf/record/TextCharsAtom.java index 570506b719..349ee8166b 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/TextCharsAtom.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/TextCharsAtom.java @@ -48,7 +48,7 @@ public final class TextCharsAtom extends RecordAtom { /** Updates the text in the Atom. */ public void setText(String text) { // Convert to little endian unicode - _text = IOUtils.safelyAllocate(text.length()*2, MAX_RECORD_LENGTH); + _text = IOUtils.safelyAllocate(text.length() * 2L, MAX_RECORD_LENGTH); StringUtil.putUnicodeLE(text,_text,0); // Update the size (header bytes 5-8) diff --git a/src/scratchpad/src/org/apache/poi/hslf/record/TextSpecInfoRun.java b/src/scratchpad/src/org/apache/poi/hslf/record/TextSpecInfoRun.java index 3b35258ace..8a046c1613 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/record/TextSpecInfoRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/record/TextSpecInfoRun.java @@ -20,7 +20,10 @@ package org.apache.poi.hslf.record; import java.io.IOException; import java.io.OutputStream; -import org.apache.poi.util.*; +import org.apache.poi.util.BitField; +import org.apache.poi.util.IOUtils; +import org.apache.poi.util.LittleEndian; +import org.apache.poi.util.LittleEndianByteArrayInputStream; public class TextSpecInfoRun { @@ -39,14 +42,14 @@ public class TextSpecInfoRun { grammar(new BitField(4)), /** the text is spelled correct */ correct(new BitField(0)); - + final BitField bitField; - + SpellInfoEnum(BitField bitField) { this.bitField = bitField; } } - + /** A bit that specifies whether the spellInfo field exists. */ private static final BitField spellFld = new BitField(0X00000001); /** A bit that specifies whether the lid field exists. */ @@ -62,7 +65,7 @@ public class TextSpecInfoRun { // reserved1 - MUST be zero and MUST be ignored. /** A bit that specifies whether the smartTags field exists. */ private static final BitField smartTagFld = new BitField(0X00000200); - // reserved2 - MUST be zero and MUST be ignored. + // reserved2 - MUST be zero and MUST be ignored. /** * An optional unsigned integer that specifies an identifier for a character @@ -76,7 +79,7 @@ public class TextSpecInfoRun { * only if fPp10ext is TRUE. **/ private static final BitField grammarErrorFld = new BitField(0X80000000); - + //Length of special info run. private int length; @@ -97,7 +100,7 @@ public class TextSpecInfoRun { * reserved (13 bits): MUST be zero and MUST be ignored. */ private short spellInfo = -1; - + /** * An optional TxLCID that specifies the language identifier of this text. * It MUST exist if and only if lang is TRUE. @@ -108,13 +111,13 @@ public class TextSpecInfoRun { * > 0x0400 = A valid LCID as specified by [MS-LCID]. */ private short langId = -1; - + /** * An optional TxLCID that specifies the alternate language identifier of this text. * It MUST exist if and only if altLang is TRUE. */ private short altLangId = -1; - + /** * An optional signed integer that specifies whether the text contains bidirectional * characters. It MUST exist if and only if fBidi is TRUE. @@ -122,7 +125,7 @@ public class TextSpecInfoRun { * 0x0001 = Contains bidirectional characters. */ private short bidi = -1; - + private int pp10extMask = -1; private byte[] smartTagsBytes; @@ -135,7 +138,7 @@ public class TextSpecInfoRun { setLength(len); setLangId((short)0); } - + public TextSpecInfoRun(LittleEndianByteArrayInputStream source) { length = source.readInt(); mask = source.readInt(); @@ -157,7 +160,7 @@ public class TextSpecInfoRun { if (smartTagFld.isSet(mask)) { // An unsigned integer specifies the count of items in rgSmartTagIndex. int count = source.readInt(); - smartTagsBytes = IOUtils.safelyAllocate(4+count*4, MAX_RECORD_LENGTH); + smartTagsBytes = IOUtils.safelyAllocate(4 + count * 4L, MAX_RECORD_LENGTH); LittleEndian.putInt(smartTagsBytes, 0, count); // An array of SmartTagIndex that specifies the indices. // The count of items in the array is specified by count. @@ -186,11 +189,13 @@ public class TextSpecInfoRun { pp10extFld, pp10extMask, "pp10 extension field", smartTagFld, smartTagsBytes, "smart tags" }; - + for (int i=0; i polyList = new ArrayList<>(); - + @Override public HwmfRecordType getWmfRecordType() { return HwmfRecordType.polyPolygon; @@ -577,7 +577,6 @@ public class HwmfDraw { startAngle += 360; } - boolean fillShape; int arcClosure; switch (getWmfRecordType()) { default: @@ -671,7 +670,7 @@ public class HwmfDraw { return "{ index: "+objectIndex +" }"; } } - + static int readBounds(LittleEndianInputStream leis, Rectangle2D bounds) { /** * The 16-bit signed integers that defines the corners of the bounding rectangle. diff --git a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java index 42fe7fd30d..91de8b1b40 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/converter/WordToHtmlConverter.java @@ -92,7 +92,7 @@ public class WordToHtmlConverter extends AbstractWordConverter /** * Creates new instance of {@link WordToHtmlConverter}. Can be used for * output several {@link HWPFDocument}s into single HTML document. - * + * * @param document XML DOM Document used as HTML document */ public WordToHtmlConverter( Document document ) { @@ -102,7 +102,7 @@ public class WordToHtmlConverter extends AbstractWordConverter public WordToHtmlConverter( HtmlDocumentFacade htmlDocumentFacade ) { this.htmlDocumentFacade = htmlDocumentFacade; } - + private static String getSectionStyle( Section section ) { float leftMargin = section.getMarginLeft() / TWIPS_PER_INCH; @@ -132,9 +132,9 @@ public class WordToHtmlConverter extends AbstractWordConverter /** * Java main() interface to interact with {@link WordToHtmlConverter}

- * + * * Usage: WordToHtmlConverter infile outfile

- * + * * Where infile is an input .doc file ( Word 95-2007) which will be rendered * as HTML into outfile */ @@ -344,11 +344,11 @@ public class WordToHtmlConverter extends AbstractWordConverter if ( aspectRatioX > 0 ) { - imageWidth = picture.getDxaGoal() * aspectRatioX / 1000.f + imageWidth = aspectRatioX / 1000.f * picture.getDxaGoal() / TWIPS_PER_INCH; - cropRight = picture.getDxaCropRight() * aspectRatioX / 1000.f + cropRight = aspectRatioX / 1000.f * picture.getDxaCropRight() / TWIPS_PER_INCH; - cropLeft = picture.getDxaCropLeft() * aspectRatioX / 1000.f + cropLeft = aspectRatioX / 1000.f * picture.getDxaCropLeft() / TWIPS_PER_INCH; } else @@ -360,11 +360,11 @@ public class WordToHtmlConverter extends AbstractWordConverter if ( aspectRatioY > 0 ) { - imageHeight = picture.getDyaGoal() * aspectRatioY / 1000.f + imageHeight = aspectRatioY / 1000.f * picture.getDyaGoal() / TWIPS_PER_INCH; - cropTop = picture.getDyaCropTop() * aspectRatioY / 1000.f + cropTop = aspectRatioY / 1000.f * picture.getDyaCropTop() / TWIPS_PER_INCH; - cropBottom = picture.getDyaCropBottom() * aspectRatioY / 1000.f + cropBottom = aspectRatioY / 1000.f * picture.getDyaCropBottom() / TWIPS_PER_INCH; } else diff --git a/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmUtils.java b/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmUtils.java index 83b9ce4888..e47f956512 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmUtils.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/sprm/SprmUtils.java @@ -37,7 +37,7 @@ public final class SprmUtils public static byte[] shortArrayToByteArray(short[] convert) { - byte[] buf = IOUtils.safelyAllocate(convert.length * LittleEndian.SHORT_SIZE, MAX_RECORD_LENGTH); + byte[] buf = IOUtils.safelyAllocate(convert.length * (long)LittleEndian.SHORT_SIZE, MAX_RECORD_LENGTH); for (int x = 0; x < convert.length; x++) { @@ -102,7 +102,7 @@ public final class SprmUtils break; default: //should never happen - throw new RuntimeException("Invalid sprm type"); + throw new RuntimeException("Invalid sprm type"); } LittleEndian.putShort(sprm, 0, instruction); list.add(sprm); diff --git a/src/scratchpad/src/org/apache/poi/hwpf/sprm/TableSprmCompressor.java b/src/scratchpad/src/org/apache/poi/hwpf/sprm/TableSprmCompressor.java index 875b81b7fe..cbef3587ce 100644 --- a/src/scratchpad/src/org/apache/poi/hwpf/sprm/TableSprmCompressor.java +++ b/src/scratchpad/src/org/apache/poi/hwpf/sprm/TableSprmCompressor.java @@ -81,7 +81,9 @@ public final class TableSprmCompressor { int itcMac = newTAP.getItcMac(); byte[] buf = IOUtils.safelyAllocate( - 1 + (LittleEndian.SHORT_SIZE*(itcMac + 1)) + (TableCellDescriptor.SIZE*itcMac), + 1 + + (LittleEndian.SHORT_SIZE*((long)itcMac + 1)) + + (TableCellDescriptor.SIZE*(long)itcMac), MAX_RECORD_LENGTH); buf[0] = (byte)itcMac; -- 2.39.5