Fix inconsistent indents/whitespace

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1690794 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2015-07-13 18:40:25 +00:00
parent 00129c4158
commit efdadf909f
1 changed files with 211 additions and 216 deletions

View File

@ -45,242 +45,237 @@ import org.apache.poi.util.POILogger;
* not a file format one.</p> * not a file format one.</p>
*/ */
public final class CFRecordsAggregate extends RecordAggregate { public final class CFRecordsAggregate extends RecordAggregate {
/** Excel 97-2003 allows up to 3 conditional formating rules */ /** Excel 97-2003 allows up to 3 conditional formating rules */
private static final int MAX_97_2003_CONDTIONAL_FORMAT_RULES = 3; private static final int MAX_97_2003_CONDTIONAL_FORMAT_RULES = 3;
private static final POILogger logger = POILogFactory.getLogger(CFRecordsAggregate.class); private static final POILogger logger = POILogFactory.getLogger(CFRecordsAggregate.class);
private final CFHeaderBase header; private final CFHeaderBase header;
/** List of CFRuleRecord objects */ /** List of CFRuleRecord objects */
private final List<CFRuleBase> rules; private final List<CFRuleBase> rules;
private CFRecordsAggregate(CFHeaderBase pHeader, CFRuleBase[] pRules) { private CFRecordsAggregate(CFHeaderBase pHeader, CFRuleBase[] pRules) {
if(pHeader == null) { if(pHeader == null) {
throw new IllegalArgumentException("header must not be null"); throw new IllegalArgumentException("header must not be null");
} }
if(pRules == null) { if(pRules == null) {
throw new IllegalArgumentException("rules must not be null"); throw new IllegalArgumentException("rules must not be null");
} }
if(pRules.length > MAX_97_2003_CONDTIONAL_FORMAT_RULES) { if(pRules.length > MAX_97_2003_CONDTIONAL_FORMAT_RULES) {
logger.log(POILogger.WARN, "Excel versions before 2007 require that " logger.log(POILogger.WARN, "Excel versions before 2007 require that "
+ "No more than " + MAX_97_2003_CONDTIONAL_FORMAT_RULES + "No more than " + MAX_97_2003_CONDTIONAL_FORMAT_RULES
+ " rules may be specified, " + pRules.length + " were found," + " rules may be specified, " + pRules.length + " were found,"
+ " this file will cause problems with old Excel versions"); + " this file will cause problems with old Excel versions");
} }
if (pRules.length != pHeader.getNumberOfConditionalFormats()) { if (pRules.length != pHeader.getNumberOfConditionalFormats()) {
throw new RuntimeException("Mismatch number of rules"); throw new RuntimeException("Mismatch number of rules");
} }
header = pHeader; header = pHeader;
rules = new ArrayList<CFRuleBase>(pRules.length); rules = new ArrayList<CFRuleBase>(pRules.length);
for (int i = 0; i < pRules.length; i++) { for (int i = 0; i < pRules.length; i++) {
checkRuleType(pRules[i]); checkRuleType(pRules[i]);
rules.add(pRules[i]); rules.add(pRules[i]);
} }
} }
public CFRecordsAggregate(CellRangeAddress[] regions, CFRuleBase[] rules) { public CFRecordsAggregate(CellRangeAddress[] regions, CFRuleBase[] rules) {
this(createHeader(regions, rules), rules); this(createHeader(regions, rules), rules);
} }
private static CFHeaderBase createHeader(CellRangeAddress[] regions, CFRuleBase[] rules) { private static CFHeaderBase createHeader(CellRangeAddress[] regions, CFRuleBase[] rules) {
if (rules.length == 0 || rules[0] instanceof CFRuleRecord) { if (rules.length == 0 || rules[0] instanceof CFRuleRecord) {
return new CFHeaderRecord(regions, rules.length); return new CFHeaderRecord(regions, rules.length);
} }
return new CFHeader12Record(regions, rules.length); return new CFHeader12Record(regions, rules.length);
} }
/** /**
* Create CFRecordsAggregate from a list of CF Records * Create CFRecordsAggregate from a list of CF Records
* @param rs - the stream to read from * @param rs - the stream to read from
* @return CFRecordsAggregate object * @return CFRecordsAggregate object
*/ */
public static CFRecordsAggregate createCFAggregate(RecordStream rs) { public static CFRecordsAggregate createCFAggregate(RecordStream rs) {
Record rec = rs.getNext(); Record rec = rs.getNext();
if (rec.getSid() != CFHeaderRecord.sid && if (rec.getSid() != CFHeaderRecord.sid &&
rec.getSid() != CFHeader12Record.sid) { rec.getSid() != CFHeader12Record.sid) {
throw new IllegalStateException("next record sid was " + rec.getSid() throw new IllegalStateException("next record sid was " + rec.getSid()
+ " instead of " + CFHeaderRecord.sid + " or " + + " instead of " + CFHeaderRecord.sid + " or " +
CFHeader12Record.sid + " as expected"); CFHeader12Record.sid + " as expected");
} }
CFHeaderBase header = (CFHeaderBase)rec; CFHeaderBase header = (CFHeaderBase)rec;
int nRules = header.getNumberOfConditionalFormats(); int nRules = header.getNumberOfConditionalFormats();
CFRuleBase[] rules = new CFRuleBase[nRules]; CFRuleBase[] rules = new CFRuleBase[nRules];
for (int i = 0; i < rules.length; i++) { for (int i = 0; i < rules.length; i++) {
rules[i] = (CFRuleBase) rs.getNext(); rules[i] = (CFRuleBase) rs.getNext();
} }
return new CFRecordsAggregate(header, rules);
}
/** return new CFRecordsAggregate(header, rules);
* 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 <code>null</code>. * Create a deep clone of the record
*/ */
public CFHeaderBase getHeader() public CFRecordsAggregate cloneCFAggregate() {
{ CFRuleBase[] newRecs = new CFRuleBase[rules.size()];
return header; for (int i = 0; i < newRecs.length; i++) {
} newRecs[i] = (CFRuleRecord) getRule(i).clone();
}
private void checkRuleIndex(int idx) { return new CFRecordsAggregate((CFHeaderBase)header.clone(), newRecs);
if(idx < 0 || idx >= rules.size()) { }
throw new IllegalArgumentException("Bad rule record index (" + idx
+ ") nRules=" + rules.size()); /**
} * @return the header. Never <code>null</code>.
} */
private void checkRuleType(CFRuleBase r) { public CFHeaderBase getHeader() {
if (header instanceof CFHeaderRecord && return header;
r instanceof CFRuleRecord) { }
return;
} 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 && if (header instanceof CFHeader12Record &&
r instanceof CFRule12Record) { r instanceof CFRule12Record) {
return; return;
} }
throw new IllegalArgumentException("Header and Rule must both be CF or both be CF12, can't mix"); throw new IllegalArgumentException("Header and Rule must both be CF or both be CF12, can't mix");
} }
public CFRuleBase getRule(int idx) { public CFRuleBase getRule(int idx) {
checkRuleIndex(idx); checkRuleIndex(idx);
return rules.get(idx); return rules.get(idx);
} }
public void setRule(int idx, CFRuleBase r) { public void setRule(int idx, CFRuleBase r) {
if (r == null) { if (r == null) {
throw new IllegalArgumentException("r must not be null"); throw new IllegalArgumentException("r must not be null");
} }
checkRuleIndex(idx); 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");
}
checkRuleType(r); checkRuleType(r);
rules.add(r); rules.set(idx, r);
header.setNumberOfConditionalFormats(rules.size()); }
} public void addRule(CFRuleBase r) {
public int getNumberOfRules() { if (r == null) {
return rules.size(); 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 * String representation of CFRecordsAggregate
*/ */
public String toString() public String toString() {
{ StringBuffer buffer = new StringBuffer();
StringBuffer buffer = new StringBuffer(); String type = "CF";
String type = "CF"; if (header instanceof CFHeader12Record) {
if (header instanceof CFHeader12Record) { type = "CF12";
type = "CF12"; }
}
buffer.append("[").append(type).append("]\n"); buffer.append("[").append(type).append("]\n");
if( header != null ) if( header != null ) {
{ buffer.append(header.toString());
buffer.append(header.toString()); }
} for(int i=0; i<rules.size(); i++) {
for(int i=0; i<rules.size(); i++) CFRuleBase cfRule = rules.get(i);
{ buffer.append(cfRule.toString());
CFRuleBase cfRule = rules.get(i); }
buffer.append(cfRule.toString());
}
buffer.append("[/").append(type).append("]\n"); buffer.append("[/").append(type).append("]\n");
return buffer.toString(); return buffer.toString();
} }
public void visitContainedRecords(RecordVisitor rv) { public void visitContainedRecords(RecordVisitor rv) {
rv.visitRecord(header); rv.visitRecord(header);
for(int i=0; i<rules.size(); i++) { for(int i=0; i<rules.size(); i++) {
CFRuleBase rule = rules.get(i); CFRuleBase rule = rules.get(i);
rv.visitRecord(rule); rv.visitRecord(rule);
} }
} }
/** /**
* @return <code>false</code> if this whole {@link CFHeaderRecord} / {@link CFRuleRecord}s should be deleted * @return <code>false</code> if this whole {@link CFHeaderRecord} / {@link CFRuleRecord}s should be deleted
*/ */
public boolean updateFormulasAfterCellShift(FormulaShifter shifter, int currentExternSheetIx) { public boolean updateFormulasAfterCellShift(FormulaShifter shifter, int currentExternSheetIx) {
CellRangeAddress[] cellRanges = header.getCellRanges(); CellRangeAddress[] cellRanges = header.getCellRanges();
boolean changed = false; boolean changed = false;
List<CellRangeAddress> temp = new ArrayList<CellRangeAddress>(); List<CellRangeAddress> temp = new ArrayList<CellRangeAddress>();
for (int i = 0; i < cellRanges.length; i++) { for (int i = 0; i < cellRanges.length; i++) {
CellRangeAddress craOld = cellRanges[i]; CellRangeAddress craOld = cellRanges[i];
CellRangeAddress craNew = shiftRange(shifter, craOld, currentExternSheetIx); CellRangeAddress craNew = shiftRange(shifter, craOld, currentExternSheetIx);
if (craNew == null) { if (craNew == null) {
changed = true; changed = true;
continue; continue;
} }
temp.add(craNew); temp.add(craNew);
if (craNew != craOld) { if (craNew != craOld) {
changed = true; changed = true;
} }
} }
if (changed) { if (changed) {
int nRanges = temp.size(); int nRanges = temp.size();
if (nRanges == 0) { if (nRanges == 0) {
return false; return false;
} }
CellRangeAddress[] newRanges = new CellRangeAddress[nRanges]; CellRangeAddress[] newRanges = new CellRangeAddress[nRanges];
temp.toArray(newRanges); temp.toArray(newRanges);
header.setCellRanges(newRanges); header.setCellRanges(newRanges);
} }
for(int i=0; i<rules.size(); i++) {
CFRuleBase rule = rules.get(i);
Ptg[] ptgs;
ptgs = rule.getParsedExpression1();
if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
rule.setParsedExpression1(ptgs);
}
ptgs = rule.getParsedExpression2();
if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
rule.setParsedExpression2(ptgs);
}
if (rule instanceof CFRule12Record) {
CFRule12Record rule12 = (CFRule12Record)rule;
ptgs = rule12.getParsedExpressionScale();
if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
rule12.setParsedExpressionScale(ptgs);
}
}
}
return true;
}
private static CellRangeAddress shiftRange(FormulaShifter shifter, CellRangeAddress cra, int currentExternSheetIx) { for(int i=0; i<rules.size(); i++) {
// FormulaShifter works well in terms of Ptgs - so convert CellRangeAddress to AreaPtg (and back) here CFRuleBase rule = rules.get(i);
AreaPtg aptg = new AreaPtg(cra.getFirstRow(), cra.getLastRow(), cra.getFirstColumn(), cra.getLastColumn(), false, false, false, false); Ptg[] ptgs;
Ptg[] ptgs = { aptg, }; ptgs = rule.getParsedExpression1();
if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
if (!shifter.adjustFormula(ptgs, currentExternSheetIx)) { rule.setParsedExpression1(ptgs);
return cra; }
} ptgs = rule.getParsedExpression2();
Ptg ptg0 = ptgs[0]; if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
if (ptg0 instanceof AreaPtg) { rule.setParsedExpression2(ptgs);
AreaPtg bptg = (AreaPtg) ptg0; }
return new CellRangeAddress(bptg.getFirstRow(), bptg.getLastRow(), bptg.getFirstColumn(), bptg.getLastColumn()); if (rule instanceof CFRule12Record) {
} CFRule12Record rule12 = (CFRule12Record)rule;
if (ptg0 instanceof AreaErrPtg) { ptgs = rule12.getParsedExpressionScale();
return null; if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
} rule12.setParsedExpressionScale(ptgs);
throw new IllegalStateException("Unexpected shifted ptg class (" + ptg0.getClass().getName() + ")"); }
} }
}
return true;
}
private static CellRangeAddress shiftRange(FormulaShifter shifter, CellRangeAddress cra, int currentExternSheetIx) {
// FormulaShifter works well in terms of Ptgs - so convert CellRangeAddress to AreaPtg (and back) here
AreaPtg aptg = new AreaPtg(cra.getFirstRow(), cra.getLastRow(), cra.getFirstColumn(), cra.getLastColumn(), false, false, false, false);
Ptg[] ptgs = { aptg, };
if (!shifter.adjustFormula(ptgs, currentExternSheetIx)) {
return cra;
}
Ptg ptg0 = ptgs[0];
if (ptg0 instanceof AreaPtg) {
AreaPtg bptg = (AreaPtg) ptg0;
return new CellRangeAddress(bptg.getFirstRow(), bptg.getLastRow(), bptg.getFirstColumn(), bptg.getLastColumn());
}
if (ptg0 instanceof AreaErrPtg) {
return null;
}
throw new IllegalStateException("Unexpected shifted ptg class (" + ptg0.getClass().getName() + ")");
}
} }