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:
parent
65aa10e10f
commit
fda28eae8b
@ -45,242 +45,237 @@ import org.apache.poi.util.POILogger;
|
||||
* not a file format one.</p>
|
||||
*/
|
||||
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<CFRuleBase> rules;
|
||||
/** List of CFRuleRecord objects */
|
||||
private final List<CFRuleBase> 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<CFRuleBase>(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<CFRuleBase>(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 <code>null</code>.
|
||||
*/
|
||||
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 <code>null</code>.
|
||||
*/
|
||||
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; i<rules.size(); i++)
|
||||
{
|
||||
CFRuleBase cfRule = rules.get(i);
|
||||
buffer.append(cfRule.toString());
|
||||
}
|
||||
buffer.append("[").append(type).append("]\n");
|
||||
if( header != null ) {
|
||||
buffer.append(header.toString());
|
||||
}
|
||||
for(int i=0; i<rules.size(); i++) {
|
||||
CFRuleBase cfRule = rules.get(i);
|
||||
buffer.append(cfRule.toString());
|
||||
}
|
||||
buffer.append("[/").append(type).append("]\n");
|
||||
return buffer.toString();
|
||||
}
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
public void visitContainedRecords(RecordVisitor rv) {
|
||||
rv.visitRecord(header);
|
||||
for(int i=0; i<rules.size(); i++) {
|
||||
CFRuleBase rule = rules.get(i);
|
||||
rv.visitRecord(rule);
|
||||
}
|
||||
}
|
||||
public void visitContainedRecords(RecordVisitor rv) {
|
||||
rv.visitRecord(header);
|
||||
for(int i=0; i<rules.size(); i++) {
|
||||
CFRuleBase rule = rules.get(i);
|
||||
rv.visitRecord(rule);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return <code>false</code> 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<CellRangeAddress> temp = new ArrayList<CellRangeAddress>();
|
||||
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 <code>false</code> 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<CellRangeAddress> temp = new ArrayList<CellRangeAddress>();
|
||||
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<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;
|
||||
}
|
||||
if (changed) {
|
||||
int nRanges = temp.size();
|
||||
if (nRanges == 0) {
|
||||
return false;
|
||||
}
|
||||
CellRangeAddress[] newRanges = new CellRangeAddress[nRanges];
|
||||
temp.toArray(newRanges);
|
||||
header.setCellRanges(newRanges);
|
||||
}
|
||||
|
||||
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() + ")");
|
||||
}
|
||||
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) {
|
||||
// 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() + ")");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user