KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > xni > parser > CSVConfiguration


1 /*
2  * Copyright 2001,2002,2004,2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package xni.parser;
18
19 import java.io.BufferedReader JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.InputStreamReader JavaDoc;
23 import java.io.Reader JavaDoc;
24 import java.util.StringTokenizer JavaDoc;
25
26 import org.apache.xerces.util.NamespaceSupport;
27 import org.apache.xerces.util.XMLAttributesImpl;
28 import org.apache.xerces.util.XMLStringBuffer;
29 import org.apache.xerces.xni.QName;
30 import org.apache.xerces.xni.XMLAttributes;
31 import org.apache.xerces.xni.XMLDTDContentModelHandler;
32 import org.apache.xerces.xni.XMLString;
33 import org.apache.xerces.xni.XNIException;
34 import org.apache.xerces.xni.parser.XMLInputSource;
35
36 /**
37  * This example is a very simple parser configuration that can
38  * parse files with comma-separated values (CSV) to generate XML
39  * events. For example, the following CSV document:
40  * <pre>
41  * Andy Clark,16 Jan 1973,Cincinnati
42  * </pre>
43  * produces the following XML "document" as represented by the
44  * XNI streaming document information:
45  * <pre>
46  * &lt;?xml version='1.0' encoding='UTF-8' standalone='true'?&gt;
47  * &lt;!DOCTYPE csv [
48  * &lt;!ELEMENT csv (row)*&gt;
49  * &lt;!ELEMENT row (col)*&gt;
50  * &lt;!ELEMENT col (#PCDATA)&gt;
51  * ]&gt;
52  * &lt;csv&gt;
53  * &lt;row&gt;
54  * &lt;col&gt;Andy Clark&lt;/col&gt;
55  * &lt;col&gt;16 Jan 1973&lt;/col&gt;
56  * &lt;col&gt;Cincinnati&lt;/col&gt;
57  * &lt;/row&gt;
58  * &lt;/csv&gt;
59  * </pre>
60  *
61  * @author Andy Clark, IBM
62  *
63  * @version $Id: CSVConfiguration.java,v 1.9 2005/01/11 13:47:23 mrglavas Exp $
64  */

