if (args.length == 0) {
System.err.println("Usage: java "
+ SimpleCSVProcessor.class.getName()
+ " <csvFile> [xsltFile]");
System.err.println(" - csvFile is required");
System.err.println(" - xsltFile is optional");
System.exit(1);
}
String csvFileName = args[0];
String xsltFileName = (args.length > 1) ? args[1] : null;
TransformerFactory transFact = TransformerFactory.newInstance();
if (transFact.getFeature(SAXTransformerFactory.FEATURE)) {
SAXTransformerFactory saxTransFact =
(SAXTransformerFactory) transFact;
TransformerHandler transHand = null;
if (xsltFileName == null) {
transHand = saxTransFact.newTransformerHandler();
} else {
transHand = saxTransFact.newTransformerHandler(
new StreamSource(new File(xsltFileName)));
}
// set the destination for the XSLT transformation
transHand.setResult(new StreamResult(System.out));
// hook the CSVXMLReader to the CSV file
CSVXMLReader csvReader = new CSVXMLReader();
InputSource csvInputSrc = new InputSource(
new FileReader(csvFileName));
// attach the XSLT processor to the CSVXMLReader
csvReader.setContentHandler(transHand);
csvReader.parse(csvInputSrc);
} else {
System.err.println("SAXTransformerFactory is not supported.");
System.exit(1);
}