KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FormFieldCSVOutput


1 import au.id.jericho.lib.html.*;
2 import java.util.*;
3 import java.io.*;
4 import java.net.*;
5
6 public class FormFieldCSVOutput {
7     // newValuesMap is designed to emulate the data structure returned by the
8
// javax.servlet.ServletRequest.getParameterMap() method.
9
private static Map newValuesMap=new LinkedHashMap();
10     static {
11         newValuesMap.put("Name",new String JavaDoc[] {"Humphrey Bear"});
12         newValuesMap.put("Title",new String JavaDoc[] {"Prime Minister"});
13         newValuesMap.put("Member",new String JavaDoc[] {"on"});
14         newValuesMap.put("Address",new String JavaDoc[] {"The Lodge\nDeakin ACT 2600\nAustralia"});
15         newValuesMap.put("MailingList",new String JavaDoc[] {"A","B"});
16         newValuesMap.put("FavouriteFair",new String JavaDoc[] {"honey"});
17         newValuesMap.put("FavouriteSports",new String JavaDoc[] {"BB","AFL"});
18     }
19
20     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
21         String JavaDoc sourceUrlString="data/form.html";
22         if (args.length==0)
23           System.err.println("Using default argument of \""+sourceUrlString+'"');
24         else
25             sourceUrlString=args[0];
26         if (sourceUrlString.indexOf(':')==-1) sourceUrlString="file:"+sourceUrlString;
27         Source source=new Source(new URL(sourceUrlString));
28         source.setLogWriter(new OutputStreamWriter(System.err)); // send log messages to stderr
29
FormFields formFields=source.findFormFields();
30         Writer out=new FileWriter("FormData.csv");
31         Util.outputCSVLine(out,formFields.getColumnLabels());
32         Util.outputCSVLine(out,formFields.getColumnValues(newValuesMap));
33         out.close();
34         System.err.println("\nThe following form submission data has been output to the CSV file \nFormData.csv, based on the data structure defined in the HTML document \n"+sourceUrlString+'\n');
35         System.err.println(format(newValuesMap));
36         System.err.println("The FormData.csv file will open automatically after you press a key.");
37   }
38
39     private static String JavaDoc format(Map valuesMap) {
40         StringBuffer JavaDoc sb=new StringBuffer JavaDoc();
41         for (Iterator i=valuesMap.entrySet().iterator(); i.hasNext();) {
42             Map.Entry entry=(Map.Entry)i.next();
43             String JavaDoc key=(String JavaDoc)entry.getKey();
44             String JavaDoc[] values=(String JavaDoc[])entry.getValue();
45             sb.append(key).append(":\n");
46             for (int j=0; j<values.length; j++) sb.append("- ").append(values[j]).append('\n');
47             sb.append('\n');
48         }
49         return sb.toString();
50     }
51 }
52
Popular Tags