The list of XF records and formatting records for the workbook
Method from jxl.biff.FormattingRecords Detail: |
public final void addFormat(DisplayFormat fr) throws NumFormatRecordsException {
// Handle the case the where the index number in the read Excel
// file exhibits some major weirdness
if (fr.isInitialized() &&
fr.getFormatIndex() >= maxFormatRecordsIndex)
{
logger.warn("Format index exceeds Excel maximum - assigning custom " +
"number");
fr.initialize(nextCustomIndexNumber);
nextCustomIndexNumber++;
}
// Initialize the format record with a custom index number
if (!fr.isInitialized())
{
fr.initialize(nextCustomIndexNumber);
nextCustomIndexNumber++;
}
if (nextCustomIndexNumber > maxFormatRecordsIndex)
{
nextCustomIndexNumber = maxFormatRecordsIndex;
throw new NumFormatRecordsException();
}
if (fr.getFormatIndex() >= nextCustomIndexNumber)
{
nextCustomIndexNumber = fr.getFormatIndex() + 1;
}
if (!fr.isBuiltIn())
{
formatsList.add(fr);
formats.put(new Integer(fr.getFormatIndex()), fr);
}
}
Adds a cell format to the hash map, keyed on its index. If the format
record is not initialized, then its index number is determined and its
initialize method called. If the font is not a built in format, then it
is added to the list of formats for writing out |
public final void addStyle(XFRecord xf) throws NumFormatRecordsException {
if (!xf.isInitialized())
{
int pos = xfRecords.size();
xf.initialize(pos, this, fonts);
xfRecords.add(xf);
}
else
{
// The XF record has probably been read in. If the index is greater
// Than the size of the list, then it is not a preset format,
// so add it
if (xf.getXFIndex() >= xfRecords.size())
{
xfRecords.add(xf);
}
}
}
Adds an extended formatting record to the list. If the XF record passed
in has not been initialized, its index is determined based on the
xfRecords list, and
this position is passed to the XF records initialize method |
public RGB getColourRGB(Colour c) {
if (palette == null)
{
return c.getDefaultRGB();
}
return palette.getColourRGB(c);
}
Accessor for the RGB value for the specified colour |
public final DateFormat getDateFormat(int pos) {
XFRecord xfr = (XFRecord) xfRecords.get(pos);
if (xfr.isDate())
{
return xfr.getDateFormat();
}
FormatRecord fr = (FormatRecord)
formats.get(new Integer(xfr.getFormatRecord()));
if (fr == null)
{
return null;
}
return fr.isDate() ? fr.getDateFormat() : null;
}
Gets the DateFormat used to format the cell. |
protected final Fonts getFonts() {
return fonts;
}
Accessor for the fonts used by this workbook |
FormatRecord getFormatRecord(int index) {
return (FormatRecord)
formats.get(new Integer(index));
}
|
public final NumberFormat getNumberFormat(int pos) {
XFRecord xfr = (XFRecord) xfRecords.get(pos);
if (xfr.isNumber())
{
return xfr.getNumberFormat();
}
FormatRecord fr = (FormatRecord)
formats.get(new Integer(xfr.getFormatRecord()));
if (fr == null)
{
return null;
}
return fr.isNumber() ? fr.getNumberFormat() : null;
}
Gets the NumberFormat used to format the cell. |
protected final int getNumberOfFormatRecords() {
return formatsList.size();
}
Gets the number of formatting records on the list. This is used by the
writable subclass because there is an upper limit on the amount of
format records that are allowed to be present in an excel sheet |
public PaletteRecord getPalette() {
return palette;
}
Accessor for the colour palette |
public final XFRecord getXFRecord(int index) {
return (XFRecord) xfRecords.get(index);
}
Gets the XFRecord for the specified index. Used when copying individual
cells |
public final boolean isDate(int pos) {
XFRecord xfr = (XFRecord) xfRecords.get(pos);
if (xfr.isDate())
{
return true;
}
FormatRecord fr = (FormatRecord)
formats.get(new Integer(xfr.getFormatRecord()));
return fr == null ? false : fr.isDate();
}
Sees if the extended formatting record at the specified position
represents a date. First checks against the built in formats, and
then checks against the hash map of FormatRecords |
public IndexMapping rationalize(IndexMapping fontMapping,
IndexMapping formatMapping) {
// Update the index codes for the XF records using the format
// mapping and the font mapping
// at the same time
XFRecord xfr = null;
for (Iterator it = xfRecords.iterator(); it.hasNext();)
{
xfr = (XFRecord) it.next();
if (xfr.getFormatRecord() >= customFormatStartIndex)
{
xfr.setFormatIndex(formatMapping.getNewIndex(xfr.getFormatRecord()));
}
xfr.setFontIndex(fontMapping.getNewIndex(xfr.getFontIndex()));
}
ArrayList newrecords = new ArrayList(minXFRecords);
IndexMapping mapping = new IndexMapping(xfRecords.size());
int numremoved = 0;
int numXFRecords = Math.min(minXFRecords, xfRecords.size());
// Copy across the fundamental styles
for (int i = 0; i < numXFRecords; i++)
{
newrecords.add(xfRecords.get(i));
mapping.setMapping(i, i);
}
if (numXFRecords < minXFRecords)
{
logger.warn("There are less than the expected minimum number of " +
"XF records");
return mapping;
}
// Iterate through the old list
for (int i = minXFRecords; i < xfRecords.size(); i++)
{
XFRecord xf = (XFRecord) xfRecords.get(i);
// Compare against formats already on the list
boolean duplicate = false;
for (Iterator it = newrecords.iterator();
it.hasNext() && !duplicate;)
{
XFRecord xf2 = (XFRecord) it.next();
if (xf2.equals(xf))
{
duplicate = true;
mapping.setMapping(i, mapping.getNewIndex(xf2.getXFIndex()));
numremoved++;
}
}
// If this format is not a duplicate then add it to the new list
if (!duplicate)
{
newrecords.add(xf);
mapping.setMapping(i, i - numremoved);
}
}
// It is sufficient to merely change the xf index field on all XFRecords
// In this case, CellValues which refer to defunct format records
// will nevertheless be written out with the correct index number
for (Iterator i = xfRecords.iterator(); i.hasNext();)
{
XFRecord xf = (XFRecord) i.next();
xf.rationalize(mapping);
}
// Set the new list
xfRecords = newrecords;
return mapping;
}
Rationalizes the cell formats. Duplicate
formats are removed and the format indexed of the cells
adjusted accordingly |
public IndexMapping rationalizeDisplayFormats() {
ArrayList newformats = new ArrayList();
int numremoved = 0;
IndexMapping mapping = new IndexMapping(nextCustomIndexNumber);
// Iterate through the old list
Iterator i = formatsList.iterator();
DisplayFormat df = null;
DisplayFormat df2 = null;
boolean duplicate = false;
while (i.hasNext())
{
df = (DisplayFormat) i.next();
Assert.verify(!df.isBuiltIn());
// Compare against formats already on the list
Iterator i2 = newformats.iterator();
duplicate = false;
while (i2.hasNext() && !duplicate)
{
df2 = (DisplayFormat) i2.next();
if (df2.equals(df))
{
duplicate = true;
mapping.setMapping(df.getFormatIndex(),
mapping.getNewIndex(df2.getFormatIndex()));
numremoved++;
}
}
// If this format is not a duplicate then add it to the new list
if (!duplicate)
{
newformats.add(df);
int indexnum = df.getFormatIndex() - numremoved;
if (indexnum > maxFormatRecordsIndex)
{
logger.warn("Too many number formats - using default format.");
indexnum = 0; // the default number format index
}
mapping.setMapping(df.getFormatIndex(),
df.getFormatIndex() - numremoved);
}
}
// Set the new list
formatsList = newformats;
// Update the index codes for the remaining formats
i = formatsList.iterator();
while (i.hasNext())
{
df = (DisplayFormat) i.next();
df.initialize(mapping.getNewIndex(df.getFormatIndex()));
}
return mapping;
}
Rationalizes the display formats. Duplicate
formats are removed and the format indices of the cells
adjusted accordingly. It is invoked immediately prior to writing
writing out the sheet |
public IndexMapping rationalizeFonts() {
return fonts.rationalize();
}
Rationalizes all the fonts, removing duplicate entries |
public void setColourRGB(Colour c,
int r,
int g,
int b) {
if (palette == null)
{
palette = new PaletteRecord();
}
palette.setColourRGB(c, r, g, b);
}
Sets the RGB value for the specified colour for this workbook |
public void setPalette(PaletteRecord pr) {
palette = pr;
}
Called from the WorkbookParser to set the colour palette |
public void write(File outputFile) throws IOException {
// Write out all the formats
Iterator i = formatsList.iterator();
FormatRecord fr = null;
while (i.hasNext())
{
fr = (FormatRecord) i.next();
outputFile.write(fr);
}
// Write out the styles
i = xfRecords.iterator();
XFRecord xfr = null;
while (i.hasNext())
{
xfr = (XFRecord) i.next();
outputFile.write(xfr);
}
// Write out the style records
BuiltInStyle style = new BuiltInStyle(0x10, 3);
outputFile.write(style);
style = new BuiltInStyle(0x11, 6);
outputFile.write(style);
style = new BuiltInStyle(0x12, 4);
outputFile.write(style);
style = new BuiltInStyle(0x13, 7);
outputFile.write(style);
style = new BuiltInStyle(0x0, 0);
outputFile.write(style);
style = new BuiltInStyle(0x14, 5);
outputFile.write(style);
}
Writes out all the format records and the XF records |