FileDocCategorySizeDatePackage
CSVConfiguration.javaAPI DocApache Xerces 3.0.19101Fri Sep 14 20:33:56 BST 2007xni.parser

CSVConfiguration

public class CSVConfiguration extends AbstractConfiguration
This example is a very simple parser configuration that can parse files with comma-separated values (CSV) to generate XML events. For example, the following CSV document:
Andy Clark,16 Jan 1973,Cincinnati
produces the following XML "document" as represented by the XNI streaming document information:
<?xml version='1.0' encoding='UTF-8' standalone='true'?>
<!DOCTYPE csv [
<!ELEMENT csv (row)*>
<!ELEMENT row (col)*>
<!ELEMENT col (#PCDATA)>
]>
<csv>
<row>
<col>Andy Clark</col>
<col>16 Jan 1973</col>
<col>Cincinnati</col>
</row>
</csv>
author
Andy Clark, IBM
version
$Id: CSVConfiguration.java 447690 2006-09-19 02:41:53Z mrglavas $

Fields Summary
protected static final org.apache.xerces.xni.QName
CSV
A QName for the <csv> element name.
protected static final org.apache.xerces.xni.QName
ROW
A QName for the <row> element name.
protected static final org.apache.xerces.xni.QName
COL
A QName for the <col> element name.
protected static final org.apache.xerces.xni.XMLAttributes
EMPTY_ATTRS
An empty list of attributes.
private final org.apache.xerces.xni.XMLString
NEWLINE
A newline XMLString.
private final org.apache.xerces.xni.XMLString
NEWLINE_ONE_SPACE
A newline + one space XMLString.
private final org.apache.xerces.xni.XMLString
NEWLINE_TWO_SPACES
A newline + two spaces XMLString.
private final org.apache.xerces.util.XMLStringBuffer
fStringBuffer
A string buffer for use in copying string into an XMLString object for passing to the characters method.
Constructors Summary
Methods Summary
public booleangetFeature(java.lang.String featureId)

 return false; 
public java.lang.ObjectgetProperty(java.lang.String propertyId)

 return null; 
public voidparse(org.apache.xerces.xni.parser.XMLInputSource source)
Parse an XML document.

The parser can use this method to instruct this configuration to begin parsing an XML document from any valid input source (a character stream, a byte stream, or a URI).

Parsers may not invoke this method while a parse is in progress. Once a parse is complete, the parser may then parse another XML document.

This method is synchronous: it will not return until parsing has ended. If a client application wants to terminate parsing early, it should throw an exception.

param
source The input source for the top-level of the XML document.
exception
XNIException Any XNI exception, possibly wrapping another exception.
exception
IOException An IO exception from the parser, possibly from a byte stream or character stream supplied by the parser.


    //
    // XMLParserConfiguration methods
    //

                                                                                                                                                                                                                                    
        
           

        // get reader
        openInputSourceStream(source);
        Reader reader = source.getCharacterStream();
        if (reader == null) {
            InputStream stream = source.getByteStream();
            reader = new InputStreamReader(stream);
        }
        BufferedReader bufferedReader = new BufferedReader(reader);

        // start document
        if (fDocumentHandler != null) {
            fDocumentHandler.startDocument(null, "UTF-8", new NamespaceSupport(), null);
            fDocumentHandler.xmlDecl("1.0", "UTF-8", "true", null);
            fDocumentHandler.doctypeDecl("csv", null, null, null);
        }
        if (fDTDHandler != null) {
            fDTDHandler.startDTD(null, null);
            fDTDHandler.elementDecl("csv", "(row)*", null);
            fDTDHandler.elementDecl("row", "(col)*", null);
            fDTDHandler.elementDecl("col", "(#PCDATA)", null);
        }
        if (fDTDContentModelHandler != null) {
            fDTDContentModelHandler.startContentModel("csv", null);
            fDTDContentModelHandler.startGroup(null);
            fDTDContentModelHandler.element("row", null);
            fDTDContentModelHandler.endGroup(null);
            short csvOccurs = XMLDTDContentModelHandler.OCCURS_ZERO_OR_MORE;
            fDTDContentModelHandler.occurrence(csvOccurs, null);
            fDTDContentModelHandler.endContentModel(null);
            
            fDTDContentModelHandler.startContentModel("row", null);
            fDTDContentModelHandler.startGroup(null);
            fDTDContentModelHandler.element("col", null);
            fDTDContentModelHandler.endGroup(null);
            short rowOccurs = XMLDTDContentModelHandler.OCCURS_ZERO_OR_MORE;
            fDTDContentModelHandler.occurrence(rowOccurs, null);
            fDTDContentModelHandler.endContentModel(null);
        
            fDTDContentModelHandler.startContentModel("col", null);
            fDTDContentModelHandler.startGroup(null);
            fDTDContentModelHandler.pcdata(null);
            fDTDContentModelHandler.endGroup(null);
            fDTDContentModelHandler.endContentModel(null);
        }
        if (fDTDHandler != null) {
            fDTDHandler.endDTD(null);
        }
        if (fDocumentHandler != null) {
            fDocumentHandler.startElement(CSV, EMPTY_ATTRS, null);
        }

        // read lines
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            if (fDocumentHandler != null) {
                fDocumentHandler.ignorableWhitespace(NEWLINE_ONE_SPACE, null);
                fDocumentHandler.startElement(ROW, EMPTY_ATTRS, null);
                StringTokenizer tokenizer = new StringTokenizer(line, ",");
                while (tokenizer.hasMoreTokens()) {
                    fDocumentHandler.ignorableWhitespace(NEWLINE_TWO_SPACES, null);
                    fDocumentHandler.startElement(COL, EMPTY_ATTRS, null);
                    String token = tokenizer.nextToken();
                    fStringBuffer.clear();
                    fStringBuffer.append(token);
                    fDocumentHandler.characters(fStringBuffer, null);
                    fDocumentHandler.endElement(COL, null);
                }
                fDocumentHandler.ignorableWhitespace(NEWLINE_ONE_SPACE, null);
                fDocumentHandler.endElement(ROW, null);
            }
        }
        bufferedReader.close();

        // end document
        if (fDocumentHandler != null) {
            fDocumentHandler.ignorableWhitespace(NEWLINE, null);
            fDocumentHandler.endElement(CSV, null);
            fDocumentHandler.endDocument(null);
        }

    
public voidsetFeature(java.lang.String featureId, boolean state)

public voidsetProperty(java.lang.String propertyId, java.lang.Object value)