diff options
author | Javen O'Neal <onealj@apache.org> | 2016-07-10 11:18:55 +0000 |
---|---|---|
committer | Javen O'Neal <onealj@apache.org> | 2016-07-10 11:18:55 +0000 |
commit | b5eced282da069f0352db107a997f25c81e5f573 (patch) | |
tree | 056b2a722faf9a7672ea711c62957a6471193b62 /src/java/org/apache/poi/ss/util/CellUtil.java | |
parent | 96a24aad62e3394bcc7e04f32ec55c360baa80dc (diff) | |
download | poi-b5eced282da069f0352db107a997f25c81e5f573.tar.gz poi-b5eced282da069f0352db107a997f25c81e5f573.zip |
bug 59837: update CellUtil to handle VerticalAlignment and HorizontalAlignment enums
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1752077 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/poi/ss/util/CellUtil.java')
-rw-r--r-- | src/java/org/apache/poi/ss/util/CellUtil.java | 326 |
1 files changed, 277 insertions, 49 deletions
diff --git a/src/java/org/apache/poi/ss/util/CellUtil.java b/src/java/org/apache/poi/ss/util/CellUtil.java index 4f8c9cd183..4f446c10dc 100644 --- a/src/java/org/apache/poi/ss/util/CellUtil.java +++ b/src/java/org/apache/poi/ss/util/CellUtil.java @@ -17,18 +17,28 @@ package org.apache.poi.ss.util; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.Locale; import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; +import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.Font; +import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; +import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; +import org.apache.poi.util.Removal; /** * Various utility functions that make working with a cells and rows easier. The various methods @@ -41,6 +51,8 @@ import org.apache.poi.ss.usermodel.Workbook; *@author (secondary) Avinash Kewalramani akewalramani@accelrys.com */ public final class CellUtil { + + private static final POILogger log = POILogFactory.getLogger(CellUtil.class); // FIXME: Move these constants into an enum public static final String ALIGNMENT = "alignment"; @@ -49,6 +61,9 @@ public final class CellUtil { public static final String BORDER_RIGHT = "borderRight"; public static final String BORDER_TOP = "borderTop"; public static final String BOTTOM_BORDER_COLOR = "bottomBorderColor"; + public static final String LEFT_BORDER_COLOR = "leftBorderColor"; + public static final String RIGHT_BORDER_COLOR = "rightBorderColor"; + public static final String TOP_BORDER_COLOR = "topBorderColor"; public static final String DATA_FORMAT = "dataFormat"; public static final String FILL_BACKGROUND_COLOR = "fillBackgroundColor"; public static final String FILL_FOREGROUND_COLOR = "fillForegroundColor"; @@ -56,13 +71,39 @@ public final class CellUtil { public static final String FONT = "font"; public static final String HIDDEN = "hidden"; public static final String INDENTION = "indention"; - public static final String LEFT_BORDER_COLOR = "leftBorderColor"; public static final String LOCKED = "locked"; - public static final String RIGHT_BORDER_COLOR = "rightBorderColor"; public static final String ROTATION = "rotation"; - public static final String TOP_BORDER_COLOR = "topBorderColor"; public static final String VERTICAL_ALIGNMENT = "verticalAlignment"; public static final String WRAP_TEXT = "wrapText"; + + private static final Set<String> shortValues = Collections.unmodifiableSet( + new HashSet<String>(Arrays.asList( + BOTTOM_BORDER_COLOR, + LEFT_BORDER_COLOR, + RIGHT_BORDER_COLOR, + TOP_BORDER_COLOR, + FILL_FOREGROUND_COLOR, + FILL_BACKGROUND_COLOR, + INDENTION, + DATA_FORMAT, + FONT, + ROTATION + ))); + private static final Set<String> booleanValues = Collections.unmodifiableSet( + new HashSet<String>(Arrays.asList( + LOCKED, + HIDDEN, + WRAP_TEXT + ))); + private static final Set<String> borderTypeValues = Collections.unmodifiableSet( + new HashSet<String>(Arrays.asList( + BORDER_BOTTOM, + BORDER_LEFT, + BORDER_RIGHT, + BORDER_TOP + ))); + + private static UnicodeMapping unicodeMappings[]; @@ -84,9 +125,9 @@ public final class CellUtil { /** * Get a row from the spreadsheet, and create it if it doesn't exist. * - *@param rowIndex The 0 based row number - *@param sheet The sheet that the row is part of. - *@return The row indicated by the rowCounter + * @param rowIndex The 0 based row number + * @param sheet The sheet that the row is part of. + * @return The row indicated by the rowCounter */ public static Row getRow(int rowIndex, Sheet sheet) { Row row = sheet.getRow(rowIndex); @@ -100,9 +141,9 @@ public final class CellUtil { /** * Get a specific cell from a row. If the cell doesn't exist, then create it. * - *@param row The row that the cell is part of - *@param columnIndex The column index that the cell is in. - *@return The cell indicated by the column. + * @param row The row that the cell is part of + * @param columnIndex The column index that the cell is in. + * @return The cell indicated by the column. */ public static Cell getCell(Row row, int columnIndex) { Cell cell = row.getCell(columnIndex); @@ -149,29 +190,63 @@ public final class CellUtil { /** * Take a cell, and align it. + * + * This is superior to cell.getCellStyle().setAlignment(align) because + * this method will not modify the CellStyle object that may be referenced + * by multiple cells. Instead, this method will search for existing CellStyles + * that match the desired CellStyle, creating a new CellStyle with the desired + * style if no match exists. * * @param cell the cell to set the alignment for * @param workbook The workbook that is being worked with. * @param align the column alignment to use. * - * @deprecated 3.15-beta2. Removed in 3.17. Use {@link #setAlignment(Cell, short)} instead. + * @deprecated 3.15-beta2. Use {@link #setAlignment(Cell, HorizontalAlignment)} instead. * * @see CellStyle for alignment options */ + @Deprecated + @Removal(version="3.17") public static void setAlignment(Cell cell, Workbook workbook, short align) { - setAlignment(cell, align); + setAlignment(cell, HorizontalAlignment.forInt(align)); } /** * Take a cell, and align it. + * + * This is superior to cell.getCellStyle().setAlignment(align) because + * this method will not modify the CellStyle object that may be referenced + * by multiple cells. Instead, this method will search for existing CellStyles + * that match the desired CellStyle, creating a new CellStyle with the desired + * style if no match exists. * * @param cell the cell to set the alignment for - * @param align the column alignment to use. + * @param align the horizontal alignment to use. * - * @see CellStyle for alignment options + * @see HorizontalAlignment for alignment options + * @since POI 3.15 beta 3 */ - public static void setAlignment(Cell cell, short align) { - setCellStyleProperty(cell, ALIGNMENT, Short.valueOf(align)); + public static void setAlignment(Cell cell, HorizontalAlignment align) { + setCellStyleProperty(cell, ALIGNMENT, align); + } + + /** + * Take a cell, and vertically align it. + * + * This is superior to cell.getCellStyle().setVerticalAlignment(align) because + * this method will not modify the CellStyle object that may be referenced + * by multiple cells. Instead, this method will search for existing CellStyles + * that match the desired CellStyle, creating a new CellStyle with the desired + * style if no match exists. + * + * @param cell the cell to set the alignment for + * @param align the vertical alignment to use. + * + * @see VerticalAlignment for alignment options + * @since POI 3.15 beta 3 + */ + public static void setVerticalAlignment(Cell cell, VerticalAlignment align) { + setCellStyleProperty(cell, VERTICAL_ALIGNMENT, align); } /** @@ -182,8 +257,10 @@ public final class CellUtil { * @param font The Font that you want to set. * @throws IllegalArgumentException if <tt>font</tt> and <tt>cell</tt> do not belong to the same workbook * - * @deprecated 3.15-beta2. Removed in 3.17. Use {@link #setFont(Cell, Font)} instead. + * @deprecated 3.15-beta2. Use {@link #setFont(Cell, Font)} instead. */ + @Deprecated + @Removal(version="3.17") public static void setFont(Cell cell, Workbook workbook, Font font) { setFont(cell, font); } @@ -239,7 +316,7 @@ public final class CellUtil { CellStyle originalStyle = cell.getCellStyle(); CellStyle newStyle = null; Map<String, Object> values = getFormatProperties(originalStyle); - values.putAll(properties); + putAll(properties, values); // index seems like what index the cellstyle is in the list of styles for a workbook. // not good to compare on! @@ -283,8 +360,10 @@ public final class CellUtil { * @param propertyName The name of the property that is to be changed. * @param propertyValue The value of the property that is to be changed. * - * @deprecated 3.15-beta2. Removed in 3.17. Use {@link #setCellStyleProperty(Cell, String, Object)} instead. + * @deprecated 3.15-beta2. Use {@link #setCellStyleProperty(Cell, String, Object)} instead. */ + @Deprecated + @Removal(version="3.17") public static void setCellStyleProperty(Cell cell, Workbook workbook, String propertyName, Object propertyValue) { setCellStyleProperty(cell, propertyName, propertyValue); @@ -324,28 +403,58 @@ public final class CellUtil { */ private static Map<String, Object> getFormatProperties(CellStyle style) { Map<String, Object> properties = new HashMap<String, Object>(); - putShort(properties, ALIGNMENT, style.getAlignment()); - putBorderStyle(properties, BORDER_BOTTOM, style.getBorderBottom()); - putBorderStyle(properties, BORDER_LEFT, style.getBorderLeft()); - putBorderStyle(properties, BORDER_RIGHT, style.getBorderRight()); - putBorderStyle(properties, BORDER_TOP, style.getBorderTop()); - putShort(properties, BOTTOM_BORDER_COLOR, style.getBottomBorderColor()); - putShort(properties, DATA_FORMAT, style.getDataFormat()); - putShort(properties, FILL_PATTERN, style.getFillPattern()); - putShort(properties, FILL_FOREGROUND_COLOR, style.getFillForegroundColor()); - putShort(properties, FILL_BACKGROUND_COLOR, style.getFillBackgroundColor()); - putShort(properties, FONT, style.getFontIndex()); - putBoolean(properties, HIDDEN, style.getHidden()); - putShort(properties, INDENTION, style.getIndention()); - putShort(properties, LEFT_BORDER_COLOR, style.getLeftBorderColor()); - putBoolean(properties, LOCKED, style.getLocked()); - putShort(properties, RIGHT_BORDER_COLOR, style.getRightBorderColor()); - putShort(properties, ROTATION, style.getRotation()); - putShort(properties, TOP_BORDER_COLOR, style.getTopBorderColor()); - putShort(properties, VERTICAL_ALIGNMENT, style.getVerticalAlignment()); - putBoolean(properties, WRAP_TEXT, style.getWrapText()); + put(properties, ALIGNMENT, style.getAlignmentEnum()); + put(properties, VERTICAL_ALIGNMENT, style.getVerticalAlignmentEnum()); + put(properties, BORDER_BOTTOM, style.getBorderBottom()); + put(properties, BORDER_LEFT, style.getBorderLeft()); + put(properties, BORDER_RIGHT, style.getBorderRight()); + put(properties, BORDER_TOP, style.getBorderTop()); + put(properties, BOTTOM_BORDER_COLOR, style.getBottomBorderColor()); + put(properties, DATA_FORMAT, style.getDataFormat()); + put(properties, FILL_PATTERN, style.getFillPatternEnum()); + put(properties, FILL_FOREGROUND_COLOR, style.getFillForegroundColor()); + put(properties, FILL_BACKGROUND_COLOR, style.getFillBackgroundColor()); + put(properties, FONT, style.getFontIndex()); + put(properties, HIDDEN, style.getHidden()); + put(properties, INDENTION, style.getIndention()); + put(properties, LEFT_BORDER_COLOR, style.getLeftBorderColor()); + put(properties, LOCKED, style.getLocked()); + put(properties, RIGHT_BORDER_COLOR, style.getRightBorderColor()); + put(properties, ROTATION, style.getRotation()); + put(properties, TOP_BORDER_COLOR, style.getTopBorderColor()); + put(properties, WRAP_TEXT, style.getWrapText()); return properties; } + + /** + * Copies the entries in src to dest, using the preferential data type + * so that maps can be compared for equality + * + * @param src the property map to copy from (read-only) + * @param dest the property map to copy into + * @since POI 3.15 beta 3 + */ + private static void putAll(final Map<String, Object> src, Map<String, Object> dest) { + for (final String key : src.keySet()) { + if (shortValues.contains(key)) { + dest.put(key, getShort(src, key)); + } else if (booleanValues.contains(key)) { + dest.put(key, getBoolean(src, key)); + } else if (borderTypeValues.contains(key)) { + dest.put(key, getBorderStyle(src, key)); + } else if (ALIGNMENT.equals(key)) { + dest.put(key, getHorizontalAlignment(src, key)); + } else if (VERTICAL_ALIGNMENT.equals(key)) { + dest.put(key, getVerticalAlignment(src, key)); + } else if (FILL_PATTERN.equals(key)) { + dest.put(key, getFillPattern(src, key)); + } else { + if (log.check(POILogger.INFO)) { + log.log(POILogger.INFO, "Ignoring unrecognized CellUtil format properties key: " + key); + } + } + } + } /** * Sets the format properties of the given style based on the given map. @@ -356,14 +465,15 @@ public final class CellUtil { * @see #getFormatProperties(CellStyle) */ private static void setFormatProperties(CellStyle style, Workbook workbook, Map<String, Object> properties) { - style.setAlignment(getShort(properties, ALIGNMENT)); + style.setAlignment(getHorizontalAlignment(properties, ALIGNMENT)); + style.setVerticalAlignment(getVerticalAlignment(properties, VERTICAL_ALIGNMENT)); style.setBorderBottom(getBorderStyle(properties, BORDER_BOTTOM)); style.setBorderLeft(getBorderStyle(properties, BORDER_LEFT)); style.setBorderRight(getBorderStyle(properties, BORDER_RIGHT)); style.setBorderTop(getBorderStyle(properties, BORDER_TOP)); style.setBottomBorderColor(getShort(properties, BOTTOM_BORDER_COLOR)); style.setDataFormat(getShort(properties, DATA_FORMAT)); - style.setFillPattern(getShort(properties, FILL_PATTERN)); + 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))); @@ -374,7 +484,6 @@ public final class CellUtil { style.setRightBorderColor(getShort(properties, RIGHT_BORDER_COLOR)); style.setRotation(getShort(properties, ROTATION)); style.setTopBorderColor(getShort(properties, TOP_BORDER_COLOR)); - style.setVerticalAlignment(getShort(properties, VERTICAL_ALIGNMENT)); style.setWrapText(getBoolean(properties, WRAP_TEXT)); } @@ -409,13 +518,120 @@ public final class CellUtil { } // @deprecated 3.15 beta 2. getBorderStyle will only work on BorderStyle enums instead of codes in the future. else if (value instanceof Short) { + if (log.check(POILogger.WARN)) { + log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map uses Short values for " + + name + ". Should use BorderStyle enums instead."); + } + System.out.println("BorderStyle short usage"); short code = ((Short) value).shortValue(); border = BorderStyle.valueOf(code); } + else if (value == null) { + border = BorderStyle.NONE; + } else { - throw new RuntimeException("Unexpected border style class. Must be BorderStyle or Short (deprecated)"); + throw new RuntimeException("Unexpected border style class. Must be BorderStyle or Short (deprecated)."); } - return (border != null) ? border : BorderStyle.NONE; + return border; + } + + /** + * Utility method that returns the named FillPatternType value form the given map. + * + * @param properties map of named properties (String -> Object) + * @param name property name + * @return FillPatternType style if set, otherwise {@link FillPatternType#NO_FILL} + * @since POI 3.15 beta 3 + */ + private static FillPatternType getFillPattern(Map<String, Object> properties, String name) { + Object value = properties.get(name); + FillPatternType pattern; + if (value instanceof FillPatternType) { + pattern = (FillPatternType) value; + } + // @deprecated 3.15 beta 2. getFillPattern will only work on FillPatternType enums instead of codes in the future. + else if (value instanceof Short) { + if (log.check(POILogger.WARN)) { + log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map uses Short values for " + + name + ". Should use FillPatternType enums instead."); + } + System.out.println("FillPatternType short usage"); + short code = ((Short) value).shortValue(); + pattern = FillPatternType.forInt(code); + } + else if (value == null) { + pattern = FillPatternType.NO_FILL; + } + else { + throw new RuntimeException("Unexpected fill pattern style class. Must be FillPatternType or Short (deprecated)."); + } + return pattern; + } + + /** + * Utility method that returns the named HorizontalAlignment value form the given map. + * + * @param properties map of named properties (String -> Object) + * @param name property name + * @return HorizontalAlignment style if set, otherwise {@link HorizontalAlignment#GENERAL} + * @since POI 3.15 beta 3 + */ + private static HorizontalAlignment getHorizontalAlignment(Map<String, Object> properties, String name) { + Object value = properties.get(name); + HorizontalAlignment align; + if (value instanceof HorizontalAlignment) { + align = (HorizontalAlignment) value; + } + // @deprecated 3.15 beta 2. getHorizontalAlignment will only work on HorizontalAlignment enums instead of codes in the future. + else if (value instanceof Short) { + if (log.check(POILogger.WARN)) { + log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map used a Short value for " + + name + ". Should use HorizontalAlignment enums instead."); + } + System.out.println("HorizontalAlignment short usage"); + short code = ((Short) value).shortValue(); + align = HorizontalAlignment.forInt(code); + } + else if (value == null) { + align = HorizontalAlignment.GENERAL; + } + else { + throw new RuntimeException("Unexpected horizontal alignment style class. Must be HorizontalAlignment or Short (deprecated)."); + } + return align; + } + + /** + * Utility method that returns the named VerticalAlignment value form the given map. + * + * @param properties map of named properties (String -> Object) + * @param name property name + * @return VerticalAlignment style if set, otherwise {@link VerticalAlignment#BOTTOM} + * @since POI 3.15 beta 3 + */ + private static VerticalAlignment getVerticalAlignment(Map<String, Object> properties, String name) { + Object value = properties.get(name); + VerticalAlignment align; + if (value instanceof VerticalAlignment) { + align = (VerticalAlignment) value; + } + // @deprecated 3.15 beta 2. getVerticalAlignment will only work on VerticalAlignment enums instead of codes in the future. + else if (value instanceof Short) { + if (log.check(POILogger.WARN)) { + log.log(POILogger.WARN, "Deprecation warning: CellUtil properties map used a Short value for " + + name + ". Should use VerticalAlignment enums instead."); + } + System.out.println("VerticalAlignment usage " + name + " " + value); + short code = ((Short) value).shortValue(); + align = VerticalAlignment.forInt(code); + } + else if (value == null) { + align = VerticalAlignment.BOTTOM; + } + else { + throw new RuntimeException("Unexpected vertical alignment style class. Must be VerticalAlignment or Short (deprecated)."); + } + return align; } /** @@ -434,6 +650,17 @@ public final class CellUtil { } return false; } + + /** + * Utility method that puts the given value to the given map. + * + * @param properties map of properties (String -> Object) + * @param name property name + * @param value property value + */ + private static void put(Map<String, Object> properties, String name, Object value) { + properties.put(name, value); + } /** * Utility method that puts the named short value to the given map. @@ -443,18 +670,19 @@ public final class CellUtil { * @param value property value */ private static void putShort(Map<String, Object> properties, String name, short value) { - properties.put(name, Short.valueOf(value)); + properties.put(name, value); } - /** - * Utility method that puts the named short value to the given map. + /** + * Utility method that puts the named BorderStyle, HorizontalAlignment, VerticalAlignment, etc + * value to the given map. * * @param properties map of properties (String -> Object) * @param name property name * @param value property value */ - private static void putBorderStyle(Map<String, Object> properties, String name, BorderStyle border) { - properties.put(name, border); + private static void putEnum(Map<String, Object> properties, String name, Enum<?> value) { + properties.put(name, value); } /** @@ -465,7 +693,7 @@ public final class CellUtil { * @param value property value */ private static void putBoolean(Map<String, Object> properties, String name, boolean value) { - properties.put(name, Boolean.valueOf(value)); + properties.put(name, value); } /** |