You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ExcelAntHandlerTask.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.ss.excelant;
  16. import org.apache.poi.ss.excelant.util.ExcelAntWorkbookUtil;
  17. import org.apache.tools.ant.BuildException;
  18. import org.apache.tools.ant.Project;
  19. import org.apache.tools.ant.Task;
  20. /**
  21. * This is the class that backs the {@code <handler>} tag in the Ant task.
  22. * <p>
  23. * Its purpose is to provide a way to manipulate a workbook in the course
  24. * of an ExcelAnt task. The idea being to model a way for test writers to
  25. * simulate the behaviors of the workbook.
  26. * <p>
  27. * Suppose, for example, you have a workbook that has a worksheet that
  28. * reacts to values entered or selected by the user. It's possible in
  29. * Excel to change other cells based on this but this isn't easily possible
  30. * in POI. In ExcelAnt we handle this using the Handler, which is a Java
  31. * class you write to manipulate the workbook.
  32. * <p>
  33. * In order to use this tag you must write a class that implements the
  34. * {@code IExcelAntWorkbookHandler} interface. After writing the
  35. * class you should package it and it's dependencies into a jar file to
  36. * add as library in your Ant build file.
  37. */
  38. public class ExcelAntHandlerTask extends Task {
  39. private String className ;
  40. private ExcelAntWorkbookUtil wbUtil ;
  41. public void setClassName( String cName ) {
  42. className = cName ;
  43. }
  44. protected void setEAWorkbookUtil( ExcelAntWorkbookUtil wkbkUtil ) {
  45. wbUtil = wkbkUtil ;
  46. }
  47. @Override
  48. public void execute() throws BuildException {
  49. log( "handling the workbook with class " + className, Project.MSG_INFO ) ;
  50. try {
  51. Class<?> clazz = Class.forName( className ) ;
  52. Object handlerObj = clazz.getDeclaredConstructor().newInstance() ;
  53. if( handlerObj instanceof IExcelAntWorkbookHandler ) {
  54. IExcelAntWorkbookHandler iHandler = (IExcelAntWorkbookHandler)handlerObj ;
  55. iHandler.setWorkbook( wbUtil.getWorkbook() ) ;
  56. iHandler.execute() ;
  57. }
  58. } catch( Exception e ) {
  59. throw new BuildException( e.getMessage(), e ) ;
  60. }
  61. }
  62. }