Phones.java
Created with JBuilder
package phones;
import java.io.*;
import java.net.*;
import java.util.*;

/**
 * Class which creates HTML documents from an XJS and DTD document.
 *
 * @author  Justin Higa
 * @version 1.0
 */
public class Phones {
  static javax.xml.marshal.XMLScanner scanner;

  public static void setScanner (javax.xml.marshal.XMLScanner scanner) {
      Department.scanner = scanner;
  }

 /**
  * Reads in an xml file containing information about ta's and faculty
  * members within the ICS department. Two html pages are created. One page
  * displays a table of the faculty members with their name, office location,
  * phone numbers, and email address. The other page is a TA page with displays
  * their name, office location, office hours and courses they are ta's for.
  */
  public Phones () {
    try {
      InputStream stream = getClass ().getResourceAsStream ("ics.xml");
      try {
        Department ics = Department.unmarshal (stream);

        /* open 2 HTML files - TA and Faculty */
        PrintWriter faFP = new PrintWriter(new BufferedWriter(new FileWriter("faculty.html")));
        PrintWriter taFP = new PrintWriter(new BufferedWriter(new FileWriter("ta.html")));

        /* Start writing beginning of HTML pages */
        this.writeHTMLHeader(faFP, ics, "Faculty");
        this.writeTableHeading(faFP, "Faculty");
        this.writeHTMLHeader(taFP, ics, "TA");
        this.writeTableHeading(taFP, "TA");

        /* Person Info */
        Iterator iterator = ics.getPersons ().iterator ();
        int count = 0;
        while (iterator.hasNext ()) {
          count++;
          Person person = (Person) iterator.next ();
          /* Check Status - only create tables for TA's and Faculty */
          Iterator statusIterator = person.getStatus ().iterator ();
          while (statusIterator.hasNext()) {
            Status status = (Status) statusIterator.next();
            StatusType type = status.getType();
            if (type == type.FACULTY) {
              /* Create a faculty entry */
              faFP.println("    <tr>");
              faFP.println("      <td>" + this.getFullName(person) + "</td>");
              faFP.println("      <td>" + this.getLocations(person) + "</td>");
              faFP.println("      <td>" + this.getPhoneNumbers(person) + "</td>");
              faFP.println("      <td>" + this.getEmails(person) + "</td>");
              faFP.println("    </tr>");
            } else if (type == type.TA) {
              /* Create a TA entry */
              taFP.println("    <tr>");
              taFP.println("      <td>" + this.getFullName(person) + "</td>");
              taFP.println("      <td>" + this.getOfficeHours(person) + "</td>");
              taFP.println("      <td>" + this.getLocations(person) + "</td>");
              taFP.println("      <td>" + this.getCourses(person) + "</td>");
              taFP.println("    </tr>");
            }
          }
        }
        this.writeHTMLFooter(faFP);
        this.writeHTMLFooter(taFP);
      } catch (Exception error) {
        try {
          System.err.println ("Error \"" + error.getMessage ()
            + "\" on " + scanner.position ());

        } catch (Exception dummy) {}
        error.printStackTrace ();
      } finally {stream.close ();}
    } catch (Exception error) {error.printStackTrace ();}
  }

  /**
   * Returns a string of containing the full name of the person p. The middle
   * name is optional.
   *
   * @param     p Person object containing info about the person.
   * @return    full name of the person p.
   */
  private String getFullName (Person p) {
    String fullName = "";
    if (p.getMiddle() == null) {
      fullName = p.getFirst () + " " + p.getLast ();
    } else {
      fullName = p.getFirst () + " " + p.getMiddle() + " " + p.getLast ();
    }
    if (fullName.equals("")) fullName = " ";
    return fullName;
  }

  /**
   * Returns a string of courses.
   *
   * @param     p Person object containing info about the person.
   * @return    the list of courses for the person p.
   */
  private String getCourses (Person p) {
    String courses = "";
    Iterator coursesIterator = p.getCourses().iterator();
    while (coursesIterator.hasNext()) {
      Courses course = (Courses) coursesIterator.next();
      courses += course.getContent() + "<br>";
    }
    if (courses.equals("")) courses = " ";
    return courses;
  }

  /**
   * Returns a string office locations.
   *
   * @param     p Person object containing info about the person.
   * @return    the list of office locations
   */
  private String getLocations (Person p) {
    String locations = "";
    Iterator locationsIterator = p.getOfficeLocations().iterator();
    while (locationsIterator.hasNext()) {
      OfficeLocation location = (OfficeLocation) locationsIterator.next();
      locations += location.getLocation() + "<br>";
    }
    if (locations.equals("")) locations = " ";
    return locations;
  }