65 public class CSVConfiguration
66     extends AbstractConfiguration {
67
68     //
69
// Constants
70
//
71

72     /** A QName for the &lt;csv&gt; element name. */
73     protected static final QName CSV = new QName(null, null, "csv", null);
74
75     /** A QName for the &lt;row&gt; element name. */
76     protected static final QName ROW = new QName(null, null, "row", null);
77
78     /** A QName for the &lt;col&gt; element name. */
79     protected static final QName COL = new QName(null, null, "col", null);
80     
81     /** An empty list of attributes. */
82     protected static final XMLAttributes EMPTY_ATTRS = new XMLAttributesImpl();
83
84     /** A newline XMLString. */
85     private final XMLString NEWLINE = new XMLStringBuffer("\n");
86
87     /** A newline + one space XMLString. */
88     private final XMLString NEWLINE_ONE_SPACE = new XMLStringBuffer("\n ");
89
90     /** A newline + two spaces XMLString. */
91     private final XMLString NEWLINE_TWO_SPACES = new XMLStringBuffer("\n ");
92
93     //
94
// Data
95
//
96

97     /**
98      * A string buffer for use in copying string into an XMLString
99      * object for passing to the characters method.
100      */

101     private final XMLStringBuffer fStringBuffer = new XMLStringBuffer();
102
103     //
104
// XMLParserConfiguration methods
105
//
106

107     /**
108      * Parse an XML document.
109      * <p>
110      * The parser can use this method to instruct this configuration
111      * to begin parsing an XML document from any valid input source
112      * (a character stream, a byte stream, or a URI).
113      * <p>
114      * Parsers may not invoke this method while a parse is in progress.
115      * Once a parse is complete, the parser may then parse another XML
116      * document.
117      * <p>
118      * This method is synchronous: it will not return until parsing
119      * has ended. If a client application wants to terminate
120      * parsing early, it should throw an exception.
121      *
122      * @param source The input source for the top-level of the
123      * XML document.
124      *
125      * @exception XNIException Any XNI exception, possibly wrapping
126      * another exception.
127      * @exception IOException An IO exception from the parser, possibly
128      * from a byte stream or character stream
129      * supplied by the parser.
130      */

131     public void parse(XMLInputSource source)
132         throws IOException JavaDoc, XNIException {
133
134         // get reader
135
openInputSourceStream(source);
136         Reader JavaDoc reader = source.getCharacterStream();
137         if (reader == null) {
138             InputStream JavaDoc stream = source.getByteStream();
139             reader = new InputStreamReader JavaDoc(stream);
140         }
141         BufferedReader JavaDoc bufferedReader = new BufferedReader JavaDoc(reader);
142
143         // start document
144
if (fDocumentHandler != null) {
145             fDocumentHandler.startDocument(null, "UTF-8", new NamespaceSupport(), null);
146             fDocumentHandler.xmlDecl("1.0", "UTF-8", "true", null);
147             fDocumentHandler.doctypeDecl("csv", null, null, null);
148         }
149         if (fDTDHandler != null) {
150             fDTDHandler.startDTD(null, null);
151             fDTDHandler.elementDecl("csv", "(row)*", null);
152             fDTDHandler.elementDecl("row", "(col)*", null);
153             fDTDHandler.elementDecl("col", "(#PCDATA)", null);
154         }
155         if (fDTDContentModelHandler != null) {
156             fDTDContentModelHandler.startContentModel("csv", null);
157             fDTDContentModelHandler.startGroup(null);
158             fDTDContentModelHandler.element("row", null);
159             fDTDContentModelHandler.endGroup(null);
160             short csvOccurs = XMLDTDContentModelHandler.OCCURS_ZERO_OR_MORE;
161             fDTDContentModelHandler.occurrence(csvOccurs, null);
162             fDTDContentModelHandler.endContentModel(null);
163             
164             fDTDContentModelHandler.startContentModel("row", null);
165             fDTDContentModelHandler.startGroup(null);
166             fDTDContentModelHandler.element("col", null);
167             fDTDContentModelHandler.endGroup(null);
168             short rowOccurs = XMLDTDContentModelHandler.OCCURS_ZERO_OR_MORE;
169             fDTDContentModelHandler.occurrence(rowOccurs, null);
170             fDTDContentModelHandler.endContentModel(null);
171         
172             fDTDContentModelHandler.startContentModel("col", null);
173             fDTDContentModelHandler.startGroup(null);
174             fDTDContentModelHandler.pcdata(null);
175             fDTDContentModelHandler.endGroup(null);
176             fDTDContentModelHandler.endContentModel(null);
177         }
178         if (fDTDHandler != null) {
179             fDTDHandler.endDTD(null);
180         }
181         if (fDocumentHandler != null) {
182             fDocumentHandler.startElement(CSV, EMPTY_ATTRS, null);
183         }
184
185         // read lines
186
String JavaDoc line;
187         while ((line = bufferedReader.readLine()) != null) {
188             if (fDocumentHandler != null) {
189                 fDocumentHandler.ignorableWhitespace(NEWLINE_ONE_SPACE, null);
190                 fDocumentHandler.startElement(ROW, EMPTY_ATTRS, null);
191                 StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(line, ",");
192                 while (tokenizer.hasMoreTokens()) {
193                     fDocumentHandler.ignorableWhitespace(NEWLINE_TWO_SPACES, null);
194                     fDocumentHandler.startElement(COL, EMPTY_ATTRS, null);
195                     String JavaDoc token = tokenizer.nextToken();
196                     fStringBuffer.clear();
197                     fStringBuffer.append(token);
198                     fDocumentHandler.characters(fStringBuffer, null);
199                     fDocumentHandler.endElement(COL, null);
200                 }
201                 fDocumentHandler.ignorableWhitespace(NEWLINE_ONE_SPACE, null);
202                 fDocumentHandler.endElement(ROW, null);
203             }
204         }
205         bufferedReader.close();
206
207         // end document
208
if (fDocumentHandler != null) {
209             fDocumentHandler.ignorableWhitespace(NEWLINE, null);
210             fDocumentHandler.endElement(CSV, null);
211             fDocumentHandler.endDocument(null);
212         }
213
214     } // parse(XMLInputSource)
215

216     // NOTE: The following methods are overloaded to ignore setting
217
// of parser state so that this configuration does not
218
// throw configuration exceptions for features and properties
219
// that it doesn't care about.
220

221     public void setFeature(String JavaDoc featureId, boolean state) {}
222     public boolean getFeature(String JavaDoc featureId) { return false; }
223     public void setProperty(String JavaDoc propertyId, Object JavaDoc value) {}
224     public Object JavaDoc getProperty(String JavaDoc propertyId) { return null; }
225
226 } // class CSVConfiguration
227
Popular Tags