/** You may copy this example and use it for any purpose, commercial or otherwise. */ import com.ricebridge.csvman.CsvSpec; import com.ricebridge.csvman.CsvManager; import com.ricebridge.csvman.CsvManagerException; import com.ricebridge.csvman.LineSpec; import com.ricebridge.data.BeanSpec; import java.util.HashMap; import java.util.List; import java.util.Collections; import java.util.Comparator; import java.util.Iterator; import java.text.SimpleDateFormat; import java.io.File; import java.io.FileOutputStream; import java.io.PrintWriter; import java.io.ByteArrayOutputStream; /** This is the main class of this example. It loads the data in the * company.csv file and creates a new HTML file called company.htm, * that lists the employee data fields. * * You can specify a different input CSV file as the first command line argument. * * To compile and run this program, you will need to include csvman.jar * (in the lib folder of the CSV Manager distribution) in your CLASSPATH. */ public class MakeTable { /** This is our custom date format used in the CSV data file. */ public static SimpleDateFormat sDateInputFormat = new SimpleDateFormat("yyyy-MM-dd"); /** This is the display format for dates. */ public static SimpleDateFormat sDateDisplayFormat = new SimpleDateFormat("dd/MMM/yyyy"); /** Run the program to create the HTML table of employees. * Usage: windows: java -cp .;..\..\..\lib\csvman.jar MakeTable * unix: java -cp .:../../../lib/csvman.jar MakeTable */ public static void main( String[] args ) { String filepath = "company.csv"; // this is the default boolean streaming = false; if( 1 == args.length ) { filepath = args[0]; } try { MakeTable mt = new MakeTable(); mt.make( new File(filepath) ); } catch( CsvManagerException xme ) { // get a user friendly message from CSV Manager System.out.println( xme.getUserMessage() ); } catch( Exception e ) { // otherwise dump out a stack trace e.printStackTrace(); } } /** Make the HTML table from the CSV file. * @param pCompanyCsvFile CSV file containing company data about employees and projects */ public void make( File pCompanyCsvFile ) throws Exception { // This is the CsvManager object we'll be using. CsvManager csvman = new CsvManager(); // Create our own date converter for dates in the format YYYY-MM-DD. DateConverter dc = new DateConverter( sDateInputFormat ); // Handle yes/no fields YesNoConverter ync = new YesNoConverter(); // This matches up the data fields with the bean property methods // these values are the property method names, minus the get/set/is prefix LineSpec ls_employee = new LineSpec(new String[]{"Number","Name","HireDate","Manager","ProjectNames"}); // The hiring date field is in our custom dateformat. HashMap employee_stringconv = new HashMap(); employee_stringconv.put( "HireDate", dc ); employee_stringconv.put( "Manager", ync ); // Define a BeanSpec class describing the Employee Java Bean. BeanSpec bs_employee = new BeanSpec( Employee.class, employee_stringconv ); // Setup the CSV specification to handle company.csv CsvSpec csvspec = csvman.getCsvSpec(); // to completely ignore comment lines, use these two settings together // otherwise you'll get lots of empty lines returned csvspec.setUseComment(true); csvspec.setIgnoreEmptyLines(true); // ingore "Bad" Bob line in company.csv csvspec.setIgnoreBadLines(true); // skip headers as not real data csvspec.setStartLine(2); // load the data - we get back a List of Employee objects List employees = csvman.loadBeans( pCompanyCsvFile, ls_employee, bs_employee ); // Display error message with bad lines System.out.println( "Ignored bad lines were: "+csvman.getBadLines() ); // Create a PrintWriter for the output file FileOutputStream fos = new FileOutputStream( pCompanyCsvFile.getName().replaceFirst("\\.csv$",".htm") ); PrintWriter pw = new PrintWriter( fos ); // build table using the list Employee bean objects buildTable( employees, pw ); pw.close(); fos.close(); } /** Create the HTML table using a list of projects and employees. * @param pEmployees list of Employee objects */ private void buildTable( List pEmployees, PrintWriter pPrintWriter ) throws Exception { // sort the employees by name Collections.sort( pEmployees, new Comparator() { public int compare(Object a, Object b){ return ((Employee)a).getName().compareTo( ((Employee)b).getName() ); }}); outputStartEmployeeTable( pPrintWriter ); for( Iterator eI = pEmployees.iterator(); eI.hasNext(); ) { outputEmployee( (Employee) eI.next(), pPrintWriter ); } outputEndEmployeeTable( pPrintWriter ); } /** Output HTML tags for start of the employee table. */ public static void outputStartEmployeeTable( PrintWriter pPrintWriter ) { pPrintWriter.print("
| Number | Name | Manager | Hired | Projects |
|---|