Pipeline.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 java.util.ArrayList;
  23. import java.util.LinkedList;
  24. import java.util.List;

  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;

  27. /**
  28.  * @author fede
  29.  * @author Last changed by: $Author$
  30.  * @version $Revision$ $Date$
  31.  * @since 2.2 (Sep 12, 2004)
  32.  */
  33. public class Pipeline implements Handler {

  34.     /**
  35.      * Logger for this class
  36.      */
  37.     private static final Logger logger = LoggerFactory.getLogger(Pipeline.class);


  38.     private LinkedList components;
  39.     private List products;
  40.     private StringBuilder currentProduct;
  41.     private PipelineComponent noHandler;
  42.     private PipelineConfig pipelineConfig = new PipelineConfig();

  43.     public Pipeline() {
  44.         setComponents(new LinkedList());
  45.         setProducts(new ArrayList());


  46.         // add a no handler as the last handler
  47.         setNoHandler(NoHandler.IGNORE());
  48.         getNoHandler().setSuccessor(null);
  49.         getComponents().addFirst(getNoHandler());

  50.         // add a transparent handler as placeholder
  51.         //getComponents().addFirst(TransparentHandler.IGNORE);

  52.         //prepareNewPiece();
  53.         setCurrentProduct(new StringBuilder());
  54.         putFront(TransparentHandler.IGNORE());
  55.     }

  56.     public StringBuilder getCurrentProduct() {
  57.         logger.debug("getCurrentProduct() - start");

  58.         return currentProduct;
  59.     }

  60.     public void setCurrentProduct(StringBuilder currentProduct) {
  61.         logger.debug("setCurrentProduct(currentProduct={}) - start", currentProduct);

  62.         this.currentProduct = currentProduct;
  63.     }

  64.     private void prepareNewPiece() {
  65.         logger.debug("prepareNewPiece() - start");

  66.         setCurrentProduct(new StringBuilder());

  67.         // remove all the components down to a TrasparentHandler
  68.         try {
  69.             while (!(getComponents().getFirst() instanceof TransparentHandler)) {
  70.                 removeFront();
  71.             }
  72.         } catch (PipelineException e) {
  73.             throw new RuntimeException(e.getMessage());
  74.         }

  75.     }

  76.     public void thePieceIsDone() {
  77.         logger.debug("thePieceIsDone() - start");

  78.         getProducts().add(getCurrentProduct().toString());
  79.         prepareNewPiece();
  80.     }

  81.     public List getProducts() {
  82.         logger.debug("getProducts() - start");

  83.         return products;
  84.     }

  85.     protected void setProducts(List products) {
  86.         logger.debug("setProducts(products={}) - start", products);

  87.         this.products = products;
  88.     }

  89.     private LinkedList getComponents() {
  90.         logger.debug("getComponents() - start");

  91.         return components;
  92.     }

  93.     private void setComponents(LinkedList components) {
  94.         logger.debug("setComponents(components={}) - start", components);

  95.         this.components = components;
  96.     }

  97.     public void putFront(PipelineComponent component) {
  98.         logger.debug("putFront(component={}) - start", component);

  99.         component.setSuccessor((PipelineComponent) getComponents().getFirst());
  100.         component.setPipeline(this);
  101.         getComponents().addFirst(component);
  102.     }

  103.     public PipelineComponent removeFront() throws PipelineException {
  104.         logger.debug("removeFront() - start");

  105.         PipelineComponent first = (PipelineComponent) getComponents().getFirst();
  106.         remove(first);
  107.         return first;
  108.     }

  109.     public void remove(PipelineComponent component) throws PipelineException {
  110.         logger.debug("remove(component={}) - start", component);

  111.         if (component == getNoHandler()) {
  112.             throw new PipelineException("Cannot remove the last handler");
  113.         }

  114.         if (!getComponents().remove(component)) {
  115.             throw new PipelineException("Cannot remove a non existent component from a pipeline");
  116.         }
  117.     }

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

  121.         return true;
  122.     }

  123.     public void handle(char c) throws IllegalInputCharacterException, PipelineException {
  124.         if(logger.isDebugEnabled())
  125.             logger.debug("handle(c={}) - start", String.valueOf(c));

  126.         ((Handler) getComponents().getFirst()).handle(c);
  127.     }

  128.     public boolean allowForNoMoreInput() {
  129.         logger.debug("allowForNoMoreInput() - start");

  130.         throw new IllegalStateException("you cannot call Pipeline.allowForNoMoreInput");
  131.     }

  132.     private PipelineComponent getNoHandler() {
  133.         logger.debug("getNoHandler() - start");

  134.         return noHandler;
  135.     }

  136.     private void setNoHandler(PipelineComponent noHandler) {
  137.         logger.debug("setNoHandler(noHandler={}) - start", noHandler);

  138.         this.noHandler = noHandler;
  139.     }

  140.     public void resetProducts() {
  141.         logger.debug("resetProducts() - start");

  142.         setProducts(new ArrayList());
  143.     }

  144.     public void noMoreInput() {
  145.         logger.debug("noMoreInput() - start");

  146.         ((Handler) getComponents().getFirst()).noMoreInput();
  147.         //thePieceIsDone();
  148.     }

  149.     public PipelineConfig getPipelineConfig() {
  150.         return pipelineConfig;
  151.     }

  152.     public void setPipelineConfig(PipelineConfig pipelineConfig) {
  153.         this.pipelineConfig = pipelineConfig;
  154.     }

  155. }