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  
22  package org.dbunit.dataset.common.handlers;
23  
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * @author fede
29   * @author Last changed by: $Author$
30   * @version $Revision$ $Date$
31   * @since 2.2 (Sep 12, 2004)
32   */
33  public class EnforceHandler extends AbstractPipelineComponent {
34  
35      /**
36       * Logger for this class
37       */
38      private static final Logger logger = LoggerFactory.getLogger(EnforceHandler.class);
39  
40      private PipelineComponent [] enforcedComponents;
41      private PipelineComponent theHandlerComponent;
42  
43      private EnforceHandler(PipelineComponent [] components) {
44          setEnforcedComponents(components);
45      }
46  
47  
48      public static final PipelineComponent ENFORCE(PipelineComponent component) {
49          logger.debug("ENFORCE(component={}) - start", component);
50  
51          return EnforceHandler.ENFORCE(new PipelineComponent [] {component});
52      }
53  
54      public static final PipelineComponent ENFORCE(PipelineComponent [] components) {
55          logger.debug("ENFORCE(components={}) - start", components);
56  
57          return createPipelineComponent(new EnforceHandler(components), new ENFORCE());
58      }
59  
60      public boolean canHandle(char c) throws IllegalInputCharacterException {
61          if(logger.isDebugEnabled())
62              logger.debug("canHandle(c={}) - start", String.valueOf(c));
63  
64          for (int i = 0; i < getEnforcedComponents().length; i++) {
65              if (getEnforcedComponents()[i].canHandle(c)) {
66                  setTheHandlerComponent(getEnforcedComponents()[i]);
67                  return true;
68              }
69          }
70          throw new IllegalInputCharacterException("(working on piece #" + getPipeline().getProducts().size() + ")"
71                  + getPipeline().getCurrentProduct().toString() + ": " + "Character '" + c + "' cannot be handled");
72      }
73  
74      public void setPipeline(Pipeline pipeline) {
75          logger.debug("setPipeline(pipeline={}) - start", pipeline);
76  
77          for (int i = 0; i < getEnforcedComponents().length; i++) {
78              getEnforcedComponents()[i].setPipeline(pipeline);
79          }
80          super.setPipeline(pipeline);
81      }
82  
83      protected PipelineComponent[] getEnforcedComponents() {
84          logger.debug("getEnforcedComponents() - start");
85  
86          return enforcedComponents;
87      }
88  
89      protected void setEnforcedComponents(PipelineComponent[] enforcedComponents) {
90          logger.debug("setEnforcedComponents(enforcedComponents={}) - start", enforcedComponents);
91  
92          this.enforcedComponents = enforcedComponents;
93      }
94  
95      PipelineComponent getTheHandlerComponent() {
96          logger.debug("getTheHandlerComponent() - start");
97  
98          return theHandlerComponent;
99      }
100 
101     void setTheHandlerComponent(PipelineComponent theHandlerComponent) {
102         logger.debug("setTheHandlerComponent(theHandlerComponent={}) - start",
103                 theHandlerComponent);
104 
105         this.theHandlerComponent = theHandlerComponent;
106     }
107 
108     static private class ENFORCE extends Helper {
109 
110         /**
111          * Logger for this class
112          */
113         private static final Logger logger = LoggerFactory.getLogger(ENFORCE.class);
114 
115         public void helpWith(char c) {
116             if(logger.isDebugEnabled())
117                 logger.debug("helpWith(c={}) - start", String.valueOf(c));
118 
119             try {
120                 EnforceHandler handler = (EnforceHandler) getHandler();
121                 handler.getTheHandlerComponent().handle(c);
122                 getHandler().getPipeline().removeFront();
123             } catch (PipelineException e) {
124                 throw new RuntimeException(e.getMessage());
125             } catch (IllegalInputCharacterException e) {
126                 throw new RuntimeException(e.getMessage());
127             }
128             // ignore the char
129         }
130     }
131 }