EnforceHandler.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2004, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */

  21. package org.dbunit.dataset.common.handlers;

  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. /**
  25.  * @author fede
  26.  * @author Last changed by: $Author$
  27.  * @version $Revision$ $Date$
  28.  * @since 2.2 (Sep 12, 2004)
  29.  */
  30. public class EnforceHandler extends AbstractPipelineComponent {

  31.     /**
  32.      * Logger for this class
  33.      */
  34.     private static final Logger logger = LoggerFactory.getLogger(EnforceHandler.class);

  35.     private PipelineComponent [] enforcedComponents;
  36.     private PipelineComponent theHandlerComponent;

  37.     private EnforceHandler(PipelineComponent [] components) {
  38.         setEnforcedComponents(components);
  39.     }


  40.     public static final PipelineComponent ENFORCE(PipelineComponent component) {
  41.         logger.debug("ENFORCE(component={}) - start", component);

  42.         return EnforceHandler.ENFORCE(new PipelineComponent [] {component});
  43.     }

  44.     public static final PipelineComponent ENFORCE(PipelineComponent [] components) {
  45.         logger.debug("ENFORCE(components={}) - start", components);

  46.         return createPipelineComponent(new EnforceHandler(components), new ENFORCE());
  47.     }

  48.     public boolean canHandle(char c) throws IllegalInputCharacterException {
  49.         if(logger.isDebugEnabled())
  50.             logger.debug("canHandle(c={}) - start", String.valueOf(c));

  51.         for (int i = 0; i < getEnforcedComponents().length; i++) {
  52.             if (getEnforcedComponents()[i].canHandle(c)) {
  53.                 setTheHandlerComponent(getEnforcedComponents()[i]);
  54.                 return true;
  55.             }
  56.         }
  57.         throw new IllegalInputCharacterException("(working on piece #" + getPipeline().getProducts().size() + ")"
  58.                 + getPipeline().getCurrentProduct().toString() + ": " + "Character '" + c + "' cannot be handled");
  59.     }

  60.     public void setPipeline(Pipeline pipeline) {
  61.         logger.debug("setPipeline(pipeline={}) - start", pipeline);

  62.         for (int i = 0; i < getEnforcedComponents().length; i++) {
  63.             getEnforcedComponents()[i].setPipeline(pipeline);
  64.         }
  65.         super.setPipeline(pipeline);
  66.     }

  67.     protected PipelineComponent[] getEnforcedComponents() {
  68.         logger.debug("getEnforcedComponents() - start");

  69.         return enforcedComponents;
  70.     }

  71.     protected void setEnforcedComponents(PipelineComponent[] enforcedComponents) {
  72.         logger.debug("setEnforcedComponents(enforcedComponents={}) - start", enforcedComponents);

  73.         this.enforcedComponents = enforcedComponents;
  74.     }

  75.     PipelineComponent getTheHandlerComponent() {
  76.         logger.debug("getTheHandlerComponent() - start");

  77.         return theHandlerComponent;
  78.     }

  79.     void setTheHandlerComponent(PipelineComponent theHandlerComponent) {
  80.         logger.debug("setTheHandlerComponent(theHandlerComponent={}) - start",
  81.                 theHandlerComponent);

  82.         this.theHandlerComponent = theHandlerComponent;
  83.     }

  84.     static private class ENFORCE extends Helper {

  85.         /**
  86.          * Logger for this class
  87.          */
  88.         private static final Logger logger = LoggerFactory.getLogger(ENFORCE.class);

  89.         public void helpWith(char c) {
  90.             if(logger.isDebugEnabled())
  91.                 logger.debug("helpWith(c={}) - start", String.valueOf(c));

  92.             try {
  93.                 EnforceHandler handler = (EnforceHandler) getHandler();
  94.                 handler.getTheHandlerComponent().handle(c);
  95.                 getHandler().getPipeline().removeFront();
  96.             } catch (PipelineException e) {
  97.                 throw new RuntimeException(e.getMessage());
  98.             } catch (IllegalInputCharacterException e) {
  99.                 throw new RuntimeException(e.getMessage());
  100.             }
  101.             // ignore the char
  102.         }
  103.     }
  104. }