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: public class QuoteHandler extends AbstractPipelineComponent {
030:
031: /**
032: * Logger for this class
033: */
034: private static final Logger logger = LoggerFactory
035: .getLogger(QuoteHandler.class);
036:
037: private QuoteHandler() {
038: }
039:
040: public static final PipelineComponent ACCEPT() {
041: logger.debug("ACCEPT() - start");
042:
043: return createPipelineComponent(new QuoteHandler(), new ACCEPT());
044: }
045:
046: public static final PipelineComponent IGNORE() {
047: logger.debug("IGNORE() - start");
048:
049: return createPipelineComponent(new QuoteHandler(), new IGNORE());
050: }
051:
052: public static final PipelineComponent QUOTE() {
053: logger.debug("QUOTE() - start");
054:
055: return createPipelineComponent(new QuoteHandler(), new QUOTE());
056: }
057:
058: public static final PipelineComponent UNQUOTE() {
059: logger.debug("UNQUOTE() - start");
060:
061: return createPipelineComponent(new QuoteHandler(),
062: new UNQUOTE());
063: }
064:
065: public static final char QUOTE_CHAR = '"';
066:
067: public boolean canHandle(char c)
068: throws IllegalInputCharacterException {
069: logger.debug("canHandle(c=" + c + ") - start");
070:
071: if (c == QUOTE_CHAR) {
072: return true;
073: }
074: return false;
075: }
076:
077: static protected class QUOTE extends Helper {
078:
079: /**
080: * Logger for this class
081: */
082: private static final Logger logger = LoggerFactory
083: .getLogger(QUOTE.class);
084:
085: public void helpWith(char c) {
086: logger.debug("helpWith(c=" + c + ") - start");
087:
088: getHandler().getPipeline().putFront(
089: SeparatorHandler.ACCEPT());
090: getHandler().getPipeline().putFront(
091: WhitespacesHandler.ACCEPT());
092: getHandler().getPipeline()
093: .putFront(IsAlnumHandler.ACCEPT());
094: getHandler().getPipeline().putFront(QuoteHandler.UNQUOTE());
095: getHandler().getPipeline().putFront(EscapeHandler.ESCAPE());
096: // ignore the char
097: }
098:
099: }
100:
101: static protected class UNQUOTE extends Helper {
102:
103: /**
104: * Logger for this class
105: */
106: private static final Logger logger = LoggerFactory
107: .getLogger(UNQUOTE.class);
108:
109: public void helpWith(char c) {
110: logger.debug("helpWith(c=" + c + ") - start");
111:
112: try {
113: getHandler().getPipeline().removeFront();
114: getHandler().getPipeline().removeFront();
115: getHandler().getPipeline().removeFront();
116: getHandler().getPipeline().removeFront();
117: getHandler().getPipeline().removeFront();
118: } catch (PipelineException e) {
119: logger.error("helpWith()", e);
120:
121: throw new RuntimeException(e.getMessage());
122: }
123: // ignore the char
124: }
125:
126: public boolean allowForNoMoreInput() {
127: logger.debug("allowForNoMoreInput() - start");
128:
129: throw new IllegalStateException(
130: "end of input while waiting for a closing quote");
131: }
132: }
133:
134: }
|