KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > FormFieldSetValues


1 import au.id.jericho.lib.html.*;
2 import java.util.*;
3 import java.io.*;
4 import java.net.*;
5
6 public class FormFieldSetValues {
7     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
8         String JavaDoc sourceUrlString="data/form.html";
9         if (args.length==0)
10           System.err.println("Using default argument of \""+sourceUrlString+'"');
11         else
12             sourceUrlString=args[0];
13         if (sourceUrlString.indexOf(':')==-1) sourceUrlString="file:"+sourceUrlString;
14         Source source=new Source(new URL(sourceUrlString));
15         source.setLogWriter(new OutputStreamWriter(System.err)); // send log messages to stderr
16
FormFields formFields=source.findFormFields();
17         formFields.clearValues(); // clear any values that might be set in the source document
18
formFields.addValue("Name","Humphrey Bear");
19         formFields.addValue("Title","Prime Minister");
20         formFields.addValue("Member","on");
21         formFields.addValue("Address","The Lodge\nDeakin ACT 2600\nAustralia");
22         formFields.addValue("MailingList","A");
23         formFields.addValue("MailingList","B");
24         formFields.addValue("FavouriteFair","honey");
25         formFields.addValue("FavouriteSports","BB");
26         formFields.addValue("FavouriteSports","AFL");
27         OutputDocument outputDocument=new OutputDocument(source);
28         outputDocument.replace(formFields); // adds all segments necessary to effect changes
29

30         // modify stylesheet link since the output file is in a different directory to the input file
31
int cssPathPos=source.toString().indexOf("main.css");
32         outputDocument.insert(cssPathPos,"data/");
33
34         Writer out=new FileWriter("NewForm.html");
35         outputDocument.writeTo(out);
36         out.close();
37         System.err.println("\nThe form containing new default values has been output to NewForm.html");
38         System.err.println("This will open automatically in a web browser after you press a key.");
39   }
40 }
41
Popular Tags