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