AbstractPipelineComponent.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 abstract class AbstractPipelineComponent implements PipelineComponent {

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

  35.     private PipelineComponent successor;
  36.     private Pipeline pipeline;

  37.     private Helper helper;

  38.     protected PipelineComponent getSuccessor() {
  39.         return successor;
  40.     }

  41.     public Pipeline getPipeline() {
  42.         return pipeline;
  43.     }

  44.     public void setPipeline(Pipeline pipeline) {
  45.         logger.debug("setPipeline(pipeline={}) - start", pipeline);
  46.         this.pipeline = pipeline;
  47.     }

  48.     protected PipelineConfig getPipelineConfig() {
  49.         if(this.getPipeline() != null) {
  50.             return this.getPipeline().getPipelineConfig();
  51.         }
  52.         else {
  53.             throw new IllegalStateException("The pipeline is not set for this component. Cannot proceed");
  54.         }
  55.     }

  56.     public void setSuccessor(PipelineComponent successor) {
  57.         logger.debug("setSuccessor(successor={}) - start", successor);
  58.         this.successor = successor;
  59.     }


  60.     private StringBuilder getThePiece() {
  61.         return getPipeline().getCurrentProduct();
  62.     }

  63.     public void handle(char c) throws IllegalInputCharacterException, PipelineException {
  64.         if(logger.isDebugEnabled())
  65.             logger.debug("handle(c={}) - start", c);

  66.         if (!canHandle(c)) {
  67.             getSuccessor().handle(c);
  68.         } else {
  69.             getHelper().helpWith(c);
  70.         }
  71.     }

  72.     public void noMoreInput() {
  73.         logger.debug("noMoreInput() - start");

  74.         if (allowForNoMoreInput()) {
  75.             if (getSuccessor()!= null)
  76.                 getSuccessor().noMoreInput();
  77.         }
  78.     }

  79.     public boolean allowForNoMoreInput() {
  80.         logger.debug("allowForNoMoreInput() - start");

  81.         return getHelper().allowForNoMoreInput();
  82.     }

  83.     protected static PipelineComponent createPipelineComponent(AbstractPipelineComponent handler, Helper helper) {
  84.         logger.debug("createPipelineComponent(handler={}, helper={}) - start", handler, helper);
  85.         helper.setHandler(handler);
  86.         handler.setHelper(helper);
  87.         return handler;
  88.     }

  89.     /**
  90.      * Method invoked when the character should be accepted
  91.      * @param c
  92.      */
  93.     public void accept(char c) {
  94.         getThePiece().append(c);
  95.     }

  96.     protected Helper getHelper() {
  97.         return helper;
  98.     }

  99.     private void setHelper(Helper helper) {
  100.         logger.debug("setHelper(helper={}) - start", helper);
  101.         this.helper = helper;
  102.     }

  103.     static protected class IGNORE extends Helper {
  104.         public void helpWith(char c) {
  105.             // IGNORE
  106.         }
  107.     }

  108.     static protected class ACCEPT extends Helper {
  109.         public void helpWith(char c) {
  110.             if(logger.isDebugEnabled())
  111.                 logger.debug("helpWith(c={}) - start", c);
  112.            
  113.             getHandler().accept(c);
  114.         }
  115.     }
  116. }