KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbunit > dataset > csv > handlers > QuoteHandler


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.csv.handlers;
23
24 import org.dbunit.dataset.csv.IllegalInputCharacterException;
25
26 public class QuoteHandler extends AbstractPipelineComponent {
27
28     private QuoteHandler() {
29     }
30
31     public static final PipelineComponent ACCEPT() {
32         return createPipelineComponent(new QuoteHandler(), new ACCEPT());
33     }
34
35     public static final PipelineComponent IGNORE() {
36         return createPipelineComponent(new QuoteHandler(), new IGNORE());
37     }
38
39     public static final PipelineComponent QUOTE() {
40         return createPipelineComponent(new QuoteHandler(), new QUOTE());
41     }
42
43     public static final PipelineComponent UNQUOTE() {
44         return createPipelineComponent(new QuoteHandler(), new UNQUOTE());
45     }
46
47     public static final char QUOTE_CHAR = '"';
48
49     public boolean canHandle(char c) throws IllegalInputCharacterException {
50
51         if (c == QUOTE_CHAR) {
52             return true;
53         }
54         return false;
55     }
56
57
58     static protected class QUOTE extends Helper {
59
60         public void helpWith(char c) {
61             getHandler().getPipeline().putFront(SeparatorHandler.ACCEPT());
62             getHandler().getPipeline().putFront(WhitespacesHandler.ACCEPT());
63             getHandler().getPipeline().putFront(IsAlnumHandler.ACCEPT());
64             getHandler().getPipeline().putFront(QuoteHandler.UNQUOTE());
65             getHandler().getPipeline().putFront(EscapeHandler.ESCAPE());
66             // ignore the char
67
}
68
69     }
70
71     static protected class UNQUOTE extends Helper {
72
73         public void helpWith(char c) {
74             try {
75                 getHandler().getPipeline().removeFront();
76                 getHandler().getPipeline().removeFront();
77                 getHandler().getPipeline().removeFront();
78                 getHandler().getPipeline().removeFront();
79                 getHandler().getPipeline().removeFront();
80             } catch (PipelineException e) {
81                 throw new RuntimeException JavaDoc(e.getMessage());
82             }
83             // ignore the char
84
}
85
86         public boolean allowForNoMoreInput() {
87             throw new IllegalStateException JavaDoc("end of input while waiting for a closing quote");
88         }
89     }
90
91 }
92
Popular Tags