1 21 22 package org.dbunit.dataset.csv.handlers; 23 24 import org.dbunit.dataset.csv.IllegalInputCharacterException; 25 26 import java.util.ArrayList ; 27 import java.util.LinkedList ; 28 import java.util.List ; 29 30 public class Pipeline implements Handler { 31 32 33 private LinkedList components; 34 private List products; 35 private StringBuffer currentProduct; 36 private PipelineComponent noHandler; 37 38 public Pipeline() { 39 setComponents(new LinkedList ()); 40 setProducts(new ArrayList ()); 41 42 43 setNoHandler(NoHandler.IGNORE()); 45 getNoHandler().setSuccessor(null); 46 getComponents().addFirst(getNoHandler()); 47 48 51 setCurrentProduct(new StringBuffer ()); 53 putFront(TransparentHandler.IGNORE()); 54 } 55 56 public StringBuffer getCurrentProduct() { 57 return currentProduct; 58 } 59 60 public void setCurrentProduct(StringBuffer currentProduct) { 61 this.currentProduct = currentProduct; 62 } 63 64 private void prepareNewPiece() { 65 setCurrentProduct(new StringBuffer ()); 66 67 try { 69 while (!(getComponents().getFirst() instanceof TransparentHandler)) { 70 removeFront(); 71 } 72 } catch (PipelineException e) { 73 throw new RuntimeException (e.getMessage()); 74 } 75 76 } 77 78 public void thePieceIsDone() { 79 getProducts().add(getCurrentProduct().toString()); 80 prepareNewPiece(); 81 } 82 83 public List getProducts() { 84 return products; 85 } 86 87 protected void setProducts(List products) { 88 this.products = products; 89 } 90 91 private LinkedList getComponents() { 92 return components; 93 } 94 95 private void setComponents(LinkedList 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 ("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 ()); 144 } 145 146 public void noMoreInput() { 147 ((Handler) getComponents().getFirst()).noMoreInput(); 148 } 150 151 } 152 | Popular Tags |