From 520a6fdf689b9ccf88a85f5951166972c4581709 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 19 Jul 2015 00:08:26 +0000 Subject: [PATCH 1/7] #58130 More CF Icon unit tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691787 13f79535-47bb-0310-9956-ffa450edef68 --- .../BaseTestConditionalFormatting.java | 101 +++++++++++++++--- 1 file changed, 88 insertions(+), 13 deletions(-) diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java index b8eeb2053..1204355fa 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java @@ -101,6 +101,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { /** * Test format conditions based on a boolean formula */ + @SuppressWarnings("deprecation") public void testBooleanFormulaConditions() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sh = wb.createSheet(); @@ -136,6 +137,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertEquals("B1:B3", ranges2[0].formatAsString()); } + @SuppressWarnings("deprecation") public void testSingleFormulaConditions() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sh = wb.createSheet(); @@ -207,6 +209,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertEquals(ComparisonOperator.NOT_BETWEEN, rule9.getComparisonOperation()); } + @SuppressWarnings("deprecation") public void testCopy() { Workbook wb = _testDataProvider.createWorkbook(); Sheet sheet1 = wb.createSheet(); @@ -546,8 +549,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { Sheet s = wb.getSheet("CF"); ConditionalFormatting cf = null; ConditionalFormattingRule cr = null; - IconMultiStateFormatting icon = null; - ConditionalFormattingThreshold th = null; + // Sanity check data assertEquals("Values", s.getRow(0).getCell(0).toString()); @@ -696,22 +698,95 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertIconSetPercentages(cf, IconSet.GYRB_4_TRAFFIC_LIGHTS, 0d, 25d, 50d, 75d); - // Icons : 3 symbols - Column L - // Icons : 3 flags - Column M - // Icons : 3 symbols 2 - Column N - // Icons : 3 arrows - Column O - // Icons : 5 arrows grey - Column P - // Icons : 3 stars (ext) - Column Q - // Icons : 4 ratings - Column R - // Icons : 5 ratings - Column S - // Custom Icon+Format - Column T - // Mixed icons - Column U + // Icons : 3 symbols with backgrounds - Column L + cf = sheetCF.getConditionalFormattingAt(9); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("L2:L17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GYR_3_SYMBOLS_CIRCLE, 0d, 33d, 67d); + + // Icons : 3 flags - Column M2 Only + cf = sheetCF.getConditionalFormattingAt(10); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("M2", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GYR_3_FLAGS, 0d, 33d, 67d); + + // Icons : 3 flags - Column M (all) + cf = sheetCF.getConditionalFormattingAt(11); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("M2:M17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GYR_3_FLAGS, 0d, 33d, 67d); + + + // Icons : 3 symbols 2 (no background) - Column N + cf = sheetCF.getConditionalFormattingAt(12); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("N2:N17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GYR_3_SYMBOLS, 0d, 33d, 67d); + + + // Icons : 3 arrows - Column O + cf = sheetCF.getConditionalFormattingAt(13); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("O2:O17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GYR_3_ARROW, 0d, 33d, 67d); + + + // Icons : 5 arrows grey - Column P + cf = sheetCF.getConditionalFormattingAt(14); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("P2:P17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.GREY_5_ARROWS, 0d, 20d, 40d, 60d, 80d); + + + // Icons : 3 stars (ext) - Column Q + // TODO Support EXT formattings + + + // Icons : 4 ratings - Column R + cf = sheetCF.getConditionalFormattingAt(15); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("R2:R17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.RATINGS_4, 0d, 25d, 50d, 75d); + + + // Icons : 5 ratings - Column S + cf = sheetCF.getConditionalFormattingAt(16); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("S2:S17", cf.getFormattingRanges()[0].formatAsString()); + assertIconSetPercentages(cf, IconSet.RATINGS_5, 0d, 20d, 40d, 60d, 80d); + + + // Custom Icon+Format - Column T + cf = sheetCF.getConditionalFormattingAt(17); + assertEquals(1, cf.getFormattingRanges().length); + assertEquals("T2:T17", cf.getFormattingRanges()[0].formatAsString()); + + // TODO Support IconSet + Other CFs with 2 rules +// assertEquals(2, cf.getNumberOfRules()); +// cr = cf.getRule(0); +// assertIconSetPercentages(cr, IconSet.GYR_3_TRAFFIC_LIGHTS_BOX, 0d, 33d, 67d); +// cr = cf.getRule(1); +// assertEquals(ConditionType.FORMULA, cr.getConditionTypeType()); +// assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation()); +// // TODO Why aren't these two the same between formats? +// if (cr instanceof HSSFConditionalFormattingRule) { +// assertEquals("MOD(ROW($T1),2)=1", cr.getFormula1()); +// } else { +// assertEquals("MOD(ROW($T2),2)=1", cr.getFormula1()); +// } +// assertEquals(null, cr.getFormula2()); + + + // Mixed icons - Column U + // TODO Support EXT formattings } private void assertIconSetPercentages(ConditionalFormatting cf, IconSet iconset, Double...vals) { assertEquals(1, cf.getNumberOfRules()); ConditionalFormattingRule cr = cf.getRule(0); - + assertIconSetPercentages(cr, iconset, vals); + } + private void assertIconSetPercentages(ConditionalFormattingRule cr, IconSet iconset, Double...vals) { assertEquals(ConditionType.ICON_SET, cr.getConditionTypeType()); assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation()); assertEquals(null, cr.getFormula1()); From 22c2e794d6cc37718d7783bbee0de18ba8393d94 Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 19 Jul 2015 02:35:04 +0000 Subject: [PATCH 2/7] #58130 Fix the icon formatting read/write CF test git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691788 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/poi/hssf/record/cf/Threshold.java | 11 +++++++++++ .../ss/usermodel/BaseTestConditionalFormatting.java | 7 +++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/java/org/apache/poi/hssf/record/cf/Threshold.java b/src/java/org/apache/poi/hssf/record/cf/Threshold.java index 462027807..414bf0f4c 100644 --- a/src/java/org/apache/poi/hssf/record/cf/Threshold.java +++ b/src/java/org/apache/poi/hssf/record/cf/Threshold.java @@ -73,6 +73,14 @@ public final class Threshold { } public void setType(byte type) { this.type = type; + + // Ensure the value presense / absense is consistent for the new type + if (type == RangeType.MIN.id || type == RangeType.MAX.id || + type == RangeType.FORMULA.id) { + this.value = null; + } else if (value == null) { + this.value = 0d; + } } public void setType(int type) { this.type = (byte)type; @@ -86,6 +94,9 @@ public final class Threshold { } public void setParsedExpression(Ptg[] ptgs) { formula = Formula.create(ptgs); + if (ptgs.length > 0) { + this.value = null; + } } public Double getValue() { diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java index 1204355fa..602c33495 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java @@ -555,11 +555,11 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertEquals("Values", s.getRow(0).getCell(0).toString()); assertEquals("10.0", s.getRow(2).getCell(0).toString()); - // Check we found all the conditional formattings rules we should have + // Check we found all the conditional formatting rules we should have SheetConditionalFormatting sheetCF = s.getSheetConditionalFormatting(); int numCF = 3; int numCF12 = 15; - int numCFEX = 0; // TODO This should be 1, but we don't support CFEX formattings yet + int numCFEX = 0; // TODO This should be 2, but we don't support CFEX formattings yet assertEquals(numCF+numCF12+numCFEX, sheetCF.getNumConditionalFormattings()); int fCF = 0, fCF12 = 0, fCFEX = 0; @@ -982,8 +982,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertEquals(BorderFormatting.BORDER_HAIR, r1fp.getBorderRight()); } - // TODO Fix this test to work for HSSF - public void DISABLEDtestCreateIconFormatting() { + public void testCreateIconFormatting() { Workbook workbook = _testDataProvider.createWorkbook(); Sheet sheet = workbook.createSheet(); From 65b58b1a5d7592973e38e0aa7a35e2d25ec5411d Mon Sep 17 00:00:00 2001 From: Nick Burch Date: Sun, 19 Jul 2015 02:54:25 +0000 Subject: [PATCH 3/7] #58130 Stub the CF Color Scale unit tests git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691789 13f79535-47bb-0310-9956-ffa450edef68 --- .../BaseTestConditionalFormatting.java | 62 +++++++++++++++---- 1 file changed, 49 insertions(+), 13 deletions(-) diff --git a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java index 602c33495..1a130b751 100644 --- a/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java +++ b/src/testcases/org/apache/poi/ss/usermodel/BaseTestConditionalFormatting.java @@ -559,7 +559,7 @@ public abstract class BaseTestConditionalFormatting extends TestCase { SheetConditionalFormatting sheetCF = s.getSheetConditionalFormatting(); int numCF = 3; int numCF12 = 15; - int numCFEX = 0; // TODO This should be 2, but we don't support CFEX formattings yet + int numCFEX = 0; // TODO This should be 2, but we don't support CFEX formattings yet, see #58149 assertEquals(numCF+numCF12+numCFEX, sheetCF.getNumConditionalFormattings()); int fCF = 0, fCF12 = 0, fCFEX = 0; @@ -648,26 +648,18 @@ public abstract class BaseTestConditionalFormatting extends TestCase { // TODO Support Data Bars, then check the rest of this rule - // Colours R->G - Column F + // Colours Red->Yellow->Green - Column F cf = sheetCF.getConditionalFormattingAt(3); assertEquals(1, cf.getFormattingRanges().length); assertEquals("F2:F17", cf.getFormattingRanges()[0].formatAsString()); - - assertEquals(1, cf.getNumberOfRules()); - cr = cf.getRule(0); - assertEquals(ConditionType.COLOR_SCALE, cr.getConditionTypeType()); - // TODO Support Color Scales, then check the rest of this rule + assertColorScale(cf, "F8696B", "FFEB84", "63BE7B"); - // Colours BWR - Column G + // Colours Blue->White->Red - Column G cf = sheetCF.getConditionalFormattingAt(4); assertEquals(1, cf.getFormattingRanges().length); assertEquals("G2:G17", cf.getFormattingRanges()[0].formatAsString()); - - assertEquals(1, cf.getNumberOfRules()); - cr = cf.getRule(0); - assertEquals(ConditionType.COLOR_SCALE, cr.getConditionTypeType()); - // TODO Support Color Scales, then check the rest of this rule + assertColorScale(cf, "5A8AC6", "FCFCFF", "F8696B"); // Icons : Default - Column H, percentage thresholds @@ -808,6 +800,50 @@ public abstract class BaseTestConditionalFormatting extends TestCase { assertEquals(null, th.getFormula()); } } + + private void assertColorScale(ConditionalFormatting cf, String... colors) { + assertEquals(1, cf.getNumberOfRules()); + ConditionalFormattingRule cr = cf.getRule(0); + assertColorScale(cr, colors); + } + private void assertColorScale(ConditionalFormattingRule cr, String... colors) { + assertEquals(ConditionType.COLOR_SCALE, cr.getConditionTypeType()); + assertEquals(ComparisonOperator.NO_COMPARISON, cr.getComparisonOperation()); + assertEquals(null, cr.getFormula1()); + assertEquals(null, cr.getFormula2()); + + // TODO Implement +/* + ColorScaleFormatting color = cr.getColorScaleFormatting(); + assertNotNull(color); + assertNotNull(color.getColors()); + assertNotNull(color.getThresholds()); + assertEquals(colors.length, color.getNumControlPoints()); + assertEquals(colors.length, color.getColors().length); + assertEquals(colors.length, color.getThresholds().length); + + // Thresholds should be Min / (evenly spaced) / Max + int steps = 100 / (colors.length-1); + for (int i=0; i Date: Sun, 19 Jul 2015 04:39:17 +0000 Subject: [PATCH 4/7] Start decoding the CF color scales for HSSF git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691795 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/CFRule12Record.java | 13 +- .../record/cf/ColorGradientFormatting.java | 134 ++++++++++++++++++ 2 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java diff --git a/src/java/org/apache/poi/hssf/record/CFRule12Record.java b/src/java/org/apache/poi/hssf/record/CFRule12Record.java index c9d0f174b..3e72e5810 100644 --- a/src/java/org/apache/poi/hssf/record/CFRule12Record.java +++ b/src/java/org/apache/poi/hssf/record/CFRule12Record.java @@ -19,6 +19,7 @@ package org.apache.poi.hssf.record; import java.util.Arrays; +import org.apache.poi.hssf.record.cf.ColorGradientFormatting; import org.apache.poi.hssf.record.cf.IconMultiStateFormatting; import org.apache.poi.hssf.record.cf.Threshold; import org.apache.poi.hssf.record.common.FtrHeader; @@ -56,9 +57,9 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { private byte[] template_params; private IconMultiStateFormatting multistate; + private ColorGradientFormatting color_gradient; // TODO Parse these - private byte[] gradient_data; private byte[] databar_data; private byte[] filter_data; @@ -176,7 +177,7 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { byte type = getConditionType(); if (type == CONDITION_TYPE_COLOR_SCALE) { - gradient_data = in.readRemainder(); + color_gradient = new ColorGradientFormatting(in); } else if (type == CONDITION_TYPE_DATA_BAR) { databar_data = in.readRemainder(); } else if (type == CONDITION_TYPE_FILTER) { @@ -261,7 +262,7 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { byte type = getConditionType(); if (type == CONDITION_TYPE_COLOR_SCALE) { - out.write(gradient_data); + color_gradient.serialize(out); } else if (type == CONDITION_TYPE_DATA_BAR) { out.write(databar_data); } else if (type == CONDITION_TYPE_FILTER) { @@ -285,7 +286,7 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { byte type = getConditionType(); if (type == CONDITION_TYPE_COLOR_SCALE) { - len += gradient_data.length; + len += color_gradient.getDataLength(); } else if (type == CONDITION_TYPE_DATA_BAR) { len += databar_data.length; } else if (type == CONDITION_TYPE_FILTER) { @@ -319,9 +320,11 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { buffer.append(" .priority =").append(priority).append("\n"); buffer.append(" .template_type =").append(template_type).append("\n"); buffer.append(" .template_params=").append(HexDump.toHex(template_params)).append("\n"); - buffer.append(" .gradient_data =").append(HexDump.toHex(gradient_data)).append("\n"); buffer.append(" .databar_data =").append(HexDump.toHex(databar_data)).append("\n"); buffer.append(" .filter_data =").append(HexDump.toHex(filter_data)).append("\n"); + if (color_gradient != null) { + buffer.append(color_gradient); + } if (multistate != null) { buffer.append(multistate); } diff --git a/src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java b/src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java new file mode 100644 index 000000000..12b1f71d1 --- /dev/null +++ b/src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java @@ -0,0 +1,134 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.record.cf; + +import org.apache.poi.util.BitField; +import org.apache.poi.util.BitFieldFactory; +import org.apache.poi.util.LittleEndianInput; +import org.apache.poi.util.LittleEndianOutput; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; + +/** + * Color Gradient / Color Scale Conditional Formatting Rule Record. + * (Called Color Gradient in the file format docs, but more commonly + * Color Scale in the UI) + */ +public final class ColorGradientFormatting implements Cloneable { + private static POILogger log = POILogFactory.getLogger(ColorGradientFormatting.class); + + private byte options = 0; + private Threshold[] thresholds; + private byte[] colors; // TODO Decode + + private static BitField clamp = BitFieldFactory.getInstance(0x01); + private static BitField background = BitFieldFactory.getInstance(0x02); + + public ColorGradientFormatting() { + options = 3; + thresholds = new Threshold[3]; + } + public ColorGradientFormatting(LittleEndianInput in) { + in.readShort(); // Ignored + in.readByte(); // Reserved + int numI = in.readByte(); + int numG = in.readByte(); + if (numI != numG) { + log.log(POILogger.WARN, "Inconsistent Color Gradient defintion, found " + numI + " vs " + numG + " entries"); + } + options = in.readByte(); + + // TODO Are these correct? + thresholds = new Threshold[numI]; + for (int i=0; i Date: Sun, 19 Jul 2015 04:59:51 +0000 Subject: [PATCH 5/7] Further HSSF Color Gradient support git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691796 13f79535-47bb-0310-9956-ffa450edef68 --- .../poi/hssf/record/CFRule12Record.java | 15 ++++ .../record/cf/ColorGradientFormatting.java | 10 +++ .../usermodel/HSSFColorScaleFormatting.java | 75 +++++++++++++++++++ .../HSSFConditionalFormattingRule.java | 54 ++++++++++--- 4 files changed, 143 insertions(+), 11 deletions(-) create mode 100644 src/java/org/apache/poi/hssf/usermodel/HSSFColorScaleFormatting.java diff --git a/src/java/org/apache/poi/hssf/record/CFRule12Record.java b/src/java/org/apache/poi/hssf/record/CFRule12Record.java index 3e72e5810..e7974a092 100644 --- a/src/java/org/apache/poi/hssf/record/CFRule12Record.java +++ b/src/java/org/apache/poi/hssf/record/CFRule12Record.java @@ -202,6 +202,21 @@ public final class CFRule12Record extends CFRuleBase implements FutureRecord { return multistate; } + public boolean containsColorGradientBlock() { + return (color_gradient != null); + } + public ColorGradientFormatting getColorGradientFormatting() { + return color_gradient; + } + public ColorGradientFormatting createColorGradientFormatting() { + if (color_gradient != null) return color_gradient; + + // Convert, setup and return + setConditionType(CONDITION_TYPE_COLOR_SCALE); + color_gradient = new ColorGradientFormatting(); + return color_gradient; + } + /** * get the stack of the scale expression as a list * diff --git a/src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java b/src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java index 12b1f71d1..3b58af74e 100644 --- a/src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java +++ b/src/java/org/apache/poi/hssf/record/cf/ColorGradientFormatting.java @@ -64,6 +64,16 @@ public final class ColorGradientFormatting implements Cloneable { in.readFully(colors); } + public int getNumControlPoints() { + return thresholds.length; + } + public void setNumControlPoints(int num) { + if (num != thresholds.length) { + thresholds = new Threshold[num]; + // TODO Colors + } + } + public Threshold[] getThresholds() { return thresholds; } diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFColorScaleFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFColorScaleFormatting.java new file mode 100644 index 000000000..6e82072d4 --- /dev/null +++ b/src/java/org/apache/poi/hssf/usermodel/HSSFColorScaleFormatting.java @@ -0,0 +1,75 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ + +package org.apache.poi.hssf.usermodel; + +import org.apache.poi.hssf.record.CFRule12Record; +import org.apache.poi.hssf.record.cf.ColorGradientFormatting; +import org.apache.poi.hssf.record.cf.Threshold; +import org.apache.poi.ss.usermodel.Color; +import org.apache.poi.ss.usermodel.ConditionalFormattingThreshold; + +/** + * High level representation for Color Scale / Color Gradient + * Formatting component of Conditional Formatting settings + */ +public final class HSSFColorScaleFormatting implements org.apache.poi.ss.usermodel.ColorScaleFormatting { + private final HSSFSheet sheet; + private final CFRule12Record cfRule12Record; + private final ColorGradientFormatting colorFormatting; + + protected HSSFColorScaleFormatting(CFRule12Record cfRule12Record, HSSFSheet sheet) { + this.sheet = sheet; + this.cfRule12Record = cfRule12Record; + this.colorFormatting = this.cfRule12Record.getColorGradientFormatting(); + } + + public int getNumControlPoints() { + return colorFormatting.getNumControlPoints(); + } + public void setNumControlPoints(int num) { + colorFormatting.setNumControlPoints(num); + } + + public Color[] getColors() { + return null; // TODO + } + public void setColors(Color[] colors) { + // TODO + } + + public HSSFConditionalFormattingThreshold[] getThresholds() { + Threshold[] t = colorFormatting.getThresholds(); + HSSFConditionalFormattingThreshold[] ht = new HSSFConditionalFormattingThreshold[t.length]; + for (int i=0; inull otherwise */ public HSSFIconMultiStateFormatting getMultiStateFormatting() { return getMultiStateFormatting(false); } - /** * create a new icon / multi-state formatting object if it does not exist, * otherwise just return the existing object. @@ -209,6 +210,37 @@ public final class HSSFConditionalFormattingRule implements ConditionalFormattin return getMultiStateFormatting(true); } + private HSSFColorScaleFormatting getColorScaleFormatting(boolean create) { + CFRule12Record cfRule12Record = getCFRule12Record(create); + ColorGradientFormatting colorFormatting = cfRule12Record.getColorGradientFormatting(); + if (colorFormatting != null) + { + return new HSSFColorScaleFormatting(cfRule12Record, sheet); + } + else if( create ) + { + colorFormatting = cfRule12Record.createColorGradientFormatting(); + return new HSSFColorScaleFormatting(cfRule12Record, sheet); + } + else + { + return null; + } + } + /** + * @return color scale / gradient formatting object if defined, null otherwise + */ + public HSSFColorScaleFormatting getColorScaleFormatting() { + return getColorScaleFormatting(false); + } + /** + * create a new color scale / gradient formatting object if it does not exist, + * otherwise just return the existing object. + */ + public HSSFColorScaleFormatting createColorScaleFormatting() { + return getColorScaleFormatting(true); + } + /** * @return - the conditiontype for the cfrule */ From 8c1b7df4bc119a1b6e93ebb1db51e21585107f31 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 19 Jul 2015 12:11:19 +0000 Subject: [PATCH 6/7] Bug 57484: Allow processing of non-OOXML core namespace packages git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691821 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/TestAllFiles.java | 12 +-- .../poi/stress/POIXMLDocumentHandler.java | 37 ++------- .../apache/poi/stress/XDGFFileHandler.java | 77 +++++++++++++++++++ .../java/org/apache/poi/POIXMLDocument.java | 9 +++ .../org/apache/poi/POIXMLDocumentPart.java | 17 +++- .../org/apache/poi/TestPOIXMLDocument.java | 34 ++++++++ 6 files changed, 145 insertions(+), 41 deletions(-) create mode 100644 src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java diff --git a/src/integrationtest/org/apache/poi/TestAllFiles.java b/src/integrationtest/org/apache/poi/TestAllFiles.java index 85b058084..784b9baa2 100644 --- a/src/integrationtest/org/apache/poi/TestAllFiles.java +++ b/src/integrationtest/org/apache/poi/TestAllFiles.java @@ -103,12 +103,12 @@ public class TestAllFiles { HANDLERS.put(".vsd", new HDGFFileHandler()); // Visio - ooxml (currently unsupported) - HANDLERS.put(".vsdm", new NullFileHandler()); - HANDLERS.put(".vsdx", new NullFileHandler()); - HANDLERS.put(".vssm", new NullFileHandler()); - HANDLERS.put(".vssx", new NullFileHandler()); - HANDLERS.put(".vstm", new NullFileHandler()); - HANDLERS.put(".vstx", new NullFileHandler()); + HANDLERS.put(".vsdm", new XDGFFileHandler()); + HANDLERS.put(".vsdx", new XDGFFileHandler()); + HANDLERS.put(".vssm", new XDGFFileHandler()); + HANDLERS.put(".vssx", new XDGFFileHandler()); + HANDLERS.put(".vstm", new XDGFFileHandler()); + HANDLERS.put(".vstx", new XDGFFileHandler()); // POIFS HANDLERS.put(".ole2", new POIFSFileHandler()); diff --git a/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java b/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java index 7b0821dcc..1a8cbf6a0 100644 --- a/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java +++ b/src/integrationtest/org/apache/poi/stress/POIXMLDocumentHandler.java @@ -20,17 +20,10 @@ import static org.junit.Assert.assertNotNull; import java.io.IOException; import java.io.InputStream; -import java.util.List; import org.apache.poi.POIXMLDocument; -import org.apache.poi.openxml4j.exceptions.OpenXML4JException; -import org.apache.poi.openxml4j.opc.OPCPackage; -import org.apache.poi.openxml4j.opc.PackageAccess; -import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.poifs.crypt.Decryptor; import org.apache.poi.poifs.filesystem.POIFSFileSystem; -import org.junit.Ignore; -import org.junit.Test; public final class POIXMLDocumentHandler { protected void handlePOIXMLDocument(POIXMLDocument doc) throws Exception { @@ -44,33 +37,15 @@ public final class POIXMLDocumentHandler { protected static boolean isEncrypted(InputStream stream) throws IOException { if (POIFSFileSystem.hasPOIFSHeader(stream)) { POIFSFileSystem poifs = new POIFSFileSystem(stream); - if (poifs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { - return true; + try { + if (poifs.getRoot().hasEntry(Decryptor.DEFAULT_POIFS_ENTRY)) { + return true; + } + } finally { + poifs.close(); } throw new IOException("wrong file format or file extension for OO XML file"); } return false; } - - // a test-case to test this locally without executing the full TestAllFiles - @Ignore("POIXMLDocument cannot handle this Visio file currently...") - @Test - public void test() throws Exception { - OPCPackage pkg = OPCPackage.open("test-data/diagram/test.vsdx", PackageAccess.READ); - try { - handlePOIXMLDocument(new TestPOIXMLDocument(pkg)); - } finally { - pkg.close(); - } - } - - private final static class TestPOIXMLDocument extends POIXMLDocument { - public TestPOIXMLDocument(OPCPackage pkg) { - super(pkg); - } - - public List getAllEmbedds() throws OpenXML4JException { - return null; - } - } } diff --git a/src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java b/src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java new file mode 100644 index 000000000..3095cea12 --- /dev/null +++ b/src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java @@ -0,0 +1,77 @@ +/* ==================================================================== + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +==================================================================== */ +package org.apache.poi.stress; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.List; + +import org.apache.poi.POIXMLDocument; +import org.apache.poi.openxml4j.exceptions.OpenXML4JException; +import org.apache.poi.openxml4j.opc.OPCPackage; +import org.apache.poi.openxml4j.opc.PackageAccess; +import org.apache.poi.openxml4j.opc.PackagePart; +import org.apache.poi.util.PackageHelper; +import org.junit.Test; + +public class XDGFFileHandler extends AbstractFileHandler { + @Override + public void handleFile(InputStream stream) throws Exception { + // ignore password protected files + if (POIXMLDocumentHandler.isEncrypted(stream)) return; + + TestXDGFXMLDocument doc = new TestXDGFXMLDocument(stream); + new POIXMLDocumentHandler().handlePOIXMLDocument(doc); + } + + @Override + public void handleExtracting(File file) throws Exception { + // TODO: extraction/actual operations not supported yet + } + + // a test-case to test this locally without executing the full TestAllFiles + @Test + public void test() throws Exception { + OPCPackage pkg = OPCPackage.open("test-data/diagram/test.vsdx", PackageAccess.READ); + try { + TestXDGFXMLDocument doc = new TestXDGFXMLDocument(pkg); + new POIXMLDocumentHandler().handlePOIXMLDocument(doc); + } finally { + pkg.close(); + } + } + + // TODO: Get rid of this when full visio ooxml support is added + private final static class TestXDGFXMLDocument extends POIXMLDocument { + + public static String CORE_DOCUMENT = "http://schemas.microsoft.com/visio/2010/relationships/document"; + + public TestXDGFXMLDocument(OPCPackage pkg) { + super(pkg, CORE_DOCUMENT); + } + + public TestXDGFXMLDocument(InputStream is) throws IOException { + this(PackageHelper.open(is)); + } + + public List getAllEmbedds() throws OpenXML4JException { + return new ArrayList(); + } + } +} \ No newline at end of file diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocument.java b/src/ooxml/java/org/apache/poi/POIXMLDocument.java index 0e18ee112..0352d5c87 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocument.java +++ b/src/ooxml/java/org/apache/poi/POIXMLDocument.java @@ -57,6 +57,15 @@ public abstract class POIXMLDocument extends POIXMLDocumentPart implements Close protected POIXMLDocument(OPCPackage pkg) { super(pkg); + init(pkg); + } + + protected POIXMLDocument(OPCPackage pkg, String coreDocumentRel) { + super(pkg, coreDocumentRel); + init(pkg); + } + + private void init(OPCPackage pkg) { this.pkg = pkg; // Workaround for XMLBEANS-512 - ensure that when we parse diff --git a/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java b/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java index f051eb32a..857f0dad2 100644 --- a/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java +++ b/src/ooxml/java/org/apache/poi/POIXMLDocumentPart.java @@ -63,7 +63,7 @@ public class POIXMLDocumentPart { DEFAULT_XML_OPTIONS.setCharacterEncoding("UTF-8"); } - + private String coreDocumentRel = PackageRelationshipTypes.CORE_DOCUMENT; private PackagePart packagePart; private PackageRelationship packageRel; private POIXMLDocumentPart parent; @@ -93,7 +93,16 @@ public class POIXMLDocumentPart { * Construct POIXMLDocumentPart representing a "core document" package part. */ public POIXMLDocumentPart(OPCPackage pkg) { - PackageRelationship coreRel = pkg.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT).getRelationship(0); + this(pkg, PackageRelationshipTypes.CORE_DOCUMENT); + } + + /** + * Construct POIXMLDocumentPart representing a custom "core document" package part. + */ + public POIXMLDocumentPart(OPCPackage pkg, String coreDocumentRel) { + this.coreDocumentRel = coreDocumentRel; + PackageRelationship coreRel = pkg.getRelationshipsByType(this.coreDocumentRel).getRelationship(0); + if (coreRel == null) { coreRel = pkg.getRelationshipsByType(PackageRelationshipTypes.STRICT_CORE_DOCUMENT).getRelationship(0); if (coreRel != null) { @@ -151,10 +160,10 @@ public class POIXMLDocumentPart { */ protected final void rebase(OPCPackage pkg) throws InvalidFormatException { PackageRelationshipCollection cores = - packagePart.getRelationshipsByType(PackageRelationshipTypes.CORE_DOCUMENT); + packagePart.getRelationshipsByType(coreDocumentRel); if(cores.size() != 1) { throw new IllegalStateException( - "Tried to rebase using " + PackageRelationshipTypes.CORE_DOCUMENT + + "Tried to rebase using " + coreDocumentRel + " but found " + cores.size() + " parts of the right type" ); } diff --git a/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java b/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java index 55f4093fb..2c41b18f7 100644 --- a/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java +++ b/src/ooxml/testcases/org/apache/poi/TestPOIXMLDocument.java @@ -31,6 +31,7 @@ import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackagePart; import org.apache.poi.openxml4j.opc.PackageRelationship; +import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; import org.apache.poi.util.PackageHelper; import org.apache.poi.util.TempFile; @@ -44,6 +45,10 @@ public final class TestPOIXMLDocument extends TestCase { public OPCParser(OPCPackage pkg) { super(pkg); } + + public OPCParser(OPCPackage pkg, String coreDocumentRel) { + super(pkg, coreDocumentRel); + } @Override public List getAllEmbedds() { @@ -181,4 +186,33 @@ public final class TestPOIXMLDocument extends TestCase { part.onDocumentCreate(); //part.getTargetPart(null); } + + public void testVSDX() throws Exception { + OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx")); + + POIXMLDocument part = new OPCParser(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT); + + assertNotNull(part); + assertEquals(0, part.getRelationCounter()); + } + + public void testVSDXPart() throws Exception { + OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx")); + + POIXMLDocumentPart part = new POIXMLDocumentPart(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT); + + assertNotNull(part); + assertEquals(0, part.getRelationCounter()); + } + + public void testInvalidCoreRel() throws Exception { + OPCPackage open = PackageHelper.open(POIDataSamples.getDiagramInstance().openResourceAsStream("test.vsdx")); + + try { + new POIXMLDocumentPart(open, "somethingillegal"); + fail("Unknown core ref will throw exception"); + } catch (POIXMLException e) { + // expected here + } + } } From 907da7299689b588d1dd03f1e8c1428b5c22f204 Mon Sep 17 00:00:00 2001 From: Dominik Stadler Date: Sun, 19 Jul 2015 12:15:17 +0000 Subject: [PATCH 7/7] Use global constant, this and the previous checkin fixes pull request 17 on github git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1691822 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/poi/stress/XDGFFileHandler.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java b/src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java index 3095cea12..4c4fd6088 100644 --- a/src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java +++ b/src/integrationtest/org/apache/poi/stress/XDGFFileHandler.java @@ -27,6 +27,7 @@ import org.apache.poi.openxml4j.exceptions.OpenXML4JException; import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.openxml4j.opc.PackageAccess; import org.apache.poi.openxml4j.opc.PackagePart; +import org.apache.poi.openxml4j.opc.PackageRelationshipTypes; import org.apache.poi.util.PackageHelper; import org.junit.Test; @@ -59,11 +60,8 @@ public class XDGFFileHandler extends AbstractFileHandler { // TODO: Get rid of this when full visio ooxml support is added private final static class TestXDGFXMLDocument extends POIXMLDocument { - - public static String CORE_DOCUMENT = "http://schemas.microsoft.com/visio/2010/relationships/document"; - public TestXDGFXMLDocument(OPCPackage pkg) { - super(pkg, CORE_DOCUMENT); + super(pkg, PackageRelationshipTypes.VISIO_CORE_DOCUMENT); } public TestXDGFXMLDocument(InputStream is) throws IOException {