View Javadoc
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  package org.dbunit.dataset.common.handlers;
22  
23  import org.slf4j.Logger;
24  import org.slf4j.LoggerFactory;
25  
26  /**
27   * @author fede
28   * @author Last changed by: $Author$
29   * @version $Revision$ $Date$
30   * @since 2.2 (Sep 12, 2004)
31   */
32  public abstract class AbstractPipelineComponent implements PipelineComponent {
33  
34      /**
35       * Logger for this class
36       */
37      private static final Logger logger = LoggerFactory.getLogger(AbstractPipelineComponent.class);
38  
39      private PipelineComponent successor;
40      private Pipeline pipeline;
41  
42      private Helper helper;
43  
44      protected PipelineComponent getSuccessor() {
45          return successor;
46      }
47  
48      public Pipeline getPipeline() {
49          return pipeline;
50      }
51  
52      public void setPipeline(Pipeline pipeline) {
53          logger.debug("setPipeline(pipeline={}) - start", pipeline);
54          this.pipeline = pipeline;
55      }
56  
57      protected PipelineConfig getPipelineConfig() {
58          if(this.getPipeline() != null) {
59              return this.getPipeline().getPipelineConfig();
60          }
61          else {
62              throw new IllegalStateException("The pipeline is not set for this component. Cannot proceed");
63          }
64      }
65  
66      public void setSuccessor(PipelineComponent successor) {
67          logger.debug("setSuccessor(successor={}) - start", successor);
68          this.successor = successor;
69      }
70  
71  
72      private StringBuffer getThePiece() {
73          return getPipeline().getCurrentProduct();
74      }
75  
76      public void handle(char c) throws IllegalInputCharacterException, PipelineException {
77          if(logger.isDebugEnabled())
78              logger.debug("handle(c={}) - start", String.valueOf(c));
79  
80          if (!canHandle(c)) {
81              getSuccessor().handle(c);
82          } else {
83              getHelper().helpWith(c);
84          }
85      }
86  
87      public void noMoreInput() {
88          logger.debug("noMoreInput() - start");
89  
90          if (allowForNoMoreInput()) {
91              if (getSuccessor()!= null)
92                  getSuccessor().noMoreInput();
93          }
94      }
95  
96      public boolean allowForNoMoreInput() {
97          logger.debug("allowForNoMoreInput() - start");
98  
99          return getHelper().allowForNoMoreInput();
100     }
101 
102     protected static PipelineComponent createPipelineComponent(AbstractPipelineComponent handler, Helper helper) {
103         logger.debug("createPipelineComponent(handler={}, helper={}) - start", handler, helper);
104         helper.setHandler(handler);
105         handler.setHelper(helper);
106         return handler;
107     }
108 
109     /**
110      * Method invoked when the character should be accepted
111      * @param c
112      */
113     public void accept(char c) {
114         getThePiece().append(c);
115     }
116 
117     protected Helper getHelper() {
118         return helper;
119     }
120 
121     private void setHelper(Helper helper) {
122         logger.debug("setHelper(helper={}) - start", helper);
123         this.helper = helper;
124     }
125 
126     static protected class IGNORE extends Helper {
127         public void helpWith(char c) {
128             // IGNORE
129         }
130     }
131 
132     static protected class ACCEPT extends Helper {
133         public void helpWith(char c) {
134             if(logger.isDebugEnabled())
135                 logger.debug("helpWith(c={}) - start", String.valueOf(c));
136             
137             getHandler().accept(c);
138         }
139     }
140 }