Chris Pollett >
Old Classes
> |
HW1 Solutions Page// CourseMaker.java -- generator of course html files import java.io.*; import java.util.*; /** This class contains an application that may be used to generate an HTML syllabus for a course. It is run from the command line as: java CourseMaker coursefile Here coursefile is the name of a file containing the course data as specified in the homework 1 description. The output HTML file will be called coursefile.html This class contains one independent useful static method, generateCourseHTMLFile(fileName) which does the above HTML generation but could be called from other classes. */ public class CourseMaker { /** Reads and parse filename and outputs an HTML document course syllabus called filename.html @param filename - name of file with course data */ public static void generateCourseHTMLFile(String filename) { BufferedReader inputReader; PrintWriter outputWriter; try { inputReader = new BufferedReader( new FileReader(filename)); outputWriter = new PrintWriter( new BufferedWriter(new FileWriter(filename+".html"))); outputWriter.println("<html>"); String courseName = generateHead(inputReader, outputWriter); generateBody(inputReader, outputWriter, courseName); outputWriter.println("</html>"); inputReader.close(); outputWriter.close(); } catch(IOException ic) { quitWithMessage("Error in generating whole file.", ic); } } /* Driver for our application. @param args - args[0] is the name of the course we wish to print out. */ public static void main(String[] args) { if(args.length != 1) { System.err.println("This program should be run from"); System.err.println("the command line with a line like:"); System.err.println("\njava CourseMaker coursefile\n"); System.err.println("where coursefile contains the course data."); System.exit(1); } generateCourseHTMLFile(args[0]); } /* Read courseName from in Reader and output the head of an HTML document with courseName as time and as a keyword to out. Also, outputs Author, Content-Type and Robots meta tags. @param in - where we get data from @param out - where we write HTML to */ private static String generateHead(BufferedReader in, PrintWriter out) { String courseName = null; try { courseName = in.readLine(); out.println("<head>"); out.println(" <title>"+courseName+"</title>\n"); out.println(" <meta http-equiv=\"Content-Type\""+ "content=\"text/html\" />"); out.println(" <meta name=\"Authors\" content=\"CourseMaker\" />"); out.println(" <meta name=\"keywords\" content=\""+ courseName+"\"/>"); out.println(" <meta name=\"Robots\" content=\"NOINDEX,NOFOLLOW\"/>"); out.println("</head>"); } catch(IOException io) { quitWithMessage("Error generating head of output file.", io); } return courseName; } /* Uses the supplied courseName as well as data from the Reader in to generate the HTML for the body of the course syllabus document. @param in - where we get data from @param out - where we write HTML to @param courseName - name of course we are generating the HTML info of */ private static void generateBody(BufferedReader in, PrintWriter out, String courseName) { out.println("<body style=\"background:white\">"); out.println("<div>\n\n" +"<table style=\"background: #EEEEEE\">\n" +"<tr><td width=\"400\">"); out.println("<h3>" + courseName + "</h3>"); generateProfInfo(in, out); generatePrereqList(in, out); generateRequiredText(in, out); generateGradeBreakdown(in, out); generateTopics(in, out); out.println("\n\n</td></tr>\n</table>\n</div>"); out.println("</body>"); } /* Uses in to read in info on the professor for the course and then write out this data using HTML to out. @param in - where we get data from @param out - where we write HTML to */ private static void generateProfInfo(BufferedReader in, PrintWriter out) { try { out.println("<table border=\"0\" cellspacing=\"0\">"); out.println("<tr><td><b>Instructor:</b>"+in.readLine()); out.println("</td></tr>"); out.println("<tr><td><b>Office:</b>"+in.readLine()); out.println("</td></tr>"); out.println("<tr><td><b>Office Hours:</b>"+in.readLine()); out.println("</td></tr>"); out.println("</table>"); } catch(IOException io) { quitWithMessage("Error generating prof info of output file.",io); } } /* Uses in to read in a list of prereqs for the course and then write this out as an unordered list using HTML to out. The prereqs is in the format indicated in the HW description. @param in - where we get data from @param out - where we write HTML to */ private static void generatePrereqList(BufferedReader in, PrintWriter out) { try { out.println("<h4>Prerequisites:</h4>"); out.println("<ul>"); boolean morePrereqs = true; while(morePrereqs) { StringTokenizer tokens = new StringTokenizer(in.readLine(),":"); while(tokens.hasMoreTokens() && morePrereqs) { String prereq = tokens.nextToken(); if(prereq.equals("#endprereqs")) morePrereqs = false; else out.println("<li>"+prereq+"</li>"); } } out.println("</ul>"); } catch(IOException io) { quitWithMessage("Error generating prereq list of output file.",io); } } /* Uses in to read in a line containing the textbook for the course and then write out this textbook using HTML to out. @param in - where we get data from @param out - where we write HTML to */ private static void generateRequiredText(BufferedReader in, PrintWriter out) { try { out.println("<h4>Required Text:</h4>"); out.println("<p style=\"margin-left:.3in\">"); out.println(in.readLine()); out.println("</p>"); } catch(IOException io) { quitWithMessage("Error generating required text of output file.",io); } } /* Uses in to read in a grade breakdown table for the course and then write out this table using HTML to out. The table is in the format indicated in the HW description. @param in - where we get data from @param out - where we write HTML to */ private static void generateGradeBreakdown(BufferedReader in, PrintWriter out) { try { out.println("<h4>Grade Breakdown:</h4>"); out.println("<div style=\"margin-left:.3in\">"); out.println("<table border=\"0\" cellspacing=\"0\">"); boolean moreParts=true; boolean greyTone=true; while(moreParts) { String part = in.readLine(); if(part.equals("#end grade components")) moreParts = false; else { if(greyTone) out.println("<tr style=\"background:#CCCCCC\">"); else out.println("<tr style=\"background:#DDDDDD\">"); StringTokenizer tokens = new StringTokenizer(part); out.println("<td><b>"+tokens.nextToken()+"</b></td>"); out.println("<td>"+tokens.nextToken()+"</td>"); out.println("</tr>"); greyTone = !greyTone; } } out.println("</table>\n</div>"); } catch(IOException io) { quitWithMessage("Error generating grade breakdown of output file.",io); } } /* Uses in to read in topics paragraph for the course and then write out this paragraph using HTML to out. @param in - where we get data from @param out - where we write HTML to */ private static void generateTopics(BufferedReader in, PrintWriter out) { String line; try { out.println("<h4>Topics:</h4>\n<p>"); while( (line = in.readLine()) !=null) { out.println(line); } out.println("</p>"); } catch(IOException io) { quitWithMessage("Error generating topics of output file.", io); } } /* Output error message and quit @param mesg - message to send to err @param io - exception that caused us to want to terminate. */ private static void quitWithMessage(String mesg, IOException io) { System.err.println(mesg); io.printStackTrace(); System.exit(1); } } |