]> source.dussan.org Git - poi.git/commitdiff
Begin updating the HSSF CF code to work on the base types, so it can handle both...
authorNick Burch <nick@apache.org>
Mon, 13 Jul 2015 18:38:15 +0000 (18:38 +0000)
committerNick Burch <nick@apache.org>
Mon, 13 Jul 2015 18:38:15 +0000 (18:38 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1690793 13f79535-47bb-0310-9956-ffa450edef68

src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java
src/java/org/apache/poi/hssf/usermodel/HSSFBorderFormatting.java
src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java
src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormattingRule.java
src/java/org/apache/poi/hssf/usermodel/HSSFFontFormatting.java
src/java/org/apache/poi/hssf/usermodel/HSSFPatternFormatting.java
src/java/org/apache/poi/hssf/usermodel/HSSFSheetConditionalFormatting.java
src/testcases/org/apache/poi/hssf/record/aggregates/TestCFRecordsAggregate.java

index 85edcae21d932bf4ebf38f2e59adf6989321e82f..d6da4f686d66c2a5bdd80277135e802beefec937 100644 (file)
@@ -21,7 +21,11 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.poi.hssf.model.RecordStream;
+import org.apache.poi.hssf.record.CFHeader12Record;
+import org.apache.poi.hssf.record.CFHeaderBase;
 import org.apache.poi.hssf.record.CFHeaderRecord;
+import org.apache.poi.hssf.record.CFRule12Record;
+import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.CFRuleRecord;
 import org.apache.poi.hssf.record.Record;
 import org.apache.poi.ss.formula.FormulaShifter;
@@ -45,12 +49,12 @@ public final class CFRecordsAggregate extends RecordAggregate {
        private static final int MAX_97_2003_CONDTIONAL_FORMAT_RULES = 3;
        private static final POILogger logger = POILogFactory.getLogger(CFRecordsAggregate.class);
 
-       private final CFHeaderRecord header;
+       private final CFHeaderBase header;
 
        /** List of CFRuleRecord objects */
-       private final List<CFRuleRecord> rules;
+       private final List<CFRuleBase> rules;
 
-       private CFRecordsAggregate(CFHeaderRecord pHeader, CFRuleRecord[] pRules) {
+       private CFRecordsAggregate(CFHeaderBase pHeader, CFRuleBase[] pRules) {
                if(pHeader == null) {
                        throw new IllegalArgumentException("header must not be null");
                }
@@ -67,14 +71,21 @@ public final class CFRecordsAggregate extends RecordAggregate {
                        throw new RuntimeException("Mismatch number of rules");
                }
                header = pHeader;
-               rules = new ArrayList<CFRuleRecord>(3);
+               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, CFRuleRecord[] rules) {
-               this(new CFHeaderRecord(regions, rules.length), rules);
+       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);
        }
 
        /**
@@ -84,17 +95,19 @@ public final class CFRecordsAggregate extends RecordAggregate {
         */
        public static CFRecordsAggregate createCFAggregate(RecordStream rs) {
                Record rec = rs.getNext();
-               if (rec.getSid() != CFHeaderRecord.sid) {
+               if (rec.getSid() != CFHeaderRecord.sid &&
+                   rec.getSid() != CFHeader12Record.sid) {
                        throw new IllegalStateException("next record sid was " + rec.getSid() 
-                                       + " instead of " + CFHeaderRecord.sid + " as expected");
+                                       + " instead of " + CFHeaderRecord.sid + " or " +
+                               CFHeader12Record.sid + " as expected");
                }
 
-               CFHeaderRecord header = (CFHeaderRecord)rec;
+               CFHeaderBase header = (CFHeaderBase)rec;
                int nRules = header.getNumberOfConditionalFormats();
 
-               CFRuleRecord[] rules = new CFRuleRecord[nRules];
+               CFRuleBase[] rules = new CFRuleBase[nRules];
                for (int i = 0; i < rules.length; i++) {
-                       rules[i] = (CFRuleRecord) rs.getNext();
+                       rules[i] = (CFRuleBase) rs.getNext();
                }
                
                return new CFRecordsAggregate(header, rules);
@@ -105,18 +118,17 @@ public final class CFRecordsAggregate extends RecordAggregate {
         */
        public CFRecordsAggregate cloneCFAggregate()
        {
-         
-               CFRuleRecord[] newRecs = new CFRuleRecord[rules.size()];
+           CFRuleBase[] newRecs = new CFRuleBase[rules.size()];
                for (int i = 0; i < newRecs.length; i++) {
                        newRecs[i] = (CFRuleRecord) getRule(i).clone();
                }
-               return new CFRecordsAggregate((CFHeaderRecord) header.clone(), newRecs);
+               return new CFRecordsAggregate((CFHeaderBase)header.clone(), newRecs);
        }
 
        /**
         * @return the header. Never <code>null</code>.
         */
-       public CFHeaderRecord getHeader()
+       public CFHeaderBase getHeader()
        {
                return header;
        }
@@ -127,18 +139,31 @@ public final class CFRecordsAggregate extends RecordAggregate {
                                        + ") nRules=" + rules.size());
                }
        }
-       public CFRuleRecord getRule(int idx) {
+       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, CFRuleRecord r) {
+       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(CFRuleRecord r) {
+       public void addRule(CFRuleBase r) {
                if (r == null) {
                        throw new IllegalArgumentException("r must not be null");
                }
@@ -147,6 +172,7 @@ public final class CFRecordsAggregate extends RecordAggregate {
                            + " 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());
        }
@@ -160,25 +186,29 @@ public final class CFRecordsAggregate extends RecordAggregate {
        public String toString()
        {
                StringBuffer buffer = new StringBuffer();
+               String type = "CF";
+               if (header instanceof CFHeader12Record) {
+                   type = "CF12";
+               }
 
-               buffer.append("[CF]\n");
+               buffer.append("[").append(type).append("]\n");
                if( header != null )
                {
                        buffer.append(header.toString());
                }
                for(int i=0; i<rules.size(); i++)
                {
-                       CFRuleRecord cfRule = rules.get(i);
+                       CFRuleBase cfRule = rules.get(i);
                        buffer.append(cfRule.toString());
                }
-               buffer.append("[/CF]\n");
+        buffer.append("[/").append(type).append("]\n");
                return buffer.toString();
        }
 
        public void visitContainedRecords(RecordVisitor rv) {
                rv.visitRecord(header);
                for(int i=0; i<rules.size(); i++) {
-                       CFRuleRecord rule = rules.get(i);
+                       CFRuleBase rule = rules.get(i);
                        rv.visitRecord(rule);
                }
        }
@@ -214,7 +244,7 @@ public final class CFRecordsAggregate extends RecordAggregate {
                }
                
                for(int i=0; i<rules.size(); i++) {
-                       CFRuleRecord rule = rules.get(i);
+                       CFRuleBase rule = rules.get(i);
                        Ptg[] ptgs;
                        ptgs = rule.getParsedExpression1();
                        if (ptgs != null && shifter.adjustFormula(ptgs, currentExternSheetIx)) {
@@ -224,6 +254,13 @@ public final class CFRecordsAggregate extends RecordAggregate {
                        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;
        }
index 0e2cb9d208257e98de3611c652e33f2884c3db45..7f94f92c986fc31b9c4eb61b36467ed025fdf4e4 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.usermodel;
 
-import org.apache.poi.hssf.record.CFRuleRecord;
+import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.cf.BorderFormatting;
 
 /**
@@ -26,10 +26,10 @@ import org.apache.poi.hssf.record.cf.BorderFormatting;
  */
 public final class HSSFBorderFormatting implements org.apache.poi.ss.usermodel.BorderFormatting
 {
-       private final CFRuleRecord cfRuleRecord;
+       private final CFRuleBase cfRuleRecord;
        private final BorderFormatting borderFormatting;
        
-       protected HSSFBorderFormatting(CFRuleRecord cfRuleRecord)
+       protected HSSFBorderFormatting(CFRuleBase cfRuleRecord)
        {
                this.cfRuleRecord = cfRuleRecord;
                this.borderFormatting = cfRuleRecord.getBorderFormatting();
index 52a528331d67e69f206e8a20b7a5acaf907b02a7..dbfa49cabec29bd9d91b8c7686cf3194c89014ec 100644 (file)
@@ -16,7 +16,7 @@
 ==================================================================== */
 package org.apache.poi.hssf.usermodel;
 
-import org.apache.poi.hssf.record.CFRuleRecord;
+import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.aggregates.CFRecordsAggregate;
 import org.apache.poi.ss.usermodel.ConditionalFormatting;
 import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
@@ -147,7 +147,7 @@ public final class HSSFConditionalFormatting  implements ConditionalFormatting
         */
        public HSSFConditionalFormattingRule getRule(int idx)
        {
-               CFRuleRecord ruleRecord = cfAggregate.getRule(idx);
+               CFRuleBase ruleRecord = cfAggregate.getRule(idx);
                return new HSSFConditionalFormattingRule(_workbook, ruleRecord);
        }
 
index 0ca978aaa745ee5ceefede735ed4fc053703d71f..93af503de384010c7655ca3593a6cddd203327a6 100644 (file)
@@ -18,6 +18,7 @@
 package org.apache.poi.hssf.usermodel;
 
 import org.apache.poi.hssf.model.HSSFFormulaParser;
+import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator;
 import org.apache.poi.hssf.record.CFRuleRecord;
 import org.apache.poi.hssf.record.cf.BorderFormatting;
@@ -36,10 +37,10 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
 {
        private static final byte CELL_COMPARISON = CFRuleRecord.CONDITION_TYPE_CELL_VALUE_IS;
 
-       private final CFRuleRecord cfRuleRecord;
+       private final CFRuleBase cfRuleRecord;
        private final HSSFWorkbook workbook;
 
-       HSSFConditionalFormattingRule(HSSFWorkbook pWorkbook, CFRuleRecord pRuleRecord) {
+       HSSFConditionalFormattingRule(HSSFWorkbook pWorkbook, CFRuleBase pRuleRecord) {
                if (pWorkbook == null) {
                        throw new IllegalArgumentException("pWorkbook must not be null");
                }
@@ -50,7 +51,7 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin
                cfRuleRecord = pRuleRecord;
        }
 
-       CFRuleRecord getCfRuleRecord()
+       CFRuleBase getCfRuleRecord()
        {
                return cfRuleRecord;
        }
index 598ea9c024a71338d81d723760c7d2586d708d50..f07965c7f8f96b8b7566346ce7a3b9ad57ad049c 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.usermodel;
 
-import org.apache.poi.hssf.record.CFRuleRecord;
+import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.cf.FontFormatting;
 /**
  * High level representation for Font Formatting component
@@ -25,7 +25,6 @@ import org.apache.poi.hssf.record.cf.FontFormatting;
  */
 public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.FontFormatting
 {
-
        /** Underline type - None */
        public final static byte U_NONE              = FontFormatting.U_NONE;
        /** Underline type - Single */
@@ -39,7 +38,7 @@ public final class HSSFFontFormatting implements org.apache.poi.ss.usermodel.Fon
 
        private final FontFormatting fontFormatting;
        
-       protected HSSFFontFormatting(CFRuleRecord cfRuleRecord)
+       protected HSSFFontFormatting(CFRuleBase cfRuleRecord)
        {
                this.fontFormatting = cfRuleRecord.getFontFormatting();
        }
index 0430342868e4a88a2df76d1be4339115cbd052ad..def9d8f2415bf0df2f3aa1707e168eed3712483d 100644 (file)
@@ -17,7 +17,7 @@
 
 package org.apache.poi.hssf.usermodel;
 
-import org.apache.poi.hssf.record.CFRuleRecord;
+import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.cf.PatternFormatting;
 
 /**
@@ -25,10 +25,10 @@ import org.apache.poi.hssf.record.cf.PatternFormatting;
  */
 public class HSSFPatternFormatting implements org.apache.poi.ss.usermodel.PatternFormatting
 {
-       private final CFRuleRecord cfRuleRecord;
+       private final CFRuleBase cfRuleRecord;
        private final PatternFormatting patternFormatting;
        
-       protected HSSFPatternFormatting(CFRuleRecord cfRuleRecord)
+       protected HSSFPatternFormatting(CFRuleBase cfRuleRecord)
        {
                this.cfRuleRecord = cfRuleRecord; 
                this.patternFormatting = cfRuleRecord.getPatternFormatting();
index ce6bf76b666f0ad81ecf6e5ff67fbf1467dc2328..1064467dbfcce9b8608cde47701580a875256ec7 100644 (file)
 
 package org.apache.poi.hssf.usermodel;
 
+import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.CFRuleRecord;
 import org.apache.poi.hssf.record.aggregates.CFRecordsAggregate;
 import org.apache.poi.hssf.record.aggregates.ConditionalFormattingTable;
+import org.apache.poi.ss.SpreadsheetVersion;
 import org.apache.poi.ss.usermodel.ConditionalFormatting;
 import org.apache.poi.ss.usermodel.ConditionalFormattingRule;
 import org.apache.poi.ss.usermodel.SheetConditionalFormatting;
 import org.apache.poi.ss.util.CellRangeAddress;
-import org.apache.poi.ss.SpreadsheetVersion;
 
 /**
  * The 'Conditional Formatting' facet of <tt>HSSFSheet</tt>
@@ -146,7 +147,7 @@ public final class HSSFSheetConditionalFormatting implements SheetConditionalFor
                        throw new IllegalArgumentException("Number of rules must not exceed 3");
                }
 
-               CFRuleRecord[] rules = new CFRuleRecord[cfRules.length];
+               CFRuleBase[] rules = new CFRuleBase[cfRules.length];
                for (int i = 0; i != cfRules.length; i++) {
                        rules[i] = cfRules[i].getCfRuleRecord();
                }
index 89cca1e69b61f89f75201e6748cbe1eb3a857d80..c58f401bc4ccb080d5332e018fff99a458308845 100644 (file)
@@ -28,6 +28,7 @@ import junit.framework.TestCase;
 import org.apache.poi.hssf.model.RecordStream;
 import org.apache.poi.hssf.record.CFHeaderBase;
 import org.apache.poi.hssf.record.CFHeaderRecord;
+import org.apache.poi.hssf.record.CFRule12Record;
 import org.apache.poi.hssf.record.CFRuleBase;
 import org.apache.poi.hssf.record.CFRuleBase.ComparisonOperator;
 import org.apache.poi.hssf.record.CFRuleRecord;
@@ -128,4 +129,30 @@ public final class TestCFRecordsAggregate extends TestCase {
         }
         assertEquals(rules.length, nRules);
     }
+    
+    public void testCantMixTypes() {
+        HSSFWorkbook workbook = new HSSFWorkbook();
+        HSSFSheet sheet = workbook.createSheet();
+        CellRangeAddress[] cellRanges = {
+                new CellRangeAddress(0,1,0,0),
+                new CellRangeAddress(0,1,2,2),
+        };
+        CFRuleBase[] rules = {
+                CFRuleRecord.create(sheet, "7"),
+                CFRule12Record.create(sheet, ComparisonOperator.BETWEEN, "2", "5"),
+        };
+        try {
+            new CFRecordsAggregate(cellRanges, rules);
+            fail("Shouldn't be able to mix between types");
+        } catch (IllegalArgumentException e) {}
+        
+        
+        rules = new CFRuleBase[] { CFRuleRecord.create(sheet, "7") };
+        CFRecordsAggregate agg = new CFRecordsAggregate(cellRanges, rules);
+        
+        try {
+            agg.addRule(CFRule12Record.create(sheet, "7"));
+            fail("Shouldn't be able to mix between types");
+        } catch (IllegalArgumentException e) {}
+    }
 }