1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
33
34
35
36
37 public class Pipeline implements Handler {
38
39
40
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
57 setNoHandler(NoHandler.IGNORE());
58 getNoHandler().setSuccessor(null);
59 getComponents().addFirst(getNoHandler());
60
61
62
63
64
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
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
199 }
200
201 public PipelineConfig getPipelineConfig() {
202 return pipelineConfig;
203 }
204
205 public void setPipelineConfig(PipelineConfig pipelineConfig) {
206 this.pipelineConfig = pipelineConfig;
207 }
208
209 }