#59836 - Tracker: Replace primitives with enums

preparation for removing those many HSSFColor subclasses with an enum

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1779866 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2017-01-22 22:01:22 +00:00
parent d9bce7fff9
commit c48a0cf334
17 changed files with 1054 additions and 1650 deletions

View File

@ -17,19 +17,22 @@
package org.apache.poi.hssf.usermodel.examples; package org.apache.poi.hssf.usermodel.examples;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.FillPatternType;
/** /**
* Demonstrates many features of the user API at once. Used in the HOW-TO guide. * Demonstrates many features of the user API at once. Used in the HOW-TO guide.
*
* @author Glen Stampoultzis (glens at apache.org)
* @author Andrew Oliver (acoliver at apache.org)
*/ */
public class BigExample { public class BigExample {
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
@ -56,17 +59,17 @@ public class BigExample {
//set font 1 to 12 point type //set font 1 to 12 point type
f.setFontHeightInPoints((short) 12); f.setFontHeightInPoints((short) 12);
//make it red //make it red
f.setColor(HSSFColor.RED.index); f.setColor(HSSFColorPredefined.RED.getIndex());
// make it bold // make it bold
//arial is the default font //arial is the default font
f.setBoldweight(Font.BOLDWEIGHT_BOLD); f.setBold(true);
//set font 2 to 10 point type //set font 2 to 10 point type
f2.setFontHeightInPoints((short) 10); f2.setFontHeightInPoints((short) 10);
//make it the color at palette index 0xf (white) //make it the color at palette index 0xf (white)
f2.setColor(HSSFColor.WHITE.index); f2.setColor(HSSFColorPredefined.WHITE.getIndex());
//make it bold //make it bold
f2.setBoldweight(Font.BOLDWEIGHT_BOLD); f2.setBold(true);
//set cell stlye //set cell stlye
cs.setFont(f); cs.setFont(f);
@ -74,11 +77,11 @@ public class BigExample {
cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)")); cs.setDataFormat(HSSFDataFormat.getBuiltinFormat("($#,##0_);[Red]($#,##0)"));
//set a thin border //set a thin border
cs2.setBorderBottom(CellStyle.BORDER_THIN); cs2.setBorderBottom(BorderStyle.THIN);
//fill w fg fill color //fill w fg fill color
cs2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); cs2.setFillPattern(FillPatternType.SOLID_FOREGROUND);
// set foreground fill to red // set foreground fill to red
cs2.setFillForegroundColor(HSSFColor.RED.index); cs2.setFillForegroundColor(HSSFColorPredefined.RED.getIndex());
// set the font // set the font
cs2.setFont(f2); cs2.setFont(f2);
@ -143,7 +146,7 @@ public class BigExample {
// define the third style to be the default // define the third style to be the default
// except with a thick black border at the bottom // except with a thick black border at the bottom
cs3.setBorderBottom(CellStyle.BORDER_THICK); cs3.setBorderBottom(BorderStyle.THICK);
//create 50 cells //create 50 cells
for (int cellnum =0; cellnum < 50; cellnum++) { for (int cellnum =0; cellnum < 50; cellnum++) {

View File

@ -25,7 +25,7 @@ import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BorderStyle;
/** /**
@ -46,13 +46,13 @@ public class Borders {
// Style the cell with borders all around. // Style the cell with borders all around.
HSSFCellStyle style = wb.createCellStyle(); HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(BorderStyle.THIN); style.setBorderBottom(BorderStyle.THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index); style.setBottomBorderColor(HSSFColorPredefined.BLACK.getIndex());
style.setBorderLeft(BorderStyle.THIN); style.setBorderLeft(BorderStyle.THIN);
style.setLeftBorderColor(HSSFColor.GREEN.index); style.setLeftBorderColor(HSSFColorPredefined.GREEN.getIndex());
style.setBorderRight(BorderStyle.THIN); style.setBorderRight(BorderStyle.THIN);
style.setRightBorderColor(HSSFColor.BLUE.index); style.setRightBorderColor(HSSFColorPredefined.BLUE.getIndex());
style.setBorderTop(BorderStyle.MEDIUM_DASHED); style.setBorderTop(BorderStyle.MEDIUM_DASHED);
style.setTopBorderColor(HSSFColor.ORANGE.index); style.setTopBorderColor(HSSFColorPredefined.ORANGE.getIndex());
cell.setCellStyle(style); cell.setCellStyle(style);
// Write the output to a file // Write the output to a file

View File

@ -28,7 +28,7 @@ import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRichTextString; import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
/** /**
* Demonstrates how to work with excel cell comments. * Demonstrates how to work with excel cell comments.
@ -81,7 +81,7 @@ public class CellComments {
font.setFontName("Arial"); font.setFontName("Arial");
font.setFontHeightInPoints((short)10); font.setFontHeightInPoints((short)10);
font.setBold(true); font.setBold(true);
font.setColor(HSSFColor.RED.index); font.setColor(HSSFColorPredefined.RED.getIndex());
string.applyFont(font); string.applyFont(font);
comment2.setString(string); comment2.setString(string);

View File

@ -25,7 +25,7 @@ import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.FillPatternType;
/** /**
@ -41,7 +41,7 @@ public class FrillsAndFills {
// Aqua background // Aqua background
HSSFCellStyle style = wb.createCellStyle(); HSSFCellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index); style.setFillBackgroundColor(HSSFColorPredefined.AQUA.getIndex());
style.setFillPattern(FillPatternType.BIG_SPOTS); style.setFillPattern(FillPatternType.BIG_SPOTS);
HSSFCell cell = row.createCell(1); HSSFCell cell = row.createCell(1);
cell.setCellValue("X"); cell.setCellValue("X");
@ -49,7 +49,7 @@ public class FrillsAndFills {
// Orange "foreground", foreground being the fill foreground not the font color. // Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle(); style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index); style.setFillForegroundColor(HSSFColorPredefined.ORANGE.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND); style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
cell = row.createCell(2); cell = row.createCell(2);
cell.setCellValue("X"); cell.setCellValue("X");

View File

@ -28,13 +28,11 @@ import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFHyperlink; import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Font;
/** /**
* Demonstrates how to create hyperlinks. * Demonstrates how to create hyperlinks.
*
* @author Yegor Kozlov (yegor at apach.org)
*/ */
public class Hyperlinks { public class Hyperlinks {
@ -47,7 +45,7 @@ public class Hyperlinks {
HSSFCellStyle hlink_style = wb.createCellStyle(); HSSFCellStyle hlink_style = wb.createCellStyle();
HSSFFont hlink_font = wb.createFont(); HSSFFont hlink_font = wb.createFont();
hlink_font.setUnderline(Font.U_SINGLE); hlink_font.setUnderline(Font.U_SINGLE);
hlink_font.setColor(HSSFColor.BLUE.index); hlink_font.setColor(HSSFColorPredefined.BLUE.getIndex());
hlink_style.setFont(hlink_font); hlink_style.setFont(hlink_font);
HSSFCell cell; HSSFCell cell;

View File

@ -18,6 +18,8 @@
package org.apache.poi.hssf.view; package org.apache.poi.hssf.view;
import static org.apache.poi.hssf.view.SVTableUtils.getAWTColor;
import java.awt.Color; import java.awt.Color;
import java.awt.Component; import java.awt.Component;
import java.awt.Font; import java.awt.Font;
@ -25,7 +27,6 @@ import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.util.EventObject; import java.util.EventObject;
import java.util.Map;
import javax.swing.AbstractCellEditor; import javax.swing.AbstractCellEditor;
import javax.swing.JTable; import javax.swing.JTable;
@ -37,7 +38,7 @@ import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.FillPatternType;
/** /**
@ -47,10 +48,8 @@ import org.apache.poi.ss.usermodel.FillPatternType;
* @author Jason Height * @author Jason Height
*/ */
public class SVTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener { public class SVTableCellEditor extends AbstractCellEditor implements TableCellEditor, ActionListener {
private static final Color black = getAWTColor(new HSSFColor.BLACK()); private static final Color black = getAWTColor(HSSFColorPredefined.BLACK);
private static final Color white = getAWTColor(new HSSFColor.WHITE()); private static final Color white = getAWTColor(HSSFColorPredefined.WHITE);
private Map<Integer,HSSFColor> colors = HSSFColor.getIndexHash();
private HSSFWorkbook wb; private HSSFWorkbook wb;
private JTextField editor; private JTextField editor;
@ -142,18 +141,26 @@ public Component getTableCellEditorComponent(JTable table, Object value,
int fontstyle = Font.PLAIN; int fontstyle = Font.PLAIN;
if (isbold) fontstyle = Font.BOLD; if (isbold) {
if (isitalics) fontstyle = fontstyle | Font.ITALIC; fontstyle = Font.BOLD;
}
if (isitalics) {
fontstyle = fontstyle | Font.ITALIC;
}
int fontheight = f.getFontHeightInPoints(); int fontheight = f.getFontHeightInPoints();
if (fontheight == 9) fontheight = 10; //fix for stupid ol Windows if (fontheight == 9) {
fontheight = 10; //fix for stupid ol Windows
}
Font font = new Font(f.getFontName(),fontstyle,fontheight); Font font = new Font(f.getFontName(),fontstyle,fontheight);
editor.setFont(font); editor.setFont(font);
if (style.getFillPatternEnum() == FillPatternType.SOLID_FOREGROUND) { if (style.getFillPatternEnum() == FillPatternType.SOLID_FOREGROUND) {
editor.setBackground(getAWTColor(style.getFillForegroundColor(), white)); editor.setBackground(getAWTColor(style.getFillForegroundColor(), white));
} else editor.setBackground(white); } else {
editor.setBackground(white);
}
editor.setForeground(getAWTColor(f.getColor(), black)); editor.setForeground(getAWTColor(f.getColor(), black));
@ -202,18 +209,4 @@ public Component getTableCellEditorComponent(JTable table, Object value,
} }
return editor; return editor;
} }
/** This method retrieves the AWT Color representation from the colour hash table
*
*/
private final Color getAWTColor(int index, Color deflt) {
HSSFColor clr = colors.get(index);
if (clr == null) return deflt;
return getAWTColor(clr);
}
private static final Color getAWTColor(HSSFColor clr) {
short[] rgb = clr.getTriplet();
return new Color(rgb[0],rgb[1],rgb[2]);
}
} }

View File

@ -18,12 +18,16 @@
package org.apache.poi.hssf.view; package org.apache.poi.hssf.view;
import java.util.*; import java.awt.Color;
import java.awt.*; import java.awt.Font;
import javax.swing.border.*; import java.util.Map;
import org.apache.poi.hssf.usermodel.*; import javax.swing.border.Border;
import org.apache.poi.hssf.util.*; import javax.swing.border.EmptyBorder;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
/** /**
* SVTableCell Editor and Renderer helper functions. * SVTableCell Editor and Renderer helper functions.
@ -33,9 +37,9 @@ import org.apache.poi.hssf.util.*;
public class SVTableUtils { public class SVTableUtils {
private final static Map<Integer,HSSFColor> colors = HSSFColor.getIndexHash(); private final static Map<Integer,HSSFColor> colors = HSSFColor.getIndexHash();
/** Description of the Field */ /** Description of the Field */
public final static Color black = getAWTColor(new HSSFColor.BLACK()); public final static Color black = getAWTColor(HSSFColorPredefined.BLACK);
/** Description of the Field */ /** Description of the Field */
public final static Color white = getAWTColor(new HSSFColor.WHITE()); public final static Color white = getAWTColor(HSSFColorPredefined.WHITE);
/** Description of the Field */ /** Description of the Field */
public static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); public static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1);
@ -44,7 +48,7 @@ public class SVTableUtils {
* Creates a new font for a specific cell style * Creates a new font for a specific cell style
*/ */
public static Font makeFont(HSSFFont font) { public static Font makeFont(HSSFFont font) {
boolean isbold = font.getBoldweight() > HSSFFont.BOLDWEIGHT_NORMAL; boolean isbold = font.getBold();
boolean isitalics = font.getItalic(); boolean isitalics = font.getItalic();
int fontstyle = Font.PLAIN; int fontstyle = Font.PLAIN;
if (isbold) { if (isbold) {
@ -63,30 +67,19 @@ public class SVTableUtils {
return new Font(font.getFontName(), fontstyle, fontheight); return new Font(font.getFontName(), fontstyle, fontheight);
} }
/** This method retrieves the AWT Color representation from the colour hash table
/**
* This method retrieves the AWT Color representation from the colour hash table
* *
* @param index Description of the Parameter
* @param deflt Description of the Parameter
* @return The aWTColor value
*/ */
public final static Color getAWTColor(int index, Color deflt) { /* package */ static final Color getAWTColor(int index, Color deflt) {
HSSFColor clr = colors.get(index); HSSFColor clr = colors.get(index);
if (clr == null) { if (clr == null) {
return deflt; return deflt;
} }
return getAWTColor(clr); short[] rgb = clr.getTriplet();
return new Color(rgb[0],rgb[1],rgb[2]);
} }
/* package */ static final Color getAWTColor(HSSFColorPredefined clr) {
/**
* Gets the aWTColor attribute of the SVTableUtils class
*
* @param clr Description of the Parameter
* @return The aWTColor value
*/
public final static Color getAWTColor(HSSFColor clr) {
short[] rgb = clr.getTriplet(); short[] rgb = clr.getTriplet();
return new Color(rgb[0],rgb[1],rgb[2]); return new Color(rgb[0],rgb[1],rgb[2]);
} }

View File

@ -16,24 +16,23 @@
==================================================================== */ ==================================================================== */
package org.apache.poi.ss.examples.html; package org.apache.poi.ss.examples.html;
import java.util.Formatter;
import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette; import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import java.util.Formatter;
/** /**
* Implementation of {@link HtmlHelper} for HSSF files. * Implementation of {@link HtmlHelper} for HSSF files.
*
* @author Ken Arnold, Industrious Media LLC
*/ */
public class HSSFHtmlHelper implements HtmlHelper { public class HSSFHtmlHelper implements HtmlHelper {
private final HSSFWorkbook wb; private final HSSFWorkbook wb;
private final HSSFPalette colors; private final HSSFPalette colors;
private static final HSSFColor HSSF_AUTO = new HSSFColor.AUTOMATIC(); private static final HSSFColor HSSF_AUTO = HSSFColorPredefined.AUTOMATIC.getColor();
public HSSFHtmlHelper(HSSFWorkbook wb) { public HSSFHtmlHelper(HSSFWorkbook wb) {
this.wb = wb; this.wb = wb;
@ -45,7 +44,7 @@ public class HSSFHtmlHelper implements HtmlHelper {
@Override @Override
public void colorStyles(CellStyle style, Formatter out) { public void colorStyles(CellStyle style, Formatter out) {
HSSFCellStyle cs = (HSSFCellStyle) style; HSSFCellStyle cs = (HSSFCellStyle) style;
out.format(" /* fill pattern = %d */%n", cs.getFillPattern()); out.format(" /* fill pattern = %d */%n", cs.getFillPatternEnum().getCode());
styleColor(out, "background-color", cs.getFillForegroundColor()); styleColor(out, "background-color", cs.getFillForegroundColor());
styleColor(out, "color", cs.getFont(wb).getColor()); styleColor(out, "color", cs.getFont(wb).getColor());
styleColor(out, "border-left-color", cs.getLeftBorderColor()); styleColor(out, "border-left-color", cs.getLeftBorderColor());

View File

@ -88,7 +88,7 @@ import org.apache.poi.hssf.record.WriteAccessRecord;
import org.apache.poi.hssf.record.WriteProtectRecord; import org.apache.poi.hssf.record.WriteProtectRecord;
import org.apache.poi.hssf.record.common.UnicodeString; import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey; import org.apache.poi.hssf.record.crypto.Biff8EncryptionKey;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.poifs.crypt.CryptoFunctions; import org.apache.poi.poifs.crypt.CryptoFunctions;
import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.crypt.Decryptor;
import org.apache.poi.poifs.crypt.EncryptionInfo; import org.apache.poi.poifs.crypt.EncryptionInfo;
@ -231,9 +231,10 @@ public final class InternalWorkbook {
* @return Workbook object * @return Workbook object
*/ */
public static InternalWorkbook createWorkbook(List<Record> recs) { public static InternalWorkbook createWorkbook(List<Record> recs) {
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "Workbook (readfile) created with reclen=", log.log(DEBUG, "Workbook (readfile) created with reclen=",
Integer.valueOf(recs.size())); Integer.valueOf(recs.size()));
}
InternalWorkbook retval = new InternalWorkbook(); InternalWorkbook retval = new InternalWorkbook();
List<Record> records = new ArrayList<Record>(recs.size() / 3); List<Record> records = new ArrayList<Record>(recs.size() / 3);
retval.records.setRecords(records); retval.records.setRecords(records);
@ -244,54 +245,62 @@ public final class InternalWorkbook {
if (rec.getSid() == EOFRecord.sid) { if (rec.getSid() == EOFRecord.sid) {
records.add(rec); records.add(rec);
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found workbook eof record at " + k); log.log(DEBUG, "found workbook eof record at " + k);
}
break; break;
} }
switch (rec.getSid()) { switch (rec.getSid()) {
case BoundSheetRecord.sid : case BoundSheetRecord.sid :
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found boundsheet record at " + k); log.log(DEBUG, "found boundsheet record at " + k);
}
retval.boundsheets.add((BoundSheetRecord) rec); retval.boundsheets.add((BoundSheetRecord) rec);
retval.records.setBspos( k ); retval.records.setBspos( k );
break; break;
case SSTRecord.sid : case SSTRecord.sid :
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found sst record at " + k); log.log(DEBUG, "found sst record at " + k);
}
retval.sst = ( SSTRecord ) rec; retval.sst = ( SSTRecord ) rec;
break; break;
case FontRecord.sid : case FontRecord.sid :
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found font record at " + k); log.log(DEBUG, "found font record at " + k);
}
retval.records.setFontpos( k ); retval.records.setFontpos( k );
retval.numfonts++; retval.numfonts++;
break; break;
case ExtendedFormatRecord.sid : case ExtendedFormatRecord.sid :
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found XF record at " + k); log.log(DEBUG, "found XF record at " + k);
}
retval.records.setXfpos( k ); retval.records.setXfpos( k );
retval.numxfs++; retval.numxfs++;
break; break;
case TabIdRecord.sid : case TabIdRecord.sid :
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found tabid record at " + k); log.log(DEBUG, "found tabid record at " + k);
}
retval.records.setTabpos( k ); retval.records.setTabpos( k );
break; break;
case ProtectRecord.sid : case ProtectRecord.sid :
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found protect record at " + k); log.log(DEBUG, "found protect record at " + k);
}
retval.records.setProtpos( k ); retval.records.setProtpos( k );
break; break;
case BackupRecord.sid : case BackupRecord.sid :
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found backup record at " + k); log.log(DEBUG, "found backup record at " + k);
}
retval.records.setBackuppos( k ); retval.records.setBackuppos( k );
break; break;
case ExternSheetRecord.sid : case ExternSheetRecord.sid :
@ -299,57 +308,67 @@ public final class InternalWorkbook {
case NameRecord.sid : case NameRecord.sid :
case SupBookRecord.sid : case SupBookRecord.sid :
// LinkTable can start with either of these // LinkTable can start with either of these
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found SupBook record at " + k); log.log(DEBUG, "found SupBook record at " + k);
}
retval.linkTable = new LinkTable(recs, k, retval.records, retval.commentRecords); retval.linkTable = new LinkTable(recs, k, retval.records, retval.commentRecords);
k+=retval.linkTable.getRecordCount() - 1; k+=retval.linkTable.getRecordCount() - 1;
continue; continue;
case FormatRecord.sid : case FormatRecord.sid :
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found format record at " + k); log.log(DEBUG, "found format record at " + k);
}
retval.formats.add((FormatRecord) rec); retval.formats.add((FormatRecord) rec);
retval.maxformatid = retval.maxformatid >= ((FormatRecord)rec).getIndexCode() ? retval.maxformatid : ((FormatRecord)rec).getIndexCode(); retval.maxformatid = retval.maxformatid >= ((FormatRecord)rec).getIndexCode() ? retval.maxformatid : ((FormatRecord)rec).getIndexCode();
break; break;
case DateWindow1904Record.sid : case DateWindow1904Record.sid :
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found datewindow1904 record at " + k); log.log(DEBUG, "found datewindow1904 record at " + k);
}
retval.uses1904datewindowing = ((DateWindow1904Record)rec).getWindowing() == 1; retval.uses1904datewindowing = ((DateWindow1904Record)rec).getWindowing() == 1;
break; break;
case PaletteRecord.sid: case PaletteRecord.sid:
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found palette record at " + k); log.log(DEBUG, "found palette record at " + k);
}
retval.records.setPalettepos( k ); retval.records.setPalettepos( k );
break; break;
case WindowOneRecord.sid: case WindowOneRecord.sid:
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found WindowOneRecord at " + k); log.log(DEBUG, "found WindowOneRecord at " + k);
}
retval.windowOne = (WindowOneRecord) rec; retval.windowOne = (WindowOneRecord) rec;
break; break;
case WriteAccessRecord.sid: case WriteAccessRecord.sid:
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found WriteAccess at " + k); log.log(DEBUG, "found WriteAccess at " + k);
}
retval.writeAccess = (WriteAccessRecord) rec; retval.writeAccess = (WriteAccessRecord) rec;
break; break;
case WriteProtectRecord.sid: case WriteProtectRecord.sid:
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found WriteProtect at " + k); log.log(DEBUG, "found WriteProtect at " + k);
}
retval.writeProtect = (WriteProtectRecord) rec; retval.writeProtect = (WriteProtectRecord) rec;
break; break;
case FileSharingRecord.sid: case FileSharingRecord.sid:
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found FileSharing at " + k); log.log(DEBUG, "found FileSharing at " + k);
}
retval.fileShare = (FileSharingRecord) rec; retval.fileShare = (FileSharingRecord) rec;
break; break;
case NameCommentRecord.sid: case NameCommentRecord.sid:
final NameCommentRecord ncr = (NameCommentRecord) rec; final NameCommentRecord ncr = (NameCommentRecord) rec;
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "found NameComment at " + k); log.log(DEBUG, "found NameComment at " + k);
}
retval.commentRecords.put(ncr.getNameText(), ncr); retval.commentRecords.put(ncr.getNameText(), ncr);
break; break;
default: default:
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "ignoring record (sid=" + rec.getSid() + ") at " + k); log.log(DEBUG, "ignoring record (sid=" + rec.getSid() + ") at " + k);
}
break; break;
} }
records.add(rec); records.add(rec);
@ -376,8 +395,9 @@ public final class InternalWorkbook {
if (retval.windowOne == null) { if (retval.windowOne == null) {
retval.windowOne = createWindowOne(); retval.windowOne = createWindowOne();
} }
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "exit create workbook from existing file function"); log.log(DEBUG, "exit create workbook from existing file function");
}
return retval; return retval;
} }
@ -389,8 +409,9 @@ public final class InternalWorkbook {
*/ */
public static InternalWorkbook createWorkbook() public static InternalWorkbook createWorkbook()
{ {
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log( DEBUG, "creating new workbook from scratch" ); log.log( DEBUG, "creating new workbook from scratch" );
}
InternalWorkbook retval = new InternalWorkbook(); InternalWorkbook retval = new InternalWorkbook();
List<Record> records = new ArrayList<Record>( 30 ); List<Record> records = new ArrayList<Record>( 30 );
retval.records.setRecords(records); retval.records.setRecords(records);
@ -463,8 +484,9 @@ public final class InternalWorkbook {
records.add(InternalWorkbook.createExtendedSST()); records.add(InternalWorkbook.createExtendedSST());
records.add(EOFRecord.instance); records.add(EOFRecord.instance);
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log( DEBUG, "exit create new workbook from scratch" ); log.log( DEBUG, "exit create new workbook from scratch" );
}
return retval; return retval;
} }
@ -592,9 +614,10 @@ public final class InternalWorkbook {
*/ */
public void setSheetBof(int sheetIndex, int pos) { public void setSheetBof(int sheetIndex, int pos) {
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "setting bof for sheetnum =", Integer.valueOf(sheetIndex), log.log(DEBUG, "setting bof for sheetnum =", Integer.valueOf(sheetIndex),
" at pos=", Integer.valueOf(pos)); " at pos=", Integer.valueOf(pos));
}
checkSheets(sheetIndex); checkSheets(sheetIndex);
getBoundSheetRec(sheetIndex) getBoundSheetRec(sheetIndex)
.setPositionOfBof(pos); .setPositionOfBof(pos);
@ -626,7 +649,9 @@ public final class InternalWorkbook {
checkSheets(sheetnum); checkSheets(sheetnum);
// YK: Mimic Excel and silently truncate sheet names longer than 31 characters // YK: Mimic Excel and silently truncate sheet names longer than 31 characters
if(sheetname.length() > 31) sheetname = sheetname.substring(0, 31); if(sheetname.length() > 31) {
sheetname = sheetname.substring(0, 31);
}
BoundSheetRecord sheet = boundsheets.get(sheetnum); BoundSheetRecord sheet = boundsheets.get(sheetnum);
sheet.setSheetname(sheetname); sheet.setSheetname(sheetname);
@ -868,8 +893,9 @@ public final class InternalWorkbook {
*/ */
public int getNumSheets() { public int getNumSheets() {
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "getNumSheets=", Integer.valueOf(boundsheets.size())); log.log(DEBUG, "getNumSheets=", Integer.valueOf(boundsheets.size()));
}
return boundsheets.size(); return boundsheets.size();
} }
@ -880,8 +906,9 @@ public final class InternalWorkbook {
*/ */
public int getNumExFormats() { public int getNumExFormats() {
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "getXF=", Integer.valueOf(numxfs)); log.log(DEBUG, "getXF=", Integer.valueOf(numxfs));
}
return numxfs; return numxfs;
} }
@ -1019,8 +1046,9 @@ public final class InternalWorkbook {
*/ */
public int addSSTString(UnicodeString string) { public int addSSTString(UnicodeString string) {
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "insert to sst string='", string); log.log(DEBUG, "insert to sst string='", string);
}
if (sst == null) { if (sst == null) {
insertSST(); insertSST();
} }
@ -1038,9 +1066,10 @@ public final class InternalWorkbook {
} }
UnicodeString retval = sst.getString(str); UnicodeString retval = sst.getString(str);
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "Returning SST for index=", Integer.valueOf(str), log.log(DEBUG, "Returning SST for index=", Integer.valueOf(str),
" String= ", retval); " String= ", retval);
}
return retval; return retval;
} }
@ -1052,8 +1081,9 @@ public final class InternalWorkbook {
*/ */
public void insertSST() { public void insertSST() {
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log(DEBUG, "creating new SST via insertSST!"); log.log(DEBUG, "creating new SST via insertSST!");
}
sst = new SSTRecord(); sst = new SSTRecord();
records.add(records.size() - 1, createExtendedSST()); records.add(records.size() - 1, createExtendedSST());
records.add(records.size() - 2, sst); records.add(records.size() - 2, sst);
@ -1096,8 +1126,9 @@ public final class InternalWorkbook {
*/ */
public int serialize( int offset, byte[] data ) public int serialize( int offset, byte[] data )
{ {
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log( DEBUG, "Serializing Workbook with offsets" ); log.log( DEBUG, "Serializing Workbook with offsets" );
}
int pos = 0; int pos = 0;
@ -1133,8 +1164,9 @@ public final class InternalWorkbook {
///// DEBUG END ///// ///// DEBUG END /////
pos += len; // rec.length; pos += len; // rec.length;
} }
if (log.check( POILogger.DEBUG )) if (log.check( POILogger.DEBUG )) {
log.log( DEBUG, "Exiting serialize workbook" ); log.log( DEBUG, "Exiting serialize workbook" );
}
return pos; return pos;
} }
@ -1207,14 +1239,16 @@ public final class InternalWorkbook {
for ( int k = 0; k < records.size(); k++ ) for ( int k = 0; k < records.size(); k++ )
{ {
Record record = records.get( k ); Record record = records.get( k );
if (record instanceof SSTRecord) if (record instanceof SSTRecord) {
sst = (SSTRecord)record; sst = (SSTRecord)record;
}
if (record.getSid() == ExtSSTRecord.sid && sst != null) if (record.getSid() == ExtSSTRecord.sid && sst != null) {
retval += sst.calcExtSSTRecordSize(); retval += sst.calcExtSSTRecordSize();
else } else {
retval += record.getRecordSize(); retval += record.getRecordSize();
} }
}
return retval; return retval;
} }
@ -1250,7 +1284,9 @@ public final class InternalWorkbook {
try { try {
String username = System.getProperty("user.name"); String username = System.getProperty("user.name");
// Google App engine returns null for user.name, see Bug 53974 // Google App engine returns null for user.name, see Bug 53974
if(username == null) username = defaultUserName; if(username == null) {
username = defaultUserName;
}
retval.setUsername(username); retval.setUsername(username);
} catch (AccessControlException e) { } catch (AccessControlException e) {
@ -1799,10 +1835,10 @@ public final class InternalWorkbook {
retval.setPaletteOptions(( short ) 0); retval.setPaletteOptions(( short ) 0);
retval.setAdtlPaletteOptions(( short ) 0); retval.setAdtlPaletteOptions(( short ) 0);
retval.setFillPaletteOptions(( short ) 0x20c0); retval.setFillPaletteOptions(( short ) 0x20c0);
retval.setTopBorderPaletteIdx(HSSFColor.BLACK.index); retval.setTopBorderPaletteIdx(HSSFColorPredefined.BLACK.getIndex());
retval.setBottomBorderPaletteIdx(HSSFColor.BLACK.index); retval.setBottomBorderPaletteIdx(HSSFColorPredefined.BLACK.getIndex());
retval.setLeftBorderPaletteIdx(HSSFColor.BLACK.index); retval.setLeftBorderPaletteIdx(HSSFColorPredefined.BLACK.getIndex());
retval.setRightBorderPaletteIdx(HSSFColor.BLACK.index); retval.setRightBorderPaletteIdx(HSSFColorPredefined.BLACK.getIndex());
return retval; return retval;
} }
@ -2172,8 +2208,9 @@ public final class InternalWorkbook {
FormatRecord rec = new FormatRecord(maxformatid, formatString); FormatRecord rec = new FormatRecord(maxformatid, formatString);
int pos = 0; int pos = 0;
while ( pos < records.size() && records.get( pos ).getSid() != FormatRecord.sid ) while ( pos < records.size() && records.get( pos ).getSid() != FormatRecord.sid ) {
pos++; pos++;
}
pos += formats.size(); pos += formats.size();
formats.add( rec ); formats.add( rec );
records.add( pos, rec ); records.add( pos, rec );
@ -2226,10 +2263,11 @@ public final class InternalWorkbook {
int matches = 0; int matches = 0;
for (Record record : records) { for (Record record : records) {
if (record.getSid() == sid) { if (record.getSid() == sid) {
if (matches++ == pos) if (matches++ == pos) {
return record; return record;
} }
} }
}
return null; return null;
} }
@ -2266,7 +2304,9 @@ public final class InternalWorkbook {
Record rec = records.get(palettePos); Record rec = records.get(palettePos);
if (rec instanceof PaletteRecord) { if (rec instanceof PaletteRecord) {
palette = (PaletteRecord) rec; palette = (PaletteRecord) rec;
} else throw new RuntimeException("InternalError: Expected PaletteRecord but got a '"+rec+"'"); } else {
throw new RuntimeException("InternalError: Expected PaletteRecord but got a '"+rec+"'");
}
} }
else else
{ {
@ -2316,7 +2356,9 @@ public final class InternalWorkbook {
drawingManager = new DrawingManager2(dgg); drawingManager = new DrawingManager2(dgg);
if(bStore != null){ if(bStore != null){
for(EscherRecord bs : bStore.getChildRecords()){ for(EscherRecord bs : bStore.getChildRecords()){
if(bs instanceof EscherBSERecord) escherBSERecords.add((EscherBSERecord)bs); if(bs instanceof EscherBSERecord) {
escherBSERecords.add((EscherBSERecord)bs);
}
} }
} }
return drawingManager; return drawingManager;
@ -2344,7 +2386,9 @@ public final class InternalWorkbook {
drawingManager = new DrawingManager2(dgg); drawingManager = new DrawingManager2(dgg);
if(bStore != null){ if(bStore != null){
for(EscherRecord bs : bStore.getChildRecords()){ for(EscherRecord bs : bStore.getChildRecords()){
if(bs instanceof EscherBSERecord) escherBSERecords.add((EscherBSERecord)bs); if(bs instanceof EscherBSERecord) {
escherBSERecords.add((EscherBSERecord)bs);
}
} }
} }
} }
@ -2395,8 +2439,9 @@ public final class InternalWorkbook {
splitMenuColors.setColor4(0x100000F7); splitMenuColors.setColor4(0x100000F7);
dggContainer.addChildRecord(dgg); dggContainer.addChildRecord(dgg);
if (bstoreContainer != null) if (bstoreContainer != null) {
dggContainer.addChildRecord( bstoreContainer ); dggContainer.addChildRecord( bstoreContainer );
}
dggContainer.addChildRecord(opt); dggContainer.addChildRecord(opt);
dggContainer.addChildRecord(splitMenuColors); dggContainer.addChildRecord(splitMenuColors);

View File

@ -26,6 +26,7 @@ import org.apache.poi.hssf.record.FontRecord;
import org.apache.poi.hssf.record.FormatRecord; import org.apache.poi.hssf.record.FormatRecord;
import org.apache.poi.hssf.record.StyleRecord; import org.apache.poi.hssf.record.StyleRecord;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.BorderStyle; import org.apache.poi.ss.usermodel.BorderStyle;
import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.FillPatternType;
@ -112,6 +113,7 @@ public final class HSSFCellStyle implements CellStyle {
// avoid multi-threading issues when different workbooks are accessed in // avoid multi-threading issues when different workbooks are accessed in
// multiple threads at the same time // multiple threads at the same time
private static final ThreadLocal<Short> lastDateFormat = new ThreadLocal<Short>() { private static final ThreadLocal<Short> lastDateFormat = new ThreadLocal<Short>() {
@Override
protected Short initialValue() { protected Short initialValue() {
return Short.MIN_VALUE; return Short.MIN_VALUE;
} }
@ -273,6 +275,7 @@ public final class HSSFCellStyle implements CellStyle {
* @see #ALIGN_CENTER_SELECTION * @see #ALIGN_CENTER_SELECTION
* @deprecated POI 3.15 beta 3. Use {@link #setAlignment(HorizontalAlignment)} instead. * @deprecated POI 3.15 beta 3. Use {@link #setAlignment(HorizontalAlignment)} instead.
*/ */
@Deprecated
@Removal(version="3.17") @Removal(version="3.17")
@Override @Override
public void setAlignment(short align) public void setAlignment(short align)
@ -303,6 +306,7 @@ public final class HSSFCellStyle implements CellStyle {
* @see #ALIGN_CENTER_SELECTION * @see #ALIGN_CENTER_SELECTION
* @deprecated POI 3.15 beta 3. Use {@link #getAlignmentEnum()} instead. * @deprecated POI 3.15 beta 3. Use {@link #getAlignmentEnum()} instead.
*/ */
@Deprecated
@Override @Override
public short getAlignment() public short getAlignment()
{ {
@ -349,6 +353,7 @@ public final class HSSFCellStyle implements CellStyle {
* @see VerticalAlignment * @see VerticalAlignment
* @deprecated POI 3.15 beta 3. Use {@link #setVerticalAlignment(VerticalAlignment)} instead. * @deprecated POI 3.15 beta 3. Use {@link #setVerticalAlignment(VerticalAlignment)} instead.
*/ */
@Deprecated
@Removal(version="3.17") @Removal(version="3.17")
@Override @Override
public void setVerticalAlignment(short align) public void setVerticalAlignment(short align)
@ -375,6 +380,7 @@ public final class HSSFCellStyle implements CellStyle {
* @see VerticalAlignment * @see VerticalAlignment
* @deprecated POI 3.15 beta 3. Use {@link #getVerticalAlignmentEnum()} instead. * @deprecated POI 3.15 beta 3. Use {@link #getVerticalAlignmentEnum()} instead.
*/ */
@Deprecated
@Override @Override
public short getVerticalAlignment() public short getVerticalAlignment()
{ {
@ -480,6 +486,7 @@ public final class HSSFCellStyle implements CellStyle {
* @see #BORDER_SLANTED_DASH_DOT * @see #BORDER_SLANTED_DASH_DOT
* @deprecated 3.15 beta 2. Use {@link HSSFCellStyle#setBorderLeft(BorderStyle)} instead. * @deprecated 3.15 beta 2. Use {@link HSSFCellStyle#setBorderLeft(BorderStyle)} instead.
*/ */
@Deprecated
@Removal(version="3.17") @Removal(version="3.17")
@Override @Override
public void setBorderLeft(short border) public void setBorderLeft(short border)
@ -504,6 +511,7 @@ public final class HSSFCellStyle implements CellStyle {
* @return border type * @return border type
* @deprecated POI 3.15. Will return a BorderStyle enum in the future. Use {@link #getBorderLeftEnum()}. * @deprecated POI 3.15. Will return a BorderStyle enum in the future. Use {@link #getBorderLeftEnum()}.
*/ */
@Deprecated
@Override @Override
public short getBorderLeft() public short getBorderLeft()
{ {
@ -539,6 +547,7 @@ public final class HSSFCellStyle implements CellStyle {
* @see #BORDER_SLANTED_DASH_DOT * @see #BORDER_SLANTED_DASH_DOT
* @deprecated 3.15 beta 2. Use {@link HSSFCellStyle#setBorderRight(BorderStyle)} instead. * @deprecated 3.15 beta 2. Use {@link HSSFCellStyle#setBorderRight(BorderStyle)} instead.
*/ */
@Deprecated
@Removal(version="3.17") @Removal(version="3.17")
@Override @Override
public void setBorderRight(short border) public void setBorderRight(short border)
@ -563,6 +572,7 @@ public final class HSSFCellStyle implements CellStyle {
* @return border type * @return border type
* @deprecated POI 3.15. Will return a BorderStyle enum in the future. Use {@link #getBorderRightEnum()}. * @deprecated POI 3.15. Will return a BorderStyle enum in the future. Use {@link #getBorderRightEnum()}.
*/ */
@Deprecated
@Override @Override
public short getBorderRight() public short getBorderRight()
{ {
@ -598,6 +608,7 @@ public final class HSSFCellStyle implements CellStyle {
* @see #BORDER_SLANTED_DASH_DOT * @see #BORDER_SLANTED_DASH_DOT
* @deprecated 3.15 beta 2. Use {@link HSSFCellStyle#setBorderTop(BorderStyle)} instead. * @deprecated 3.15 beta 2. Use {@link HSSFCellStyle#setBorderTop(BorderStyle)} instead.
*/ */
@Deprecated
@Removal(version="3.17") @Removal(version="3.17")
@Override @Override
public void setBorderTop(short border) public void setBorderTop(short border)
@ -622,6 +633,7 @@ public final class HSSFCellStyle implements CellStyle {
* @return border type * @return border type
* @deprecated POI 3.15. Will return a BorderStyle enum in the future. Use {@link #getBorderTopEnum()}. * @deprecated POI 3.15. Will return a BorderStyle enum in the future. Use {@link #getBorderTopEnum()}.
*/ */
@Deprecated
@Override @Override
public short getBorderTop() public short getBorderTop()
{ {
@ -657,6 +669,7 @@ public final class HSSFCellStyle implements CellStyle {
* @see #BORDER_SLANTED_DASH_DOT * @see #BORDER_SLANTED_DASH_DOT
* @deprecated 3.15 beta 2. Use {@link HSSFCellStyle#setBorderBottom(BorderStyle)} instead. * @deprecated 3.15 beta 2. Use {@link HSSFCellStyle#setBorderBottom(BorderStyle)} instead.
*/ */
@Deprecated
@Removal(version="3.17") @Removal(version="3.17")
@Override @Override
public void setBorderBottom(short border) public void setBorderBottom(short border)
@ -681,6 +694,7 @@ public final class HSSFCellStyle implements CellStyle {
* @return border type * @return border type
* @deprecated POI 3.15. Will return a BorderStyle enum in the future. Use {@link #getBorderBottomEnum()}. * @deprecated POI 3.15. Will return a BorderStyle enum in the future. Use {@link #getBorderBottomEnum()}.
*/ */
@Deprecated
@Override @Override
public short getBorderBottom() public short getBorderBottom()
{ {
@ -806,6 +820,7 @@ public final class HSSFCellStyle implements CellStyle {
* @param fp fill pattern (set to 1 to fill w/foreground color) * @param fp fill pattern (set to 1 to fill w/foreground color)
* @deprecated POI 3.15 beta 3. Use {@link #setFillPattern(FillPatternType)} instead. * @deprecated POI 3.15 beta 3. Use {@link #setFillPattern(FillPatternType)} instead.
*/ */
@Deprecated
@Removal(version="3.17") @Removal(version="3.17")
@Override @Override
public void setFillPattern(short fp) public void setFillPattern(short fp)
@ -830,6 +845,7 @@ public final class HSSFCellStyle implements CellStyle {
* @return fill pattern * @return fill pattern
* @deprecated POI 3.15 beta 3. This method will return {@link FillPatternType} in the future. Use {@link #setFillPattern(FillPatternType)} instead. * @deprecated POI 3.15 beta 3. This method will return {@link FillPatternType} in the future. Use {@link #setFillPattern(FillPatternType)} instead.
*/ */
@Deprecated
@Override @Override
public short getFillPattern() public short getFillPattern()
{ {
@ -857,16 +873,20 @@ public final class HSSFCellStyle implements CellStyle {
* <p>0x40 0xSOMETHING</p> * <p>0x40 0xSOMETHING</p>
*/ */
private void checkDefaultBackgroundFills() { private void checkDefaultBackgroundFills() {
if (_format.getFillForeground() == org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index) { final short autoIdx = HSSFColorPredefined.AUTOMATIC.getIndex();
if (_format.getFillForeground() == autoIdx) {
//JMH: Why +1, hell why not. I guess it made some sense to someone at the time. Doesnt //JMH: Why +1, hell why not. I guess it made some sense to someone at the time. Doesnt
//to me now.... But experience has shown that when the fore is set to AUTOMATIC then the //to me now.... But experience has shown that when the fore is set to AUTOMATIC then the
//background needs to be incremented...... //background needs to be incremented......
if (_format.getFillBackground() != (org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1)) if (_format.getFillBackground() != autoIdx+1) {
setFillBackgroundColor((short)(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1)); setFillBackgroundColor((short)(autoIdx+1));
} else if (_format.getFillBackground() == org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index+1) }
} else if (_format.getFillBackground() == autoIdx+1) {
//Now if the forground changes to a non-AUTOMATIC color the background resets itself!!! //Now if the forground changes to a non-AUTOMATIC color the background resets itself!!!
if (_format.getFillForeground() != org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index) if (_format.getFillForeground() != autoIdx) {
setFillBackgroundColor(org.apache.poi.hssf.util.HSSFColor.AUTOMATIC.index); setFillBackgroundColor(autoIdx);
}
}
} }
/** /**
@ -909,13 +929,13 @@ public final class HSSFCellStyle implements CellStyle {
* @return fill color * @return fill color
*/ */
@Override @Override
public short getFillBackgroundColor() public short getFillBackgroundColor() {
{ final short autoIndex = HSSFColorPredefined.AUTOMATIC.getIndex();
short result = _format.getFillBackground(); short result = _format.getFillBackground();
//JMH: Do this ridiculous conversion, and let HSSFCellStyle //JMH: Do this ridiculous conversion, and let HSSFCellStyle
//internally migrate back and forth //internally migrate back and forth
if (result == (HSSFColor.AUTOMATIC.index+1)) { if (result == autoIndex+1) {
return HSSFColor.AUTOMATIC.index; return autoIndex;
} }
return result; return result;
} }
@ -1117,17 +1137,24 @@ public final class HSSFCellStyle implements CellStyle {
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (this == obj) return true; if (this == obj) {
if (obj == null) return false; return true;
}
if (obj == null) {
return false;
}
if (obj instanceof HSSFCellStyle) { if (obj instanceof HSSFCellStyle) {
final HSSFCellStyle other = (HSSFCellStyle) obj; final HSSFCellStyle other = (HSSFCellStyle) obj;
if (_format == null) { if (_format == null) {
if (other._format != null) if (other._format != null) {
return false; return false;
} else if (!_format.equals(other._format)) }
} else if (!_format.equals(other._format)) {
return false; return false;
if (_index != other._index) }
if (_index != other._index) {
return false; return false;
}
return true; return true;
} }
return false; return false;

View File

@ -21,6 +21,7 @@ import java.util.Locale;
import org.apache.poi.hssf.record.PaletteRecord; import org.apache.poi.hssf.record.PaletteRecord;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
/** /**
* Represents a workbook color palette. * Represents a workbook color palette.
@ -45,16 +46,13 @@ public final class HSSFPalette {
public HSSFColor getColor(short index) public HSSFColor getColor(short index)
{ {
//Handle the special AUTOMATIC case //Handle the special AUTOMATIC case
if (index == HSSFColor.AUTOMATIC.index) { if (index == HSSFColorPredefined.AUTOMATIC.getIndex()) {
return HSSFColor.AUTOMATIC.getInstance(); return HSSFColorPredefined.AUTOMATIC.getColor();
} }
byte[] b = _palette.getColor(index); byte[] b = _palette.getColor(index);
if (b != null) return (b == null) ? null : new CustomColor(index, b);
{
return new CustomColor(index, b);
}
return null;
} }
/** /**
* Retrieves the color at a given index * Retrieves the color at a given index
* *

File diff suppressed because it is too large Load Diff

View File

@ -18,8 +18,13 @@
package org.apache.poi.hssf.record; package org.apache.poi.hssf.record;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import junit.framework.AssertionFailedError; import static org.junit.Assert.assertEquals;
import junit.framework.TestCase; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import org.apache.poi.hssf.HSSFITestDataProvider; import org.apache.poi.hssf.HSSFITestDataProvider;
import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator; import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator;
@ -28,44 +33,49 @@ import org.apache.poi.hssf.record.cf.FontFormatting;
import org.apache.poi.hssf.record.cf.PatternFormatting; import org.apache.poi.hssf.record.cf.PatternFormatting;
import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.formula.ptg.Ptg; import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.ss.formula.ptg.RefNPtg; import org.apache.poi.ss.formula.ptg.RefNPtg;
import org.apache.poi.ss.formula.ptg.RefPtg; import org.apache.poi.ss.formula.ptg.RefPtg;
import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType; import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold.RangeType;
import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet; import org.apache.poi.ss.usermodel.IconMultiStateFormatting.IconSet;
import org.apache.poi.util.LittleEndian; import org.apache.poi.util.LittleEndian;
import org.junit.Test;
import junit.framework.AssertionFailedError;
/** /**
* Tests the serialization and deserialization of the TestCFRuleRecord * Tests the serialization and deserialization of the TestCFRuleRecord
* class works correctly. * class works correctly.
*/ */
@SuppressWarnings("resource") public final class TestCFRuleRecord {
public final class TestCFRuleRecord extends TestCase { @Test
public void testConstructors () { public void testConstructors () throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(); HSSFSheet sheet = workbook.createSheet();
CFRuleRecord rule1 = CFRuleRecord.create(sheet, "7"); CFRuleRecord rule1 = CFRuleRecord.create(sheet, "7");
assertEquals(CFRuleRecord.CONDITION_TYPE_FORMULA, rule1.getConditionType()); assertEquals(CFRuleBase.CONDITION_TYPE_FORMULA, rule1.getConditionType());
assertEquals(ComparisonOperator.NO_COMPARISON, rule1.getComparisonOperation()); assertEquals(ComparisonOperator.NO_COMPARISON, rule1.getComparisonOperation());
assertNotNull(rule1.getParsedExpression1()); assertNotNull(rule1.getParsedExpression1());
assertSame(Ptg.EMPTY_PTG_ARRAY, rule1.getParsedExpression2()); assertSame(Ptg.EMPTY_PTG_ARRAY, rule1.getParsedExpression2());
CFRuleRecord rule2 = CFRuleRecord.create(sheet, ComparisonOperator.BETWEEN, "2", "5"); CFRuleRecord rule2 = CFRuleRecord.create(sheet, ComparisonOperator.BETWEEN, "2", "5");
assertEquals(CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS, rule2.getConditionType()); assertEquals(CFRuleBase.CONDITION_TYPE_CELL_VALUE_IS, rule2.getConditionType());
assertEquals(ComparisonOperator.BETWEEN, rule2.getComparisonOperation()); assertEquals(ComparisonOperator.BETWEEN, rule2.getComparisonOperation());
assertNotNull(rule2.getParsedExpression1()); assertNotNull(rule2.getParsedExpression1());
assertNotNull(rule2.getParsedExpression2()); assertNotNull(rule2.getParsedExpression2());
CFRuleRecord rule3 = CFRuleRecord.create(sheet, ComparisonOperator.EQUAL, null, null); CFRuleRecord rule3 = CFRuleRecord.create(sheet, ComparisonOperator.EQUAL, null, null);
assertEquals(CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS, rule3.getConditionType()); assertEquals(CFRuleBase.CONDITION_TYPE_CELL_VALUE_IS, rule3.getConditionType());
assertEquals(ComparisonOperator.EQUAL, rule3.getComparisonOperation()); assertEquals(ComparisonOperator.EQUAL, rule3.getComparisonOperation());
assertSame(Ptg.EMPTY_PTG_ARRAY, rule3.getParsedExpression2()); assertSame(Ptg.EMPTY_PTG_ARRAY, rule3.getParsedExpression2());
assertSame(Ptg.EMPTY_PTG_ARRAY, rule3.getParsedExpression2()); assertSame(Ptg.EMPTY_PTG_ARRAY, rule3.getParsedExpression2());
workbook.close();
} }
public void testCreateCFRuleRecord() { @Test
public void testCreateCFRuleRecord() throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(); HSSFSheet sheet = workbook.createSheet();
CFRuleRecord record = CFRuleRecord.create(sheet, "7"); CFRuleRecord record = CFRuleRecord.create(sheet, "7");
@ -87,13 +97,14 @@ public final class TestCFRuleRecord extends TestCase {
// Compare // Compare
assertEquals("Output size", recordData.length+4, output.length); //includes sid+recordlength assertEquals("Output size", recordData.length+4, output.length); //includes sid+recordlength
for (int i = 0; i < recordData.length;i++) for (int i = 0; i < recordData.length;i++) {
{
assertEquals("CFRuleRecord doesn't match", recordData[i], output[i+4]); assertEquals("CFRuleRecord doesn't match", recordData[i], output[i+4]);
} }
workbook.close();
} }
public void testCreateCFRule12Record() { @Test
public void testCreateCFRule12Record() throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(); HSSFSheet sheet = workbook.createSheet();
CFRule12Record record = CFRule12Record.create(sheet, "7"); CFRule12Record record = CFRule12Record.create(sheet, "7");
@ -115,13 +126,14 @@ public final class TestCFRuleRecord extends TestCase {
// Compare // Compare
assertEquals("Output size", recordData.length+4, output.length); //includes sid+recordlength assertEquals("Output size", recordData.length+4, output.length); //includes sid+recordlength
for (int i = 0; i < recordData.length;i++) for (int i = 0; i < recordData.length;i++) {
{
assertEquals("CFRule12Record doesn't match", recordData[i], output[i+4]); assertEquals("CFRule12Record doesn't match", recordData[i], output[i+4]);
} }
workbook.close();
} }
public void testCreateIconCFRule12Record() { @Test
public void testCreateIconCFRule12Record() throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(); HSSFSheet sheet = workbook.createSheet();
CFRule12Record record = CFRule12Record.create(sheet, IconSet.GREY_5_ARROWS); CFRule12Record record = CFRule12Record.create(sheet, IconSet.GREY_5_ARROWS);
@ -155,10 +167,10 @@ public final class TestCFRuleRecord extends TestCase {
// Compare // Compare
assertEquals("Output size", recordData.length+4, output.length); //includes sid+recordlength assertEquals("Output size", recordData.length+4, output.length); //includes sid+recordlength
for (int i = 0; i < recordData.length;i++) for (int i = 0; i < recordData.length;i++) {
{
assertEquals("CFRule12Record doesn't match", recordData[i], output[i+4]); assertEquals("CFRule12Record doesn't match", recordData[i], output[i+4]);
} }
workbook.close();
} }
private void testCFRuleRecord(CFRuleRecord record) { private void testCFRuleRecord(CFRuleRecord record) {
@ -227,11 +239,11 @@ public final class TestCFRuleRecord extends TestCase {
} }
private void testPatternFormattingAccessors(PatternFormatting patternFormatting) { private void testPatternFormattingAccessors(PatternFormatting patternFormatting) {
patternFormatting.setFillBackgroundColor(HSSFColor.GREEN.index); patternFormatting.setFillBackgroundColor(HSSFColorPredefined.GREEN.getIndex());
assertEquals(HSSFColor.GREEN.index,patternFormatting.getFillBackgroundColor()); assertEquals(HSSFColorPredefined.GREEN.getIndex(),patternFormatting.getFillBackgroundColor());
patternFormatting.setFillForegroundColor(HSSFColor.INDIGO.index); patternFormatting.setFillForegroundColor(HSSFColorPredefined.INDIGO.getIndex());
assertEquals(HSSFColor.INDIGO.index,patternFormatting.getFillForegroundColor()); assertEquals(HSSFColorPredefined.INDIGO.getIndex(),patternFormatting.getFillForegroundColor());
patternFormatting.setFillPattern(PatternFormatting.DIAMONDS); patternFormatting.setFillPattern(PatternFormatting.DIAMONDS);
assertEquals(PatternFormatting.DIAMONDS,patternFormatting.getFillPattern()); assertEquals(PatternFormatting.DIAMONDS,patternFormatting.getFillPattern());
@ -258,24 +270,24 @@ public final class TestCFRuleRecord extends TestCase {
borderFormatting.setBorderTop(BorderFormatting.BORDER_HAIR); borderFormatting.setBorderTop(BorderFormatting.BORDER_HAIR);
assertEquals(BorderFormatting.BORDER_HAIR, borderFormatting.getBorderTop()); assertEquals(BorderFormatting.BORDER_HAIR, borderFormatting.getBorderTop());
borderFormatting.setBottomBorderColor(HSSFColor.AQUA.index); borderFormatting.setBottomBorderColor(HSSFColorPredefined.AQUA.getIndex());
assertEquals(HSSFColor.AQUA.index, borderFormatting.getBottomBorderColor()); assertEquals(HSSFColorPredefined.AQUA.getIndex(), borderFormatting.getBottomBorderColor());
borderFormatting.setDiagonalBorderColor(HSSFColor.RED.index); borderFormatting.setDiagonalBorderColor(HSSFColorPredefined.RED.getIndex());
assertEquals(HSSFColor.RED.index, borderFormatting.getDiagonalBorderColor()); assertEquals(HSSFColorPredefined.RED.getIndex(), borderFormatting.getDiagonalBorderColor());
assertFalse(borderFormatting.isForwardDiagonalOn()); assertFalse(borderFormatting.isForwardDiagonalOn());
borderFormatting.setForwardDiagonalOn(true); borderFormatting.setForwardDiagonalOn(true);
assertTrue(borderFormatting.isForwardDiagonalOn()); assertTrue(borderFormatting.isForwardDiagonalOn());
borderFormatting.setLeftBorderColor(HSSFColor.BLACK.index); borderFormatting.setLeftBorderColor(HSSFColorPredefined.BLACK.getIndex());
assertEquals(HSSFColor.BLACK.index, borderFormatting.getLeftBorderColor()); assertEquals(HSSFColorPredefined.BLACK.getIndex(), borderFormatting.getLeftBorderColor());
borderFormatting.setRightBorderColor(HSSFColor.BLUE.index); borderFormatting.setRightBorderColor(HSSFColorPredefined.BLUE.getIndex());
assertEquals(HSSFColor.BLUE.index, borderFormatting.getRightBorderColor()); assertEquals(HSSFColorPredefined.BLUE.getIndex(), borderFormatting.getRightBorderColor());
borderFormatting.setTopBorderColor(HSSFColor.GOLD.index); borderFormatting.setTopBorderColor(HSSFColorPredefined.GOLD.getIndex());
assertEquals(HSSFColor.GOLD.index, borderFormatting.getTopBorderColor()); assertEquals(HSSFColorPredefined.GOLD.getIndex(), borderFormatting.getTopBorderColor());
} }
@ -378,7 +390,8 @@ public final class TestCFRuleRecord extends TestCase {
assertTrue(fontFormatting.isUnderlineTypeModified()); assertTrue(fontFormatting.isUnderlineTypeModified());
} }
public void testWrite() { @Test
public void testWrite() throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(); HSSFSheet sheet = workbook.createSheet();
CFRuleRecord rr = CFRuleRecord.create(sheet, ComparisonOperator.BETWEEN, "5", "10"); CFRuleRecord rr = CFRuleRecord.create(sheet, ComparisonOperator.BETWEEN, "5", "10");
@ -397,6 +410,7 @@ public final class TestCFRuleRecord extends TestCase {
assertEquals("undocumented flags should be 0000", 0, flags & 0x03C00000); // Otherwise Excel gets unhappy assertEquals("undocumented flags should be 0000", 0, flags & 0x03C00000); // Otherwise Excel gets unhappy
// check all remaining flag bits (some are not well understood yet) // check all remaining flag bits (some are not well understood yet)
assertEquals(0x203FFFFF, flags); assertEquals(0x203FFFFF, flags);
workbook.close();
} }
private static final byte[] DATA_REFN = { private static final byte[] DATA_REFN = {
@ -413,6 +427,7 @@ public final class TestCFRuleRecord extends TestCase {
/** /**
* tRefN and tAreaN tokens must be preserved when re-serializing conditional format formulas * tRefN and tAreaN tokens must be preserved when re-serializing conditional format formulas
*/ */
@Test
public void testReserializeRefNTokens() { public void testReserializeRefNTokens() {
RecordInputStream is = TestcaseRecordInputStream.create(CFRuleRecord.sid, DATA_REFN); RecordInputStream is = TestcaseRecordInputStream.create(CFRuleRecord.sid, DATA_REFN);
@ -431,7 +446,8 @@ public final class TestCFRuleRecord extends TestCase {
TestcaseRecordInputStream.confirmRecordEncoding(CFRuleRecord.sid, DATA_REFN, data); TestcaseRecordInputStream.confirmRecordEncoding(CFRuleRecord.sid, DATA_REFN, data);
} }
public void testBug53691() { @Test
public void testBug53691() throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(); HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet(); HSSFSheet sheet = workbook.createSheet();
@ -442,12 +458,16 @@ public final class TestCFRuleRecord extends TestCase {
byte [] serializedRecord = record.serialize(); byte [] serializedRecord = record.serialize();
byte [] serializedClone = clone.serialize(); byte [] serializedClone = clone.serialize();
assertArrayEquals(serializedRecord, serializedClone); assertArrayEquals(serializedRecord, serializedClone);
workbook.close();
} }
public void testBug57231_rewrite() { @Test
HSSFWorkbook wb = HSSFITestDataProvider.instance.openSampleWorkbook("57231_MixedGasReport.xls"); public void testBug57231_rewrite() throws IOException {
assertEquals(7, wb.getNumberOfSheets()); HSSFWorkbook wb1 = HSSFITestDataProvider.instance.openSampleWorkbook("57231_MixedGasReport.xls");
wb = HSSFITestDataProvider.instance.writeOutAndReadBack(wb); assertEquals(7, wb1.getNumberOfSheets());
assertEquals(7, wb.getNumberOfSheets()); HSSFWorkbook wb2 = HSSFITestDataProvider.instance.writeOutAndReadBack(wb1);
assertEquals(7, wb2.getNumberOfSheets());
wb2.close();
wb1.close();
} }
} }

View File

@ -27,8 +27,6 @@ import org.junit.Test;
/** /**
* Verifies that custom palette editing works correctly * Verifies that custom palette editing works correctly
*
* @author Brian Sanders (bsanders at risklabs dot com)
*/ */
public final class TestPaletteRecord { public final class TestPaletteRecord {
@ -46,8 +44,8 @@ public final class TestPaletteRecord {
HSSFColor c = entry.getValue(); HSSFColor c = entry.getValue();
short[] rgbTriplet = c.getTriplet(); short[] rgbTriplet = c.getTriplet();
byte[] paletteTriplet = palette.getColor((short) index); byte[] paletteTriplet = palette.getColor((short) index);
String msg = "Expected HSSFColor constant to match PaletteRecord at index 0x" String msg = "Expected HSSFColor constant to match PaletteRecord at index" + (index == c.getIndex2() ? "2" : "") + " 0x"
+ Integer.toHexString(c.getIndex()); + Integer.toHexString(index);
assertEquals(msg, rgbTriplet[0], paletteTriplet[0] & 0xff); assertEquals(msg, rgbTriplet[0], paletteTriplet[0] & 0xff);
assertEquals(msg, rgbTriplet[1], paletteTriplet[1] & 0xff); assertEquals(msg, rgbTriplet[1], paletteTriplet[1] & 0xff);
assertEquals(msg, rgbTriplet[2], paletteTriplet[2] & 0xff); assertEquals(msg, rgbTriplet[2], paletteTriplet[2] & 0xff);

View File

@ -18,6 +18,10 @@
package org.apache.poi.hssf.usermodel; package org.apache.poi.hssf.usermodel;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import java.awt.Color; import java.awt.Color;
import java.io.IOException; import java.io.IOException;
@ -27,21 +31,19 @@ import java.util.Map;
import org.apache.poi.hssf.HSSFTestDataSamples; import org.apache.poi.hssf.HSSFTestDataSamples;
import org.apache.poi.hssf.record.PaletteRecord; import org.apache.poi.hssf.record.PaletteRecord;
import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.Font;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import junit.framework.TestCase; public final class TestHSSFPalette {
/**
* @author Brian Sanders (bsanders at risklabs dot com)
*/
public final class TestHSSFPalette extends TestCase {
private PaletteRecord _palette; private PaletteRecord _palette;
private HSSFPalette _hssfPalette; private HSSFPalette _hssfPalette;
@Override @Before
public void setUp() public void setUp() {
{
_palette = new PaletteRecord(); _palette = new PaletteRecord();
_hssfPalette = new HSSFPalette(_palette); _hssfPalette = new HSSFPalette(_palette);
} }
@ -49,6 +51,7 @@ public final class TestHSSFPalette extends TestCase {
/** /**
* Verifies that a custom palette can be created, saved, and reloaded * Verifies that a custom palette can be created, saved, and reloaded
*/ */
@Test
public void testCustomPalette() { public void testCustomPalette() {
//reading sample xls //reading sample xls
HSSFWorkbook book = HSSFTestDataSamples.openSampleWorkbook("Simple.xls"); HSSFWorkbook book = HSSFTestDataSamples.openSampleWorkbook("Simple.xls");
@ -62,9 +65,9 @@ public final class TestHSSFPalette extends TestCase {
book = HSSFTestDataSamples.writeOutAndReadBack(book); book = HSSFTestDataSamples.writeOutAndReadBack(book);
palette = book.getCustomPalette(); palette = book.getCustomPalette();
HSSFColor color = palette.getColor(HSSFColor.CORAL.index); //unmodified HSSFColor color = palette.getColor(HSSFColorPredefined.CORAL.getIndex()); //unmodified
assertNotNull("Unexpected null in custom palette (unmodified index)", color); assertNotNull("Unexpected null in custom palette (unmodified index)", color);
short[] expectedRGB = HSSFColor.CORAL.triplet; short[] expectedRGB = HSSFColorPredefined.CORAL.getTriplet();
short[] actualRGB = color.getTriplet(); short[] actualRGB = color.getTriplet();
String msg = "Expected palette position to remain unmodified"; String msg = "Expected palette position to remain unmodified";
assertEquals(msg, expectedRGB[0], actualRGB[0]); assertEquals(msg, expectedRGB[0], actualRGB[0]);
@ -83,6 +86,7 @@ public final class TestHSSFPalette extends TestCase {
/** /**
* Uses the palette from cell stylings * Uses the palette from cell stylings
*/ */
@Test
public void testPaletteFromCellColours() { public void testPaletteFromCellColours() {
HSSFWorkbook book = HSSFTestDataSamples.openSampleWorkbook("SimpleWithColours.xls"); HSSFWorkbook book = HSSFTestDataSamples.openSampleWorkbook("SimpleWithColours.xls");
@ -98,8 +102,8 @@ public final class TestHSSFPalette extends TestCase {
assertEquals("I'm plain", cellA.getStringCellValue()); assertEquals("I'm plain", cellA.getStringCellValue());
assertEquals(64, cellA.getCellStyle().getFillForegroundColor()); assertEquals(64, cellA.getCellStyle().getFillForegroundColor());
assertEquals(64, cellA.getCellStyle().getFillBackgroundColor()); assertEquals(64, cellA.getCellStyle().getFillBackgroundColor());
assertEquals(HSSFFont.COLOR_NORMAL, cellA.getCellStyle().getFont(book).getColor()); assertEquals(Font.COLOR_NORMAL, cellA.getCellStyle().getFont(book).getColor());
assertEquals(0, cellA.getCellStyle().getFillPattern()); assertEquals(FillPatternType.NO_FILL, cellA.getCellStyle().getFillPatternEnum());
assertEquals("0:0:0", p.getColor((short)64).getHexString()); assertEquals("0:0:0", p.getColor((short)64).getHexString());
assertEquals(null, p.getColor((short)32767)); assertEquals(null, p.getColor((short)32767));
@ -108,7 +112,7 @@ public final class TestHSSFPalette extends TestCase {
assertEquals(64, cellB.getCellStyle().getFillForegroundColor()); assertEquals(64, cellB.getCellStyle().getFillForegroundColor());
assertEquals(64, cellB.getCellStyle().getFillBackgroundColor()); assertEquals(64, cellB.getCellStyle().getFillBackgroundColor());
assertEquals(10, cellB.getCellStyle().getFont(book).getColor()); assertEquals(10, cellB.getCellStyle().getFont(book).getColor());
assertEquals(0, cellB.getCellStyle().getFillPattern()); assertEquals(FillPatternType.NO_FILL, cellB.getCellStyle().getFillPatternEnum());
assertEquals("0:0:0", p.getColor((short)64).getHexString()); assertEquals("0:0:0", p.getColor((short)64).getHexString());
assertEquals("FFFF:0:0", p.getColor((short)10).getHexString()); assertEquals("FFFF:0:0", p.getColor((short)10).getHexString());
@ -117,7 +121,7 @@ public final class TestHSSFPalette extends TestCase {
assertEquals(11, cellC.getCellStyle().getFillForegroundColor()); assertEquals(11, cellC.getCellStyle().getFillForegroundColor());
assertEquals(64, cellC.getCellStyle().getFillBackgroundColor()); assertEquals(64, cellC.getCellStyle().getFillBackgroundColor());
assertEquals(10, cellC.getCellStyle().getFont(book).getColor()); assertEquals(10, cellC.getCellStyle().getFont(book).getColor());
assertEquals(1, cellC.getCellStyle().getFillPattern()); assertEquals(FillPatternType.SOLID_FOREGROUND, cellC.getCellStyle().getFillPatternEnum());
assertEquals("0:FFFF:0", p.getColor((short)11).getHexString()); assertEquals("0:FFFF:0", p.getColor((short)11).getHexString());
assertEquals("FFFF:0:0", p.getColor((short)10).getHexString()); assertEquals("FFFF:0:0", p.getColor((short)10).getHexString());
@ -126,7 +130,7 @@ public final class TestHSSFPalette extends TestCase {
assertEquals(13, cellD.getCellStyle().getFillForegroundColor()); assertEquals(13, cellD.getCellStyle().getFillForegroundColor());
assertEquals(64, cellD.getCellStyle().getFillBackgroundColor()); assertEquals(64, cellD.getCellStyle().getFillBackgroundColor());
assertEquals(14, cellD.getCellStyle().getFont(book).getColor()); assertEquals(14, cellD.getCellStyle().getFont(book).getColor());
assertEquals(0, cellD.getCellStyle().getFillPattern()); assertEquals(FillPatternType.NO_FILL, cellD.getCellStyle().getFillPatternEnum());
assertEquals("FFFF:FFFF:0", p.getColor((short)13).getHexString()); assertEquals("FFFF:FFFF:0", p.getColor((short)13).getHexString());
assertEquals("FFFF:0:FFFF", p.getColor((short)14).getHexString()); assertEquals("FFFF:0:FFFF", p.getColor((short)14).getHexString());
@ -135,11 +139,12 @@ public final class TestHSSFPalette extends TestCase {
assertEquals(13, cellE.getCellStyle().getFillForegroundColor()); assertEquals(13, cellE.getCellStyle().getFillForegroundColor());
assertEquals(64, cellE.getCellStyle().getFillBackgroundColor()); assertEquals(64, cellE.getCellStyle().getFillBackgroundColor());
assertEquals(14, cellE.getCellStyle().getFont(book).getColor()); assertEquals(14, cellE.getCellStyle().getFont(book).getColor());
assertEquals(0, cellE.getCellStyle().getFillPattern()); assertEquals(FillPatternType.NO_FILL, cellE.getCellStyle().getFillPatternEnum());
assertEquals("FFFF:FFFF:0", p.getColor((short)13).getHexString()); assertEquals("FFFF:FFFF:0", p.getColor((short)13).getHexString());
assertEquals("FFFF:0:FFFF", p.getColor((short)14).getHexString()); assertEquals("FFFF:0:FFFF", p.getColor((short)14).getHexString());
} }
@Test
public void testFindSimilar() throws IOException { public void testFindSimilar() throws IOException {
HSSFWorkbook book = new HSSFWorkbook(); HSSFWorkbook book = new HSSFWorkbook();
HSSFPalette p = book.getCustomPalette(); HSSFPalette p = book.getCustomPalette();
@ -230,6 +235,7 @@ public final class TestHSSFPalette extends TestCase {
* Verifies that the generated gnumeric-format string values match the * Verifies that the generated gnumeric-format string values match the
* hardcoded values in the HSSFColor default color palette * hardcoded values in the HSSFColor default color palette
*/ */
@Test
public void testGnumericStrings() { public void testGnumericStrings() {
compareToDefaults(new ColorComparator() { compareToDefaults(new ColorComparator() {
@Override @Override
@ -243,6 +249,7 @@ public final class TestHSSFPalette extends TestCase {
/** /**
* Verifies that the palette handles invalid palette indexes * Verifies that the palette handles invalid palette indexes
*/ */
@Test
public void testBadIndexes() { public void testBadIndexes() {
//too small //too small
_hssfPalette.setColorAtIndex((short) 2, (byte) 255, (byte) 255, (byte) 255); _hssfPalette.setColorAtIndex((short) 2, (byte) 255, (byte) 255, (byte) 255);
@ -275,6 +282,7 @@ public final class TestHSSFPalette extends TestCase {
} }
} }
@Test
public void testAddColor() { public void testAddColor() {
try try
{ {

View File

@ -17,22 +17,18 @@
package org.apache.poi.hssf.util; package org.apache.poi.hssf.util;
import junit.framework.Test; import org.junit.runner.RunWith;
import junit.framework.TestSuite; import org.junit.runners.Suite;
/** /**
* Collects all tests for org.apache.poi.hssf.util. * Collects all tests for org.apache.poi.hssf.util.
*
* @author Josh Micich
*/ */
@RunWith(Suite.class)
@Suite.SuiteClasses({
TestAreaReference.class,
TestCellReference.class,
TestHSSFColor.class,
TestRKUtil.class
})
public class AllHSSFUtilTests { public class AllHSSFUtilTests {
public static Test suite() {
TestSuite result = new TestSuite(AllHSSFUtilTests.class.getName());
result.addTestSuite(TestAreaReference.class);
// result.addTestSuite(TestCellReference.class); //converted to junit4
result.addTestSuite(TestHSSFColor.class);
result.addTestSuite(TestRKUtil.class);
return result;
}
} }

View File

@ -17,38 +17,45 @@
package org.apache.poi.hssf.util; package org.apache.poi.hssf.util;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.Map; import java.util.Map;
import junit.framework.TestCase; import org.apache.poi.hssf.util.HSSFColor.HSSFColorPredefined;
/** import org.junit.Test;
* @author Nick Burch
*/ public final class TestHSSFColor {
public final class TestHSSFColor extends TestCase { @Test
public void testBasics() { public void testBasics() {
assertNotNull(HSSFColor.YELLOW.class); assertNotNull(HSSFColor.YELLOW.class);
assertTrue(HSSFColor.YELLOW.index > 0); assertTrue(HSSFColorPredefined.YELLOW.getIndex() > 0);
assertTrue(HSSFColor.YELLOW.index2 > 0); assertTrue(HSSFColorPredefined.YELLOW.getIndex2() > 0);
} }
@Test
public void testContents() { public void testContents() {
assertEquals(3, HSSFColor.YELLOW.triplet.length); short[] triplet = HSSFColorPredefined.YELLOW.getTriplet();
assertEquals(255, HSSFColor.YELLOW.triplet[0]); assertEquals(3, triplet.length);
assertEquals(255, HSSFColor.YELLOW.triplet[1]); assertEquals(255, triplet[0]);
assertEquals(0, HSSFColor.YELLOW.triplet[2]); assertEquals(255, triplet[1]);
assertEquals(0, triplet[2]);
assertEquals("FFFF:FFFF:0", HSSFColor.YELLOW.hexString); assertEquals("FFFF:FFFF:0", HSSFColorPredefined.YELLOW.getHexString());
} }
@Test
public void testTrippletHash() { public void testTrippletHash() {
Map<String, HSSFColor> tripplets = HSSFColor.getTripletHash(); Map<String, HSSFColor> tripplets = HSSFColor.getTripletHash();
assertEquals( assertEquals(
HSSFColor.MAROON.class, HSSFColor.MAROON.class,
tripplets.get(HSSFColor.MAROON.hexString).getClass() tripplets.get(HSSFColorPredefined.MAROON.getHexString()).getClass()
); );
assertEquals( assertEquals(
HSSFColor.YELLOW.class, HSSFColor.YELLOW.class,
tripplets.get(HSSFColor.YELLOW.hexString).getClass() tripplets.get(HSSFColorPredefined.YELLOW.getHexString()).getClass()
); );
} }
} }