%! 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 );
%>
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" %>