diff options
author | PJ Fanning <fanningpj@apache.org> | 2018-02-20 00:25:43 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2018-02-20 00:25:43 +0000 |
commit | 05b0de574cce4c56fc862737242846df4abc2e6e (patch) | |
tree | d63312db51c79e1cc39a0ece41bcc59669ab164e /src/java/org/apache | |
parent | 61b0d86a2d0c1444b77fda00d2450cf289322300 (diff) | |
download | poi-05b0de574cce4c56fc862737242846df4abc2e6e.tar.gz poi-05b0de574cce4c56fc862737242846df4abc2e6e.zip |
[bug-62018] use ints to index fonts
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1824826 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache')
9 files changed, 116 insertions, 36 deletions
diff --git a/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java b/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java index 3d043681be..e187540300 100644 --- a/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java +++ b/src/java/org/apache/poi/hssf/record/ExtendedFormatRecord.java @@ -1660,7 +1660,7 @@ public final class ExtendedFormatRecord @Override public String toString() { - StringBuffer buffer = new StringBuffer(); + StringBuilder buffer = new StringBuilder(); buffer.append("[EXTENDEDFORMAT]\n"); if (getXFType() == XF_STYLE) diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index 64a0d12e98..a2169018cb 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -169,7 +169,7 @@ public final class HSSFCellStyle implements CellStyle { * set the font for this style * @param font a font object created or retrieved from the HSSFWorkbook object * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont() - * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int) */ @Override public void setFont(Font font) { @@ -186,19 +186,31 @@ public final class HSSFCellStyle implements CellStyle { * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) */ @Override + @Deprecated public short getFontIndex() { return _format.getFontIndex(); } /** + * gets the index of the font for this style + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int) + * @since 4.0.0 + */ + @Override + public int getFontIntIndex() + { + return _format.getFontIndex(); + } + + /** * gets the font for this style * @param parentWorkbook The HSSFWorkbook that this style belongs to - * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#getFontIndex() - * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) + * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#getFontIntIndex() + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int) */ public HSSFFont getFont(org.apache.poi.ss.usermodel.Workbook parentWorkbook) { - return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIndex()); + return ((HSSFWorkbook) parentWorkbook).getFontAt(getFontIntIndex()); } /** diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java index d07da152b1..722dc0b748 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFFont.java @@ -25,7 +25,7 @@ import org.apache.poi.ss.usermodel.Font; * Represents a Font used in a workbook. * * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#createFont() - * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(short) + * @see org.apache.poi.hssf.usermodel.HSSFWorkbook#getFontAt(int) * @see org.apache.poi.hssf.usermodel.HSSFCellStyle#setFont(HSSFFont) */ public final class HSSFFont implements Font { @@ -46,12 +46,12 @@ public final class HSSFFont implements Font { public final static String FONT_ARIAL = "Arial"; - private FontRecord font; - private short index; + private FontRecord font; + private int index; /** Creates a new instance of HSSFFont */ - protected HSSFFont(short index, FontRecord rec) + protected HSSFFont(int index, FontRecord rec) { font = rec; this.index = index; @@ -85,7 +85,15 @@ public final class HSSFFont implements Font { * unless you're comparing which one is which) */ - public short getIndex() + public short getIndex() { return (short)index; } + + /** + * get the index within the HSSFWorkbook (sequence within the collection of Font objects) + * @return unique index number of the underlying record this Font represents (probably you don't care + * unless you're comparing which one is which) + */ + + public int getIndexAsInt() { return index; } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java index 9d8ec90dab..cd38a136f4 100644 --- a/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java @@ -178,13 +178,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss * this holds the HSSFFont objects attached to this workbook. * We only create these from the low level records as required. */ - private Map<Short,HSSFFont> fonts; + private Map<Integer,HSSFFont> fonts; /** * holds whether or not to preserve other nodes in the POIFS. Used * for macros and embedded objects. */ - private boolean preserveNodes; + private boolean preserveNodes; /** * Used to keep track of the data formatter so that all @@ -1171,13 +1171,13 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss public HSSFFont createFont() { /*FontRecord font =*/ workbook.createNewFont(); - short fontindex = (short) (getNumberOfFonts() - 1); + int fontindex = getNumberOfFontsAsInt() - 1; if (fontindex > 3) { fontindex++; // THERE IS NO FOUR!! } - if(fontindex == Short.MAX_VALUE){ + if(fontindex >= Short.MAX_VALUE){ throw new IllegalArgumentException("Maximum number of fonts was exceeded"); } @@ -1194,8 +1194,8 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss String name, boolean italic, boolean strikeout, short typeOffset, byte underline) { - short numberOfFonts = getNumberOfFonts(); - for (short i=0; i<=numberOfFonts; i++) { + int numberOfFonts = getNumberOfFontsAsInt(); + for (int i = 0; i <= numberOfFonts; i++) { // Remember - there is no 4! if(i == 4) { continue; @@ -1218,24 +1218,25 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss return null; } - /** - * get the number of fonts in the font table - * @return number of fonts - */ + @Override + @Deprecated + public short getNumberOfFonts() { + return (short)getNumberOfFontsAsInt(); + } @Override - public short getNumberOfFonts() - { - return (short) workbook.getNumberOfFontRecords(); + public int getNumberOfFontsAsInt() { + return workbook.getNumberOfFontRecords(); } - /** - * Get the font at the given index number - * @param idx index number - * @return HSSFFont at the index - */ @Override + @Deprecated public HSSFFont getFontAt(short idx) { + return getFontAt((int)idx); + } + + @Override + public HSSFFont getFontAt(int idx) { if(fonts == null) { fonts = new HashMap<>(); } @@ -1243,7 +1244,7 @@ public final class HSSFWorkbook extends POIDocument implements org.apache.poi.ss // So we don't confuse users, give them back // the same object every time, but create // them lazily - Short sIdx = Short.valueOf(idx); + Integer sIdx = Integer.valueOf(idx); if(fonts.containsKey(sIdx)) { return fonts.get(sIdx); } diff --git a/src/java/org/apache/poi/ss/usermodel/CellStyle.java b/src/java/org/apache/poi/ss/usermodel/CellStyle.java index 57b052b16d..3e9f374396 100644 --- a/src/java/org/apache/poi/ss/usermodel/CellStyle.java +++ b/src/java/org/apache/poi/ss/usermodel/CellStyle.java @@ -49,17 +49,26 @@ public interface CellStyle { * set the font for this style * @param font a font object created or retrieved from the Workbook object * @see Workbook#createFont() - * @see Workbook#getFontAt(short) + * @see Workbook#getFontAt(int) */ void setFont(Font font); /** * gets the index of the font for this style * @see Workbook#getFontAt(short) + * @deprecated use <code>getFontIntIndex()</code> instead */ + @Removal(version = "4.2") short getFontIndex(); /** + * gets the index of the font for this style + * @see Workbook#getFontAt(int) + * @since 4.0.0 + */ + int getFontIntIndex(); + + /** * set the cell's using this style to be hidden * @param hidden - whether the cell using this style should be hidden */ diff --git a/src/java/org/apache/poi/ss/usermodel/Font.java b/src/java/org/apache/poi/ss/usermodel/Font.java index 7c218531c4..66c54ba89c 100644 --- a/src/java/org/apache/poi/ss/usermodel/Font.java +++ b/src/java/org/apache/poi/ss/usermodel/Font.java @@ -18,6 +18,8 @@ package org.apache.poi.ss.usermodel; +import org.apache.poi.util.Removal; + public interface Font { /** @@ -267,9 +269,20 @@ public interface Font { * * @return unique index number of the underlying record this Font represents (probably you don't care * unless you're comparing which one is which) + * @deprecated use <code>getIndexAsInt()</code> instead */ + @Removal(version = "4.2") public short getIndex(); + /** + * get the index within the XSSFWorkbook (sequence within the collection of Font objects) + * + * @return unique index number of the underlying record this Font represents (probably you don't care + * unless you're comparing which one is which) + * @since 4.0.0 + */ + public int getIndexAsInt(); + public void setBold(boolean bold); public boolean getBold(); diff --git a/src/java/org/apache/poi/ss/usermodel/Workbook.java b/src/java/org/apache/poi/ss/usermodel/Workbook.java index f8f5b28ee1..bd34cb2b78 100644 --- a/src/java/org/apache/poi/ss/usermodel/Workbook.java +++ b/src/java/org/apache/poi/ss/usermodel/Workbook.java @@ -265,18 +265,39 @@ public interface Workbook extends Closeable, Iterable<Sheet> { * Get the number of fonts in the font table * * @return number of fonts + * @deprecated use <code>getNumberOfFontsAsInt()</code> instead */ + @Removal(version = "4.2") short getNumberOfFonts(); /** + * Get the number of fonts in the font table + * + * @return number of fonts + * @since 4.0.0 + */ + int getNumberOfFontsAsInt(); + + /** * Get the font at the given index number * * @param idx index number (0-based) * @return font at the index + * @deprecated use <code>getFontAt(int)</code> */ + @Removal(version = "4.2") Font getFontAt(short idx); /** + * Get the font at the given index number + * + * @param idx index number (0-based) + * @return font at the index + * @since 4.0.0 + */ + Font getFontAt(int idx); + + /** * Create a new Cell style and add it to the workbook's style table * * @return the new Cell Style object diff --git a/src/java/org/apache/poi/ss/util/CellUtil.java b/src/java/org/apache/poi/ss/util/CellUtil.java index e1b909917a..15036761a7 100644 --- a/src/java/org/apache/poi/ss/util/CellUtil.java +++ b/src/java/org/apache/poi/ss/util/CellUtil.java @@ -234,7 +234,7 @@ public final class CellUtil { public static void setFont(Cell cell, Font font) { // Check if font belongs to workbook Workbook wb = cell.getSheet().getWorkbook(); - final short fontIndex = font.getIndex(); + final int fontIndex = font.getIndexAsInt(); if (!wb.getFontAt(fontIndex).equals(font)) { throw new IllegalArgumentException("Font does not belong to this workbook"); } @@ -408,7 +408,7 @@ public final class CellUtil { style.setFillPattern(getFillPattern(properties, FILL_PATTERN)); style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR)); style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR)); - style.setFont(workbook.getFontAt(getShort(properties, FONT))); + style.setFont(workbook.getFontAt(getInt(properties, FONT))); style.setHidden(getBoolean(properties, HIDDEN)); style.setIndention(getShort(properties, INDENTION)); style.setLeftBorderColor(getShort(properties, LEFT_BORDER_COLOR)); @@ -429,8 +429,24 @@ public final class CellUtil { */ private static short getShort(Map<String, Object> properties, String name) { Object value = properties.get(name); - if (value instanceof Short) { - return ((Short) value).shortValue(); + if (value instanceof Number) { + return ((Number) value).shortValue(); + } + return 0; + } + + /** + * Utility method that returns the named int value form the given map. + * + * @param properties map of named properties (String -> Object) + * @param name property name + * @return zero if the property does not exist, or is not a {@link Integer} + * otherwise the property value + */ + private static int getInt(Map<String, Object> properties, String name) { + Object value = properties.get(name); + if (value instanceof Number) { + return ((Number) value).intValue(); } return 0; } diff --git a/src/java/org/apache/poi/ss/util/SheetUtil.java b/src/java/org/apache/poi/ss/util/SheetUtil.java index 8f1345230c..2293ebcb1c 100644 --- a/src/java/org/apache/poi/ss/util/SheetUtil.java +++ b/src/java/org/apache/poi/ss/util/SheetUtil.java @@ -144,7 +144,7 @@ public class SheetUtil { if (cellType == CellType.FORMULA) cellType = cell.getCachedFormulaResultType(); - Font font = wb.getFontAt(style.getFontIndex()); + Font font = wb.getFontAt(style.getFontIntIndex()); double width = -1; if (cellType == CellType.STRING) { @@ -266,7 +266,7 @@ public class SheetUtil { */ @Internal public static int getDefaultCharWidth(final Workbook wb) { - Font defaultFont = wb.getFontAt((short) 0); + Font defaultFont = wb.getFontAt( 0); AttributedString str = new AttributedString(String.valueOf(defaultChar)); copyAttributes(defaultFont, str, 0, 1); |