001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.dataset.csv.handlers;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.dataset.csv.IllegalInputCharacterException;
028:
029: import java.util.ArrayList;
030: import java.util.LinkedList;
031: import java.util.List;
032:
033: public class Pipeline implements Handler {
034:
035: /**
036: * Logger for this class
037: */
038: private static final Logger logger = LoggerFactory
039: .getLogger(Pipeline.class);
040:
041: private LinkedList components;
042: private List products;
043: private StringBuffer currentProduct;
044: private PipelineComponent noHandler;
045:
046: public Pipeline() {
047: setComponents(new LinkedList());
048: setProducts(new ArrayList());
049:
050: // add a no handler as the last handler
051: setNoHandler(NoHandler.IGNORE());
052: getNoHandler().setSuccessor(null);
053: getComponents().addFirst(getNoHandler());
054:
055: // add a trasparent handler as placeholder
056: //getComponents().addFirst(TransparentHandler.IGNORE);
057:
058: //prepareNewPiece();
059: setCurrentProduct(new StringBuffer());
060: putFront(TransparentHandler.IGNORE());
061: }
062:
063: public StringBuffer getCurrentProduct() {
064: logger.debug("getCurrentProduct() - start");
065:
066: return currentProduct;
067: }
068:
069: public void setCurrentProduct(StringBuffer currentProduct) {
070: logger.debug("setCurrentProduct(currentProduct="
071: + currentProduct + ") - start");
072:
073: this .currentProduct = currentProduct;
074: }
075:
076: private void prepareNewPiece() {
077: logger.debug("prepareNewPiece() - start");
078:
079: setCurrentProduct(new StringBuffer());
080:
081: // remove all the components down to a TrasparentHandler
082: try {
083: while (!(getComponents().getFirst() instanceof TransparentHandler)) {
084: removeFront();
085: }
086: } catch (PipelineException e) {
087: logger.error("prepareNewPiece()", e);
088:
089: throw new RuntimeException(e.getMessage());
090: }
091:
092: }
093:
094: public void thePieceIsDone() {
095: logger.debug("thePieceIsDone() - start");
096:
097: getProducts().add(getCurrentProduct().toString());
098: prepareNewPiece();
099: }
100:
101: public List getProducts() {
102: logger.debug("getProducts() - start");
103:
104: return products;
105: }
106:
107: protected void setProducts(List products) {
108: logger.debug("setProducts(products=" + products + ") - start");
109:
110: this .products = products;
111: }
112:
113: private LinkedList getComponents() {
114: logger.debug("getComponents() - start");
115:
116: return components;
117: }
118:
119: private void setComponents(LinkedList components) {
120: logger.debug("setComponents(components=" + components
121: + ") - start");
122:
123: this .components = components;
124: }
125:
126: public void putFront(PipelineComponent component) {
127: logger.debug("putFront(component=" + component + ") - start");
128:
129: component.setSuccessor((PipelineComponent) getComponents()
130: .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()
139: .getFirst();
140: remove(first);
141: return first;
142: }
143:
144: public void remove(PipelineComponent component)
145: throws PipelineException {
146: logger.debug("remove(component=" + component + ") - start");
147:
148: if (component == getNoHandler()) {
149: throw new PipelineException(
150: "Cannot remove the last handler");
151: }
152:
153: if (!getComponents().remove(component)) {
154: throw new PipelineException(
155: "Cannot remove a non existent component from a pipeline");
156: }
157: }
158:
159: public boolean canHandle(char c)
160: throws IllegalInputCharacterException {
161: logger.debug("canHandle(c=" + c + ") - start");
162:
163: return true;
164: }
165:
166: public void handle(char c) throws IllegalInputCharacterException,
167: PipelineException {
168: logger.debug("handle(c=" + c + ") - start");
169:
170: ((Handler) getComponents().getFirst()).handle(c);
171: }
172:
173: public boolean allowForNoMoreInput() {
174: logger.debug("allowForNoMoreInput() - start");
175:
176: throw new IllegalStateException(
177: "you cannot call Pipeline.allowForNoMoreInput");
178: }
179:
180: private PipelineComponent getNoHandler() {
181: logger.debug("getNoHandler() - start");
182:
183: return noHandler;
184: }
185:
186: private void setNoHandler(PipelineComponent noHandler) {
187: logger.debug("setNoHandler(noHandler=" + noHandler
188: + ") - start");
189:
190: this .noHandler = noHandler;
191: }
192:
193: public void resetProducts() {
194: logger.debug("resetProducts() - start");
195:
196: setProducts(new ArrayList());
197: }
198:
199: public void noMoreInput() {
200: logger.debug("noMoreInput() - start");
201:
202: ((Handler) getComponents().getFirst()).noMoreInput();
203: //thePieceIsDone();
204: }
205:
206: }
|