diff --git a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java index d6da4f686..c43e4f5ae 100644 --- a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java +++ b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java @@ -45,242 +45,237 @@ import org.apache.poi.util.POILogger; * not a file format one.

*/ public final class CFRecordsAggregate extends RecordAggregate { - /** Excel 97-2003 allows up to 3 conditional formating rules */ - private static final int MAX_97_2003_CONDTIONAL_FORMAT_RULES = 3; - private static final POILogger logger = POILogFactory.getLogger(CFRecordsAggregate.class); + /** Excel 97-2003 allows up to 3 conditional formating rules */ + private static final int MAX_97_2003_CONDTIONAL_FORMAT_RULES = 3; + private static final POILogger logger = POILogFactory.getLogger(CFRecordsAggregate.class); - private final CFHeaderBase header; + private final CFHeaderBase header; - /** List of CFRuleRecord objects */ - private final List rules; + /** List of CFRuleRecord objects */ + private final List rules; - private CFRecordsAggregate(CFHeaderBase pHeader, CFRuleBase[] pRules) { - if(pHeader == null) { - throw new IllegalArgumentException("header must not be null"); - } - if(pRules == null) { - throw new IllegalArgumentException("rules must not be null"); - } - if(pRules.length > MAX_97_2003_CONDTIONAL_FORMAT_RULES) { - logger.log(POILogger.WARN, "Excel versions before 2007 require that " - + "No more than " + MAX_97_2003_CONDTIONAL_FORMAT_RULES - + " rules may be specified, " + pRules.length + " were found," - + " this file will cause problems with old Excel versions"); - } - if (pRules.length != pHeader.getNumberOfConditionalFormats()) { - throw new RuntimeException("Mismatch number of rules"); - } - header = pHeader; - rules = new ArrayList(pRules.length); - for (int i = 0; i < pRules.length; i++) { - checkRuleType(pRules[i]); - rules.add(pRules[i]); - } - } + private CFRecordsAggregate(CFHeaderBase pHeader, CFRuleBase[] pRules) { + if(pHeader == null) { + throw new IllegalArgumentException("header must not be null"); + } + if(pRules == null) { + throw new IllegalArgumentException("rules must not be null"); + } + if(pRules.length > MAX_97_2003_CONDTIONAL_FORMAT_RULES) { + logger.log(POILogger.WARN, "Excel versions before 2007 require that " + + "No more than " + MAX_97_2003_CONDTIONAL_FORMAT_RULES + + " rules may be specified, " + pRules.length + " were found," + + " this file will cause problems with old Excel versions"); + } + if (pRules.length != pHeader.getNumberOfConditionalFormats()) { + throw new RuntimeException("Mismatch number of rules"); + } + header = pHeader; + rules = new ArrayList(pRules.length); + for (int i = 0; i < pRules.length; i++) { + checkRuleType(pRules[i]); + rules.add(pRules[i]); + } + } - public CFRecordsAggregate(CellRangeAddress[] regions, CFRuleBase[] rules) { - this(createHeader(regions, rules), rules); - } - private static CFHeaderBase createHeader(CellRangeAddress[] regions, CFRuleBase[] rules) { - if (rules.length == 0 || rules[0] instanceof CFRuleRecord) { - return new CFHeaderRecord(regions, rules.length); - } - return new CFHeader12Record(regions, rules.length); - } + public CFRecordsAggregate(CellRangeAddress[] regions, CFRuleBase[] rules) { + this(createHeader(regions, rules), rules); + } + private static CFHeaderBase createHeader(CellRangeAddress[] regions, CFRuleBase[] rules) { + if (rules.length == 0 || rules[0] instanceof CFRuleRecord) { + return new CFHeaderRecord(regions, rules.length); + } + return new CFHeader12Record(regions, rules.length); + } - /** - * Create CFRecordsAggregate from a list of CF Records - * @param rs - the stream to read from - * @return CFRecordsAggregate object - */ - public static CFRecordsAggregate createCFAggregate(RecordStream rs) { - Record rec = rs.getNext(); - if (rec.getSid() != CFHeaderRecord.sid && - rec.getSid() != CFHeader12Record.sid) { - throw new IllegalStateException("next record sid was " + rec.getSid() - + " instead of " + CFHeaderRecord.sid + " or " + - CFHeader12Record.sid + " as expected"); - } + /** + * Create CFRecordsAggregate from a list of CF Records + * @param rs - the stream to read from + * @return CFRecordsAggregate object + */ + public static CFRecordsAggregate createCFAggregate(RecordStream rs) { + Record rec = rs.getNext(); + if (rec.getSid() != CFHeaderRecord.sid && + rec.getSid() != CFHeader12Record.sid) { + throw new IllegalStateException("next record sid was " + rec.getSid() + + " instead of " + CFHeaderRecord.sid + " or " + + CFHeader12Record.sid + " as expected"); + } - CFHeaderBase header = (CFHeaderBase)rec; - int nRules = header.getNumberOfConditionalFormats(); + CFHeaderBase header = (CFHeaderBase)rec; + int nRules = header.getNumberOfConditionalFormats(); - CFRuleBase[] rules = new CFRuleBase[nRules]; - for (int i = 0; i < rules.length; i++) { - rules[i] = (CFRuleBase) rs.getNext(); - } - - return new CFRecordsAggregate(header, rules); - } + CFRuleBase[] rules = new CFRuleBase[nRules]; + for (int i = 0; i < rules.length; i++) { + rules[i] = (CFRuleBase) rs.getNext(); + } - /** - * Create a deep clone of the record - */ - public CFRecordsAggregate cloneCFAggregate() - { - CFRuleBase[] newRecs = new CFRuleBase[rules.size()]; - for (int i = 0; i < newRecs.length; i++) { - newRecs[i] = (CFRuleRecord) getRule(i).clone(); - } - return new CFRecordsAggregate((CFHeaderBase)header.clone(), newRecs); - } + return new CFRecordsAggregate(header, rules); + } - /** - * @return the header. Never null. - */ - public CFHeaderBase getHeader() - { - return header; - } - - private void checkRuleIndex(int idx) { - if(idx < 0 || idx >= rules.size()) { - throw new IllegalArgumentException("Bad rule record index (" + idx - + ") nRules=" + rules.size()); - } - } - private void checkRuleType(CFRuleBase r) { - if (header instanceof CFHeaderRecord && - r instanceof CFRuleRecord) { - return; - } + /** + * Create a deep clone of the record + */ + public CFRecordsAggregate cloneCFAggregate() { + CFRuleBase[] newRecs = new CFRuleBase[rules.size()]; + for (int i = 0; i < newRecs.length; i++) { + newRecs[i] = (CFRuleRecord) getRule(i).clone(); + } + return new CFRecordsAggregate((CFHeaderBase)header.clone(), newRecs); + } + + /** + * @return the header. Never null. + */ + public CFHeaderBase getHeader() { + return header; + } + + private void checkRuleIndex(int idx) { + if(idx < 0 || idx >= rules.size()) { + throw new IllegalArgumentException("Bad rule record index (" + idx + + ") nRules=" + rules.size()); + } + } + private void checkRuleType(CFRuleBase r) { + if (header instanceof CFHeaderRecord && + r instanceof CFRuleRecord) { + return; + } if (header instanceof CFHeader12Record && r instanceof CFRule12Record) { return; } throw new IllegalArgumentException("Header and Rule must both be CF or both be CF12, can't mix"); - } - - public CFRuleBase getRule(int idx) { - checkRuleIndex(idx); - return rules.get(idx); - } - public void setRule(int idx, CFRuleBase r) { - if (r == null) { - throw new IllegalArgumentException("r must not be null"); - } - checkRuleIndex(idx); - checkRuleType(r); - rules.set(idx, r); - } - public void addRule(CFRuleBase r) { - if (r == null) { - throw new IllegalArgumentException("r must not be null"); - } - if(rules.size() >= MAX_97_2003_CONDTIONAL_FORMAT_RULES) { - logger.log(POILogger.WARN, "Excel versions before 2007 cannot cope with" - + " any more than " + MAX_97_2003_CONDTIONAL_FORMAT_RULES - + " - this file will cause problems with old Excel versions"); - } + } + + public CFRuleBase getRule(int idx) { + checkRuleIndex(idx); + return rules.get(idx); + } + public void setRule(int idx, CFRuleBase r) { + if (r == null) { + throw new IllegalArgumentException("r must not be null"); + } + checkRuleIndex(idx); checkRuleType(r); - rules.add(r); - header.setNumberOfConditionalFormats(rules.size()); - } - public int getNumberOfRules() { - return rules.size(); - } + rules.set(idx, r); + } + public void addRule(CFRuleBase r) { + if (r == null) { + throw new IllegalArgumentException("r must not be null"); + } + if(rules.size() >= MAX_97_2003_CONDTIONAL_FORMAT_RULES) { + logger.log(POILogger.WARN, "Excel versions before 2007 cannot cope with" + + " any more than " + MAX_97_2003_CONDTIONAL_FORMAT_RULES + + " - this file will cause problems with old Excel versions"); + } + checkRuleType(r); + rules.add(r); + header.setNumberOfConditionalFormats(rules.size()); + } + public int getNumberOfRules() { + return rules.size(); + } - /** - * String representation of CFRecordsAggregate - */ - public String toString() - { - StringBuffer buffer = new StringBuffer(); - String type = "CF"; - if (header instanceof CFHeader12Record) { - type = "CF12"; - } + /** + * String representation of CFRecordsAggregate + */ + public String toString() { + StringBuffer buffer = new StringBuffer(); + String type = "CF"; + if (header instanceof CFHeader12Record) { + type = "CF12"; + } - buffer.append("[").append(type).append("]\n"); - if( header != null ) - { - buffer.append(header.toString()); - } - for(int i=0; ifalse if this whole {@link CFHeaderRecord} / {@link CFRuleRecord}s should be deleted - */ - public boolean updateFormulasAfterCellShift(FormulaShifter shifter, int currentExternSheetIx) { - CellRangeAddress[] cellRanges = header.getCellRanges(); - boolean changed = false; - List temp = new ArrayList(); - for (int i = 0; i < cellRanges.length; i++) { - CellRangeAddress craOld = cellRanges[i]; - CellRangeAddress craNew = shiftRange(shifter, craOld, currentExternSheetIx); - if (craNew == null) { - changed = true; - continue; - } - temp.add(craNew); - if (craNew != craOld) { - changed = true; - } - } + /** + * @return false if this whole {@link CFHeaderRecord} / {@link CFRuleRecord}s should be deleted + */ + public boolean updateFormulasAfterCellShift(FormulaShifter shifter, int currentExternSheetIx) { + CellRangeAddress[] cellRanges = header.getCellRanges(); + boolean changed = false; + List temp = new ArrayList(); + for (int i = 0; i < cellRanges.length; i++) { + CellRangeAddress craOld = cellRanges[i]; + CellRangeAddress craNew = shiftRange(shifter, craOld, currentExternSheetIx); + if (craNew == null) { + changed = true; + continue; + } + temp.add(craNew); + if (craNew != craOld) { + changed = true; + } + } - if (changed) { - int nRanges = temp.size(); - if (nRanges == 0) { - return false; - } - CellRangeAddress[] newRanges = new CellRangeAddress[nRanges]; - temp.toArray(newRanges); - header.setCellRanges(newRanges); - } - - for(int i=0; i