diff --git a/src/documentation/content/xdocs/changes.xml b/src/documentation/content/xdocs/changes.xml
index ab9a4c796..5722a7be4 100644
--- a/src/documentation/content/xdocs/changes.xml
+++ b/src/documentation/content/xdocs/changes.xml
@@ -26,6 +26,7 @@
* Primarily used by test cases when testing for specific parsing exceptions.
null
if formula was null.
+ */
+ private static Ptg[] parseFormula(String formula, Workbook workbook)
+ {
+ if(formula == null) {
+ return null;
+ }
+ return FormulaParser.parse(formula, workbook);
+ }
+
+ // TODO - treat formulas as token arrays instead of Stacks throughout the rest of POI
+ private static Stack convertToTokenStack(Ptg[] ptgs)
+ {
+ Stack parsedExpression = new Stack();
+ // fill the Ptg Stack with Ptgs of new formula
+ for (int k = 0; k < ptgs.length; k++)
+ {
+ parsedExpression.push(ptgs[ k ]);
+ }
+ return parsedExpression;
+ }
+ private static Ptg[] toArray(Stack ptgs) {
+ Ptg[] result = new Ptg[ptgs.size()];
+ ptgs.toArray(result);
+ return result;
+ }
}
diff --git a/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java b/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java
index 99890f805..47be0497d 100755
--- a/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java
+++ b/src/java/org/apache/poi/hssf/record/ExternalNameRecord.java
@@ -37,7 +37,7 @@ public final class ExternalNameRecord extends Record {
private static final int OPT_PICTURE_LINK = 0x0004;
private static final int OPT_STD_DOCUMENT_NAME = 0x0008;
private static final int OPT_OLE_LINK = 0x0010;
-// private static final int OPT_CLIP_FORMAT_MASK = 0x7FE0;
+// private static final int OPT_CLIP_FORMAT_MASK = 0x7FE0;
private static final int OPT_ICONIFIED_PICTURE_LINK= 0x8000;
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 d6e81c7ea..81154ebb2 100644
--- a/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java
+++ b/src/java/org/apache/poi/hssf/record/aggregates/CFRecordsAggregate.java
@@ -24,6 +24,7 @@ import org.apache.poi.hssf.record.CFHeaderRecord;
import org.apache.poi.hssf.record.CFRuleRecord;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.RecordInputStream;
+import org.apache.poi.hssf.util.Region;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
@@ -37,19 +38,38 @@ import org.apache.poi.util.POILogger;
*/
public final class CFRecordsAggregate extends Record
{
+ /** Excel allows up to 3 conditional formating rules */
+ private static final int MAX_CONDTIONAL_FORMAT_RULES = 3;
+
public final static short sid = -2008; // not a real BIFF record
- private static POILogger log = POILogFactory.getLogger(CFRecordsAggregate.class);
+ private static POILogger log = POILogFactory.getLogger(CFRecordsAggregate.class);
- private CFHeaderRecord header;
+ private final CFHeaderRecord header;
- // List of CFRuleRecord objects
+ /** List of CFRuleRecord objects */
private final List rules;
- public CFRecordsAggregate()
- {
- header = null;
- rules = new ArrayList(3);
+ private CFRecordsAggregate(CFHeaderRecord pHeader, CFRuleRecord[] 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_CONDTIONAL_FORMAT_RULES) {
+ throw new IllegalArgumentException("No more than "
+ + MAX_CONDTIONAL_FORMAT_RULES + " rules may be specified");
+ }
+ header = pHeader;
+ rules = new ArrayList(3);
+ for (int i = 0; i < pRules.length; i++) {
+ rules.add(pRules[i]);
+ }
+ }
+
+ public CFRecordsAggregate(Region[] regions, CFRuleRecord[] rules) {
+ this(new CFHeaderRecord(regions), rules);
}
/**
@@ -60,42 +80,46 @@ public final class CFRecordsAggregate extends Record
*/
public static CFRecordsAggregate createCFAggregate(List recs, int pOffset)
{
-
- int offset = pOffset;
- CFRecordsAggregate cfRecords = new CFRecordsAggregate();
- ArrayList records = new ArrayList(4);
-
- Record rec = ( Record ) recs.get(offset++);
-
- if (rec.getSid() == CFHeaderRecord.sid)
- {
- records.add(rec);
- cfRecords.header = (CFHeaderRecord)rec;
-
- int nRules = cfRecords.header.getNumberOfConditionalFormats();
- int rulesCount = 0;
- while( offsetnull
.
*/
public CFHeaderRecord getHeader()
{
return header;
}
-
- /**
- * @return the rules
- */
- public List getRules()
- {
- return rules;
+
+ private void checkRuleIndex(int idx) {
+ if(idx < 0 || idx >= rules.size()) {
+ throw new IllegalArgumentException("Bad rule record index (" + idx
+ + ") nRules=" + rules.size());
+ }
+ }
+ public CFRuleRecord getRule(int idx) {
+ checkRuleIndex(idx);
+ return (CFRuleRecord) rules.get(idx);
+ }
+ public void setRule(int idx, CFRuleRecord r) {
+ checkRuleIndex(idx);
+ rules.set(idx, r);
+ }
+ public void addRule(CFRuleRecord r) {
+ if(rules.size() >= MAX_CONDTIONAL_FORMAT_RULES) {
+ throw new IllegalStateException("Cannot have more than "
+ + MAX_CONDTIONAL_FORMAT_RULES + " conditional format rules");
+ }
+ rules.add(r);
+ header.setNumberOfConditionalFormats(rules.size());
+ }
+ public int getNumberOfRules() {
+ return rules.size();
}
/**
diff --git a/src/java/org/apache/poi/hssf/record/cf/CellRange.java b/src/java/org/apache/poi/hssf/record/cf/CellRange.java
index 88ec15b4a..16093ee58 100644
--- a/src/java/org/apache/poi/hssf/record/cf/CellRange.java
+++ b/src/java/org/apache/poi/hssf/record/cf/CellRange.java
@@ -17,79 +17,127 @@
package org.apache.poi.hssf.record.cf;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.poi.hssf.util.Region;
+
/**
- * CellRange.java
- * Created on January 22, 2008, 10:05 PM
*
* @author Dmitriy Kumshayev
*/
-
-public class CellRange
+public final class CellRange
{
- private int field_1_first_row;
- private int field_2_last_row;
- private short field_3_first_column;
- private short field_4_last_column;
-
- public CellRange(int firstRow, int lastRow, short firstColumn, short lastColumn)
- {
- this.field_1_first_row = firstRow;
- this.field_2_last_row = lastRow;
- this.field_3_first_column = firstColumn;
- this.field_4_last_column = lastColumn;
- validateRegion();
+ /**
+ * max index for both row and column
+ *
+ * Note - this value converts to -1 when cast to a short
+ */
+ private static final int MAX_INDEX = Integer.MAX_VALUE;
+
+ private static final Region[] EMPTY_REGION_ARRAY = { };
+
+ private int _firstRow;
+ private int _lastRow;
+ private int _firstColumn;
+ private int _lastColumn;
+
+ /**
+ *
+ * @param firstRow
+ * @param lastRow pass -1 for full column ranges
+ * @param firstColumn
+ * @param lastColumn pass -1 for full row ranges
+ */
+ public CellRange(int firstRow, int lastRow, int firstColumn, int lastColumn)
+ {
+ if(!isValid(firstRow, lastRow, firstColumn, lastColumn)) {
+ throw new IllegalArgumentException("invalid cell range (" + firstRow + ", " + lastRow
+ + ", " + firstColumn + ", " + lastColumn + ")");
+ }
+ _firstRow = firstRow;
+ _lastRow = convertM1ToMax(lastRow);
+ _firstColumn = firstColumn;
+ _lastColumn = convertM1ToMax(lastColumn);
}
-
- private void validateRegion()
- {
- if( field_1_first_row < 0 ||
- field_2_last_row < -1 ||
- field_3_first_column < 0 ||
- field_4_last_column < -1 ||
- field_2_last_row>=0 && field_2_last_row
* The name matching is case insensitive.
- * @return true
if the name specifies a standard worksheet function,
+ * @return true
if the name specifies a standard worksheet function,
* false
if the name should be assumed to be an external function.
*/
public static final boolean isInternalFunctionName(String name) {
short ix = FunctionMetadataRegistry.lookupIndexByName(name.toUpperCase());
return ix >= 0;
}
-
+
protected String lookupName(short index) {
if(index == FunctionMetadataRegistry.FUNCTION_INDEX_EXTERNAL) {
return "#external#";
@@ -127,7 +127,7 @@ public abstract class AbstractFunctionPtg extends OperationPtg {
}
return fm.getName();
}
-
+
/**
* Resolves internal function names into function indexes.
*
@@ -145,7 +145,7 @@ public abstract class AbstractFunctionPtg extends OperationPtg {
public byte getDefaultOperandClass() {
return returnClass;
}
-
+
public byte getParameterClass(int index) {
try {
return paramClass[index];
diff --git a/src/java/org/apache/poi/hssf/record/formula/FuncPtg.java b/src/java/org/apache/poi/hssf/record/formula/FuncPtg.java
index a94355a0f..69edf88aa 100644
--- a/src/java/org/apache/poi/hssf/record/formula/FuncPtg.java
+++ b/src/java/org/apache/poi/hssf/record/formula/FuncPtg.java
@@ -27,11 +27,11 @@ import org.apache.poi.hssf.record.formula.function.FunctionMetadataRegistry;
* @author Danny Mui (dmui at apache dot org) (Leftover handling)
*/
public final class FuncPtg extends AbstractFunctionPtg {
-
+
public final static byte sid = 0x21;
public final static int SIZE = 3;
private int numParams=0;
-
+
/**
* FuncPtgs are defined to be 4 bytes but the actual FuncPtg uses only 2 bytes.
* If we have leftOvers that are read from the file we should serialize them back out.
@@ -39,18 +39,18 @@ public final class FuncPtg extends AbstractFunctionPtg {
* If the leftovers are removed, a prompt "Warning: Data may have been lost occurs in Excel"
*/
//protected byte[] leftOvers = null;
-
+
private FuncPtg() {
- //Required for clone methods
+ //Required for clone methods
}
- /**Creates new function pointer from a byte array
- * usually called while reading an excel file.
+ /**Creates new function pointer from a byte array
+ * usually called while reading an excel file.
*/
public FuncPtg(RecordInputStream in) {
//field_1_num_args = data[ offset + 0 ];
field_2_fnc_index = in.readShort();
-
+
FunctionMetadata fm = FunctionMetadataRegistry.getFunctionByIndex(field_2_fnc_index);
if(fm == null) {
throw new RuntimeException("Invalid built-in function index (" + field_2_fnc_index + ")");
@@ -62,12 +62,12 @@ public final class FuncPtg extends AbstractFunctionPtg {
numParams = numberOfParameters;
paramClass = new byte[] { Ptg.CLASS_VALUE, }; // TODO
}
-
+
public void writeBytes(byte[] array, int offset) {
array[offset+0]= (byte) (sid + ptgClass);
LittleEndian.putShort(array,offset+1,field_2_fnc_index);
}
-
+
public int getNumberOfOperands() {
return numParams;
}
@@ -79,11 +79,11 @@ public final class FuncPtg extends AbstractFunctionPtg {
ptg.setClass(ptgClass);
return ptg;
}
-
+
public int getSize() {
return SIZE;
}
-
+
public String toString() {
StringBuffer sb = new StringBuffer(64);
sb.append(getClass().getName()).append(" [");
diff --git a/src/java/org/apache/poi/hssf/record/formula/FuncVarPtg.java b/src/java/org/apache/poi/hssf/record/formula/FuncVarPtg.java
index b4732c728..fb6527139 100644
--- a/src/java/org/apache/poi/hssf/record/formula/FuncVarPtg.java
+++ b/src/java/org/apache/poi/hssf/record/formula/FuncVarPtg.java
@@ -15,7 +15,6 @@
limitations under the License.
==================================================================== */
-
package org.apache.poi.hssf.record.formula;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.hssf.record.RecordInputStream;
@@ -27,22 +26,22 @@ import org.apache.poi.hssf.record.formula.function.FunctionMetadataRegistry;
* @author Jason Height (jheight at chariot dot net dot au)
*/
public final class FuncVarPtg extends AbstractFunctionPtg{
-
+
public final static byte sid = 0x22;
- private final static int SIZE = 4;
-
+ private final static int SIZE = 4;
+
private FuncVarPtg() {
//Required for clone methods
}
- /**Creates new function pointer from a byte array
- * usually called while reading an excel file.
+ /**Creates new function pointer from a byte array
+ * usually called while reading an excel file.
*/
public FuncVarPtg(RecordInputStream in) {
field_1_num_args = in.readByte();
field_2_fnc_index = in.readShort();
}
-
+
/**
* Create a function ptg from a string tokenised by the parser
*/
@@ -59,17 +58,17 @@ public final class FuncVarPtg extends AbstractFunctionPtg{
paramClass = new byte[] {Ptg.CLASS_VALUE};
}
}
-
+
public void writeBytes(byte[] array, int offset) {
array[offset+0]=(byte) (sid + ptgClass);
array[offset+1]=field_1_num_args;
LittleEndian.putShort(array,offset+2,field_2_fnc_index);
}
-
+
public int getNumberOfOperands() {
return field_1_num_args;
}
-
+
public Object clone() {
FuncVarPtg ptg = new FuncVarPtg();
ptg.field_1_num_args = field_1_num_args;
@@ -77,11 +76,11 @@ public final class FuncVarPtg extends AbstractFunctionPtg{
ptg.setClass(ptgClass);
return ptg;
}
-
+
public int getSize() {
return SIZE;
}
-
+
public String toString() {
StringBuffer sb = new StringBuffer(64);
sb.append(getClass().getName()).append(" [");
diff --git a/src/java/org/apache/poi/hssf/record/formula/function/FunctionDataBuilder.java b/src/java/org/apache/poi/hssf/record/formula/function/FunctionDataBuilder.java
index b304ec3d4..2009ebae1 100644
--- a/src/java/org/apache/poi/hssf/record/formula/function/FunctionDataBuilder.java
+++ b/src/java/org/apache/poi/hssf/record/formula/function/FunctionDataBuilder.java
@@ -23,7 +23,7 @@ import java.util.Map;
import java.util.Set;
/**
- * Temporarily collects FunctionMetadata instances for creation of a
+ * Temporarily collects FunctionMetadata instances for creation of a
* FunctionMetadataRegistry.
*
* @author Josh Micich
diff --git a/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadata.java b/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadata.java
index 9df2db93c..94f1659b5 100644
--- a/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadata.java
+++ b/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadata.java
@@ -17,6 +17,7 @@
package org.apache.poi.hssf.record.formula.function;
/**
+ * Holds information about Excel built-in functions.
*
* @author Josh Micich
*/
@@ -46,7 +47,7 @@ public final class FunctionMetadata {
return _maxParams;
}
public boolean hasFixedArgsLength() {
- return _minParams == _maxParams;
+ return _minParams == _maxParams;
}
public String toString() {
StringBuffer sb = new StringBuffer(64);
diff --git a/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadataReader.java b/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadataReader.java
index bd50bf04d..bd72a9223 100644
--- a/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadataReader.java
+++ b/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadataReader.java
@@ -46,7 +46,7 @@ final class FunctionMetadataReader {
public static FunctionMetadataRegistry createRegistry() {
InputStream is = FunctionMetadataReader.class.getResourceAsStream(METADATA_FILE_NAME);
- if(is == null) {
+ if (is == null) {
throw new RuntimeException("resource '" + METADATA_FILE_NAME + "' not found");
}
diff --git a/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadataRegistry.java b/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadataRegistry.java
index 0cc8de37d..fe243bf01 100644
--- a/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadataRegistry.java
+++ b/src/java/org/apache/poi/hssf/record/formula/function/FunctionMetadataRegistry.java
@@ -19,7 +19,11 @@ package org.apache.poi.hssf.record.formula.function;
import java.util.Map;
import java.util.Set;
-
+/**
+ * Allows clients to get FunctionMetadata instances for any built-in function of Excel.
+ *
+ * @author Josh Micich
+ */
public final class FunctionMetadataRegistry {
/**
* The name of the IF function (i.e. "IF"). Extracted as a constant for clarity.
@@ -35,7 +39,6 @@ public final class FunctionMetadataRegistry {
private static FunctionMetadataRegistry getInstance() {
if (_instance == null) {
_instance = FunctionMetadataReader.createRegistry();
-// _instance = POIFunctionMetadataCreator.createInstance();
}
return _instance;
}
diff --git a/src/java/org/apache/poi/hssf/record/formula/functions/Pmt.java b/src/java/org/apache/poi/hssf/record/formula/functions/Pmt.java
index 58628053c..68a8d43dc 100644
--- a/src/java/org/apache/poi/hssf/record/formula/functions/Pmt.java
+++ b/src/java/org/apache/poi/hssf/record/formula/functions/Pmt.java
@@ -46,15 +46,15 @@ public final class Pmt extends FinanceFunction {
if(args.length < 3 || args.length > 5) {
return ErrorEval.VALUE_INVALID;
}
-
- try {
- // evaluate first three (always present) args
+
+ try {
+ // evaluate first three (always present) args
double rate = evalArg(args[0], srcRow, srcCol);
double nper = evalArg(args[1], srcRow, srcCol);
- double pv = evalArg(args[2], srcRow, srcCol);
+ double pv = evalArg(args[2], srcRow, srcCol);
double fv = 0;
boolean arePaymentsAtPeriodBeginning = false;
-
+
switch (args.length) {
case 5:
ValueEval ve = singleOperandNumericAsBoolean(args[4], srcRow, srcCol);
@@ -67,10 +67,10 @@ public final class Pmt extends FinanceFunction {
}
double d = FinanceLib.pmt(rate, nper, pv, fv, arePaymentsAtPeriodBeginning);
if (Double.isNaN(d)) {
- return (ValueEval) ErrorEval.VALUE_INVALID;
+ return ErrorEval.VALUE_INVALID;
}
if (Double.isInfinite(d)) {
- return (ValueEval) ErrorEval.NUM_ERROR;
+ return ErrorEval.NUM_ERROR;
}
return new NumberEval(d);
} catch (EvaluationException e) {
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFBorderFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFBorderFormatting.java
index 622f3a674..408c774b6 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFBorderFormatting.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFBorderFormatting.java
@@ -26,94 +26,39 @@ import org.apache.poi.hssf.record.cf.BorderFormatting;
* @author Dmitriy Kumshayev
*
*/
-public class HSSFBorderFormatting
+public final class HSSFBorderFormatting
{
- /**
- * No border
- */
+ /** No border */
+ public final static short BORDER_NONE = BorderFormatting.BORDER_NONE;
+ /** Thin border */
+ public final static short BORDER_THIN = BorderFormatting.BORDER_THIN;
+ /** Medium border */
+ public final static short BORDER_MEDIUM = BorderFormatting.BORDER_MEDIUM;
+ /** dash border */
+ public final static short BORDER_DASHED = BorderFormatting.BORDER_DASHED;
+ /** dot border */
+ public final static short BORDER_HAIR = BorderFormatting.BORDER_HAIR;
+ /** Thick border */
+ public final static short BORDER_THICK = BorderFormatting.BORDER_THICK;
+ /** double-line border */
+ public final static short BORDER_DOUBLE = BorderFormatting.BORDER_DOUBLE;
+ /** hair-line border */
+ public final static short BORDER_DOTTED = BorderFormatting.BORDER_DOTTED;
+ /** Medium dashed border */
+ public final static short BORDER_MEDIUM_DASHED = BorderFormatting.BORDER_MEDIUM_DASHED;
+ /** dash-dot border */
+ public final static short BORDER_DASH_DOT = BorderFormatting.BORDER_DASH_DOT;
+ /** medium dash-dot border */
+ public final static short BORDER_MEDIUM_DASH_DOT = BorderFormatting.BORDER_MEDIUM_DASH_DOT;
+ /** dash-dot-dot border */
+ public final static short BORDER_DASH_DOT_DOT = BorderFormatting.BORDER_DASH_DOT_DOT;
+ /** medium dash-dot-dot border */
+ public final static short BORDER_MEDIUM_DASH_DOT_DOT = BorderFormatting.BORDER_MEDIUM_DASH_DOT_DOT;
+ /** slanted dash-dot border */
+ public final static short BORDER_SLANTED_DASH_DOT = BorderFormatting.BORDER_SLANTED_DASH_DOT;
- public final static short BORDER_NONE = BorderFormatting.BORDER_NONE;
-
- /**
- * Thin border
- */
-
- public final static short BORDER_THIN = BorderFormatting.BORDER_THIN;
-
- /**
- * Medium border
- */
-
- public final static short BORDER_MEDIUM = BorderFormatting.BORDER_MEDIUM;
-
- /**
- * dash border
- */
-
- public final static short BORDER_DASHED = BorderFormatting.BORDER_DASHED;
-
- /**
- * dot border
- */
-
- public final static short BORDER_HAIR = BorderFormatting.BORDER_HAIR;
-
- /**
- * Thick border
- */
-
- public final static short BORDER_THICK = BorderFormatting.BORDER_THICK;
-
- /**
- * double-line border
- */
-
- public final static short BORDER_DOUBLE = BorderFormatting.BORDER_DOUBLE;
-
- /**
- * hair-line border
- */
-
- public final static short BORDER_DOTTED = BorderFormatting.BORDER_DOTTED;
-
- /**
- * Medium dashed border
- */
-
- public final static short BORDER_MEDIUM_DASHED = BorderFormatting.BORDER_MEDIUM_DASHED;
-
- /**
- * dash-dot border
- */
-
- public final static short BORDER_DASH_DOT = BorderFormatting.BORDER_DASH_DOT;
-
- /**
- * medium dash-dot border
- */
-
- public final static short BORDER_MEDIUM_DASH_DOT = BorderFormatting.BORDER_MEDIUM_DASH_DOT;
-
- /**
- * dash-dot-dot border
- */
-
- public final static short BORDER_DASH_DOT_DOT = BorderFormatting.BORDER_DASH_DOT_DOT;
-
- /**
- * medium dash-dot-dot border
- */
-
- public final static short BORDER_MEDIUM_DASH_DOT_DOT = BorderFormatting.BORDER_MEDIUM_DASH_DOT_DOT;
-
- /**
- * slanted dash-dot border
- */
-
- public final static short BORDER_SLANTED_DASH_DOT = BorderFormatting.BORDER_SLANTED_DASH_DOT;
-
-
- private BorderFormatting borderFormatting;
+
+ private final BorderFormatting borderFormatting;
public HSSFBorderFormatting()
{
@@ -124,5 +69,124 @@ public class HSSFBorderFormatting
{
return borderFormatting;
}
-
+
+ public short getBorderBottom()
+ {
+ return borderFormatting.getBorderBottom();
+ }
+
+ public short getBorderDiagonal()
+ {
+ return borderFormatting.getBorderDiagonal();
+ }
+
+ public short getBorderLeft()
+ {
+ return borderFormatting.getBorderLeft();
+ }
+
+ public short getBorderRight()
+ {
+ return borderFormatting.getBorderRight();
+ }
+
+ public short getBorderTop()
+ {
+ return borderFormatting.getBorderTop();
+ }
+
+ public short getBottomBorderColor()
+ {
+ return borderFormatting.getBottomBorderColor();
+ }
+
+ public short getDiagonalBorderColor()
+ {
+ return borderFormatting.getDiagonalBorderColor();
+ }
+
+ public short getLeftBorderColor()
+ {
+ return borderFormatting.getLeftBorderColor();
+ }
+
+ public short getRightBorderColor()
+ {
+ return borderFormatting.getRightBorderColor();
+ }
+
+ public short getTopBorderColor()
+ {
+ return borderFormatting.getTopBorderColor();
+ }
+
+ public boolean isBackwardDiagonalOn()
+ {
+ return borderFormatting.isBackwardDiagonalOn();
+ }
+
+ public boolean isForwardDiagonalOn()
+ {
+ return borderFormatting.isForwardDiagonalOn();
+ }
+
+ public void setBackwardDiagonalOn(boolean on)
+ {
+ borderFormatting.setBackwardDiagonalOn(on);
+ }
+
+ public void setBorderBottom(short border)
+ {
+ borderFormatting.setBorderBottom(border);
+ }
+
+ public void setBorderDiagonal(short border)
+ {
+ borderFormatting.setBorderDiagonal(border);
+ }
+
+ public void setBorderLeft(short border)
+ {
+ borderFormatting.setBorderLeft(border);
+ }
+
+ public void setBorderRight(short border)
+ {
+ borderFormatting.setBorderRight(border);
+ }
+
+ public void setBorderTop(short border)
+ {
+ borderFormatting.setBorderTop(border);
+ }
+
+ public void setBottomBorderColor(short color)
+ {
+ borderFormatting.setBottomBorderColor(color);
+ }
+
+ public void setDiagonalBorderColor(short color)
+ {
+ borderFormatting.setDiagonalBorderColor(color);
+ }
+
+ public void setForwardDiagonalOn(boolean on)
+ {
+ borderFormatting.setForwardDiagonalOn(on);
+ }
+
+ public void setLeftBorderColor(short color)
+ {
+ borderFormatting.setLeftBorderColor(color);
+ }
+
+ public void setRightBorderColor(short color)
+ {
+ borderFormatting.setRightBorderColor(color);
+ }
+
+ public void setTopBorderColor(short color)
+ {
+ borderFormatting.setTopBorderColor(color);
+ }
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java
index c7018a0e1..d9e470a68 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFConditionalFormatting.java
@@ -16,9 +16,7 @@
==================================================================== */
package org.apache.poi.hssf.usermodel;
-import java.util.ArrayList;
-import java.util.List;
-
+import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.CFHeaderRecord;
import org.apache.poi.hssf.record.CFRuleRecord;
import org.apache.poi.hssf.record.aggregates.CFRecordsAggregate;
@@ -86,13 +84,9 @@ import org.apache.poi.hssf.util.Region;
*/
public final class HSSFConditionalFormatting
{
- private final HSSFSheet sheet;
+ private final Workbook workbook;
private final CFRecordsAggregate cfAggregate;
- HSSFConditionalFormatting(HSSFSheet sheet) {
- this(sheet, new CFRecordsAggregate());
- }
-
HSSFConditionalFormatting(HSSFSheet sheet, CFRecordsAggregate cfAggregate)
{
if(sheet == null) {
@@ -101,36 +95,25 @@ public final class HSSFConditionalFormatting
if(cfAggregate == null) {
throw new IllegalArgumentException("cfAggregate must not be null");
}
- this.sheet = sheet;
+ workbook = sheet.workbook.getWorkbook();
this.cfAggregate = cfAggregate;
}
CFRecordsAggregate getCFRecordsAggregate() {
return cfAggregate;
}
- public void setFormattingRegions(Region[] regions)
- {
- if( regions != null)
- {
- CFHeaderRecord header = cfAggregate.getHeader();
- header.setCellRanges(mergeCellRanges(toCellRangeList(regions)));
- }
- }
-
/**
- * @return array of Regions. never null
+ * @return array of Regions. never null
*/
public Region[] getFormattingRegions()
{
CFHeaderRecord cfh = cfAggregate.getHeader();
-
- List cellRanges = cfh.getCellRanges();
-
- return toRegionArray(cellRanges);
+ CellRange[] cellRanges = cfh.getCellRanges();
+ return CellRange.convertCellRangesToRegions(cellRanges);
}
/**
- * set a Conditional Formatting rule at position idx.
+ * Replaces an existing Conditional Formatting rule at position idx.
* Excel allows to create up to 3 Conditional Formatting rules.
* This method can be useful to modify existing Conditional Formatting rules.
*
@@ -139,11 +122,7 @@ public final class HSSFConditionalFormatting
*/
public void setRule(int idx, HSSFConditionalFormattingRule cfRule)
{
- if (idx < 0 || idx > 2) {
- throw new IllegalArgumentException("idx must be between 0 and 2 but was ("
- + idx + ")");
- }
- cfAggregate.getRules().set(idx, cfRule);
+ cfAggregate.setRule(idx, cfRule.getCfRuleRecord());
}
/**
@@ -153,136 +132,24 @@ public final class HSSFConditionalFormatting
*/
public void addRule(HSSFConditionalFormattingRule cfRule)
{
- cfAggregate.getRules().add(cfRule);
+ cfAggregate.addRule(cfRule.getCfRuleRecord());
}
/**
- * get a Conditional Formatting rule at position idx.
- * @param idx
- * @return a Conditional Formatting rule at position idx.
+ * @return the Conditional Formatting rule at position idx.
*/
public HSSFConditionalFormattingRule getRule(int idx)
{
- CFRuleRecord ruleRecord = (CFRuleRecord)cfAggregate.getRules().get(idx);
- return new HSSFConditionalFormattingRule(sheet.workbook, ruleRecord);
+ CFRuleRecord ruleRecord = cfAggregate.getRule(idx);
+ return new HSSFConditionalFormattingRule(workbook, ruleRecord);
}
/**
* @return number of Conditional Formatting rules.
*/
- public int getNumbOfRules()
+ public int getNumberOfRules()
{
- return cfAggregate.getRules().size();
- }
-
-
- /**
- * Do all possible cell merges between cells of the list so that:
- *
null
to signify 'border unchanged'
*/
- public void setBorderFormattingUnchanged()
+ public void setBorderFormatting(HSSFBorderFormatting borderFmt)
{
- cfRuleRecord.setBorderFormattingUnchanged();
+ BorderFormatting block = borderFmt==null ? null : borderFmt.getBorderFormattingBlock();
+ cfRuleRecord.setBorderFormatting(block);
}
- /**
- * Keep Pattern Formatting unchanged for this Conditional Formatting Rule
+ /**
+ * @param patternFmt pass null
to signify 'pattern unchanged'
*/
- public void setPatternFormattingUnchanged()
+ public void setPatternFormatting(HSSFPatternFormatting patternFmt)
{
- cfRuleRecord.setPatternFormattingUnchanged();
- }
-
- public void setFontFormatting(HSSFFontFormatting fontFormatting)
- {
- if( fontFormatting!=null )
- {
- cfRuleRecord.setFontFormatting(fontFormatting.getFontFormattingBlock());
- }
- else
- {
- setFontFormattingUnchanged();
- }
- }
- public void setBorderFormatting(HSSFBorderFormatting borderFormatting)
- {
- if( borderFormatting != null )
- {
- cfRuleRecord.setBorderFormatting(borderFormatting.getBorderFormattingBlock());
- }
- else
- {
- setBorderFormattingUnchanged();
- }
- }
- public void setPatternFormatting(HSSFPatternFormatting patternFormatting)
- {
- if( patternFormatting != null)
- {
- cfRuleRecord.setPatternFormatting(patternFormatting.getPatternFormattingBlock());
- }
- else
- {
- setPatternFormattingUnchanged();
- }
- }
-
- public void setCellComparisonCondition(byte comparisonOperation, String formula1, String formula2)
- {
- cfRuleRecord.setConditionType(CELL_COMPARISON);
- cfRuleRecord.setComparisonOperation(comparisonOperation);
-
- // Formula 1
- setFormula1(formula1);
-
- // Formula 2
- setFormula1(formula2);
- }
-
- public void setFormulaCondition(String formula)
- {
- cfRuleRecord.setConditionType(FORMULA);
- // Formula 1
- setFormula1(formula);
- }
-
- public void setFormula1(String formula)
- {
- // Formula 1
- if( formula != null)
- {
- Stack parsedExpression = parseFormula(formula);
- if( parsedExpression != null )
- {
- cfRuleRecord.setParsedExpression1(parsedExpression);
- }
- else
- {
- cfRuleRecord.setParsedExpression1(null);
- }
- }
- else
- {
- cfRuleRecord.setParsedExpression1(null);
- }
- }
-
- public void setFormula2(String formula)
- {
- // Formula 2
- if( formula != null)
- {
- Stack parsedExpression = parseFormula(formula);
- if( parsedExpression != null )
- {
- cfRuleRecord.setParsedExpression2(parsedExpression);
- }
- else
- {
- cfRuleRecord.setParsedExpression2(null);
- }
- }
- else
- {
- cfRuleRecord.setParsedExpression2(null);
- }
+ PatternFormatting block = patternFmt==null ? null : patternFmt.getPatternFormattingBlock();
+ cfRuleRecord.setPatternFormatting(block);
}
public String getFormula1()
{
- return toFormulaString(cfRuleRecord.getParsedExpression1());
+ return toFormulaString(cfRuleRecord.getParsedExpression1());
}
public String getFormula2()
{
byte conditionType = cfRuleRecord.getConditionType();
- switch(conditionType)
- {
- case CELL_COMPARISON:
+ if (conditionType == CELL_COMPARISON) {
+ byte comparisonOperation = cfRuleRecord.getComparisonOperation();
+ switch(comparisonOperation)
{
- byte comparisonOperation = cfRuleRecord.getComparisonOperation();
- switch(comparisonOperation)
- {
- case COMPARISON_OPERATOR_BETWEEN:
- case COMPARISON_OPERATOR_NOT_BETWEEN:
- return toFormulaString(cfRuleRecord.getParsedExpression2());
- }
+ case ComparisonOperator.BETWEEN:
+ case ComparisonOperator.NOT_BETWEEN:
+ return toFormulaString(cfRuleRecord.getParsedExpression2());
}
}
return null;
}
- private String toFormulaString(List parsedExpression)
+ private String toFormulaString(Ptg[] parsedExpression)
{
String formula = null;
if(parsedExpression!=null)
{
- formula = FormulaParser.toFormulaString(workbook.getWorkbook(),parsedExpression);
+ formula = FormulaParser.toFormulaString(workbook, parsedExpression);
}
return formula;
}
-
-
- private Stack parseFormula(String formula2)
- {
- FormulaParser parser =
- new FormulaParser(formula2, workbook.getWorkbook());
- parser.parse();
-
- Stack parsedExpression = convertToTokenStack(parser.getRPNPtg());
- parsedExpression = convertToTokenStack(parser.getRPNPtg());
- return parsedExpression;
- }
-
- private static Stack convertToTokenStack(Ptg[] ptgs)
- {
- if( ptgs != null)
- {
- Stack parsedExpression = new Stack();
- // fill the Ptg Stack with Ptgs of new formula
- for (int k = 0; k < ptgs.length; k++)
- {
- parsedExpression.push(ptgs[ k ]);
- }
- return parsedExpression;
- }
- else
- {
- return null;
- }
- }
-
-
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java b/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java
index 9e69fa255..2d3d6781c 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFPatriarch.java
@@ -58,7 +58,7 @@ public class HSSFPatriarch
*/
HSSFPatriarch(HSSFSheet sheet, EscherAggregate boundAggregate)
{
- this.boundAggregate = boundAggregate;
+ this.boundAggregate = boundAggregate;
this.sheet = sheet;
}
@@ -197,29 +197,29 @@ public class HSSFPatriarch
* to work on some charts so far)
*/
public boolean containsChart() {
- // TODO - support charts properly in usermodel
-
- // We're looking for a EscherOptRecord
- EscherOptRecord optRecord = (EscherOptRecord)
- boundAggregate.findFirstWithId(EscherOptRecord.RECORD_ID);
- if(optRecord == null) {
- // No opt record, can't have chart
- return false;
- }
-
- for(Iterator it = optRecord.getEscherProperties().iterator(); it.hasNext();) {
- EscherProperty prop = (EscherProperty)it.next();
- if(prop.getPropertyNumber() == 896 && prop.isComplex()) {
- EscherComplexProperty cp = (EscherComplexProperty)prop;
- String str = StringUtil.getFromUnicodeLE(cp.getComplexData());
- System.err.println(str);
- if(str.equals("Chart 1\0")) {
- return true;
- }
- }
- }
+ // TODO - support charts properly in usermodel
+
+ // We're looking for a EscherOptRecord
+ EscherOptRecord optRecord = (EscherOptRecord)
+ boundAggregate.findFirstWithId(EscherOptRecord.RECORD_ID);
+ if(optRecord == null) {
+ // No opt record, can't have chart
+ return false;
+ }
+
+ for(Iterator it = optRecord.getEscherProperties().iterator(); it.hasNext();) {
+ EscherProperty prop = (EscherProperty)it.next();
+ if(prop.getPropertyNumber() == 896 && prop.isComplex()) {
+ EscherComplexProperty cp = (EscherComplexProperty)prop;
+ String str = StringUtil.getFromUnicodeLE(cp.getComplexData());
+ //System.err.println(str);
+ if(str.equals("Chart 1\0")) {
+ return true;
+ }
+ }
+ }
- return false;
+ return false;
}
/**
@@ -258,6 +258,6 @@ public class HSSFPatriarch
* Returns the aggregate escher record we're bound to
*/
protected EscherAggregate _getBoundAggregate() {
- return boundAggregate;
+ return boundAggregate;
}
}
diff --git a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
index 0e8759f4f..4aad1e1b1 100644
--- a/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
+++ b/src/java/org/apache/poi/hssf/usermodel/HSSFSheet.java
@@ -36,6 +36,7 @@ import org.apache.poi.hssf.model.FormulaParser;
import org.apache.poi.hssf.model.Sheet;
import org.apache.poi.hssf.model.Workbook;
import org.apache.poi.hssf.record.CellValueRecordInterface;
+import org.apache.poi.hssf.record.CFRuleRecord;
import org.apache.poi.hssf.record.DVALRecord;
import org.apache.poi.hssf.record.DVRecord;
import org.apache.poi.hssf.record.EOFRecord;
@@ -1106,8 +1107,8 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet
* @param leftcol the left column to show in desktop window pane
*/
public void showInPane(short toprow, short leftcol){
- this.sheet.setTopRow((short)toprow);
- this.sheet.setLeftCol((short)leftcol);
+ this.sheet.setTopRow(toprow);
+ this.sheet.setLeftCol(leftcol);
}
/**
@@ -1454,7 +1455,7 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet
int i = 0;
while (iterator.hasNext()) {
PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.next();
- returnValue[i++] = (int)breakItem.main;
+ returnValue[i++] = breakItem.main;
}
return returnValue;
}
@@ -1822,7 +1823,7 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet
*
* @return cell comment or null
if not found
*/
- public HSSFComment getCellComment(int row, int column){
+ public HSSFComment getCellComment(int row, int column) {
// Don't call findCellComment directly, otherwise
// two calls to this method will result in two
// new HSSFComment instances, which is bad
@@ -1846,25 +1847,26 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet
* with a cell comparison operator and
* formatting rules such as font format, border format and pattern format
*
- * @param comparisonOperation - one of the following values: - *
+ *
null
)
+ * @param bordFmt - border formatting rules (may be null
)
+ * @param patternFmt - pattern formatting rules (may be null
)
*/
public HSSFConditionalFormattingRule createConditionalFormattingRule(
byte comparisonOperation,
@@ -1872,14 +1874,11 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet
String formula2,
HSSFFontFormatting fontFmt,
HSSFBorderFormatting bordFmt,
- HSSFPatternFormatting patternFmt)
- {
- HSSFConditionalFormattingRule cf = new HSSFConditionalFormattingRule(workbook);
- cf.setFontFormatting(fontFmt);
- cf.setBorderFormatting(bordFmt);
- cf.setPatternFormatting(patternFmt);
- cf.setCellComparisonCondition(comparisonOperation, formula1, formula2);
- return cf;
+ HSSFPatternFormatting patternFmt) {
+
+ Workbook wb = workbook.getWorkbook();
+ CFRuleRecord rr = CFRuleRecord.create(wb, comparisonOperation, formula1, formula2);
+ return new HSSFConditionalFormattingRule(wb, rr, fontFmt, bordFmt, patternFmt);
}
/**
@@ -1888,38 +1887,19 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet
*
* The formatting rules are applied by Excel when the value of the formula not equal to 0.
*
- * @param comparisonOperation - one of the following values: - *
null
)
+ * @param bordFmt - border formatting rules (may be null
)
+ * @param patternFmt - pattern formatting rules (may be null
)
*/
public HSSFConditionalFormattingRule createConditionalFormattingRule(
String formula,
HSSFFontFormatting fontFmt,
HSSFBorderFormatting bordFmt,
- HSSFPatternFormatting patternFmt)
- {
- HSSFConditionalFormattingRule cf = new HSSFConditionalFormattingRule(workbook);
- cf.setFontFormatting(fontFmt);
- cf.setBorderFormatting(bordFmt);
- cf.setPatternFormatting(patternFmt);
- cf.setFormulaCondition(formula);
- return cf;
+ HSSFPatternFormatting patternFmt) {
+ Workbook wb = workbook.getWorkbook();
+ CFRuleRecord rr = CFRuleRecord.create(wb, formula);
+ return new HSSFConditionalFormattingRule(wb, rr, fontFmt, bordFmt, patternFmt);
}
/**
@@ -1934,8 +1914,7 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet
* @param cf HSSFConditionalFormatting object
* @return index of the new Conditional Formatting object
*/
- public int addConditionalFormatting( HSSFConditionalFormatting cf )
- {
+ public int addConditionalFormatting( HSSFConditionalFormatting cf ) {
CFRecordsAggregate cfraClone = cf.getCFRecordsAggregate().cloneCFAggregate();
return sheet.addConditionalFormatting(cfraClone);
@@ -1945,46 +1924,46 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet
* Allows to add a new Conditional Formatting set to the sheet.
*
* @param regions - list of rectangular regions to apply conditional formatting rules
- * @param cfRules - set of up to three conditional formatting rules
+ * @param hcfRules - set of up to three conditional formatting rules
*
* @return index of the newly created Conditional Formatting object
*/
- public int addConditionalFormatting( Region [] regions, HSSFConditionalFormattingRule [] cfRules )
- {
- HSSFConditionalFormatting cf = new HSSFConditionalFormatting(this);
- cf.setFormattingRegions(regions);
- if( cfRules != null )
- {
- for( int i=0; i!= cfRules.length; i++ )
- {
- cf.addRule(cfRules[i]);
- }
- }
- return sheet.addConditionalFormatting(cf.getCFRecordsAggregate());
+ public int addConditionalFormatting(Region [] regions, HSSFConditionalFormattingRule [] hcfRules) {
+ if (regions == null) {
+ throw new IllegalArgumentException("regions must not be null");
+ }
+ if (hcfRules == null) {
+ throw new IllegalArgumentException("hcfRules must not be null");
+ }
+
+ CFRuleRecord[] rules = new CFRuleRecord[hcfRules.length];
+ for (int i = 0; i != hcfRules.length; i++) {
+ rules[i] = hcfRules[i].getCfRuleRecord();
+ }
+ CFRecordsAggregate cfra = new CFRecordsAggregate(regions, rules);
+ return sheet.addConditionalFormatting(cfra);
}
/**
* gets Conditional Formatting object at a particular index
- * @param index of the Conditional Formatting object to fetch
+ *
+ * @param index
+ * of the Conditional Formatting object to fetch
* @return Conditional Formatting object
*/
-
- public HSSFConditionalFormatting getConditionalFormattingAt(int index)
- {
+ public HSSFConditionalFormatting getConditionalFormattingAt(int index) {
CFRecordsAggregate cf = sheet.getCFRecordsAggregateAt(index);
- if( cf != null )
- {
- return new HSSFConditionalFormatting(this,cf);
+ if (cf == null) {
+ return null;
}
- return null;
+ return new HSSFConditionalFormatting(this,cf);
}
/**
* @return number of Conditional Formatting objects of the sheet
*/
- public int getNumConditionalFormattings()
- {
+ public int getNumConditionalFormattings() {
return sheet.getNumConditionalFormattings();
}
@@ -1992,8 +1971,7 @@ public class HSSFSheet implements org.apache.poi.ss.usermodel.Sheet
* removes a Conditional Formatting object by index
* @param index of a Conditional Formatting object to remove
*/
- public void removeConditionalFormatting(int index)
- {
+ public void removeConditionalFormatting(int index) {
sheet.removeConditionalFormatting(index);
}
}
diff --git a/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java b/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java
index d8ddc7d2f..135b625f1 100644
--- a/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java
+++ b/src/scratchpad/src/org/apache/poi/hslf/model/Sheet.java
@@ -265,6 +265,31 @@ public abstract class Sheet {
}
}
+ /**
+ * Removes the specified shape from this sheet.
+ *
+ * @param shape shape to be removed from this sheet, if present.
+ * @return true if the shape was deleted.
+ */
+ public boolean removeShape(Shape shape) {
+ PPDrawing ppdrawing = getPPDrawing();
+
+ EscherContainerRecord dg = (EscherContainerRecord) ppdrawing.getEscherRecords()[0];
+ EscherContainerRecord spgr = null;
+
+ for (Iterator it = dg.getChildRecords().iterator(); it.hasNext();) {
+ EscherRecord rec = (EscherRecord) it.next();
+ if (rec.getRecordId() == EscherContainerRecord.SPGR_CONTAINER) {
+ spgr = (EscherContainerRecord) rec;
+ break;
+ }
+ }
+ if(spgr == null) return false;
+
+ List lst = spgr.getChildRecords();
+ return lst.remove(shape.getSpContainer());
+ }
+
/**
* Return the master sheet .
*/
diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java
index 6dd5d9be1..1fe8f7e5e 100644
--- a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java
+++ b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestShapes.java
@@ -26,6 +26,7 @@ import java.awt.Rectangle;
import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.IOException;
import java.util.ArrayList;
/**
@@ -279,4 +280,31 @@ public class TestShapes extends TestCase {
line = (Line)grshape[1];
assertEquals(new Rectangle(300, 300, 500, 0), line.getAnchor());
}
+
+ /**
+ * Test functionality of Sheet.removeShape(Shape shape)
+ */
+ public void testRemoveShapes() throws IOException {
+ String file = System.getProperty("HSLF.testdata.path")+ "/with_textbox.ppt";
+ SlideShow ppt = new SlideShow(new HSLFSlideShow(file));
+ Slide sl = ppt.getSlides()[0];
+ Shape[] sh = sl.getShapes();
+ assertEquals("expected four shaped in " + file, 4, sh.length);
+ //remove all
+ for (int i = 0; i < sh.length; i++) {
+ boolean ok = sl.removeShape(sh[i]);
+ assertTrue("Failed to delete shape #" + i, ok);
+ }
+ //now Slide.getShapes() should return an empty array
+ assertEquals("expected 0 shaped in " + file, 0, sl.getShapes().length);
+
+ //serialize and read again. The file should be readable and contain no shapes
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ ppt.write(out);
+ out.close();
+
+ ppt = new SlideShow(new ByteArrayInputStream(out.toByteArray()));
+ sl = ppt.getSlides()[0];
+ assertEquals("expected 0 shaped in " + file, 0, sl.getShapes().length);
+ }
}
diff --git a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
index 3b98aed0a..2589aa90d 100644
--- a/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
+++ b/src/testcases/org/apache/poi/hssf/model/TestFormulaParser.java
@@ -14,7 +14,7 @@
See the License for the specific language governing permissions and
limitations under the License.
==================================================================== */
-
+
package org.apache.poi.hssf.model;
import junit.framework.AssertionFailedError;
@@ -54,7 +54,7 @@ import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
- * Test the low level formula parser functionality. High level tests are to
+ * Test the low level formula parser functionality. High level tests are to
* be done via usermodel/HSSFCell.setFormulaValue() .
* Some tests are also done in scratchpad, if they need
* HSSFFormulaEvaluator, which is there
@@ -71,7 +71,7 @@ public final class TestFormulaParser extends TestCase {
assertNotNull("Ptg array should not be null", result);
return result;
}
-
+
public void testSimpleFormula() {
FormulaParser fp = new FormulaParser("2+2",null);
fp.parse();
@@ -86,9 +86,9 @@ public final class TestFormulaParser extends TestCase {
assertTrue("",(ptgs[0] instanceof IntPtg));
assertTrue("",(ptgs[1] instanceof IntPtg));
assertTrue("",(ptgs[2] instanceof AddPtg));
-
+
}
-
+
public void testFormulaWithSpace2() {
Ptg[] ptgs;
FormulaParser fp;
@@ -97,7 +97,7 @@ public final class TestFormulaParser extends TestCase {
ptgs = fp.getRPNPtg();
assertTrue("five tokens expected, got "+ptgs.length,ptgs.length == 5);
}
-
+
public void testFormulaWithSpaceNRef() {
Ptg[] ptgs;
FormulaParser fp;
@@ -106,7 +106,7 @@ public final class TestFormulaParser extends TestCase {
ptgs = fp.getRPNPtg();
assertTrue("two tokens expected, got "+ptgs.length,ptgs.length == 2);
}
-
+
public void testFormulaWithString() {
Ptg[] ptgs;
FormulaParser fp;
@@ -172,7 +172,7 @@ public final class TestFormulaParser extends TestCase {
}
-
+
/**
* Make sure the ptgs are generated properly with two functions embedded
*
@@ -225,7 +225,7 @@ public final class TestFormulaParser extends TestCase {
assertEquals("4 Ptgs expected", 4, asts.length);
}
-
+
/**
* Bug Reported by xt-jens.riis@nokia.com (Jens Riis)
* Refers to Bug #17582
@@ -247,7 +247,7 @@ public final class TestFormulaParser extends TestCase {
}
-
+
public void testSimpleLogical() {
FormulaParser fp=new FormulaParser("IF(A1true
only when parsing the target tables */
private boolean _isInsideTable;
-
+
private final List _rowData;
private final StringBuffer _textNodeBuffer;
private final List _rowNoteFlags;
private boolean _cellHasNote;
-
+
private final FunctionDataCollector _fdc;
private String _lastHeadingText;
-
+
public EFFDocHandler(FunctionDataCollector fdc) {
_fdc = fdc;
_elemNameStack = new Stack();
@@ -216,7 +216,7 @@ public class ExcelFileFormatDocFunctionExtractor {
_textNodeBuffer = new StringBuffer();
_rowNoteFlags = new ArrayList();
}
-
+
private boolean matchesTargetPath() {
return matchesPath(0, TABLE_BASE_PATH_NAMES);
}
@@ -365,7 +365,7 @@ public class ExcelFileFormatDocFunctionExtractor {
xr.setContentHandler(new EFFDocHandler(fdc));
InputSource inSrc = new InputSource(is);
-
+
try {
xr.parse(inSrc);
is.close();
@@ -407,30 +407,30 @@ public class ExcelFileFormatDocFunctionExtractor {
}
private static void outputLicenseHeader(PrintStream ps) {
- String[] lines= {
- "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.",
- };
- for (int i = 0; i < lines.length; i++) {
- ps.print("# ");
- ps.println(lines[i]);
- }
- ps.println();
- }
+ String[] lines= {
+ "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.",
+ };
+ for (int i = 0; i < lines.length; i++) {
+ ps.print("# ");
+ ps.println(lines[i]);
+ }
+ ps.println();
+ }
- /**
+ /**
* Helps identify the source file
*/
private static String getFileCRC(File f) {
@@ -451,10 +451,10 @@ public class ExcelFileFormatDocFunctionExtractor {
}
return "0x" + Long.toHexString(crc.getValue()).toUpperCase();
}
-
+
private static File getSourceFile() {
- if (true) {
- File dir = new File("c:/josh/ref-docs");
+ if (false) {
+ File dir = new File("c:/temp");
File effDocFile = new File(dir, SOURCE_DOC_FILE_NAME);
return effDocFile;
}
@@ -464,7 +464,7 @@ public class ExcelFileFormatDocFunctionExtractor {
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
-
+
File result;
byte[]buf = new byte[2048];
try {
@@ -488,16 +488,15 @@ public class ExcelFileFormatDocFunctionExtractor {
System.out.println("file downloaded ok");
return result;
}
-
+
public static void main(String[] args) {
-
+
File effDocFile = getSourceFile();
if(!effDocFile.exists()) {
throw new RuntimeException("file '" + effDocFile.getAbsolutePath() + "' does not exist");
}
-
+
File outFile = new File("functionMetadata-asGenerated.txt");
processFile(effDocFile, outFile);
}
-
}
diff --git a/src/testcases/org/apache/poi/hssf/record/formula/function/TestFunctionMetadataRegistry.java b/src/testcases/org/apache/poi/hssf/record/formula/function/TestFunctionMetadataRegistry.java
index edb215ec5..c175c473b 100644
--- a/src/testcases/org/apache/poi/hssf/record/formula/function/TestFunctionMetadataRegistry.java
+++ b/src/testcases/org/apache/poi/hssf/record/formula/function/TestFunctionMetadataRegistry.java
@@ -18,26 +18,27 @@
package org.apache.poi.hssf.record.formula.function;
import junit.framework.TestCase;
+
/**
*
* @author Josh Micich
*/
public final class TestFunctionMetadataRegistry extends TestCase {
- public void testWellKnownFunctions() {
- confirmFunction(0, "COUNT");
- confirmFunction(1, "IF");
-
- }
+ public void testWellKnownFunctions() {
+ confirmFunction(0, "COUNT");
+ confirmFunction(1, "IF");
- private static void confirmFunction(int index, String funcName) {
- FunctionMetadata fm;
- fm = FunctionMetadataRegistry.getFunctionByIndex(index);
- assertNotNull(fm);
- assertEquals(funcName, fm.getName());
-
- fm = FunctionMetadataRegistry.getFunctionByName(funcName);
- assertNotNull(fm);
- assertEquals(index, fm.getIndex());
- }
+ }
+
+ private static void confirmFunction(int index, String funcName) {
+ FunctionMetadata fm;
+ fm = FunctionMetadataRegistry.getFunctionByIndex(index);
+ assertNotNull(fm);
+ assertEquals(funcName, fm.getName());
+
+ fm = FunctionMetadataRegistry.getFunctionByName(funcName);
+ assertNotNull(fm);
+ assertEquals(index, fm.getIndex());
+ }
}
diff --git a/src/testcases/org/apache/poi/hssf/record/formula/function/TestParseMissingBuiltInFuncs.java b/src/testcases/org/apache/poi/hssf/record/formula/function/TestParseMissingBuiltInFuncs.java
index fe1b8fccc..3671d37c1 100644
--- a/src/testcases/org/apache/poi/hssf/record/formula/function/TestParseMissingBuiltInFuncs.java
+++ b/src/testcases/org/apache/poi/hssf/record/formula/function/TestParseMissingBuiltInFuncs.java
@@ -44,8 +44,8 @@ public final class TestParseMissingBuiltInFuncs extends TestCase {
}
AbstractFunctionPtg func = (AbstractFunctionPtg) ptgF;
if(func.getFunctionIndex() == 255) {
- throw new AssertionFailedError("Failed to recognise built-in function in formula '"
- + formula + "'");
+ throw new AssertionFailedError("Failed to recognise built-in function in formula '"
+ + formula + "'");
}
assertEquals(expPtgArraySize, ptgs.length);
diff --git a/src/testcases/org/apache/poi/hssf/record/formula/function/TestReadMissingBuiltInFuncs.java b/src/testcases/org/apache/poi/hssf/record/formula/function/TestReadMissingBuiltInFuncs.java
index 7e1d0317e..f1e6bcfac 100644
--- a/src/testcases/org/apache/poi/hssf/record/formula/function/TestReadMissingBuiltInFuncs.java
+++ b/src/testcases/org/apache/poi/hssf/record/formula/function/TestReadMissingBuiltInFuncs.java
@@ -48,7 +48,7 @@ public final class TestReadMissingBuiltInFuncs extends TestCase {
}
sht = wb.getSheetAt(0);
}
-
+
public void testDatedif() {
String formula;
@@ -56,9 +56,9 @@ public final class TestReadMissingBuiltInFuncs extends TestCase {
formula = getCellFormula(0);
} catch (IllegalStateException e) {
if(e.getMessage().startsWith("Too few arguments")) {
- if(e.getMessage().indexOf("AttrPtg") > 0) {
- throw afe("tAttrVolatile not supported in FormulaParser.toFormulaString");
- }
+ if(e.getMessage().indexOf("AttrPtg") > 0) {
+ throw afe("tAttrVolatile not supported in FormulaParser.toFormulaString");
+ }
throw afe("NOW() registered with 1 arg instead of 0");
}
if(e.getMessage().startsWith("too much stuff")) {
@@ -70,7 +70,7 @@ public final class TestReadMissingBuiltInFuncs extends TestCase {
assertEquals("DATEDIF(NOW(),NOW(),\"d\")", formula);
}
public void testDdb() {
-
+
String formula = getCellFormula(1);
if("externalflag(1,1,1,1,1)".equals(formula)) {
throw afe("DDB() not registered");
@@ -78,14 +78,14 @@ public final class TestReadMissingBuiltInFuncs extends TestCase {
assertEquals("DDB(1,1,1,1,1)", formula);
}
public void testAtan() {
-
+
String formula = getCellFormula(2);
if(formula.equals("ARCTAN(1)")) {
throw afe("func ix 18 registered as ARCTAN() instead of ATAN()");
}
assertEquals("ATAN(1)", formula);
}
-
+
public void testUsdollar() {
String formula = getCellFormula(3);
@@ -128,7 +128,7 @@ public final class TestReadMissingBuiltInFuncs extends TestCase {
}
assertEquals("ISNONTEXT(\"abc\")", formula);
}
-
+
private String getCellFormula(int rowIx) {
String result = sht.getRow(rowIx).getCell((short)0).getCellFormula();
if (false) {
diff --git a/src/testcases/org/apache/poi/hssf/record/formula/functions/AllIndividualFunctionEvaluationTests.java b/src/testcases/org/apache/poi/hssf/record/formula/functions/AllIndividualFunctionEvaluationTests.java
index 66d2a1d27..5973d7cb2 100755
--- a/src/testcases/org/apache/poi/hssf/record/formula/functions/AllIndividualFunctionEvaluationTests.java
+++ b/src/testcases/org/apache/poi/hssf/record/formula/functions/AllIndividualFunctionEvaluationTests.java
@@ -27,9 +27,8 @@ import junit.framework.TestSuite;
*/
public final class AllIndividualFunctionEvaluationTests {
- // TODO - have this suite incorporated into a higher level one
public static Test suite() {
- TestSuite result = new TestSuite("Tests for org.apache.poi.hssf.record.formula.functions");
+ TestSuite result = new TestSuite(AllIndividualFunctionEvaluationTests.class.getName());
result.addTestSuite(TestAverage.class);
result.addTestSuite(TestCountFuncs.class);
result.addTestSuite(TestDate.class);
diff --git a/src/testcases/org/apache/poi/hssf/record/formula/functions/TestPmt.java b/src/testcases/org/apache/poi/hssf/record/formula/functions/TestPmt.java
index 935615aca..2fecef704 100644
--- a/src/testcases/org/apache/poi/hssf/record/formula/functions/TestPmt.java
+++ b/src/testcases/org/apache/poi/hssf/record/formula/functions/TestPmt.java
@@ -30,7 +30,7 @@ import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
* @author Josh Micich
*/
public final class TestPmt extends TestCase {
-
+
private static void confirm(double expected, NumberEval ne) {
// only asserting accuracy to 4 fractional digits
assertEquals(expected, ne.getNumberValue(), 0.00005);
@@ -61,12 +61,12 @@ public final class TestPmt extends TestCase {
confirm(expected, invokeNormal(args));
}
-
+
public void testBasic() {
confirm(-1037.0321, (0.08/12), 10, 10000, 0, false);
confirm(-1030.1643, (0.08/12), 10, 10000, 0, true);
}
-
+
public void test3args() {
Eval[] args = {
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
index 94c19cbc0..f05c1d115 100644
--- a/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestBugs.java
@@ -357,14 +357,14 @@ extends TestCase {
book.createSheet("TEST");
HSSFSheet sheet = book.cloneSheet(0);
book.setSheetName(1,"CLONE");
- sheet.createRow(0).createCell((short)0).setCellValue("Test");
+ sheet.createRow(0).createCell((short)0).setCellValue(new HSSFRichTextString("Test"));
book.write(out);
book = new HSSFWorkbook(new ByteArrayInputStream(out.toByteArray()));
sheet = book.getSheet("CLONE");
HSSFRow row = sheet.getRow(0);
HSSFCell cell = row.getCell((short)0);
- System.out.println(cell.getStringCellValue());
+ assertEquals("Test", cell.getRichStringCellValue().getString());
}
/**
diff --git a/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConfditionalFormatting.java b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConfditionalFormatting.java
new file mode 100644
index 000000000..6186d227e
--- /dev/null
+++ b/src/testcases/org/apache/poi/hssf/usermodel/TestHSSFConfditionalFormatting.java
@@ -0,0 +1,90 @@
+/* ====================================================================
+ 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 junit.framework.TestCase;
+
+import org.apache.poi.hssf.record.CFRuleRecord.ComparisonOperator;
+import org.apache.poi.hssf.util.HSSFColor;
+import org.apache.poi.hssf.util.Region;
+/**
+ *
+ * @author Dmitriy Kumshayev
+ */
+public final class TestHSSFConfditionalFormatting extends TestCase
+{
+ public void testLastAndFirstColumns()
+ {
+ HSSFWorkbook workbook = new HSSFWorkbook();
+ HSSFSheet sheet = workbook.createSheet();
+ String formula = "7";
+
+ HSSFFontFormatting fontFmt = new HSSFFontFormatting();
+ fontFmt.setFontStyle(true, false);
+
+ HSSFBorderFormatting bordFmt = new HSSFBorderFormatting();
+ bordFmt.setBorderBottom(HSSFBorderFormatting.BORDER_THIN);
+ bordFmt.setBorderTop(HSSFBorderFormatting.BORDER_THICK);
+ bordFmt.setBorderLeft(HSSFBorderFormatting.BORDER_DASHED);
+ bordFmt.setBorderRight(HSSFBorderFormatting.BORDER_DOTTED);
+
+ HSSFPatternFormatting patternFmt = new HSSFPatternFormatting();
+ patternFmt.setFillBackgroundColor(HSSFColor.RED.index);
+
+ HSSFConditionalFormattingRule [] cfRules =
+ {
+ sheet.createConditionalFormattingRule(formula, fontFmt, bordFmt, patternFmt),
+ sheet.createConditionalFormattingRule(ComparisonOperator.BETWEEN, "1", "2", fontFmt, bordFmt, patternFmt)
+ };
+
+ short col = 1;
+ Region [] regions =
+ {
+ new Region(0,col,-1,col)
+ };
+
+ sheet.addConditionalFormatting(regions, cfRules);
+ sheet.addConditionalFormatting(regions, cfRules);
+
+ // Verification
+ assertEquals(2, sheet.getNumConditionalFormattings());
+ sheet.removeConditionalFormatting(1);
+ assertEquals(1, sheet.getNumConditionalFormattings());
+ HSSFConditionalFormatting cf = sheet.getConditionalFormattingAt(0);
+ assertNotNull(cf);
+
+ regions = cf.getFormattingRegions();
+ assertNotNull(regions);
+ assertEquals(1, regions.length);
+ Region r = regions[0];
+ assertEquals(1, r.getColumnFrom());
+ assertEquals(1, r.getColumnTo());
+ assertEquals(0, r.getRowFrom());
+ assertEquals(-1, r.getRowTo());
+
+ assertEquals(2, cf.getNumberOfRules());
+
+ HSSFConditionalFormattingRule rule1 = cf.getRule(0);
+ assertEquals("7",rule1.getFormula1());
+ assertNull(rule1.getFormula2());
+
+ HSSFConditionalFormattingRule rule2 = cf.getRule(1);
+ assertEquals("2",rule2.getFormula2());
+ assertEquals("1",rule2.getFormula1());
+ }
+}