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: package org.dbunit.dataset.csv.handlers;
022:
023: import org.slf4j.Logger;
024: import org.slf4j.LoggerFactory;
025:
026: import org.dbunit.dataset.csv.IllegalInputCharacterException;
027:
028: public abstract class AbstractPipelineComponent implements
029: PipelineComponent {
030:
031: /**
032: * Logger for this class
033: */
034: private static final Logger logger = LoggerFactory
035: .getLogger(AbstractPipelineComponent.class);
036:
037: private PipelineComponent successor;
038: private Pipeline pipeline;
039:
040: private Helper helper;
041:
042: protected PipelineComponent getSuccessor() {
043: logger.debug("getSuccessor() - start");
044:
045: return successor;
046: }
047:
048: public Pipeline getPipeline() {
049: logger.debug("getPipeline() - start");
050:
051: return pipeline;
052: }
053:
054: public void setPipeline(Pipeline pipeline) {
055: logger.debug("setPipeline(pipeline=" + pipeline + ") - start");
056:
057: this .pipeline = pipeline;
058: }
059:
060: public void setSuccessor(PipelineComponent successor) {
061: logger.debug("setSuccessor(successor=" + successor
062: + ") - start");
063:
064: this .successor = successor;
065: }
066:
067: private StringBuffer getThePiece() {
068: logger.debug("getThePiece() - start");
069:
070: return getPipeline().getCurrentProduct();
071: }
072:
073: public void handle(char c) throws IllegalInputCharacterException,
074: PipelineException {
075: logger.debug("handle(c=" + c + ") - start");
076:
077: if (!canHandle(c)) {
078: getSuccessor().handle(c);
079: } else {
080: getHelper().helpWith(c);
081: }
082: }
083:
084: public void noMoreInput() {
085: logger.debug("noMoreInput() - start");
086:
087: if (allowForNoMoreInput()) {
088: if (getSuccessor() != null)
089: getSuccessor().noMoreInput();
090: }
091: }
092:
093: public boolean allowForNoMoreInput() {
094: logger.debug("allowForNoMoreInput() - start");
095:
096: return getHelper().allowForNoMoreInput();
097: }
098:
099: protected static PipelineComponent createPipelineComponent(
100: AbstractPipelineComponent handler, Helper helper) {
101: logger.debug("createPipelineComponent(handler=" + handler
102: + ", helper=" + helper + ") - start");
103:
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: logger.debug("accept(c=" + c + ") - start");
115:
116: getThePiece().append(c);
117: }
118:
119: protected Helper getHelper() {
120: logger.debug("getHelper() - start");
121:
122: return helper;
123: }
124:
125: private void setHelper(Helper helper) {
126: logger.debug("setHelper(helper=" + helper + ") - start");
127:
128: this .helper = helper;
129: }
130:
131: static protected class IGNORE extends Helper {
132:
133: /**
134: * Logger for this class
135: */
136: private static final Logger logger = LoggerFactory
137: .getLogger(IGNORE.class);
138:
139: public void helpWith(char c) {
140: // IGNORE
141: }
142: }
143:
144: static protected class ACCEPT extends Helper {
145:
146: /**
147: * Logger for this class
148: */
149: private static final Logger logger = LoggerFactory
150: .getLogger(ACCEPT.class);
151:
152: public void helpWith(char c) {
153: logger.debug("helpWith(c=" + c + ") - start");
154:
155: getHandler().accept(c);
156: }
157: }
158: }
|