<%! public static final String PN = "csvman-demo"; %><% pageContext.setAttribute( "title", "Demonstration of Ricebridge Java CSV Manager Component - user-friendly and well documented for software developers" ); %> <%@ include file="/products/csvman/inc/demo-init.htm" %> <%@ include file="/inc/product.htm" %> <%@ include file="/inc/page-head-wide.htm" %> <%@ include file="/products/csvman/inc/pnd.htm" %> <% makeDataMap(); String csvmanerrmsg = null; String sample = request.getParameter("sample"); if( null == sample ) { sample = "Planets"; } boolean hasHeaders = true; boolean useTab = false; String csvtext = ""; String form = ""; CsvManager csvman = new CsvManager(); CsvSpec csvspec = csvman.getCsvSpec(); List data = null; CsvManagerException csvmanex = null; form = request.getParameter("form"); if( null == form ) { form = ""; } if( "yes".equals( form ) ) { try { // Since we are using HTML tables, we just handle this ourselves // CsvManager will however handle headers for ResultSet and TableModel data sets. hasHeaders = "y".equals(request.getParameter("headers")); csvtext = request.getParameter("csvtext"); if( null == csvtext ) { csvtext = ""; } String sep = request.getParameter("sep"); if( null != sep && !"".equals(sep) ) { csvspec.setSeparator(sep); } String tabsep = request.getParameter("tabsep"); if( null != tabsep ) { csvspec.setSeparator( "\t" ); useTab = true; } String quote = request.getParameter("quote"); if( null != quote && !"".equals(quote) ) { csvspec.setQuote(quote.charAt(0)); } String usequote = request.getParameter("usequote"); csvspec.setUseQuote( "y".equals(usequote) ); String doublequote = request.getParameter("doublequote"); csvspec.setDoubleQuote( "y".equals(doublequote) ); String escape = request.getParameter("escape"); if( null != escape && !"".equals(escape) ) { csvspec.setEscape(escape.charAt(0)); } String useescape = request.getParameter("useescape"); csvspec.setUseEscape( "y".equals(useescape) ); String useescapemap = request.getParameter("useescapemap"); csvspec.setUseEscapeMap( "y".equals(useescapemap) ); String ignoreempty = request.getParameter("ignoreempty"); csvspec.setIgnoreEmptyLines( "y".equals(ignoreempty) ); String startline = request.getParameter("startline"); try { csvspec.setStartLine( Long.parseLong(startline) ); } catch( Exception e ) {} String endline = request.getParameter("endline"); try { csvspec.setEndLine( Long.parseLong(endline) ); } catch( Exception e ) {} csvman.setCsvSpec( csvspec ); csvman.setIgnoreBadLines( true ); data = csvman.loadFromString( csvtext ); } catch( CsvManagerException csve ) { csvmanex = csve; csvmanerrmsg = csve.toString(); } } else { csvtext = (String) sDataText.get(sample); } System.out.println( csvtext ); %>

CSV Manager Demonstration

<% if( null != csvmanerrmsg ) { %>

Invalid Settings:

<%=csvmanerrmsg%>

<% } %>

You can use this page to try out CSV Manager. Just dump your CSV data into the CSV Data Input text area below. To get you started, we've provided some sets of sample data, so that you can see how CSV Manager works.

Like to know how this demo works? No problem, here's the source code for this page.

<% if( null != data ) { %>

Parsed Data:

<% String td; String trclass; String[] line; int maxfields = 0; for( int lineI = 0; lineI < data.size(); lineI++ ) { line = (String[]) data.get(lineI); if( line.length > maxfields ) { maxfields = line.length; } } for( int lineI = 0; lineI < data.size(); lineI++ ) { td = (hasHeaders && 0 == lineI) ? "th" : "td"; trclass = (1 == lineI % 2) ? "class=\"odd\"" : ""; line = (String[]) data.get(lineI); %> > <% for( int fieldI = 0; fieldI < maxfields; fieldI++ ) { %> <<%=td%>> <%=fieldI> <% } %> <% } %>

Parse Statistics:

Line Count:<%=csvman.getLineCount()%>
Bad Line Count:<%=csvman.getBadLineCount()%>
Avg Time/Line:<%=tf.format(csvman.getAverageTimePerLineInSeconds())%>s
Total Time:<%=tf.format(csvman.getTimeTakenInSeconds())%>s
<% if( 0 < csvman.getBadLineCount() ) { %>

Parse Errors:

<% List badlines = csvman.getBadLines(); for( Iterator badlineT = badlines.iterator(); badlineT.hasNext(); ) { BadLine badline = (BadLine) badlineT.next(); %> <% } %>
Line NumberError Message
<%=badline.getLineNumber()%><%=badline.getErrorMsg()%>
<% } else { %> <%@ include file="/products/csvman/inc/demo-am.htm" %> <% } %> <% } %>

Use these settings to control how your CSV data is parsed. Please note that this page requires JavaScript to work correctly.



CSV Data Input
checked<%}%>> Headers (in first line) Separator(s) checked<%}%> onclick="document.f.sep.value=this.checked?'':'<%=escapeHtml(csvspec.getSeparator().equals("\t")?",":csvspec.getSeparator())%>';"> Use TAB as Separator
" size="1"> Quote checked<%}%>> Use Quotes checked<%}%>> Double Quote Escape
" size="1"> Escape checked<%}%>> Use Escape checked<%}%>> Use Escape Map (\n, \r, \t, \b)
checked<%}%>> Ignore Empty Lines " size="5"> Start Line " size="5"> End Line
    <% for( Iterator sampleT = sSamples.iterator(); sampleT.hasNext(); ) { String sampleName = (String) sampleT.next(); %>
  • style="background:#fff;"<%}%>><%=sampleName%>
  • <% } %>


