aboutsummaryrefslogtreecommitdiffstats
path: root/poi-excelant/src/main/java/org/apache/poi/ss/excelant/ExcelAntTask.java
blob: 4fc0b099148ef4442d40b595c1e45f595993b4a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/* ====================================================================
   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.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.Locale;

import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;
import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtilFactory;
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.
 */
public class ExcelAntTask extends Task {

    public static final String VERSION = "0.5.0" ;

    private String excelFileName ;

    private boolean failOnError;

    private ExcelAntWorkbookUtil workbookUtil ;

    private ExcelAntPrecision precision ;

    private LinkedList<ExcelAntTest> tests ;
    private LinkedList<ExcelAntUserDefinedFunction> functions ;

    public ExcelAntTask() {
        tests = new LinkedList<>() ;
        functions = new LinkedList<>() ;
    }

    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 ) ;
    }

    @Override
    public void execute() throws BuildException {
        checkClassPath();

        int totalCount = 0 ;
        int successCount = 0 ;

        StringBuilder versionBffr = new StringBuilder() ;
        versionBffr.append(  "ExcelAnt version " ) ;
        versionBffr.append( VERSION ) ;
        versionBffr.append( " Copyright 2011" ) ;
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy", Locale.ROOT ) ;
        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 ) ;

        workbookUtil = ExcelAntWorkbookUtilFactory.getInstance(excelFileName);

        for (ExcelAntTest test : tests) {
            log("executing test: " + test.getName(), Project.MSG_DEBUG);

            if (workbookUtil == null) {
                workbookUtil = ExcelAntWorkbookUtilFactory.getInstance(excelFileName);
            }

            for (ExcelAntUserDefinedFunction eaUdf : functions) {
                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) {
                    throw new BuildException("Test " + test.getName() + " failed.");
                }
            }
            totalCount++;

            workbookUtil = null;
        }

        if( !tests.isEmpty() ) {
            log( successCount + "/" + totalCount + " tests passed.", Project.MSG_INFO );
        }
        workbookUtil = null;
    }


    /**
     * 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 (Exception 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-lite.jar, xmlbeans.jar" ,
                    e, getLocation());
        }

    }
}