
/*
     WebPage.java
      Very simple code for producing web pages.
      This code needs lots of modification to set up default fonts, colors, etc.

      MAH 10/19/01
*/
import java.io.*;

public class WebPage {

     // StringBuffer is more efficient than String when you are doing lots of appends.
     StringBuffer header  = new StringBuffer();
     StringBuffer body = new StringBuffer();
     StringBuffer content  = new StringBuffer();
     StringBuffer footer = new StringBuffer();


     // default way of creating a web page
     WebPage () {

         makeDefaultHeader();
         makeDefaultBody();
         makeDefaultContent();
         makeDefaultFooter();

     }
    public void outputWebPage(String FileName) {

        try {
            PrintStream ps=  new PrintStream(new FileOutputStream(new File(FileName)));
            ps.println(header);
            ps.println(body);
            ps.println(content);
            ps.println(footer);
            ps.close();
        }
         catch (IOException ioe) {
                 System.err.println("Error in WebPage " + ioe.toString());
        }
 }

    public void makeHeader(String title) {

        header.append("<html><head><title>" );
        header.append(title);
        header.append("</title></head>");

    }
     public void makeDefaultHeader() {
            header.append(  "<html><head><title> Default Title </title></head>");
    }

    public void makeDefaultContent() {
        content.append( "<h4> Default Content</h4>");

    }

      public void addToContent(StringBuffer addition) {
            content.append(addition);
        }

      public void addToContent(String addition) {
          content.append(addition);
      }


    public void makeDefaultBody () {
        body.append( "<body>");

    }

    public StringBuffer makeURL(String loc, String label) {
        StringBuffer url = new StringBuffer("<a href=" );
        url.append(loc);
        url.append(">" );
        url.append(label);
        url.append("</a>");
        return url;
    }


    public void makeDefaultFooter() {
        footer.append( "</body></html>");
    }

    public static void main (String args[]) {

        String fname;
        WebPage w = new WebPage();
        /* This code shows you how to use the args[] parameter
         to accept input from the command line
         You can use this from textpad by checking the "prompt for parameters"
         box in the Run Java Application menu.  When you are prompted, leave
         the call to WebPage but add the name of the file to save out to after it.
         */

        if (args.length > 0)
           fname = args[0];
        else
            fname = "temp.html";

        System.out.println("Writing out web page to " + fname);
        w.addToContent("<p>");
        w.addToContent(w.makeURL("temp2.html","a simple link"));
        w.outputWebPage(fname);
    }

}