If this demonstration isn't working correctly with your data, let us know and we'll fix it for you.

<%@ include file="/products/csvman/inc/demo-am.htm" %>

* The source code for this page has deliberately been kept self-contained within the JSP page, but you will have to delete the include directives before it will work independently.

<%! private static DecimalFormat tf = new DecimalFormat( "#0.000" ); private static ArrayList sSamples = null; private static HashMap sDataText = null; private static HashMap sDataSpec = null; private static HashMap sHasHeaders = null; public static void makeDataMap() { ArrayList samples = new ArrayList(); HashMap dataText = new HashMap(); HashMap dataSpec = new HashMap(); HashMap hasHeaders = new HashMap(); CsvSpec csvspec; samples.add( "Planets" ); samples.add( "Passwords" ); samples.add( "Quotes" ); samples.add( "Escapes" ); samples.add( "Clear" ); String sn = "Planets"; dataText.put( sn , "Planet, Diameter (km), Orbit (AU), Period (days)\n" + "Mercury, 4880, 0.38, 87.97\n" + "Venus, 12104, 0.72, 224.70\n" + "Earth, 12756, 1.0, 365.26\n" + "Mars, 6794, 1.52, 686.98\n" + "Jupiter, 142984, 5.20, 4332.71\n" + "Saturn, 120536, 9.54, 10759.50\n" + "Uranus, 51118, 19.22, 30685.00\n" + "Neptune, 49532, 30.06, 60190.00\n" + "Pluto, 2274, 39.5, 90550.00\n" ); csvspec = new CsvSpec(); dataSpec.put( sn, csvspec ); hasHeaders.put( sn, new Boolean(true) ); sn = "Passwords"; dataText.put( sn ,"root:x:0:0:root:/root:/bin/bash\n" +"bin:x:1:1:bin:/bin:/sbin/nologin\n" +"daemon:x:2:2:daemon:/sbin:/sbin/nologin\n" +"richard:x:500:500:richard:/home/richard:/bin/bash" ); csvspec = new CsvSpec(); csvspec.setSeparator(":"); dataSpec.put( sn, csvspec ); hasHeaders.put( sn, new Boolean(false) ); sn = "Quotes"; dataText.put( sn ,"\"\"\"Always forgive your enemies; nothing annoys them so much.\"\"\"; Oscar Wilde\n" +"\"\"\"A large income is the best recipe for happiness I ever heard of.\"\"\"; Jane Austen\n" +"\"\"\"I sent the club a wire stating,\n 'PLEASE ACCEPT MY RESIGNATION. I DON'T WANT TO BELONG TO ANY CLUB THAT WILL ACCEPT ME AS A MEMBER.'\"\"\"; Groucho Marx\n" +"" ); csvspec = new CsvSpec(); csvspec.setSeparator(";"); dataSpec.put( sn, csvspec ); hasHeaders.put( sn, new Boolean(false) ); sn = "Escapes"; dataText.put( sn ,"File\tSize\tDescription\n" +"c:\\\\Program Files\\\\data\\\\data.csv\t16Kb\tAll\\\tmy\\\tdata\n" +"c:\\\\Program Files\\\\data\\\\book.txt\t87Kb\tMy notes for:\\nA Treatise on CSV\n" +"\n" +"Empty line\t\tabove to be ignored\n" ); csvspec = new CsvSpec(); csvspec.setSeparator("\t"); csvspec.setUseQuote( false ); csvspec.setDoubleQuote( false ); csvspec.setUseEscape( true ); csvspec.setUseEscapeMap( true ); csvspec.setIgnoreEmptyLines( true ); dataSpec.put( sn, csvspec ); hasHeaders.put( sn, new Boolean(true) ); sn = "Clear"; dataText.put( sn, "" ); csvspec = new CsvSpec(); dataSpec.put( sn, csvspec ); hasHeaders.put( sn, new Boolean(false) ); sSamples = samples; sDataText = dataText; sDataSpec = dataSpec; sHasHeaders = hasHeaders; } public String escapeHtml( String pSource ) { return escapeHtml( pSource, false ); } public String escapeHtml( String pSource, boolean pLineBreak ) { if( null == pSource ) { return ""; } int numChars = pSource.length(); StringBuffer sb = new StringBuffer( numChars + 22 ); char c; for( int charI = 0; charI < numChars; charI++ ) { c = pSource.charAt(charI); switch(c) { case '<': sb.append("<"); break; case '>': sb.append(">"); break; case '&': sb.append("&"); break; case '\"': sb.append("""); break; case '\n': sb.append( pLineBreak?"
":"\n"); break; default: sb.append(c); break; } } return sb.toString(); } public String escapeJS( String pSource ) { if( null == pSource ) { return ""; } int numChars = pSource.length(); StringBuffer sb = new StringBuffer( numChars + 22 ); char c; for( int charI = 0; charI < numChars; charI++ ) { c = pSource.charAt(charI); switch(c) { case '\'': sb.append("\\'"); break; case '\n': sb.append("\\n"); break; case '\\': sb.append("\\\\"); break; default: sb.append(c); break; } } String result = sb.toString(); return result; } %> <%@ include file="/inc/page-foot.htm" %> <%@ include file="/products/csvman/inc/sidebar.htm" %> <%@ include file="/inc/end.htm" %>