160 likes | 172 Views
Making Text for the Web part 2. Barb Ericson Georgia Institute of Technology March 2006. Writing HTML. Now that you know basic HTML You can write a program that outputs an HTML page Use a BufferedWriter and FileWriter to write out the file From java.io
E N D
Making Text for the Webpart 2 Barb Ericson Georgia Institute of Technology March 2006 Georgia Institute of Technology
Writing HTML • Now that you know basic HTML • You can write a program that outputs an HTML page • Use a BufferedWriter and FileWriter to write out the file • From java.io • Use the write method to output the HTML • Use the newLine method to force a new line Georgia Institute of Technology
public class WebPageWriter { /** * Method to write an example web (HTML) page * @param fileName the file to write to */ public void writeExamplePage(String fileName) { // try the following try { // open a file for writing BufferedWriter writer = new BufferedWriter(new FileWriter(fileName)); // start the html page writer.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD " + "HTML 4.01 Transition//EN\""); writer.newLine(); writer.write("\"http://www.w3.org/TR/html4/loose.dtd\">"); writer.newLine(); writer.write("<html>"); writer.newLine(); // write out the header writer.write("<head><title>A simple web page" + "</title></head>"); writer.newLine(); // write out the body writer.write("<body>"); writer.newLine(); writer.write("<h1>A Simple Heading</h1>"); writer.newLine(); writer.write("<p>Some simple text</p>"); writer.newLine(); writer.write("</body>"); writer.newLine(); // end the page writer.write("</html>"); // close the writer writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } } Writing a Program to Generate HTML Georgia Institute of Technology
Write Example Page • Test this method • Compile the class • Execute the method • Check the resulting HTML page • But why would we want to do this? • Why write a program to output an HTML page that we could write by hand? Georgia Institute of Technology
Writing Programs that Output HTML • Write programs • To have reusable parts • To communicate process • To allow for tailoring • So breakup the writing of an HTML page • Into reusable parts that allow for tailoring • Break tasks into subtasks (procedural abstraction) • Create helper methods • Methods that help another method accomplish a task • These are usually private methods Georgia Institute of Technology
Method to Start an HTML Page /** * Method to write the doctype and html tags * @param writer the writer to use * @throws IOException */ private void writeStart(BufferedWriter writer) throws IOException { // write the document type writer.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD " + "HTML 4.01 Transition//EN\""); writer.newLine(); writer.write("\"http://www.w3.org/TR/html4/loose.dtd\">"); writer.newLine(); // start the html page writer.write("<html>"); writer.newLine(); } Georgia Institute of Technology
Write out the Heading and Title /** * Method to write the title out * @param writer the writer to use * @param title the title to use * @throws IOException */ private void writeHead(BufferedWriter writer, String title) throws IOException { writer.write("<head><title>" + title + "</title></head>"); writer.newLine(); } Georgia Institute of Technology
Start the Body /** * Method to write the body of the page * @param writer the writer to use * @param body the body to write * @throws IOException */ private void writeBody(BufferedWriter writer, String body) throws IOException { writer.write("<body>" + body + "</body>"); writer.newLine(); } Georgia Institute of Technology
Finish the Page /** * Method to finish the html page * @param writer the writer to use * @throws IOException */ private void writeEnd(BufferedWriter writer) throws IOException { writer.newLine(); writer.write("</body>"); writer.newLine(); writer.write("</html>"); } Georgia Institute of Technology
/** * Method for writing a homepage for the passed * name * @param name the person's name * @param interests a list of the person's interests */ public void writeHomepageV2(String name, String interests) { // try the following try { // open a file for writing BufferedWriter writer = new BufferedWriter(new FileWriter(name + ".html")); // write the start writeStart(writer); // write the header writeHead(writer,name + "'s Homepage"); // write the body writeBody(writer,"<h1>Welcome to " + name + "'s Homepage</h1>" + "<p> I am interested in " + interests); // end the page writeEnd(writer); // close the writer writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } Method to Write a Homepage Georgia Institute of Technology
How this Works • Methods can call other methods • If the other methods aren't needed outside the class make them private • These are called helper methods • When you call • writeHomepageV2("Barb Ericson","horseback riding, reading, and genealogy"); • This method will call the helper methods as it encounters them Georgia Institute of Technology
Create a Web page from a Directory • Get all the image files in a directory • Ending with .jpg • And write out an html page that has thumbnails (smaller versions) of the images • Use the helper methods • To write out the basics of the HTML page • Get the line separator for the current system • And use that to add a new line String endOfLine = System.getProperty("line.separator"); Georgia Institute of Technology
public void createImagePage(String directory) { String name = null; String body = ""; String endOfLine = System.getProperty("line.separator"); // try the following try { // create the File object File dir = new File(directory); // get the full path name of the directory String pathName = directory + dir.getName() + ".html"; BufferedWriter writer = new BufferedWriter(new FileWriter(pathName)); // write the start writeStart(writer); // write the head writeHead(writer,"Thumbnails from " + directory); // get the array of items in the directory String[] items = dir.list(); // loop through the array for (int i = 0; i < items.length; i++) { name = items[i]; if (name.indexOf(".jpg") >= 0) { body = body + "<p>Filename: " + name + "<img src='" + name + "' height='150'/></p>" + endOfLine; } } // write the body writeBody(writer,body); // write the end writeEnd(writer); // close the writer writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } Create Image Page Georgia Institute of Technology
Main for Testing Create Image Page public static void main(String[] args) { WebPageWriter writer = new WebPageWriter(); String dir = "c:/intro-prog-java/mediasources/"; writer.createImagePage(dir); } Georgia Institute of Technology
Exercise • Add a createImageTablePage method that puts 4 images in the first row of a table on a web page and then puts the file names in the row below the images • Add a createSoundPage that lists all the sound files in the mediasources directory in a bulleted list • Use a hyperlink around the name of the sound file and it will play when you click on it • <a href="soundFile.wav">soundFile.sav</a> Georgia Institute of Technology
Summary • You can use Java programs to create HTML • You can break up a long public method into smaller private methods • Procedural abstraction • HTML pages can be generated from information in a directory • Like all the pictures or sound files Georgia Institute of Technology