  /**
   * Returns a string of office hours.
   *
   * @param     p Person object containing info about the person.
   * @return    a string containing all the office hours in a Day: Hours
   *              format.
   */
  private String getOfficeHours (Person p) {
    String times = "";
    OfficeHours hours = p.getOfficeHours();
    if (hours != null) {
      Iterator timesIterator = hours.getTimes().iterator();
      while (timesIterator.hasNext()) {
        Times time = (Times) timesIterator.next();
        TimesType day = time.getDay();
        times += day + ": " + time.getHrs() + "<br>";
      }
    }
    if (times.equals("")) times = " ";
    return times;
  }

  /**
   * Returns a string of phone numbers.
   *
   * @param     p Person object containing info about the person.
   * @return    the list of phone numbers for the person p
   */
  private String getPhoneNumbers (Person p) {
    String phones = "";
    Iterator phonesIterator = p.getPhones().iterator ();
    while (phonesIterator.hasNext ()) {
      PhoneNumber phone = (PhoneNumber) phonesIterator.next ();
      PhoneType type = phone.getType();
      phones += type + ": " + phone.getNumber() + "<br>";
    }
    if (phones.equals("")) phones = " ";
    return phones;
  }

  /**
   * Returns a string of email addresses as hyper-links to the email address.
   *
   * @param     p Person object containing info about the person.
   * @return    the list of email addresses for the person p
   */
  private String getEmails (Person p) {
    String emails = "";
    Iterator emailsIterator = p.getEmails().iterator();
    while (emailsIterator.hasNext ()) {
      Email email = (Email) emailsIterator.next ();
      emails += "<a href=\"mailto:" + email.getAddress() + "\">" + email.getAddress() + "</a><br>";
    }
    if (emails.equals("")) emails = " ";
    return emails;
  }


  /**
   * Creates HTML code for the faculty/ta web page. Displays some department
   * information as well.
   *
   * @param     fp  file handle to the buffer stream.
   * @param     ics Department object that contains info about department
   * @param     type Type of page to generate header for
   */
  private void writeHTMLHeader (PrintWriter fp, Department ics, String type) {
    fp.println("<html>");
    fp.println("  <head><title>ICS Department Information</title>");
    fp.println("    <style type=\"text/css\">");
    fp.println("      .heading {font-weight: bold; text-align: center; background-color: #FFFF99;}");
    fp.println("    </style>");
    fp.println("  </head>");
    fp.println("  <body>");

    fp.println("  <p>");
    fp.println("  <h2>"+ ics.getName () + " Department Information:</h2>");
    fp.println("  Office Location: " + ics.getOfficeLocation() + "<br>");
    fp.println("  Office: " + ics.getOffice() + "<br>");
    fp.println("  Website: <a href=\"" + ics.getWebsite() + "\">" + ics.getWebsite() + "</a><br>");
    fp.println("  Email: <a href=\"mailto:" + ics.getEmail() + "\">" + ics.getEmail() + "</a><br>");
    fp.println("  </p>");
    fp.println("  <h3>" + type + " Information</h3>");
    fp.println("  <table cellpadding=\"5\" border=\"1\">");
  }

  /**
   * Creates HTML code for the table headings for either TA or Faculty
   *
   * @param     fp  file handle to the buffer stream.
   * @param     type Type of page to generate header for
   */
  private void writeTableHeading (PrintWriter fp, String type) {
    fp.println("    <tr class=\"heading\">");
    fp.println("      <td>Name</td>");
    if (type.equals("TA")) {
      fp.println("      <td>Office Locations</td>");
      fp.println("      <td>Office Hours</td>");
      fp.println("      <td>Courses</td>");
    } else if (type.equals("Faculty")) {
      fp.println("      <td>Office Locations</td>");
      fp.println("      <td>Phone Numbers</td>");
      fp.println("      <td>Email</td>");
    }
    fp.println("    </tr>");
  }

  /**
   * Creates HTML code for end of the faculty/ta web page.
   *
   * @param     fp  file handle to the buffer stream.
   */
  private void writeHTMLFooter (PrintWriter fp) {
    fp.println("  </table>");
    fp.println("  <br>");
    fp.println("  <a href=\"faculty.html\">Go to Faculty Information</a><br>");
    fp.println("  <a href=\"ta.html\">Go to TA Information</a>");
    fp.println("  </body>");
    fp.println("</html>");
    fp.close();
  }


  public static void main (String dummy []) {new Phones ();}

}


Phones.java
Created with JBuilder