001: /*
002: *
003: * The DbUnit Database Testing Framework
004: * Copyright (C)2002-2004, DbUnit.org
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: */
021:
022: package org.dbunit.dataset.csv.handlers;
023:
024: import org.slf4j.Logger;
025: import org.slf4j.LoggerFactory;
026:
027: import org.dbunit.dataset.csv.IllegalInputCharacterException;
028:
029: /**
030: * author: fede
031: * 4-set-2003 10.20.45
032: * $Revision: 554 $
033: */
034: public class EnforceHandler extends AbstractPipelineComponent {
035:
036: /**
037: * Logger for this class
038: */
039: private static final Logger logger = LoggerFactory
040: .getLogger(EnforceHandler.class);
041:
042: private PipelineComponent[] enforcedComponents;
043: private PipelineComponent theHandlerComponent;
044:
045: private EnforceHandler(PipelineComponent[] components) {
046: setEnforcedComponents(components);
047: }
048:
049: public static final PipelineComponent ENFORCE(
050: PipelineComponent component) {
051: logger.debug("ENFORCE(component=" + component + ") - start");
052:
053: return EnforceHandler
054: .ENFORCE(new PipelineComponent[] { component });
055: }
056:
057: public static final PipelineComponent ENFORCE(
058: PipelineComponent[] components) {
059: logger.debug("ENFORCE(components=" + components + ") - start");
060:
061: return createPipelineComponent(new EnforceHandler(components),
062: new ENFORCE());
063: }
064:
065: public boolean canHandle(char c)
066: throws IllegalInputCharacterException {
067: logger.debug("canHandle(c=" + c + ") - start");
068:
069: for (int i = 0; i < getEnforcedComponents().length; i++) {
070: if (getEnforcedComponents()[i].canHandle(c)) {
071: setTheHandlerComponent(getEnforcedComponents()[i]);
072: return true;
073: }
074: }
075: throw new IllegalInputCharacterException("(working on piece #"
076: + getPipeline().getProducts().size() + ")"
077: + getPipeline().getCurrentProduct().toString() + ": "
078: + "Character '" + c + "' cannot be handled");
079: }
080:
081: public void setPipeline(Pipeline pipeline) {
082: logger.debug("setPipeline(pipeline=" + pipeline + ") - start");
083:
084: for (int i = 0; i < getEnforcedComponents().length; i++) {
085: getEnforcedComponents()[i].setPipeline(pipeline);
086: }
087: super .setPipeline(pipeline);
088: }
089:
090: protected PipelineComponent[] getEnforcedComponents() {
091: logger.debug("getEnforcedComponents() - start");
092:
093: return enforcedComponents;
094: }
095:
096: protected void setEnforcedComponents(
097: PipelineComponent[] enforcedComponents) {
098: logger.debug("setEnforcedComponents(enforcedComponents="
099: + enforcedComponents + ") - start");
100:
101: this .enforcedComponents = enforcedComponents;
102: }
103:
104: PipelineComponent getTheHandlerComponent() {
105: logger.debug("getTheHandlerComponent() - start");
106:
107: return theHandlerComponent;
108: }
109:
110: void setTheHandlerComponent(PipelineComponent theHandlerComponent) {
111: logger.debug("setTheHandlerComponent(theHandlerComponent="
112: + theHandlerComponent + ") - start");
113:
114: this .theHandlerComponent = theHandlerComponent;
115: }
116:
117: static private class ENFORCE extends Helper {
118:
119: /**
120: * Logger for this class
121: */
122: private static final Logger logger = LoggerFactory
123: .getLogger(ENFORCE.class);
124:
125: public void helpWith(char c) {
126: logger.debug("helpWith(c=" + c + ") - start");
127:
128: try {
129: EnforceHandler handler = (EnforceHandler) getHandler();
130: handler.getTheHandlerComponent().handle(c);
131: getHandler().getPipeline().removeFront();
132: } catch (PipelineException e) {
133: logger.error("helpWith()", e);
134:
135: throw new RuntimeException(e.getMessage());
136: } catch (IllegalInputCharacterException e) {
137: logger.error("helpWith()", e);
138:
139: throw new RuntimeException(e.getMessage());
140: }
141: // ignore the char
142: }
143: }
144: }
|