INRIA & FluxMedia

fr.fluxmedia.tmcontrib.generator
Class CSVXMLReader

java.lang.Object
  extended by fr.fluxmedia.tmcontrib.generator.AbstractXMLReader
      extended by fr.fluxmedia.tmcontrib.generator.CSVXMLReader
All Implemented Interfaces:
org.xml.sax.XMLReader

public class CSVXMLReader
extends AbstractXMLReader

A class for parsing CSV (comma separated value) files. File datas are converted in SAX2 events. A simple CSV file can look like :

 a,b,c
 d,e,f
 
Quotes can be used when values contain comma :
 a,"b,c",d
 e,"f,g","h,i"
 
Separator can be set with setSeparator This class is defined in the following book "Java and XSLT" by Eric M. Burke (O'Reilly)

Author:
triolet

Field Summary
private static org.xml.sax.Attributes EMPTY_ATTR
           
private  char separator
           
 
Constructor Summary
CSVXMLReader(java.lang.String separator)
           
 
Method Summary
private  java.lang.String cleanupQuotes(java.lang.String token)
          Removes quotes around a token
private  int locateFirstDelimiter(java.lang.String curLine)
          Locates the first separator character in the current line
 void parse(org.xml.sax.InputSource input)
          Analyses a CSV file.
private  void parseLine(java.lang.String curLine, org.xml.sax.ContentHandler ch)
          Recursive analyse of a line
 void setSeparator(java.lang.String separator)
          Sets the separator attribute of the CSVXMLReader object
 
Methods inherited from class fr.fluxmedia.tmcontrib.generator.AbstractXMLReader
getContentHandler, getDTDHandler, getEntityResolver, getErrorHandler, getFeature, getProperty, parse, setContentHandler, setDTDHandler, setEntityResolver, setErrorHandler, setFeature, setProperty
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

EMPTY_ATTR

private static final org.xml.sax.Attributes EMPTY_ATTR

separator

private char separator
Constructor Detail

CSVXMLReader

public CSVXMLReader(java.lang.String separator)
Method Detail

parse

public void parse(org.xml.sax.InputSource input)
           throws java.io.IOException,
                  org.xml.sax.SAXException
Analyses a CSV file. SAX2 events are sending to the ContentHandler.

Specified by:
parse in interface org.xml.sax.XMLReader
Specified by:
parse in class AbstractXMLReader
Parameters:
input - The input to parse
Throws:
java.io.IOException - if input has not been found
org.xml.sax.SAXException - an exception that can wrap others exceptions

setSeparator

public void setSeparator(java.lang.String separator)
Sets the separator attribute of the CSVXMLReader object

Parameters:
separator - The new separator value

parseLine

private void parseLine(java.lang.String curLine,
                       org.xml.sax.ContentHandler ch)
                throws java.io.IOException,
                       org.xml.sax.SAXException
Recursive analyse of a line

Parameters:
curLine - The current line
ch - The contentHandler which receives SAX2 events
Throws:
java.io.IOException - if an I/O operation failed.
org.xml.sax.SAXException - an exception that can wrap others exceptions

locateFirstDelimiter

private int locateFirstDelimiter(java.lang.String curLine)
Locates the first separator character in the current line

Parameters:
curLine - The current line
Returns:
The index of the first separator character in the line, -1 if not found

cleanupQuotes

private java.lang.String cleanupQuotes(java.lang.String token)
Removes quotes around a token

Parameters:
token - The token with quotes to remove
Returns:
The token without quotes

INRIA & FluxMedia

..no bottom yet...