import java.io.*; import java.net.URL; import org.xml.sax.*; import org.xml.sax.helpers.*; /** * A utility class that parses a Comma Separated Values (CSV) file * and outputs its contents using SAX2 events. The format of CSV that * this class reads is identical to the export format for Microsoft * Excel. For simple values, the CSV file may look like this: *
* a,b,c * d,e,f ** Quotes are used as delimiters when the values contain commas: *
* a,"b,c",d * e,"f,g","h,i" ** And double quotes are used when the values contain quotes. This parser * is smart enough to trim spaces around commas, as well. * * @author Eric M. Burke */ public class CSVXMLReader extends AbstractXMLReader { // an empty attribute for use with SAX private static final Attributes EMPTY_ATTR = new AttributesImpl(); /** * Parse a CSV file. SAX events are delivered to the ContentHandler * that was registered via
setContentHandler.
*
* @param input the comma separated values file to parse.
*/
public void parse(InputSource input) throws IOException,
SAXException {
// if no handler is registered to receive events, don't bother
// to parse the CSV file
ContentHandler ch = getContentHandler();
if (ch == null) {
return;
}
// convert the InputSource into a BufferedReader
BufferedReader br = null;
if (input.getCharacterStream() != null) {
br = new BufferedReader(input.getCharacterStream());
} else if (input.getByteStream() != null) {
br = new BufferedReader(new InputStreamReader(
input.getByteStream()));
} else if (input.getSystemId() != null) {
java.net.URL url = new URL(input.getSystemId());
br = new BufferedReader(new InputStreamReader(url.openStream()));
} else {
throw new SAXException("Invalid InputSource object");
}
ch.startDocument();
// emit