import java.io.*; import java.util.HashMap; import java.util.Iterator; import java.util.ListIterator; import java.util.TreeSet; import java.util.Collection; import java.util.StringTokenizer; import java.util.LinkedList; /** * HTMLUtils is a collection static methods that can be used to print out HTML constructs * such as form widgets and tables. All methods accept PrintWriter objects to print out * to. */ public class HTMLUtils { /** * Prints out meta-tags regcogized by various browsers, telling them * not to cache the page. * @param out The PrintWriter to print to. * @exception IOException if an error occurs while writing to the PrintWriter. */ public static void printExpireTags(PrintWriter out) throws IOException { out.println(""); out.println(""); out.println(""); } /** * Prints out a page that will redirect the user to another page using JavaScript. * @param out The PrintWriter to print to. * @param msg A message to display to the user specific to why a redirection is occuring. * @param url The absolute or relative path to redirect the user to. * @exception IOException if an error occurs while writing to the PrintWriter. */ public static void printRedirectPage(PrintWriter out, String msg, String url) throws IOException { printExpireTags(out); out.println(""); out.println("Redirection Page"); out.println(""); out.println(""); out.println(""); out.println("
"); out.println("
"); out.println("Contact us | Help"); out.println("
"); out.println(msg + "
"); out.println("You should be automatically redirected to an appropriate page in a few seconds.
"); out.println("If you are not automatically redirected, click here."); out.println("
"); } /** * Prints out a standard header to an HTML page. Used to print out the same header * on each page of the advisement system. * @param out The PrintWriter to print to. * @param title A message to display in the title bar of the browser. * @exception IOException if an error occurs while writing to the PrintWriter. */ public static void printHeader(PrintWriter out, String title) throws IOException { printExpireTags(out); out.println(""); out.println(title); out.println(""); //out.println(""); out.println(""); out.println("
"); out.println("
"); out.println("Contact us | Help"); out.println("
"); } /** * Prints out an HTML table to an HTML Page. See the system documentation * on the frontend for an explaination of how to use this method. * @param out The PrintWriter to print to. * @param format A collection of String objects. See system documentation for details. * @exception IOException if an error occurs while writing to the PrintWriter. */ public static void printTable(PrintWriter out, Collection format) throws IOException { Iterator iter = format.iterator(); boolean nextIsHeader = false; boolean isFirstTable = true; out.println("
"); while(iter.hasNext()) { String line = (String) iter.next(); StringTokenizer k = new StringTokenizer(line, "|"); LinkedList ll = new LinkedList(); while(k.hasMoreTokens()) ll.addLast(k.nextToken()); if(ll.isEmpty()) continue; ListIterator llIter = ll.listIterator(); String elem = (String) llIter.next(); if(elem.equals("*") && ll.size() == 3) { if(!isFirstTable) out.println(""); else isFirstTable = false; out.println("


"); elem = (String) llIter.next(); out.println("

"); out.println("

" + elem + "

"); elem = (String) llIter.next(); nextIsHeader = elem.equals("y"); out.println(""); continue; } else llIter.previous(); out.println(""); int i=0; String colors[] = { "cfcfcf", "efefef" }; while(llIter.hasNext()) { if(nextIsHeader) out.print(""); //out.print(llIter.next()); printList(out, (String)llIter.next()); if(nextIsHeader) out.println(""); else out.println(""); i = (i == 0) ? 1 : 0; } nextIsHeader = false; out.println(""); } out.println("
"); } private static void printList(PrintWriter out, String str) { StringTokenizer k = new StringTokenizer(str, ","); if(k.countTokens() == 1) { out.println(str); return; } out.println(""); } /** * Prints out an HTML form select (Usually rendered natively as a combo box). * @param out The PrintWriter to print to. * @param description A message to be displayed on top of the select object. * @param name The name field of the select object. * @param options A HashMap mapping option names to values. * @param defSelection The option selected by default (this can be null). * @exception IOException if an error occurs while writing to the PrintWriter. */ public static void printSelect(PrintWriter out, String description, String name, HashMap options, String defSelection) throws IOException { out.println(description + "
"); out.println(""); } /** * Prints out an HTML form input object. Type is selectable by parameter. * @param out The PrintWriter to print to. * @param type The type of the input object (ex. text, password, button, etc.) * @param description A message to appear on top of the input object. * @param name The name field of the input object. * @param sideText A String to appear on the side of the input object. (can be null) * @param size the number of characters wide the input object is. * @param maxSize the maximum number of characters allowed to be entered. * @exception IOException if an error occurs while writing to the PrintWriter. */ public static void printTextBox(PrintWriter out, String type, String description, String name, String sideText, String size, String maxSize) throws IOException { out.println(description + "
"); out.print(""); if(sideText != null) out.println(sideText); else out.println(); } /** * Prints out a standard footer to an HTML page. Used to print out the same footer * on each page of the advisement system. * @param out The PrintWriter to print to. * @exception IOException if an error occurs while writing to the PrintWriter. */ public static void printFooter(PrintWriter out) throws IOException { out.println("
"); out.println(" Click here to logout
"); out.println("
"); out.println(""); } /** * Prints out an error message in a new paragraph. * @param out The PrintWriter to print to. * @param errorMsg The message to print. * @exception IOException if an error occurs while writing to the PrintWriter. */ public static void printError(PrintWriter out, String errorMsg) throws IOException { out.println("

"); out.println("An error occured while processing your request:
"); out.println(errorMsg); out.println("
"); } /** * Prints out an exception and its stack trace to the HTML page. * @param out The PrintWriter to print to. * @param e the exception to print. * @exception IOException if an error occurs while writing to the PrintWriter. */ public static void printException(PrintWriter out, Exception e) throws IOException { if(out == null) return; StackTraceElement[] ste = e.getStackTrace(); out.println("

"); out.println("An error occured while processing your request:
"); out.println(e.getMessage()); out.println("
"); for(int i=0; i < ste.length; ++i) { out.print(ste[i].toString()); out.println("
"); } } }