KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > csv > handlers > Pipeline


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.csv.handlers;
23
24 import org.dbunit.dataset.csv.IllegalInputCharacterException;
25
26 import java.util.ArrayList JavaDoc;
27 import java.util.LinkedList JavaDoc;
28 import java.util.List JavaDoc;
29
30 public class Pipeline implements Handler {
31
32
33     private LinkedList JavaDoc components;
34     private List JavaDoc products;
35     private StringBuffer JavaDoc currentProduct;
36     private PipelineComponent noHandler;
37
38     public Pipeline() {
39         setComponents(new LinkedList JavaDoc());
40         setProducts(new ArrayList JavaDoc());
41
42
43         // add a no handler as the last handler
44
setNoHandler(NoHandler.IGNORE());
45         getNoHandler().setSuccessor(null);
46         getComponents().addFirst(getNoHandler());
47
48         // add a trasparent handler as placeholder
49
//getComponents().addFirst(TransparentHandler.IGNORE);
50

51         //prepareNewPiece();
52
setCurrentProduct(new StringBuffer JavaDoc());
53         putFront(TransparentHandler.IGNORE());
54     }
55
56     public StringBuffer JavaDoc getCurrentProduct() {
57         return currentProduct;
58     }
59
60     public void setCurrentProduct(StringBuffer JavaDoc currentProduct) {
61         this.currentProduct = currentProduct;
62     }
63
64     private void prepareNewPiece() {
65         setCurrentProduct(new StringBuffer JavaDoc());
66
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 JavaDoc(e.getMessage());
74         }
75
76     }
77
78     public void thePieceIsDone() {
79         getProducts().add(getCurrentProduct().toString());
80         prepareNewPiece();
81     }
82
83     public List JavaDoc getProducts() {
84         return products;
85     }
86
87     protected void setProducts(List JavaDoc products) {
88         this.products = products;
89     }
90
91     private LinkedList JavaDoc getComponents() {
92         return components;
93     }
94
95     private void setComponents(LinkedList JavaDoc components) {
96         this.components = components;
97     }
98
99     public void putFront(PipelineComponent component) {
100         component.setSuccessor((PipelineComponent) getComponents().getFirst());
101         component.setPipeline(this);
102         getComponents().addFirst(component);
103     }
104
105     public PipelineComponent removeFront() throws PipelineException {
106         PipelineComponent first = (PipelineComponent) getComponents().getFirst();
107         remove(first);
108         return first;
109     }
110
111     public void remove(PipelineComponent component) throws PipelineException {
112
113         if (component == getNoHandler()) {
114             throw new PipelineException("Cannot remove the last handler");
115         }
116
117         if (!getComponents().remove(component)) {
118             throw new PipelineException("Cannot remove a non existent component from a pipeline");
119         }
120     }
121
122     public boolean canHandle(char c) throws IllegalInputCharacterException {
123         return true;
124     }
125
126     public void handle(char c) throws IllegalInputCharacterException, PipelineException {
127         ((Handler) getComponents().getFirst()).handle(c);
128     }
129
130     public boolean allowForNoMoreInput() {
131         throw new IllegalStateException JavaDoc("you cannot call Pipeline.allowForNoMoreInput");
132     }
133
134     private PipelineComponent getNoHandler() {
135         return noHandler;
136     }
137
138     private void setNoHandler(PipelineComponent noHandler) {
139         this.noHandler = noHandler;
140     }
141
142     public void resetProducts() {
143         setProducts(new ArrayList JavaDoc());
144     }
145
146     public void noMoreInput() {
147         ((Handler) getComponents().getFirst()).noMoreInput();
148         //thePieceIsDone();
149
}
150
151 }
152
Popular Tags