1 21 22 package org.dbunit.dataset.csv.handlers; 23 24 import org.dbunit.dataset.csv.IllegalInputCharacterException; 25 26 31 public class EnforceHandler extends AbstractPipelineComponent { 32 33 private PipelineComponent [] enforcedComponents; 34 private PipelineComponent theHandlerComponent; 35 36 private EnforceHandler(PipelineComponent [] components) { 37 setEnforcedComponents(components); 38 } 39 40 41 public static final PipelineComponent ENFORCE(PipelineComponent component) { 42 return EnforceHandler.ENFORCE(new PipelineComponent [] {component}); 43 } 44 45 public static final PipelineComponent ENFORCE(PipelineComponent [] components) { 46 return createPipelineComponent(new EnforceHandler(components), new ENFORCE()); 47 } 48 49 public boolean canHandle(char c) throws IllegalInputCharacterException { 50 for (int i = 0; i < getEnforcedComponents().length; i++) { 51 if (getEnforcedComponents()[i].canHandle(c)) { 52 setTheHandlerComponent(getEnforcedComponents()[i]); 53 return true; 54 } 55 } 56 throw new IllegalInputCharacterException("(working on piece #" + getPipeline().getProducts().size() + ")" 57 + getPipeline().getCurrentProduct().toString() + ": " + "Character '" + c + "' cannot be handled"); 58 } 59 60 public void setPipeline(Pipeline pipeline) { 61 for (int i = 0; i < getEnforcedComponents().length; i++) { 62 getEnforcedComponents()[i].setPipeline(pipeline); 63 } 64 super.setPipeline(pipeline); 65 } 66 67 protected PipelineComponent[] getEnforcedComponents() { 68 return enforcedComponents; 69 } 70 71 protected void setEnforcedComponents(PipelineComponent[] enforcedComponents) { 72 this.enforcedComponents = enforcedComponents; 73 } 74 75 PipelineComponent getTheHandlerComponent() { 76 return theHandlerComponent; 77 } 78 79 void setTheHandlerComponent(PipelineComponent theHandlerComponent) { 80 this.theHandlerComponent = theHandlerComponent; 81 } 82 83 static private class ENFORCE extends Helper { 84 85 public void helpWith(char c) { 86 try { 87 EnforceHandler handler = (EnforceHandler) getHandler(); 88 handler.getTheHandlerComponent().handle(c); 89 getHandler().getPipeline().removeFront(); 90 } catch (PipelineException e) { 91 throw new RuntimeException (e.getMessage()); 92 } catch (IllegalInputCharacterException e) { 93 throw new RuntimeException (e.getMessage()); 94 } 95 } 97 } 98 } 99 | Popular Tags |