import java.util.*; import java.io.*; class Html { /** * Html.java

* *

* * Revised based on Pat Durante's cgi_lib.java * * //@version 1.0 * //@author Pat L. Durante * * @version 1.0 aug-13-2002 * @author Xiannong Meng */ public static Hashtable ParseToken(String inBuffer) { Hashtable form_data = new Hashtable(); // // Split the name value pairs at the ampersand (&) // StringTokenizer pair_tokenizer = new StringTokenizer(inBuffer,"&"); while (pair_tokenizer.hasMoreTokens()) { String pair = urlDecode(pair_tokenizer.nextToken()); // // Split into key and value // StringTokenizer keyval_tokenizer = new StringTokenizer(pair,"="); String key = new String(); String value = new String(); if (keyval_tokenizer.hasMoreTokens()) key = keyval_tokenizer.nextToken(); else ; // ERROR - shouldn't ever occur if (keyval_tokenizer.hasMoreTokens()) value = keyval_tokenizer.nextToken(); else ; // ERROR - shouldn't ever occur // // Add key and associated value into the form_data Hashtable // form_data.put(key,value); } return form_data; } /** * * URL decode a string.

* * Data passed through the CGI API is URL encoded by the browser. * All spaces are turned into plus characters (+) and all "special" * characters are hex escaped into a %dd format (where dd is the hex * ASCII value that represents the original character). You probably * won't ever need to call this routine directly; it is used by the * ReadParse method to decode the form data. * * @param in The string you wish to decode. * * @return The decoded string. * */ public static String urlDecode(String in) { StringBuffer out = new StringBuffer(in.length()); int i = 0; int j = 0; while (i < in.length()) { char ch = in.charAt(i); i++; if (ch == '+') ch = ' '; else if (ch == '%') { ch = (char)Integer.parseInt(in.substring(i,i+2), 16); i+=2; } out.append(ch); j++; } return new String(out); } /** * * Generate a standard HTTP HTML header. * * @return A String containing the standard HTTP HTML header. * */ public static String Header() { return "Content-type: text/html\n\n"; } /** * * Generate some vanilla HTML that you usually * want to include at the top of any HTML page you generate. * * @param Title The title you want to put on the page. * * @return A String containing the top portion of an HTML file. * */ public static String HtmlTop(String Title) { String Top = new String(); Top = "\n"; Top+= "\n"; Top+= "\n"; Top+= Title; Top+= "\n"; Top+= "\n"; Top+= "\n"; Top+= "\n"; return Top; } /** * * Generate some vanilla HTML that you usually * want to include at the bottom of any HTML page you generate. * * @return A String containing the bottom portion of an HTML file. * */ public static String HtmlBot() { return "\n\n"; } /** * * Neatly format all of the form data using HTML. * * @param form_data The Hashtable containing the form data which was * parsed using the ReadParse method. * * @return A String containing an HTML representation of all of the * form variables and the associated values. * */ public static String Variables(Hashtable form_data) { String returnString; returnString = "

\n"; for (Enumeration e = form_data.keys() ; e.hasMoreElements() ;) { String key = (String)e.nextElement(); String value = (String)form_data.get(key); returnString += "
" + key + "
:" + value + ":
\n"; } returnString += "
\n"; return returnString; } }