import java.io.*; public class PageGenerator { public String title; public PageGenerator( String title ) { this.title = title; } public void writePage( String filename ) { String fullFilename = FileChooser.getMediaPath( filename+".html" ); try { BufferedWriter output = new BufferedWriter( new FileWriter(fullFilename) ); this.writePreamble( output ); this.writeHeader( output ); this.writeBody( output ); this.writePostamble( output ); output.close(); } catch (IOException ex) { System.out.println( "An error occurred reading the file " + filename ); } } // ----- standard helpers private void writePreamble( BufferedWriter output ) throws IOException { output.write( "" ); output.newLine(); output.write( "" ); output.newLine(); } private void writeHeader( BufferedWriter output ) throws IOException { output.write( "" ); output.newLine(); output.write( "" ); output.newLine(); output.write( title ); output.newLine(); output.write( "" ); output.newLine(); output.write( "" ); output.newLine(); } private void writePostamble( BufferedWriter output ) throws IOException { output.write( "" ); output.newLine(); } // ----- customized helpers private void writeBody( BufferedWriter output ) throws IOException { output.write( "" ); output.newLine(); this.writeComic( output ); this.writeQuote( output ); output.write( "" ); output.newLine(); } private void writeComic( BufferedWriter output ) throws IOException { FoxTrotDownloader comicReader = new FoxTrotDownloader(); String comicFilename = comicReader.getTodaysCartoon(); output.write( "

Today's FoxTrot

" ); output.newLine(); output.write( "
" ); output.newLine(); output.write( "\""" ); output.newLine(); output.write( "
" ); output.newLine(); } private void writeQuote( BufferedWriter output ) throws IOException { QuoteFileReader quoteReader = new QuoteFileReader(); quoteReader.loadQuotes( FileChooser.getMediaPath("quotes-sample.txt") ); String quote = quoteReader.getRandomQuote(); output.write( "

Today's Random Thought

" ); output.newLine(); output.write( "

" ); output.newLine(); output.write( quote ); output.newLine(); output.write( "

" ); output.newLine(); } }