--- /dev/null
+/* ====================================================================
+ 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.ss.excelant;
+
+import org.apache.poi.ss.excelant.util.ExcelAntEvaluationResult;
+import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+
+/**
+ * Instances of this class are used to evaluate a single cell. This is usually
+ * after some values have been set. The evaluation is actually performed
+ * by a WorkbookUtil instance. The evaluate() method of the WorkbookUtil
+ * class returns an EvaluationResult which encapsulates the results and
+ * information from the evaluation.
+ *
+ * @author Jon Svede ( jon [at] loquatic [dot] com )
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+
+ *
+ */
+public class ExcelAntEvaluateCell extends Task {
+
+ private String cell ;
+ private double expectedValue ;
+ private double precision ;
+ private double precisionToUse ;
+ private double globalPrecision ;
+ private boolean requiredToPass = false ;
+
+
+ private ExcelAntEvaluationResult result ;
+
+ private ExcelAntWorkbookUtil wbUtil ;
+
+ private boolean showDelta = false ;
+
+
+ public ExcelAntEvaluateCell() {}
+
+ protected void setWorkbookUtil( ExcelAntWorkbookUtil wb ) {
+ wbUtil = wb ;
+ }
+
+ public void setShowDelta( boolean value ) {
+ showDelta = value ;
+ }
+
+ protected boolean showDelta() {
+ return showDelta ;
+ }
+
+ public void setCell(String cell) {
+ this.cell = cell;
+ }
+
+ public void setRequiredToPass( boolean val ) {
+ requiredToPass = val ;
+ }
+
+ protected boolean requiredToPass() {
+ return requiredToPass ;
+ }
+
+ public void setExpectedValue(double expectedValue) {
+ this.expectedValue = expectedValue;
+ }
+
+ public void setPrecision(double precision) {
+ this.precision = precision;
+ }
+
+ protected void setGlobalPrecision( double prec ) {
+ globalPrecision = prec ;
+ }
+
+ protected String getCell() {
+ return cell;
+ }
+
+ protected double getExpectedValue() {
+ return expectedValue;
+ }
+
+ protected double getPrecision() {
+ return precisionToUse;
+ }
+
+ public void execute() throws BuildException {
+
+ precisionToUse = 0 ;
+
+ // if there is a globalPrecision we will use it unless there is also
+ // precision set at the evaluate level, then we use that. If there
+ // is not a globalPrecision, we will use the local precision.
+ log( "test precision = " + precision + "\tglobal precision = " + globalPrecision, Project.MSG_VERBOSE ) ;
+ if( globalPrecision > 0 ) {
+ if( precision > 0 ) {
+ precisionToUse = precision ;
+ log( "Using evaluate precision of " + precision + " over the " +
+ "global precision of " + globalPrecision, Project.MSG_VERBOSE ) ;
+ } else {
+ precisionToUse = globalPrecision ;
+ log( "Using global precision of " + globalPrecision, Project.MSG_VERBOSE ) ;
+ }
+ } else {
+ precisionToUse = precision ;
+ log( "Using evaluate precision of " + precision, Project.MSG_VERBOSE ) ;
+ }
+ result = wbUtil.evaluateCell(cell, expectedValue, precisionToUse ) ;
+
+ StringBuffer sb = new StringBuffer() ;
+ sb.append( "evaluation of cell " ) ;
+ sb.append( cell ) ;
+ sb.append( " resulted in " ) ;
+ sb.append( result.getReturnValue() ) ;
+ if( showDelta == true ) {
+ sb.append( " with a delta of " + result.getDelta() ) ;
+ }
+
+ log( sb.toString(), Project.MSG_DEBUG) ;
+
+ }
+
+ public ExcelAntEvaluationResult getResult() {
+ return result ;
+ }
+
+
+}
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.excelant;\r
+\r
+import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;\r
+import org.apache.tools.ant.BuildException;\r
+import org.apache.tools.ant.Project;\r
+import org.apache.tools.ant.Task;\r
+\r
+/**\r
+ * This is the class that backs the <handler> tag in the Ant task.\r
+ * <p>\r
+ * Its purpose is to provide a way to manipulate a workbook in the course\r
+ * of an ExcelAnt task. The idea being to model a way for test writers to\r
+ * simulate the behaviors of the workbook. \r
+ * <p>\r
+ * Suppose, for example, you have a workbook that has a worksheet that\r
+ * reacts to values entered or selected by the user. It's possible in\r
+ * Excel to change other cells based on this but this isn't easily possible\r
+ * in POI. In ExcelAnt we handle this using the Handler, which is a Java\r
+ * class you write to manipulate the workbook. \r
+ * <p>\r
+ * In order to use this tag you must write a class that implements the \r
+ * <code>IExcelAntWorkbookHandler</code> interface. After writing the\r
+ * class you should package it and it's dependencies into a jar file to \r
+ * add as library in your Ant build file.\r
+ * \r
+ * @author Jon Svede ( jon [at] loquatic [dot] com )\r
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )\r
+ *\r
+ */\r
+public class ExcelAntHandlerTask extends Task {\r
+ \r
+ private String className ;\r
+ \r
+ private ExcelAntWorkbookUtil wbUtil ;\r
+\r
+ public void setClassName( String cName ) {\r
+ className = cName ;\r
+ }\r
+ \r
+ protected void setEAWorkbookUtil( ExcelAntWorkbookUtil wkbkUtil ) {\r
+ wbUtil = wkbkUtil ;\r
+ }\r
+ \r
+ public void execute() throws BuildException {\r
+ log( "handling the workbook with class " + className, Project.MSG_INFO ) ;\r
+ try {\r
+ Class clazz = Class.forName( className ) ;\r
+ Object handlerObj = clazz.newInstance() ;\r
+ if( handlerObj instanceof IExcelAntWorkbookHandler ) {\r
+ IExcelAntWorkbookHandler iHandler = (IExcelAntWorkbookHandler)handlerObj ;\r
+ iHandler.setWorkbook( wbUtil.getWorkbook() ) ;\r
+ iHandler.execute() ;\r
+ }\r
+ } catch( Exception e ) {\r
+ throw new BuildException( e.getMessage(), e ) ;\r
+ }\r
+ }\r
+ }\r
--- /dev/null
+/* ====================================================================
+ 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.ss.excelant;
+
+import org.apache.tools.ant.taskdefs.Typedef;
+
+/**
+ *
+ * @author Jon Svede ( jon [at] loquatic [dot] com )
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ *
+ */
+public class ExcelAntPrecision extends Typedef {
+
+ private double value ;
+
+ public void setValue( double precision ) {
+ value = precision ;
+ }
+
+ public double getValue() {
+ return value ;
+ }
+}
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.excelant;\r
+\r
+import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;\r
+import org.apache.tools.ant.Task;\r
+\r
+/**\r
+ * \r
+ * @author Jon Svede ( jon [at] loquatic [dot] com )\r
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )\r
+ * \r
+ */\r
+public abstract class ExcelAntSet extends Task {\r
+ \r
+ protected String cellStr ;\r
+ \r
+ protected ExcelAntWorkbookUtil wbUtil ;\r
+ \r
+ public void setCell( String cellName ) {\r
+ cellStr = cellName ;\r
+ }\r
+ \r
+ public String getCell() {\r
+ return cellStr ;\r
+ }\r
+ \r
+ \r
+ public void setWorkbookUtil( ExcelAntWorkbookUtil wb ) {\r
+ wbUtil = wb ;\r
+ }\r
+\r
+}\r
--- /dev/null
+/* ====================================================================
+ 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.ss.excelant;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+
+/**
+ * Class for use in an Ant build script that sets the value of an Excel
+ * sheet cell using the cell id ('Sheet Name'!cellId).
+ *
+ * @author Jon Svede ( jon [at] loquatic [dot] com )
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ *
+ */
+public class ExcelAntSetDoubleCell extends ExcelAntSet {
+
+
+ private double cellValue ;
+
+ public ExcelAntSetDoubleCell() {}
+
+ /**
+ * Set the value of the specified cell as the double passed in.
+ * @param value
+ */
+ public void setValue( double value ) {
+ cellValue = value ;
+ }
+
+ /**
+ * Return the cell value as a double.
+ * @return
+ */
+ public double getCellValue() {
+ return cellValue;
+ }
+
+ public void execute() throws BuildException {
+
+ wbUtil.setDoubleValue(cellStr, cellValue ) ;
+
+ log( "set cell " + cellStr + " to value " + cellValue + " as double.", Project.MSG_DEBUG ) ;
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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.ss.excelant;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+
+/**
+ * Class for use in an Ant build script that sets the formula of an Excel
+ * sheet cell using the cell id ('Sheet Name'!cellId).
+ *
+ * @author Jon Svede ( jon [at] loquatic [dot] com )
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ *
+ */
+public class ExcelAntSetFormulaCell extends ExcelAntSet {
+
+
+ private String cellValue ;
+
+ public ExcelAntSetFormulaCell() {}
+
+ public void setValue( String value ) {
+ cellValue = value ;
+ }
+
+ protected String getCellValue() {
+ return cellValue;
+ }
+
+ public void execute() throws BuildException {
+
+ wbUtil.setFormulaValue( cellStr, cellValue ) ;
+
+ log( "set cell " + cellStr + " to formula " + cellValue, Project.MSG_DEBUG ) ;
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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.ss.excelant;
+
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+
+/**
+ * Class for use in an Ant build script that sets the value of an Excel
+ * sheet cell using the cell id ('Sheet Name'!cellId).
+ *
+ * @author Jon Svede ( jon [at] loquatic [dot] com )
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ *
+ */
+public class ExcelAntSetStringCell extends ExcelAntSet {
+
+
+ private String stringValue ;
+
+
+ public ExcelAntSetStringCell() {}
+
+
+ /**
+ * Set the value of the cell to the String passed in.
+ * @param value
+ */
+ public void setValue(String value ) {
+ stringValue = value ;
+ }
+
+ /**
+ * Return the value that will be set into the cell.
+ * @return
+ */
+ public String getCellValue() {
+ return stringValue;
+ }
+
+ public void execute() throws BuildException {
+
+ wbUtil.setStringValue(cellStr, stringValue ) ;
+
+ log( "set cell " + cellStr + " to value " + stringValue + " as String.", Project.MSG_DEBUG ) ;
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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.ss.excelant;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.LinkedList;
+
+import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;
+import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtilFactory;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.WorkbookFactory;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+
+/**
+ * Ant task class for testing Excel workbook cells.
+ *
+ * @author Jon Svede ( jon [at] loquatic [dot] com )
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ *
+ */
+public class ExcelAntTask extends Task {
+
+ public static final String VERSION = "0.5.0" ;
+
+ private String excelFileName ;
+
+ private boolean failOnError = false ;
+
+ private ExcelAntWorkbookUtil workbookUtil ;
+
+ private ExcelAntPrecision precision ;
+
+ private LinkedList<ExcelAntTest> tests ;
+ private LinkedList<ExcelAntUserDefinedFunction> functions ;
+
+ public ExcelAntTask() {
+ tests = new LinkedList<ExcelAntTest>() ;
+ functions = new LinkedList<ExcelAntUserDefinedFunction>() ;
+ }
+
+ public void addPrecision( ExcelAntPrecision prec ) {
+ precision = prec ;
+ }
+
+ public void setFailOnError( boolean value ) {
+ failOnError = value ;
+ }
+ public void setFileName( String fileName ) {
+ excelFileName = fileName ;
+ }
+
+ public void addTest( ExcelAntTest testElement ) {
+ tests.add( testElement ) ;
+ }
+
+ public void addUdf( ExcelAntUserDefinedFunction def ) {
+ functions.add( def ) ;
+ }
+
+ public void execute() throws BuildException {
+ checkClassPath();
+
+ int totalCount = 0 ;
+ int successCount = 0 ;
+
+ StringBuffer versionBffr = new StringBuffer() ;
+ versionBffr.append( "ExcelAnt version " ) ;
+ versionBffr.append( VERSION ) ;
+ versionBffr.append( " Copyright 2011" ) ;
+ SimpleDateFormat sdf = new SimpleDateFormat( "yyyy" ) ;
+ double currYear = Double.parseDouble( sdf.format( new Date() ) );
+ if( currYear > 2011 ) {
+ versionBffr.append( "-" ) ;
+ versionBffr.append( currYear ) ;
+ }
+ log( versionBffr.toString(), Project.MSG_INFO ) ;
+
+ log( "Using input file: " + excelFileName, Project.MSG_INFO ) ;
+
+ Workbook targetWorkbook = loadWorkbook() ;
+ if( targetWorkbook == null ) {
+ log( "Unable to load " + excelFileName +
+ ". Verify the file exists and can be read.",
+ Project.MSG_ERR ) ;
+ return ;
+ }
+ if( tests != null && tests.size() > 0 ) {
+
+ Iterator<ExcelAntTest> testsIt = tests.iterator() ;
+ while( testsIt.hasNext() ) {
+ ExcelAntTest test = testsIt.next();
+
+ log( "executing test: " + test.getName(), Project.MSG_DEBUG ) ;
+
+ workbookUtil = ExcelAntWorkbookUtilFactory.getInstance( excelFileName ) ;
+
+ if( functions != null ) {
+ Iterator<ExcelAntUserDefinedFunction> functionsIt = functions.iterator() ;
+ while( functionsIt.hasNext() ) {
+ ExcelAntUserDefinedFunction eaUdf = functionsIt.next() ;
+ try {
+ workbookUtil.addFunction(eaUdf.getFunctionAlias(), eaUdf.getClassName() ) ;
+ } catch ( Exception e) {
+ throw new BuildException( e.getMessage(), e );
+ }
+ }
+ }
+ test.setWorkbookUtil( workbookUtil ) ;
+
+ if( precision != null && precision.getValue() > 0 ) {
+ log( "setting precision for the test " + test.getName(), Project.MSG_VERBOSE ) ;
+ test.setPrecision( precision.getValue() ) ;
+ }
+
+ test.execute() ;
+
+ if( test.didTestPass() ) {
+ successCount++ ;
+ } else {
+ if( failOnError == true ) {
+ throw new BuildException( "Test " + test.getName() + " failed." ) ;
+ }
+ }
+ totalCount++ ;
+
+ workbookUtil = null ;
+ }
+ log( successCount + "/" + totalCount + " tests passed.", Project.MSG_INFO ) ;
+ workbookUtil = null ;
+ }
+ }
+
+
+ private Workbook loadWorkbook() {
+ if (excelFileName == null) {
+ throw new BuildException("fileName attribute must be set!",
+ getLocation());
+ }
+
+ Workbook workbook;
+ File workbookFile = new File( excelFileName ) ;
+ try {
+ FileInputStream fis = new FileInputStream( workbookFile ) ;
+ workbook = WorkbookFactory.create( fis ) ;
+ } catch (Exception e) {
+ throw new BuildException("Cannot load file " + excelFileName
+ + ". Make sure the path and file permissions are correct.", e, getLocation());
+ }
+ return workbook ;
+ }
+
+
+ /**
+ * ExcelAnt depends on external libraries not included in the Ant distribution.
+ * Give user a sensible message if any if the required jars are missing.
+ */
+ private void checkClassPath(){
+ try {
+ Class.forName("org.apache.poi.hssf.usermodel.HSSFWorkbook");
+ Class.forName("org.apache.poi.ss.usermodel.WorkbookFactory");
+ } catch (Throwable e) {
+ throw new BuildException(
+ "The <classpath> for <excelant> must include poi.jar and poi-ooxml.jar " +
+ "if not in Ant's own classpath. Processing .xlsx spreadsheets requires " +
+ "additional poi-ooxml-schemas.jar, xmlbeans.jar and dom4j.jar" ,
+ e, getLocation());
+ }
+
+ }
+}
--- /dev/null
+/* ====================================================================
+ 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.ss.excelant;
+
+import java.util.Iterator;
+import java.util.LinkedList;
+
+import org.apache.poi.ss.excelant.util.ExcelAntEvaluationResult;
+import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.Task;
+
+/**
+ * This class represents a single test. In order for the test any and all
+ * ExcelAntEvaluateCell evaluations must pass. Therefore it is recommended
+ * that you use only 1 evaluator but you can use more if you choose.
+ *
+ * @author Jon Svede ( jon [at] loquatic [dot] com )
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ *
+ */
+public class ExcelAntTest extends Task{
+
+ private LinkedList<ExcelAntSet> setters ;
+ private LinkedList<ExcelAntEvaluateCell>evaluators ;
+
+ private LinkedList<Task> testTasks ;
+
+ private String name ;
+
+ private double globalPrecision ;
+
+ private boolean showSuccessDetails = false ;
+
+ private boolean showFailureDetail = false ;
+ LinkedList<String> failureMessages ;
+
+
+ private ExcelAntWorkbookUtil workbookUtil ;
+
+ private boolean passed = true ;
+
+
+ public ExcelAntTest() {
+ setters = new LinkedList<ExcelAntSet>() ;
+ evaluators = new LinkedList<ExcelAntEvaluateCell>() ;
+ failureMessages = new LinkedList<String>() ;
+ testTasks = new LinkedList<Task>() ;
+ }
+
+ public void setPrecision( double precision ) {
+ globalPrecision = precision ;
+ }
+
+ public void setWorkbookUtil( ExcelAntWorkbookUtil wbUtil ) {
+ workbookUtil = wbUtil ;
+ }
+
+
+ public void setShowFailureDetail( boolean value ) {
+ showFailureDetail = value ;
+ }
+
+ public void setName( String nm ) {
+ name = nm ;
+ }
+
+ public String getName() {
+ return name ;
+ }
+
+ public void setShowSuccessDetails( boolean details ) {
+ showSuccessDetails = details ;
+ }
+
+ public boolean showSuccessDetails() {
+ return showSuccessDetails ;
+ }
+
+ public void addSetDouble( ExcelAntSetDoubleCell setter ) {
+ addSetter( setter ) ;
+ }
+
+ public void addSetString( ExcelAntSetStringCell setter ){
+ addSetter( setter ) ;
+ }
+
+ public void addSetFormula( ExcelAntSetFormulaCell setter ) {
+ addSetter( setter ) ;
+ }
+
+ public void addHandler( ExcelAntHandlerTask handler ) {
+ testTasks.add( handler ) ;
+ }
+
+ private void addSetter( ExcelAntSet setter ) {
+// setters.add( setter );
+ testTasks.add( setter ) ;
+ }
+
+ public void addEvaluate( ExcelAntEvaluateCell evaluator ) {
+// evaluators.add( evaluator ) ;
+ testTasks.add( evaluator ) ;
+ }
+
+// public LinkedList<ExcelAntSet> getSetters() {
+// return setters;
+// }
+
+ protected LinkedList<ExcelAntEvaluateCell> getEvaluators() {
+ return evaluators;
+ }
+
+ public void execute() throws BuildException {
+
+ Iterator<Task> taskIt = testTasks.iterator() ;
+
+ int testCount = evaluators.size() ;
+ int failureCount = 0 ;
+
+ // roll over all sub task elements in one loop. This allows the
+ // ordering of the sub elements to be considered.
+ while( taskIt.hasNext() ) {
+ Task task = taskIt.next() ;
+
+ // log( task.getClass().getName(), Project.MSG_INFO ) ;
+
+ if( task instanceof ExcelAntSet ) {
+ ExcelAntSet set = (ExcelAntSet) task ;
+ set.setWorkbookUtil(workbookUtil) ;
+ set.execute() ;
+ }
+
+ if( task instanceof ExcelAntHandlerTask ) {
+ ExcelAntHandlerTask handler = (ExcelAntHandlerTask)task ;
+ handler.setEAWorkbookUtil(workbookUtil ) ;
+ handler.execute() ;
+ }
+
+ if (task instanceof ExcelAntEvaluateCell ) {
+ ExcelAntEvaluateCell eval = (ExcelAntEvaluateCell)task ;
+ eval.setWorkbookUtil( workbookUtil ) ;
+
+ if( globalPrecision > 0 ) {
+ log( "setting globalPrecision to " + globalPrecision + " in the evaluator", Project.MSG_VERBOSE ) ;
+ eval.setGlobalPrecision( globalPrecision ) ;
+ }
+
+ try {
+ eval.execute() ;
+ ExcelAntEvaluationResult result = eval.getResult() ;
+ if( result.didTestPass() &&
+ result.evaluationCompleteWithError() == false ) {
+ if( showSuccessDetails == true ) {
+ log("Succeeded when evaluating " +
+ result.getCellName() + ". It evaluated to " +
+ result.getReturnValue() + " when the value of " +
+ eval.getExpectedValue() + " with precision of " +
+ eval.getPrecision(), Project.MSG_INFO ) ;
+ }
+ } else {
+ if( showFailureDetail == true ) {
+ failureMessages.add( "\tFailed to evaluate cell " +
+ result.getCellName() + ". It evaluated to " +
+ result.getReturnValue() + " when the value of " +
+ eval.getExpectedValue() + " with precision of " +
+ eval.getPrecision() + " was expected." ) ;
+
+ }
+ passed = false ;
+ failureCount++ ;
+
+ if( eval.requiredToPass() == true ) {
+ throw new BuildException( "\tFailed to evaluate cell " +
+ result.getCellName() + ". It evaluated to " +
+ result.getReturnValue() + " when the value of " +
+ eval.getExpectedValue() + " with precision of " +
+ eval.getPrecision() + " was expected." ) ;
+ }
+ }
+ } catch( NullPointerException npe ) {
+ // this means the cell reference in the test is bad.
+ log( "Cell assignment " + eval.getCell() + " in test " + getName() +
+ " appears to point to an empy cell. Please check the " +
+ " reference in the ant script.", Project.MSG_ERR ) ;
+ }
+ }
+ }
+
+ if( passed == false ) {
+ log( "Test named " + name + " failed because " + failureCount +
+ " of " + testCount + " evaluations failed to " +
+ "evaluate correctly.",
+ Project.MSG_ERR ) ;
+ if( showFailureDetail == true && failureMessages.size() > 0 ) {
+ Iterator<String> failures = failureMessages.iterator() ;
+ while( failures.hasNext() ) {
+ log( failures.next(), Project.MSG_ERR ) ;
+ }
+ }
+ }
+ }
+
+ public boolean didTestPass() {
+
+ return passed ;
+ }
+ }
--- /dev/null
+/* ====================================================================
+ 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.ss.excelant;
+
+import org.apache.tools.ant.taskdefs.Typedef;
+
+/**
+ * This class encapsulates the Strings necessary to create the User Defined
+ * Function instances that will be passed to POI's Evaluator instance.
+ *
+ * @author Jon Svede ( jon [at] loquatic [dot] com )
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ *
+ */
+public class ExcelAntUserDefinedFunction extends Typedef {
+
+
+ public String functionAlias ;
+
+ public String className ;
+
+
+ public ExcelAntUserDefinedFunction() {}
+
+ protected String getFunctionAlias() {
+ return functionAlias;
+ }
+
+ public void setFunctionAlias(String functionAlias) {
+ this.functionAlias = functionAlias;
+ }
+
+ protected String getClassName() {
+ return className;
+ }
+
+ public void setClassName(String className) {
+ this.className = className;
+ }
+}
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.excelant;\r
+\r
+import org.apache.poi.ss.usermodel.Workbook;\r
+\r
+\r
+/**\r
+ * In Excel there are many ways to handle manipulating a workbook based \r
+ * on some arbitrary user action (onChange, etc). You use this interface\r
+ * to create classes that will handle the workbook in whatever manner is needed\r
+ * that cannot be handled by POI.\r
+ * <p>\r
+ * For example, suppose that in Excel when you update a cell the workbook\r
+ * does some calculations and updates other cells based on that change. In\r
+ * ExcelAnt you would set the value of the cell then write your own handler\r
+ * then call that from your Ant task after the set task.\r
+ * \r
+ * @author Jon Svede ( jon [at] loquatic [dot] com )\r
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )\r
+ *\r
+ */\r
+public interface IExcelAntWorkbookHandler {\r
+ \r
+ \r
+ public void setWorkbook( Workbook workbook ) ;\r
+ \r
+ public void execute() ;\r
+ \r
+\r
+}\r
--- /dev/null
+/* ====================================================================
+ 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.ss.excelant.util;
+
+/**
+ * A simple class that encapsulates information about a cell evaluation
+ * from POI.
+ *
+ * @author Jon Svede ( jon [at] loquatic [dot] com )
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ *
+ */
+public class ExcelAntEvaluationResult {
+
+ /**
+ * This boolean flag is used to determine if the evaluation completed
+ * without error. This alone doesn't ensure that the evaluation was
+ * sucessful.
+ */
+ private boolean evaluationCompletedWithError ;
+
+ /**
+ * This boolean flag is used to determine if the result was within
+ * the specified precision.
+ */
+ private boolean didPass ;
+
+ /**
+ * This is the actual value returned from the evaluation.
+ */
+ private double returnValue ;
+
+ /**
+ * Any error message String values that need to be returned.
+ */
+ private String errorMessage ;
+
+ /**
+ * Stores the absolute value of the delta for this evaluation.
+ */
+ private double actualDelta ;
+
+ /**
+ * This stores the fully qualified cell name (sheetName!cellId).
+ */
+ private String cellName ;
+
+
+
+ public ExcelAntEvaluationResult( boolean completedWithError,
+ boolean passed,
+ double retValue,
+ String errMessage,
+ double delta,
+ String cellId ) {
+
+ evaluationCompletedWithError = completedWithError;
+ didPass = passed;
+ returnValue = retValue;
+ errorMessage = errMessage;
+ actualDelta = delta ;
+ cellName = cellId ;
+ }
+
+ public double getReturnValue() {
+ return returnValue;
+ }
+
+ public String getErrorMessage() {
+ return errorMessage;
+ }
+
+ public boolean didTestPass() {
+ return didPass ;
+ }
+
+ public boolean evaluationCompleteWithError() {
+ return evaluationCompletedWithError ;
+ }
+
+ public double getDelta() {
+ return actualDelta ;
+ }
+
+ public String getCellName() {
+ return cellName ;
+ }
+
+ @Override
+ public String toString() {
+ return "ExcelAntEvaluationResult [evaluationCompletedWithError="
+ + evaluationCompletedWithError + ", didPass=" + didPass
+ + ", returnValue=" + returnValue + ", errorMessage="
+ + errorMessage + ", actualDelta=" + actualDelta + ", cellName="
+ + cellName + "]";
+ }
+
+
+}
--- /dev/null
+/* ====================================================================
+ 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.ss.excelant.util;
+
+import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.formula.functions.FreeRefFunction;
+import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
+import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
+import org.apache.poi.ss.formula.udf.UDFFinder;
+import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.util.CellReference;
+import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.apache.tools.ant.BuildException;
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.taskdefs.Typedef;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.Iterator;
+
+/**
+ * A general utility class that abstracts the POI details of loading the
+ * workbook, accessing and updating cells.
+ *
+ * @author Jon Svede ( jon [at] loquatic [dot] com )
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
+ *
+ */
+public class ExcelAntWorkbookUtil extends Typedef {
+
+ private String excelFileName;
+
+ private Workbook workbook;
+
+ private HashMap<String, FreeRefFunction> xlsMacroList;
+
+ /**
+ * Constructs an instance using a String that contains the fully qualified
+ * path of the Excel file. This constructor initializes a Workbook instance
+ * based on that file name.
+ *
+ * @param fName
+ */
+ protected ExcelAntWorkbookUtil(String fName) {
+ excelFileName = fName;
+ xlsMacroList = new HashMap<String, FreeRefFunction>() ;
+ loadWorkbook();
+
+ }
+
+ /**
+ * Constructs an instance based on a Workbook instance.
+ *
+ * @param wb
+ */
+ protected ExcelAntWorkbookUtil(Workbook wb) {
+ workbook = wb;
+ xlsMacroList = new HashMap<String, FreeRefFunction>() ;
+ }
+
+ /**
+ * Loads the member variable workbook based on the fileName variable.
+ * @return
+ */
+ private Workbook loadWorkbook() {
+
+ File workbookFile = new File(excelFileName);
+ try {
+ FileInputStream fis = new FileInputStream(workbookFile);
+ try {
+ workbook = WorkbookFactory.create(fis);
+ } finally {
+ fis.close();
+ }
+ } catch(Exception e) {
+ throw new BuildException("Cannot load file " + excelFileName
+ + ". Make sure the path and file permissions are correct.", e);
+ }
+
+ return workbook ;
+ }
+
+ /**
+ * Used to add a UDF to the evaluator.
+ * @param name
+ * @param clazzName
+ * @throws ClassNotFoundException
+ * @throws InstantiationException
+ * @throws IllegalAccessException
+ */
+ public void addFunction( String name, String clazzName ) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
+ Class<?> clazzInst = Class.forName( clazzName ) ;
+ Object newInst = clazzInst.newInstance() ;
+ if( newInst instanceof FreeRefFunction ) {
+ addFunction( name, (FreeRefFunction)newInst ) ;
+ }
+
+ }
+
+ /**
+ * Updates the internal HashMap of functions with instance and alias passed
+ * in.
+ *
+ * @param name
+ * @param func
+ */
+ protected void addFunction(String name, FreeRefFunction func) {
+ xlsMacroList.put(name, func);
+ }
+
+ /**
+ * returns a UDFFinder that contains all of the functions added.
+ *
+ * @return
+ */
+ protected UDFFinder getFunctions() {
+
+ String[] names = new String[xlsMacroList.size()];
+ FreeRefFunction[] functions = new FreeRefFunction[xlsMacroList.size()];
+
+ Iterator<String> keysIt = xlsMacroList.keySet().iterator();
+ int x = 0;
+ while (keysIt.hasNext()) {
+ String name = keysIt.next();
+ FreeRefFunction function = xlsMacroList.get(name);
+ names[x] = name;
+ functions[x] = function;
+ }
+
+ UDFFinder udff1 = new DefaultUDFFinder(names, functions);
+ UDFFinder udff = new AggregatingUDFFinder(udff1);
+
+ return udff;
+
+ }
+
+ /**
+ * Returns a formula evaluator that is loaded with the functions that
+ * have been supplied.
+ *
+ * @param fileName
+ * @return
+ */
+ protected FormulaEvaluator getEvaluator( String fileName ) {
+ FormulaEvaluator evaluator ;
+ if (fileName.endsWith(".xlsx")) {
+ if( xlsMacroList != null && xlsMacroList.size() > 0 ) {
+ evaluator = XSSFFormulaEvaluator.create( (XSSFWorkbook) workbook,
+ null,
+ getFunctions() ) ;
+ }
+ evaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
+ } else {
+ if( xlsMacroList != null && xlsMacroList.size() > 0 ) {
+ evaluator = HSSFFormulaEvaluator.create( (HSSFWorkbook)workbook,
+ null,
+ getFunctions() ) ;
+ }
+
+ evaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
+ }
+
+ return evaluator ;
+
+ }
+
+ /**
+ * Returns the Workbook instance associated with this WorkbookUtil.
+ *
+ * @return
+ */
+ public Workbook getWorkbook() {
+ return workbook;
+ }
+
+ /**
+ * Returns the fileName that was used to initialize this instance. May
+ * return null if the instance was constructed from a Workbook object.
+ *
+ * @return
+ */
+ public String getFileName() {
+ return excelFileName;
+ }
+
+ /**
+ * Returns the list of sheet names.
+ *
+ * @return
+ */
+ public ArrayList<String> getSheets() {
+ ArrayList<String> sheets = new ArrayList<String>() ;
+
+ int sheetCount = workbook.getNumberOfSheets() ;
+
+ for( int x=0; x<sheetCount; x++ ) {
+ sheets.add( workbook.getSheetName( x ) ) ;
+ }
+
+ return sheets ;
+ }
+
+ /**
+ * This method uses a String in standard Excel format (SheetName!CellId) to
+ * locate the cell and set it to the value of the double in value.
+ *
+ * @param cellName
+ * @param value
+ */
+ public void setDoubleValue(String cellName, double value) {
+ log("starting setCellValue()", Project.MSG_DEBUG);
+ Cell cell = getCell(cellName);
+ log("working on cell: " + cell, Project.MSG_DEBUG);
+ cell.setCellValue(value);
+ log("after cell.setCellValue()", Project.MSG_DEBUG);
+
+ log("set cell " + cellName + " to value " + value, Project.MSG_DEBUG);
+ }
+
+ /**
+ * Utility method for setting the value of a Cell with a String.
+ *
+ * @param cellName
+ * @param value
+ */
+ public void setStringValue( String cellName, String value ) {
+ Cell cell = getCell(cellName);
+ cell.setCellValue(value);
+ }
+
+ /**
+ * Utility method for setting the value of a Cell with a Formula.
+ *
+ * @param cellName
+ * @param formula
+ */
+ public void setFormulaValue( String cellName, String formula ) {
+ Cell cell = getCell(cellName);
+ cell.setCellFormula( formula );
+ }
+
+ /**
+ * Utility method for setting the value of a Cell with a Date.
+ * @param cellName
+ * @param date
+ */
+ public void setDateValue( String cellName, Date date ) {
+ Cell cell = getCell(cellName);
+ cell.setCellValue( date ) ;
+ }
+ /**
+ * Uses a String in standard Excel format (SheetName!CellId) to locate a
+ * cell and evaluate it.
+ *
+ * @param cellName
+ * @param expectedValue
+ * @param precision
+ */
+ public ExcelAntEvaluationResult evaluateCell(String cellName, double expectedValue,
+ double precision) {
+
+ ExcelAntEvaluationResult evalResults = null;
+
+ Cell cell = getCell(cellName);
+
+ FormulaEvaluator evaluator = getEvaluator( excelFileName );
+
+
+ CellValue resultOfEval = evaluator.evaluate(cell);
+
+ if (resultOfEval.getErrorValue() == 0) {
+ // the evaluation did not encounter errors
+ double result = resultOfEval.getNumberValue();
+ double delta = Math.abs(result - expectedValue);
+ if (delta > precision) {
+ evalResults = new ExcelAntEvaluationResult(false, false,
+ resultOfEval.getNumberValue(),
+ "Results was out of range based on precision " + " of "
+ + precision + ". Delta was actually " + delta, delta, cellName );
+ } else {
+ evalResults = new ExcelAntEvaluationResult(false, true,
+ resultOfEval.getNumberValue(),
+ "Evaluation passed without error within in range.", delta, cellName );
+ }
+ } else {
+ String errorMeaning = null ;
+ try {
+ errorMeaning = ErrorConstants.getText( resultOfEval
+ .getErrorValue() ) ;
+ } catch( IllegalArgumentException iae ) {
+ errorMeaning = "unknown error code: " +
+ Byte.toString( resultOfEval.getErrorValue() ) ;
+ }
+
+ evalResults = new ExcelAntEvaluationResult(false, false,
+ resultOfEval.getNumberValue(),
+ "Evaluation failed due to an evaluation error of "
+ + resultOfEval.getErrorValue()
+ + " which is "
+ + errorMeaning, 0, cellName );
+ }
+
+ return evalResults;
+ }
+
+ /**
+ * Returns a Cell as a String value.
+ *
+ * @param cellName
+ * @return
+ */
+ public String getCellAsString( String cellName ) {
+ Cell cell = getCell( cellName ) ;
+ if( cell != null ) {
+ return cell.getStringCellValue() ;
+ }
+ return "" ;
+ }
+
+
+ /**
+ * Returns the value of the Cell as a double.
+ *
+ * @param cellName
+ * @return
+ */
+ public double getCellAsDouble( String cellName ) {
+ Cell cell = getCell( cellName ) ;
+ if( cell != null ) {
+ return cell.getNumericCellValue() ;
+ }
+ return 0.0 ;
+ }
+ /**
+ * Returns a cell reference based on a String in standard Excel format
+ * (SheetName!CellId). This method will create a new cell if the
+ * requested cell isn't initialized yet.
+ *
+ * @param cellName
+ * @return
+ */
+ private Cell getCell(String cellName) {
+
+ CellReference cellRef = new CellReference(cellName);
+ String sheetName = cellRef.getSheetName();
+ Sheet sheet = workbook.getSheet(sheetName);
+ if(sheet == null) {
+ throw new BuildException("Sheet not found: " + sheetName);
+ }
+
+ int rowIdx = cellRef.getRow();
+ int colIdx = cellRef.getCol();
+ Row row = sheet.getRow(rowIdx);
+
+ if( row == null ) {
+ row = sheet.createRow( rowIdx ) ;
+ }
+
+ Cell cell = row.getCell(colIdx);
+
+ if( cell == null ) {
+ cell = row.createCell( colIdx ) ;
+ }
+
+ return cell;
+ }
+
+}
--- /dev/null
+/* ====================================================================\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to You under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+==================================================================== */\r
+\r
+package org.apache.poi.ss.excelant.util;\r
+\r
+import java.util.HashMap;\r
+\r
+\r
+/**\r
+ * This is a factory class maps file names to WorkbookUtil instances. This\r
+ * helps ExcelAnt be more efficient when being run many times in an Ant build.\r
+ *\r
+ * @author Jon Svede ( jon [at] loquatic [dot] com )\r
+ * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )\r
+ *\r
+ */\r
+public class ExcelAntWorkbookUtilFactory {\r
+\r
+ private static HashMap<String, ExcelAntWorkbookUtil> workbookUtilMap ;\r
+\r
+ private static ExcelAntWorkbookUtilFactory factory ;\r
+\r
+ private ExcelAntWorkbookUtilFactory() {\r
+ workbookUtilMap = new HashMap<String, ExcelAntWorkbookUtil>() ;\r
+ }\r
+\r
+ /**\r
+ * Using the fileName, check the internal map to see if an instance\r
+ * of the WorkbookUtil exists. If not, then add an instance to the map.\r
+ *\r
+ * @param fileName\r
+ * @return\r
+ */\r
+ public static ExcelAntWorkbookUtil getInstance( String fileName ) {\r
+\r
+ if( factory == null ) {\r
+ factory = new ExcelAntWorkbookUtilFactory() ;\r
+ }\r
+ if( workbookUtilMap != null &&\r
+ workbookUtilMap.containsKey( fileName ) ) {\r
+ return workbookUtilMap.get( fileName ) ;\r
+ }\r
+\r
+ ExcelAntWorkbookUtil wbu = new ExcelAntWorkbookUtil( fileName ) ;\r
+ workbookUtilMap.put( fileName, wbu ) ;\r
+ return wbu ;\r
+ }\r
+\r
+}\r
+++ /dev/null
-/* ====================================================================
- 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.ss.excelant;
-
-import org.apache.poi.ss.excelant.util.ExcelAntEvaluationResult;
-import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;
-import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Project;
-import org.apache.tools.ant.Task;
-
-/**
- * Instances of this class are used to evaluate a single cell. This is usually
- * after some values have been set. The evaluation is actually performed
- * by a WorkbookUtil instance. The evaluate() method of the WorkbookUtil
- * class returns an EvaluationResult which encapsulates the results and
- * information from the evaluation.
- *
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
-
- *
- */
-public class ExcelAntEvaluateCell extends Task {
-
- private String cell ;
- private double expectedValue ;
- private double precision ;
- private double precisionToUse ;
- private double globalPrecision ;
- private boolean requiredToPass = false ;
-
-
- private ExcelAntEvaluationResult result ;
-
- private ExcelAntWorkbookUtil wbUtil ;
-
- private boolean showDelta = false ;
-
-
- public ExcelAntEvaluateCell() {}
-
- protected void setWorkbookUtil( ExcelAntWorkbookUtil wb ) {
- wbUtil = wb ;
- }
-
- public void setShowDelta( boolean value ) {
- showDelta = value ;
- }
-
- protected boolean showDelta() {
- return showDelta ;
- }
-
- public void setCell(String cell) {
- this.cell = cell;
- }
-
- public void setRequiredToPass( boolean val ) {
- requiredToPass = val ;
- }
-
- protected boolean requiredToPass() {
- return requiredToPass ;
- }
-
- public void setExpectedValue(double expectedValue) {
- this.expectedValue = expectedValue;
- }
-
- public void setPrecision(double precision) {
- this.precision = precision;
- }
-
- protected void setGlobalPrecision( double prec ) {
- globalPrecision = prec ;
- }
-
- protected String getCell() {
- return cell;
- }
-
- protected double getExpectedValue() {
- return expectedValue;
- }
-
- protected double getPrecision() {
- return precisionToUse;
- }
-
- public void execute() throws BuildException {
-
- precisionToUse = 0 ;
-
- // if there is a globalPrecision we will use it unless there is also
- // precision set at the evaluate level, then we use that. If there
- // is not a globalPrecision, we will use the local precision.
- log( "test precision = " + precision + "\tglobal precision = " + globalPrecision, Project.MSG_VERBOSE ) ;
- if( globalPrecision > 0 ) {
- if( precision > 0 ) {
- precisionToUse = precision ;
- log( "Using evaluate precision of " + precision + " over the " +
- "global precision of " + globalPrecision, Project.MSG_VERBOSE ) ;
- } else {
- precisionToUse = globalPrecision ;
- log( "Using global precision of " + globalPrecision, Project.MSG_VERBOSE ) ;
- }
- } else {
- precisionToUse = precision ;
- log( "Using evaluate precision of " + precision, Project.MSG_VERBOSE ) ;
- }
- result = wbUtil.evaluateCell(cell, expectedValue, precisionToUse ) ;
-
- StringBuffer sb = new StringBuffer() ;
- sb.append( "evaluation of cell " ) ;
- sb.append( cell ) ;
- sb.append( " resulted in " ) ;
- sb.append( result.getReturnValue() ) ;
- if( showDelta == true ) {
- sb.append( " with a delta of " + result.getDelta() ) ;
- }
-
- log( sb.toString(), Project.MSG_DEBUG) ;
-
- }
-
- public ExcelAntEvaluationResult getResult() {
- return result ;
- }
-
-
-}
+++ /dev/null
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.ss.excelant;\r
-\r
-import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;\r
-import org.apache.tools.ant.BuildException;\r
-import org.apache.tools.ant.Project;\r
-import org.apache.tools.ant.Task;\r
-\r
-/**\r
- * This is the class that backs the <handler> tag in the Ant task.\r
- * <p>\r
- * Its purpose is to provide a way to manipulate a workbook in the course\r
- * of an ExcelAnt task. The idea being to model a way for test writers to\r
- * simulate the behaviors of the workbook. \r
- * <p>\r
- * Suppose, for example, you have a workbook that has a worksheet that\r
- * reacts to values entered or selected by the user. It's possible in\r
- * Excel to change other cells based on this but this isn't easily possible\r
- * in POI. In ExcelAnt we handle this using the Handler, which is a Java\r
- * class you write to manipulate the workbook. \r
- * <p>\r
- * In order to use this tag you must write a class that implements the \r
- * <code>IExcelAntWorkbookHandler</code> interface. After writing the\r
- * class you should package it and it's dependencies into a jar file to \r
- * add as library in your Ant build file.\r
- * \r
- * @author Jon Svede ( jon [at] loquatic [dot] com )\r
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )\r
- *\r
- */\r
-public class ExcelAntHandlerTask extends Task {\r
- \r
- private String className ;\r
- \r
- private ExcelAntWorkbookUtil wbUtil ;\r
-\r
- public void setClassName( String cName ) {\r
- className = cName ;\r
- }\r
- \r
- protected void setEAWorkbookUtil( ExcelAntWorkbookUtil wkbkUtil ) {\r
- wbUtil = wkbkUtil ;\r
- }\r
- \r
- public void execute() throws BuildException {\r
- log( "handling the workbook with class " + className, Project.MSG_INFO ) ;\r
- try {\r
- Class clazz = Class.forName( className ) ;\r
- Object handlerObj = clazz.newInstance() ;\r
- if( handlerObj instanceof IExcelAntWorkbookHandler ) {\r
- IExcelAntWorkbookHandler iHandler = (IExcelAntWorkbookHandler)handlerObj ;\r
- iHandler.setWorkbook( wbUtil.getWorkbook() ) ;\r
- iHandler.execute() ;\r
- }\r
- } catch( Exception e ) {\r
- throw new BuildException( e.getMessage(), e ) ;\r
- }\r
- }\r
- }\r
+++ /dev/null
-/* ====================================================================
- 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.ss.excelant;
-
-import org.apache.tools.ant.taskdefs.Typedef;
-
-/**
- *
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
- *
- */
-public class ExcelAntPrecision extends Typedef {
-
- private double value ;
-
- public void setValue( double precision ) {
- value = precision ;
- }
-
- public double getValue() {
- return value ;
- }
-}
+++ /dev/null
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.ss.excelant;\r
-\r
-import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;\r
-import org.apache.tools.ant.Task;\r
-\r
-/**\r
- * \r
- * @author Jon Svede ( jon [at] loquatic [dot] com )\r
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )\r
- * \r
- */\r
-public abstract class ExcelAntSet extends Task {\r
- \r
- protected String cellStr ;\r
- \r
- protected ExcelAntWorkbookUtil wbUtil ;\r
- \r
- public void setCell( String cellName ) {\r
- cellStr = cellName ;\r
- }\r
- \r
- public String getCell() {\r
- return cellStr ;\r
- }\r
- \r
- \r
- public void setWorkbookUtil( ExcelAntWorkbookUtil wb ) {\r
- wbUtil = wb ;\r
- }\r
-\r
-}\r
+++ /dev/null
-/* ====================================================================
- 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.ss.excelant;
-
-import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Project;
-
-/**
- * Class for use in an Ant build script that sets the value of an Excel
- * sheet cell using the cell id ('Sheet Name'!cellId).
- *
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
- *
- */
-public class ExcelAntSetDoubleCell extends ExcelAntSet {
-
-
- private double cellValue ;
-
- public ExcelAntSetDoubleCell() {}
-
- /**
- * Set the value of the specified cell as the double passed in.
- * @param value
- */
- public void setValue( double value ) {
- cellValue = value ;
- }
-
- /**
- * Return the cell value as a double.
- * @return
- */
- public double getCellValue() {
- return cellValue;
- }
-
- public void execute() throws BuildException {
-
- wbUtil.setDoubleValue(cellStr, cellValue ) ;
-
- log( "set cell " + cellStr + " to value " + cellValue + " as double.", Project.MSG_DEBUG ) ;
- }
-}
+++ /dev/null
-/* ====================================================================
- 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.ss.excelant;
-
-import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Project;
-
-/**
- * Class for use in an Ant build script that sets the formula of an Excel
- * sheet cell using the cell id ('Sheet Name'!cellId).
- *
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
- *
- */
-public class ExcelAntSetFormulaCell extends ExcelAntSet {
-
-
- private String cellValue ;
-
- public ExcelAntSetFormulaCell() {}
-
- public void setValue( String value ) {
- cellValue = value ;
- }
-
- protected String getCellValue() {
- return cellValue;
- }
-
- public void execute() throws BuildException {
-
- wbUtil.setFormulaValue( cellStr, cellValue ) ;
-
- log( "set cell " + cellStr + " to formula " + cellValue, Project.MSG_DEBUG ) ;
- }
-}
+++ /dev/null
-/* ====================================================================
- 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.ss.excelant;
-
-import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Project;
-
-/**
- * Class for use in an Ant build script that sets the value of an Excel
- * sheet cell using the cell id ('Sheet Name'!cellId).
- *
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
- *
- */
-public class ExcelAntSetStringCell extends ExcelAntSet {
-
-
- private String stringValue ;
-
-
- public ExcelAntSetStringCell() {}
-
-
- /**
- * Set the value of the cell to the String passed in.
- * @param value
- */
- public void setValue(String value ) {
- stringValue = value ;
- }
-
- /**
- * Return the value that will be set into the cell.
- * @return
- */
- public String getCellValue() {
- return stringValue;
- }
-
- public void execute() throws BuildException {
-
- wbUtil.setStringValue(cellStr, stringValue ) ;
-
- log( "set cell " + cellStr + " to value " + stringValue + " as String.", Project.MSG_DEBUG ) ;
- }
-}
+++ /dev/null
-/* ====================================================================
- 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.ss.excelant;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.text.SimpleDateFormat;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.LinkedList;
-
-import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;
-import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtilFactory;
-import org.apache.poi.ss.usermodel.Workbook;
-import org.apache.poi.ss.usermodel.WorkbookFactory;
-import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Project;
-import org.apache.tools.ant.Task;
-
-/**
- * Ant task class for testing Excel workbook cells.
- *
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
- *
- */
-public class ExcelAntTask extends Task {
-
- public static final String VERSION = "0.5.0" ;
-
- private String excelFileName ;
-
- private boolean failOnError = false ;
-
- private ExcelAntWorkbookUtil workbookUtil ;
-
- private ExcelAntPrecision precision ;
-
- private LinkedList<ExcelAntTest> tests ;
- private LinkedList<ExcelAntUserDefinedFunction> functions ;
-
- public ExcelAntTask() {
- tests = new LinkedList<ExcelAntTest>() ;
- functions = new LinkedList<ExcelAntUserDefinedFunction>() ;
- }
-
- public void addPrecision( ExcelAntPrecision prec ) {
- precision = prec ;
- }
-
- public void setFailOnError( boolean value ) {
- failOnError = value ;
- }
- public void setFileName( String fileName ) {
- excelFileName = fileName ;
- }
-
- public void addTest( ExcelAntTest testElement ) {
- tests.add( testElement ) ;
- }
-
- public void addUdf( ExcelAntUserDefinedFunction def ) {
- functions.add( def ) ;
- }
-
- public void execute() throws BuildException {
- checkClassPath();
-
- int totalCount = 0 ;
- int successCount = 0 ;
-
- StringBuffer versionBffr = new StringBuffer() ;
- versionBffr.append( "ExcelAnt version " ) ;
- versionBffr.append( VERSION ) ;
- versionBffr.append( " Copyright 2011" ) ;
- SimpleDateFormat sdf = new SimpleDateFormat( "yyyy" ) ;
- double currYear = Double.parseDouble( sdf.format( new Date() ) );
- if( currYear > 2011 ) {
- versionBffr.append( "-" ) ;
- versionBffr.append( currYear ) ;
- }
- log( versionBffr.toString(), Project.MSG_INFO ) ;
-
- log( "Using input file: " + excelFileName, Project.MSG_INFO ) ;
-
- Workbook targetWorkbook = loadWorkbook() ;
- if( targetWorkbook == null ) {
- log( "Unable to load " + excelFileName +
- ". Verify the file exists and can be read.",
- Project.MSG_ERR ) ;
- return ;
- }
- if( tests != null && tests.size() > 0 ) {
-
- Iterator<ExcelAntTest> testsIt = tests.iterator() ;
- while( testsIt.hasNext() ) {
- ExcelAntTest test = testsIt.next();
-
- log( "executing test: " + test.getName(), Project.MSG_DEBUG ) ;
-
- workbookUtil = ExcelAntWorkbookUtilFactory.getInstance( excelFileName ) ;
-
- if( functions != null ) {
- Iterator<ExcelAntUserDefinedFunction> functionsIt = functions.iterator() ;
- while( functionsIt.hasNext() ) {
- ExcelAntUserDefinedFunction eaUdf = functionsIt.next() ;
- try {
- workbookUtil.addFunction(eaUdf.getFunctionAlias(), eaUdf.getClassName() ) ;
- } catch ( Exception e) {
- throw new BuildException( e.getMessage(), e );
- }
- }
- }
- test.setWorkbookUtil( workbookUtil ) ;
-
- if( precision != null && precision.getValue() > 0 ) {
- log( "setting precision for the test " + test.getName(), Project.MSG_VERBOSE ) ;
- test.setPrecision( precision.getValue() ) ;
- }
-
- test.execute() ;
-
- if( test.didTestPass() ) {
- successCount++ ;
- } else {
- if( failOnError == true ) {
- throw new BuildException( "Test " + test.getName() + " failed." ) ;
- }
- }
- totalCount++ ;
-
- workbookUtil = null ;
- }
- log( successCount + "/" + totalCount + " tests passed.", Project.MSG_INFO ) ;
- workbookUtil = null ;
- }
- }
-
-
- private Workbook loadWorkbook() {
- if (excelFileName == null) {
- throw new BuildException("fileName attribute must be set!",
- getLocation());
- }
-
- Workbook workbook;
- File workbookFile = new File( excelFileName ) ;
- try {
- FileInputStream fis = new FileInputStream( workbookFile ) ;
- workbook = WorkbookFactory.create( fis ) ;
- } catch (Exception e) {
- throw new BuildException("Cannot load file " + excelFileName
- + ". Make sure the path and file permissions are correct.", e, getLocation());
- }
- return workbook ;
- }
-
-
- /**
- * ExcelAnt depends on external libraries not included in the Ant distribution.
- * Give user a sensible message if any if the required jars are missing.
- */
- private void checkClassPath(){
- try {
- Class.forName("org.apache.poi.hssf.usermodel.HSSFWorkbook");
- Class.forName("org.apache.poi.ss.usermodel.WorkbookFactory");
- } catch (Throwable e) {
- throw new BuildException(
- "The <classpath> for <excelant> must include poi.jar and poi-ooxml.jar " +
- "if not in Ant's own classpath. Processing .xlsx spreadsheets requires " +
- "additional poi-ooxml-schemas.jar, xmlbeans.jar and dom4j.jar" ,
- e, getLocation());
- }
-
- }
-}
+++ /dev/null
-/* ====================================================================
- 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.ss.excelant;
-
-import java.util.Iterator;
-import java.util.LinkedList;
-
-import org.apache.poi.ss.excelant.util.ExcelAntEvaluationResult;
-import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;
-import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Project;
-import org.apache.tools.ant.Task;
-
-/**
- * This class represents a single test. In order for the test any and all
- * ExcelAntEvaluateCell evaluations must pass. Therefore it is recommended
- * that you use only 1 evaluator but you can use more if you choose.
- *
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
- *
- */
-public class ExcelAntTest extends Task{
-
- private LinkedList<ExcelAntSet> setters ;
- private LinkedList<ExcelAntEvaluateCell>evaluators ;
-
- private LinkedList<Task> testTasks ;
-
- private String name ;
-
- private double globalPrecision ;
-
- private boolean showSuccessDetails = false ;
-
- private boolean showFailureDetail = false ;
- LinkedList<String> failureMessages ;
-
-
- private ExcelAntWorkbookUtil workbookUtil ;
-
- private boolean passed = true ;
-
-
- public ExcelAntTest() {
- setters = new LinkedList<ExcelAntSet>() ;
- evaluators = new LinkedList<ExcelAntEvaluateCell>() ;
- failureMessages = new LinkedList<String>() ;
- testTasks = new LinkedList<Task>() ;
- }
-
- public void setPrecision( double precision ) {
- globalPrecision = precision ;
- }
-
- public void setWorkbookUtil( ExcelAntWorkbookUtil wbUtil ) {
- workbookUtil = wbUtil ;
- }
-
-
- public void setShowFailureDetail( boolean value ) {
- showFailureDetail = value ;
- }
-
- public void setName( String nm ) {
- name = nm ;
- }
-
- public String getName() {
- return name ;
- }
-
- public void setShowSuccessDetails( boolean details ) {
- showSuccessDetails = details ;
- }
-
- public boolean showSuccessDetails() {
- return showSuccessDetails ;
- }
-
- public void addSetDouble( ExcelAntSetDoubleCell setter ) {
- addSetter( setter ) ;
- }
-
- public void addSetString( ExcelAntSetStringCell setter ){
- addSetter( setter ) ;
- }
-
- public void addSetFormula( ExcelAntSetFormulaCell setter ) {
- addSetter( setter ) ;
- }
-
- public void addHandler( ExcelAntHandlerTask handler ) {
- testTasks.add( handler ) ;
- }
-
- private void addSetter( ExcelAntSet setter ) {
-// setters.add( setter );
- testTasks.add( setter ) ;
- }
-
- public void addEvaluate( ExcelAntEvaluateCell evaluator ) {
-// evaluators.add( evaluator ) ;
- testTasks.add( evaluator ) ;
- }
-
-// public LinkedList<ExcelAntSet> getSetters() {
-// return setters;
-// }
-
- protected LinkedList<ExcelAntEvaluateCell> getEvaluators() {
- return evaluators;
- }
-
- public void execute() throws BuildException {
-
- Iterator<Task> taskIt = testTasks.iterator() ;
-
- int testCount = evaluators.size() ;
- int failureCount = 0 ;
-
- // roll over all sub task elements in one loop. This allows the
- // ordering of the sub elements to be considered.
- while( taskIt.hasNext() ) {
- Task task = taskIt.next() ;
-
- // log( task.getClass().getName(), Project.MSG_INFO ) ;
-
- if( task instanceof ExcelAntSet ) {
- ExcelAntSet set = (ExcelAntSet) task ;
- set.setWorkbookUtil(workbookUtil) ;
- set.execute() ;
- }
-
- if( task instanceof ExcelAntHandlerTask ) {
- ExcelAntHandlerTask handler = (ExcelAntHandlerTask)task ;
- handler.setEAWorkbookUtil(workbookUtil ) ;
- handler.execute() ;
- }
-
- if (task instanceof ExcelAntEvaluateCell ) {
- ExcelAntEvaluateCell eval = (ExcelAntEvaluateCell)task ;
- eval.setWorkbookUtil( workbookUtil ) ;
-
- if( globalPrecision > 0 ) {
- log( "setting globalPrecision to " + globalPrecision + " in the evaluator", Project.MSG_VERBOSE ) ;
- eval.setGlobalPrecision( globalPrecision ) ;
- }
-
- try {
- eval.execute() ;
- ExcelAntEvaluationResult result = eval.getResult() ;
- if( result.didTestPass() &&
- result.evaluationCompleteWithError() == false ) {
- if( showSuccessDetails == true ) {
- log("Succeeded when evaluating " +
- result.getCellName() + ". It evaluated to " +
- result.getReturnValue() + " when the value of " +
- eval.getExpectedValue() + " with precision of " +
- eval.getPrecision(), Project.MSG_INFO ) ;
- }
- } else {
- if( showFailureDetail == true ) {
- failureMessages.add( "\tFailed to evaluate cell " +
- result.getCellName() + ". It evaluated to " +
- result.getReturnValue() + " when the value of " +
- eval.getExpectedValue() + " with precision of " +
- eval.getPrecision() + " was expected." ) ;
-
- }
- passed = false ;
- failureCount++ ;
-
- if( eval.requiredToPass() == true ) {
- throw new BuildException( "\tFailed to evaluate cell " +
- result.getCellName() + ". It evaluated to " +
- result.getReturnValue() + " when the value of " +
- eval.getExpectedValue() + " with precision of " +
- eval.getPrecision() + " was expected." ) ;
- }
- }
- } catch( NullPointerException npe ) {
- // this means the cell reference in the test is bad.
- log( "Cell assignment " + eval.getCell() + " in test " + getName() +
- " appears to point to an empy cell. Please check the " +
- " reference in the ant script.", Project.MSG_ERR ) ;
- }
- }
- }
-
- if( passed == false ) {
- log( "Test named " + name + " failed because " + failureCount +
- " of " + testCount + " evaluations failed to " +
- "evaluate correctly.",
- Project.MSG_ERR ) ;
- if( showFailureDetail == true && failureMessages.size() > 0 ) {
- Iterator<String> failures = failureMessages.iterator() ;
- while( failures.hasNext() ) {
- log( failures.next(), Project.MSG_ERR ) ;
- }
- }
- }
- }
-
- public boolean didTestPass() {
-
- return passed ;
- }
- }
+++ /dev/null
-/* ====================================================================
- 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.ss.excelant;
-
-import org.apache.tools.ant.taskdefs.Typedef;
-
-/**
- * This class encapsulates the Strings necessary to create the User Defined
- * Function instances that will be passed to POI's Evaluator instance.
- *
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
- *
- */
-public class ExcelAntUserDefinedFunction extends Typedef {
-
-
- public String functionAlias ;
-
- public String className ;
-
-
- public ExcelAntUserDefinedFunction() {}
-
- protected String getFunctionAlias() {
- return functionAlias;
- }
-
- public void setFunctionAlias(String functionAlias) {
- this.functionAlias = functionAlias;
- }
-
- protected String getClassName() {
- return className;
- }
-
- public void setClassName(String className) {
- this.className = className;
- }
-}
+++ /dev/null
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.ss.excelant;\r
-\r
-import org.apache.poi.ss.usermodel.Workbook;\r
-\r
-\r
-/**\r
- * In Excel there are many ways to handle manipulating a workbook based \r
- * on some arbitrary user action (onChange, etc). You use this interface\r
- * to create classes that will handle the workbook in whatever manner is needed\r
- * that cannot be handled by POI.\r
- * <p>\r
- * For example, suppose that in Excel when you update a cell the workbook\r
- * does some calculations and updates other cells based on that change. In\r
- * ExcelAnt you would set the value of the cell then write your own handler\r
- * then call that from your Ant task after the set task.\r
- * \r
- * @author Jon Svede ( jon [at] loquatic [dot] com )\r
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )\r
- *\r
- */\r
-public interface IExcelAntWorkbookHandler {\r
- \r
- \r
- public void setWorkbook( Workbook workbook ) ;\r
- \r
- public void execute() ;\r
- \r
-\r
-}\r
+++ /dev/null
-/* ====================================================================
- 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.ss.excelant.util;
-
-/**
- * A simple class that encapsulates information about a cell evaluation
- * from POI.
- *
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
- *
- */
-public class ExcelAntEvaluationResult {
-
- /**
- * This boolean flag is used to determine if the evaluation completed
- * without error. This alone doesn't ensure that the evaluation was
- * sucessful.
- */
- private boolean evaluationCompletedWithError ;
-
- /**
- * This boolean flag is used to determine if the result was within
- * the specified precision.
- */
- private boolean didPass ;
-
- /**
- * This is the actual value returned from the evaluation.
- */
- private double returnValue ;
-
- /**
- * Any error message String values that need to be returned.
- */
- private String errorMessage ;
-
- /**
- * Stores the absolute value of the delta for this evaluation.
- */
- private double actualDelta ;
-
- /**
- * This stores the fully qualified cell name (sheetName!cellId).
- */
- private String cellName ;
-
-
-
- public ExcelAntEvaluationResult( boolean completedWithError,
- boolean passed,
- double retValue,
- String errMessage,
- double delta,
- String cellId ) {
-
- evaluationCompletedWithError = completedWithError;
- didPass = passed;
- returnValue = retValue;
- errorMessage = errMessage;
- actualDelta = delta ;
- cellName = cellId ;
- }
-
- public double getReturnValue() {
- return returnValue;
- }
-
- public String getErrorMessage() {
- return errorMessage;
- }
-
- public boolean didTestPass() {
- return didPass ;
- }
-
- public boolean evaluationCompleteWithError() {
- return evaluationCompletedWithError ;
- }
-
- public double getDelta() {
- return actualDelta ;
- }
-
- public String getCellName() {
- return cellName ;
- }
-
- @Override
- public String toString() {
- return "ExcelAntEvaluationResult [evaluationCompletedWithError="
- + evaluationCompletedWithError + ", didPass=" + didPass
- + ", returnValue=" + returnValue + ", errorMessage="
- + errorMessage + ", actualDelta=" + actualDelta + ", cellName="
- + cellName + "]";
- }
-
-
-}
+++ /dev/null
-/* ====================================================================
- 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.ss.excelant.util;
-
-import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.ss.formula.functions.FreeRefFunction;
-import org.apache.poi.ss.formula.udf.AggregatingUDFFinder;
-import org.apache.poi.ss.formula.udf.DefaultUDFFinder;
-import org.apache.poi.ss.formula.udf.UDFFinder;
-import org.apache.poi.ss.usermodel.*;
-import org.apache.poi.ss.util.CellReference;
-import org.apache.poi.xssf.usermodel.XSSFFormulaEvaluator;
-import org.apache.poi.xssf.usermodel.XSSFWorkbook;
-import org.apache.tools.ant.BuildException;
-import org.apache.tools.ant.Project;
-import org.apache.tools.ant.taskdefs.Typedef;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.Iterator;
-
-/**
- * A general utility class that abstracts the POI details of loading the
- * workbook, accessing and updating cells.
- *
- * @author Jon Svede ( jon [at] loquatic [dot] com )
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )
- *
- */
-public class ExcelAntWorkbookUtil extends Typedef {
-
- private String excelFileName;
-
- private Workbook workbook;
-
- private HashMap<String, FreeRefFunction> xlsMacroList;
-
- /**
- * Constructs an instance using a String that contains the fully qualified
- * path of the Excel file. This constructor initializes a Workbook instance
- * based on that file name.
- *
- * @param fName
- */
- protected ExcelAntWorkbookUtil(String fName) {
- excelFileName = fName;
- xlsMacroList = new HashMap<String, FreeRefFunction>() ;
- loadWorkbook();
-
- }
-
- /**
- * Constructs an instance based on a Workbook instance.
- *
- * @param wb
- */
- protected ExcelAntWorkbookUtil(Workbook wb) {
- workbook = wb;
- xlsMacroList = new HashMap<String, FreeRefFunction>() ;
- }
-
- /**
- * Loads the member variable workbook based on the fileName variable.
- * @return
- */
- private Workbook loadWorkbook() {
-
- File workbookFile = new File(excelFileName);
- try {
- FileInputStream fis = new FileInputStream(workbookFile);
- try {
- workbook = WorkbookFactory.create(fis);
- } finally {
- fis.close();
- }
- } catch(Exception e) {
- throw new BuildException("Cannot load file " + excelFileName
- + ". Make sure the path and file permissions are correct.", e);
- }
-
- return workbook ;
- }
-
- /**
- * Used to add a UDF to the evaluator.
- * @param name
- * @param clazzName
- * @throws ClassNotFoundException
- * @throws InstantiationException
- * @throws IllegalAccessException
- */
- public void addFunction( String name, String clazzName ) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
- Class<?> clazzInst = Class.forName( clazzName ) ;
- Object newInst = clazzInst.newInstance() ;
- if( newInst instanceof FreeRefFunction ) {
- addFunction( name, (FreeRefFunction)newInst ) ;
- }
-
- }
-
- /**
- * Updates the internal HashMap of functions with instance and alias passed
- * in.
- *
- * @param name
- * @param func
- */
- protected void addFunction(String name, FreeRefFunction func) {
- xlsMacroList.put(name, func);
- }
-
- /**
- * returns a UDFFinder that contains all of the functions added.
- *
- * @return
- */
- protected UDFFinder getFunctions() {
-
- String[] names = new String[xlsMacroList.size()];
- FreeRefFunction[] functions = new FreeRefFunction[xlsMacroList.size()];
-
- Iterator<String> keysIt = xlsMacroList.keySet().iterator();
- int x = 0;
- while (keysIt.hasNext()) {
- String name = keysIt.next();
- FreeRefFunction function = xlsMacroList.get(name);
- names[x] = name;
- functions[x] = function;
- }
-
- UDFFinder udff1 = new DefaultUDFFinder(names, functions);
- UDFFinder udff = new AggregatingUDFFinder(udff1);
-
- return udff;
-
- }
-
- /**
- * Returns a formula evaluator that is loaded with the functions that
- * have been supplied.
- *
- * @param fileName
- * @return
- */
- protected FormulaEvaluator getEvaluator( String fileName ) {
- FormulaEvaluator evaluator ;
- if (fileName.endsWith(".xlsx")) {
- if( xlsMacroList != null && xlsMacroList.size() > 0 ) {
- evaluator = XSSFFormulaEvaluator.create( (XSSFWorkbook) workbook,
- null,
- getFunctions() ) ;
- }
- evaluator = new XSSFFormulaEvaluator((XSSFWorkbook) workbook);
- } else {
- if( xlsMacroList != null && xlsMacroList.size() > 0 ) {
- evaluator = HSSFFormulaEvaluator.create( (HSSFWorkbook)workbook,
- null,
- getFunctions() ) ;
- }
-
- evaluator = new HSSFFormulaEvaluator((HSSFWorkbook) workbook);
- }
-
- return evaluator ;
-
- }
-
- /**
- * Returns the Workbook instance associated with this WorkbookUtil.
- *
- * @return
- */
- public Workbook getWorkbook() {
- return workbook;
- }
-
- /**
- * Returns the fileName that was used to initialize this instance. May
- * return null if the instance was constructed from a Workbook object.
- *
- * @return
- */
- public String getFileName() {
- return excelFileName;
- }
-
- /**
- * Returns the list of sheet names.
- *
- * @return
- */
- public ArrayList<String> getSheets() {
- ArrayList<String> sheets = new ArrayList<String>() ;
-
- int sheetCount = workbook.getNumberOfSheets() ;
-
- for( int x=0; x<sheetCount; x++ ) {
- sheets.add( workbook.getSheetName( x ) ) ;
- }
-
- return sheets ;
- }
-
- /**
- * This method uses a String in standard Excel format (SheetName!CellId) to
- * locate the cell and set it to the value of the double in value.
- *
- * @param cellName
- * @param value
- */
- public void setDoubleValue(String cellName, double value) {
- log("starting setCellValue()", Project.MSG_DEBUG);
- Cell cell = getCell(cellName);
- log("working on cell: " + cell, Project.MSG_DEBUG);
- cell.setCellValue(value);
- log("after cell.setCellValue()", Project.MSG_DEBUG);
-
- log("set cell " + cellName + " to value " + value, Project.MSG_DEBUG);
- }
-
- /**
- * Utility method for setting the value of a Cell with a String.
- *
- * @param cellName
- * @param value
- */
- public void setStringValue( String cellName, String value ) {
- Cell cell = getCell(cellName);
- cell.setCellValue(value);
- }
-
- /**
- * Utility method for setting the value of a Cell with a Formula.
- *
- * @param cellName
- * @param formula
- */
- public void setFormulaValue( String cellName, String formula ) {
- Cell cell = getCell(cellName);
- cell.setCellFormula( formula );
- }
-
- /**
- * Utility method for setting the value of a Cell with a Date.
- * @param cellName
- * @param date
- */
- public void setDateValue( String cellName, Date date ) {
- Cell cell = getCell(cellName);
- cell.setCellValue( date ) ;
- }
- /**
- * Uses a String in standard Excel format (SheetName!CellId) to locate a
- * cell and evaluate it.
- *
- * @param cellName
- * @param expectedValue
- * @param precision
- */
- public ExcelAntEvaluationResult evaluateCell(String cellName, double expectedValue,
- double precision) {
-
- ExcelAntEvaluationResult evalResults = null;
-
- Cell cell = getCell(cellName);
-
- FormulaEvaluator evaluator = getEvaluator( excelFileName );
-
-
- CellValue resultOfEval = evaluator.evaluate(cell);
-
- if (resultOfEval.getErrorValue() == 0) {
- // the evaluation did not encounter errors
- double result = resultOfEval.getNumberValue();
- double delta = Math.abs(result - expectedValue);
- if (delta > precision) {
- evalResults = new ExcelAntEvaluationResult(false, false,
- resultOfEval.getNumberValue(),
- "Results was out of range based on precision " + " of "
- + precision + ". Delta was actually " + delta, delta, cellName );
- } else {
- evalResults = new ExcelAntEvaluationResult(false, true,
- resultOfEval.getNumberValue(),
- "Evaluation passed without error within in range.", delta, cellName );
- }
- } else {
- String errorMeaning = null ;
- try {
- errorMeaning = ErrorConstants.getText( resultOfEval
- .getErrorValue() ) ;
- } catch( IllegalArgumentException iae ) {
- errorMeaning = "unknown error code: " +
- Byte.toString( resultOfEval.getErrorValue() ) ;
- }
-
- evalResults = new ExcelAntEvaluationResult(false, false,
- resultOfEval.getNumberValue(),
- "Evaluation failed due to an evaluation error of "
- + resultOfEval.getErrorValue()
- + " which is "
- + errorMeaning, 0, cellName );
- }
-
- return evalResults;
- }
-
- /**
- * Returns a Cell as a String value.
- *
- * @param cellName
- * @return
- */
- public String getCellAsString( String cellName ) {
- Cell cell = getCell( cellName ) ;
- if( cell != null ) {
- return cell.getStringCellValue() ;
- }
- return "" ;
- }
-
-
- /**
- * Returns the value of the Cell as a double.
- *
- * @param cellName
- * @return
- */
- public double getCellAsDouble( String cellName ) {
- Cell cell = getCell( cellName ) ;
- if( cell != null ) {
- return cell.getNumericCellValue() ;
- }
- return 0.0 ;
- }
- /**
- * Returns a cell reference based on a String in standard Excel format
- * (SheetName!CellId). This method will create a new cell if the
- * requested cell isn't initialized yet.
- *
- * @param cellName
- * @return
- */
- private Cell getCell(String cellName) {
-
- CellReference cellRef = new CellReference(cellName);
- String sheetName = cellRef.getSheetName();
- Sheet sheet = workbook.getSheet(sheetName);
- if(sheet == null) {
- throw new BuildException("Sheet not found: " + sheetName);
- }
-
- int rowIdx = cellRef.getRow();
- int colIdx = cellRef.getCol();
- Row row = sheet.getRow(rowIdx);
-
- if( row == null ) {
- row = sheet.createRow( rowIdx ) ;
- }
-
- Cell cell = row.getCell(colIdx);
-
- if( cell == null ) {
- cell = row.createCell( colIdx ) ;
- }
-
- return cell;
- }
-
-}
+++ /dev/null
-/* ====================================================================\r
- Licensed to the Apache Software Foundation (ASF) under one or more\r
- contributor license agreements. See the NOTICE file distributed with\r
- this work for additional information regarding copyright ownership.\r
- The ASF licenses this file to You under the Apache License, Version 2.0\r
- (the "License"); you may not use this file except in compliance with\r
- the License. You may obtain a copy of the License at\r
-\r
- http://www.apache.org/licenses/LICENSE-2.0\r
-\r
- Unless required by applicable law or agreed to in writing, software\r
- distributed under the License is distributed on an "AS IS" BASIS,\r
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
- See the License for the specific language governing permissions and\r
- limitations under the License.\r
-==================================================================== */\r
-\r
-package org.apache.poi.ss.excelant.util;\r
-\r
-import java.util.HashMap;\r
-\r
-\r
-/**\r
- * This is a factory class maps file names to WorkbookUtil instances. This\r
- * helps ExcelAnt be more efficient when being run many times in an Ant build.\r
- *\r
- * @author Jon Svede ( jon [at] loquatic [dot] com )\r
- * @author Brian Bush ( brian [dot] bush [at] nrel [dot] gov )\r
- *\r
- */\r
-public class ExcelAntWorkbookUtilFactory {\r
-\r
- private static HashMap<String, ExcelAntWorkbookUtil> workbookUtilMap ;\r
-\r
- private static ExcelAntWorkbookUtilFactory factory ;\r
-\r
- private ExcelAntWorkbookUtilFactory() {\r
- workbookUtilMap = new HashMap<String, ExcelAntWorkbookUtil>() ;\r
- }\r
-\r
- /**\r
- * Using the fileName, check the internal map to see if an instance\r
- * of the WorkbookUtil exists. If not, then add an instance to the map.\r
- *\r
- * @param fileName\r
- * @return\r
- */\r
- public static ExcelAntWorkbookUtil getInstance( String fileName ) {\r
-\r
- if( factory == null ) {\r
- factory = new ExcelAntWorkbookUtilFactory() ;\r
- }\r
- if( workbookUtilMap != null &&\r
- workbookUtilMap.containsKey( fileName ) ) {\r
- return workbookUtilMap.get( fileName ) ;\r
- }\r
-\r
- ExcelAntWorkbookUtil wbu = new ExcelAntWorkbookUtil( fileName ) ;\r
- workbookUtilMap.put( fileName, wbu ) ;\r
- return wbu ;\r
- }\r
-\r
-}\r