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 private static Map newValuesMap=new LinkedHashMap(); 10 static { 11 newValuesMap.put("Name",new String [] {"Humphrey Bear"}); 12 newValuesMap.put("Title",new String [] {"Prime Minister"}); 13 newValuesMap.put("Member",new String [] {"on"}); 14 newValuesMap.put("Address",new String [] {"The Lodge\nDeakin ACT 2600\nAustralia"}); 15 newValuesMap.put("MailingList",new String [] {"A","B"}); 16 newValuesMap.put("FavouriteFair",new String [] {"honey"}); 17 newValuesMap.put("FavouriteSports",new String [] {"BB","AFL"}); 18 } 19 20 public static void main(String [] args) throws Exception { 21 String 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)); 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 format(Map valuesMap) { 40 StringBuffer sb=new StringBuffer (); 41 for (Iterator i=valuesMap.entrySet().iterator(); i.hasNext();) { 42 Map.Entry entry=(Map.Entry)i.next(); 43 String key=(String )entry.getKey(); 44 String [] values=(String [])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 |