View Javadoc
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  
22  package org.dbunit.dataset.common.handlers;
23  
24  import java.util.ArrayList;
25  import java.util.LinkedList;
26  import java.util.List;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  
31  /**
32   * @author fede
33   * @author Last changed by: $Author$
34   * @version $Revision$ $Date$
35   * @since 2.2 (Sep 12, 2004)
36   */
37  public class Pipeline implements Handler {
38  
39      /**
40       * Logger for this class
41       */
42      private static final Logger logger = LoggerFactory.getLogger(Pipeline.class);
43  
44  
45      private LinkedList components;
46      private List products;
47      private StringBuffer currentProduct;
48      private PipelineComponent noHandler;
49  
50      public Pipeline() {
51          setComponents(new LinkedList());
52          setProducts(new ArrayList());
53  
54  
55          // add a no handler as the last handler
56          setNoHandler(NoHandler.IGNORE());
57          getNoHandler().setSuccessor(null);
58          getComponents().addFirst(getNoHandler());
59  
60          // add a transparent handler as placeholder
61          //getComponents().addFirst(TransparentHandler.IGNORE);
62  
63          //prepareNewPiece();
64          setCurrentProduct(new StringBuffer());
65          putFront(TransparentHandler.IGNORE());
66      }
67  
68      public StringBuffer getCurrentProduct() {
69          logger.debug("getCurrentProduct() - start");
70  
71          return currentProduct;
72      }
73  
74      public void setCurrentProduct(StringBuffer currentProduct) {
75          logger.debug("setCurrentProduct(currentProduct={}) - start", currentProduct);
76  
77          this.currentProduct = currentProduct;
78      }
79  
80      private void prepareNewPiece() {
81          logger.debug("prepareNewPiece() - start");
82  
83          setCurrentProduct(new StringBuffer());
84  
85          // remove all the components down to a TrasparentHandler
86          try {
87              while (!(getComponents().getFirst() instanceof TransparentHandler)) {
88                  removeFront();
89              }
90          } catch (PipelineException e) {
91              throw new RuntimeException(e.getMessage());
92          }
93  
94      }
95  
96      public void thePieceIsDone() {
97          logger.debug("thePieceIsDone() - start");
98  
99          getProducts().add(getCurrentProduct().toString());
100         prepareNewPiece();
101     }
102 
103     public List getProducts() {
104         logger.debug("getProducts() - start");
105 
106         return products;
107     }
108 
109     protected void setProducts(List products) {
110         logger.debug("setProducts(products={}) - start", products);
111 
112         this.products = products;
113     }
114 
115     private LinkedList getComponents() {
116         logger.debug("getComponents() - start");
117 
118         return components;
119     }
120 
121     private void setComponents(LinkedList components) {
122         logger.debug("setComponents(components={}) - start", components);
123 
124         this.components = components;
125     }
126 
127     public void putFront(PipelineComponent component) {
128         logger.debug("putFront(component={}) - start", component);
129 
130         component.setSuccessor((PipelineComponent) getComponents().getFirst());
131         component.setPipeline(this);
132         getComponents().addFirst(component);
133     }
134 
135     public PipelineComponent removeFront() throws PipelineException {
136         logger.debug("removeFront() - start");
137 
138         PipelineComponent first = (PipelineComponent) getComponents().getFirst();
139         remove(first);
140         return first;
141     }
142 
143     public void remove(PipelineComponent component) throws PipelineException {
144         logger.debug("remove(component={}) - start", component);
145 
146         if (component == getNoHandler()) {
147             throw new PipelineException("Cannot remove the last handler");
148         }
149 
150         if (!getComponents().remove(component)) {
151             throw new PipelineException("Cannot remove a non existent component from a pipeline");
152         }
153     }
154 
155     public boolean canHandle(char c) throws IllegalInputCharacterException {
156         if(logger.isDebugEnabled())
157             logger.debug("canHandle(c={}) - start", String.valueOf(c));
158 
159         return true;
160     }
161 
162     public void handle(char c) throws IllegalInputCharacterException, PipelineException {
163         if(logger.isDebugEnabled())
164             logger.debug("handle(c={}) - start", String.valueOf(c));
165 
166         ((Handler) getComponents().getFirst()).handle(c);
167     }
168 
169     public boolean allowForNoMoreInput() {
170         logger.debug("allowForNoMoreInput() - start");
171 
172         throw new IllegalStateException("you cannot call Pipeline.allowForNoMoreInput");
173     }
174 
175     private PipelineComponent getNoHandler() {
176         logger.debug("getNoHandler() - start");
177 
178         return noHandler;
179     }
180 
181     private void setNoHandler(PipelineComponent noHandler) {
182         logger.debug("setNoHandler(noHandler={}) - start", noHandler);
183 
184         this.noHandler = noHandler;
185     }
186 
187     public void resetProducts() {
188         logger.debug("resetProducts() - start");
189 
190         setProducts(new ArrayList());
191     }
192 
193     public void noMoreInput() {
194         logger.debug("noMoreInput() - start");
195 
196         ((Handler) getComponents().getFirst()).noMoreInput();
197         //thePieceIsDone();
198     }
199 
200     
201     private PipelineConfig pipelineConfig = new PipelineConfig();
202     public PipelineConfig getPipelineConfig() {
203         return pipelineConfig;
204     }
205 
206     public void setPipelineConfig(PipelineConfig pipelineConfig) {
207         this.pipelineConfig = pipelineConfig;
208     }
209 
210 }