KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
27  * author: fede
28  * 4-set-2003 10.20.45
29  * $Revision: 1.1 $
30  */

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 JavaDoc(e.getMessage());
92             } catch (IllegalInputCharacterException e) {
93                 throw new RuntimeException JavaDoc(e.getMessage());
94             }
95             // ignore the char
96
}
97     }
98 }
99
Popular Tags