diff options
Diffstat (limited to 'sources/ch/epfl/lamp/util/HTMLPrinter.java')
-rw-r--r-- | sources/ch/epfl/lamp/util/HTMLPrinter.java | 126 |
1 files changed, 88 insertions, 38 deletions
diff --git a/sources/ch/epfl/lamp/util/HTMLPrinter.java b/sources/ch/epfl/lamp/util/HTMLPrinter.java index df2866fcc1..8904b97b1b 100644 --- a/sources/ch/epfl/lamp/util/HTMLPrinter.java +++ b/sources/ch/epfl/lamp/util/HTMLPrinter.java @@ -9,8 +9,6 @@ package ch.epfl.lamp.util; import java.io.Writer; -import java.text.SimpleDateFormat; -import java.util.Date; /** * This class provides methods to print HTML document. @@ -21,6 +19,7 @@ import java.util.Date; public class HTMLPrinter { public static final String DEFAULT_STYLESHEET = "style.css"; + public static final String DEFAULT_JAVASCRIPT = "script.js"; //######################################################################## // Private Constants @@ -59,6 +58,11 @@ public class HTMLPrinter { */ protected final String stylesheet; + /** + * The document script. + */ + protected final String script; + //######################################################################## // Public Constructors @@ -69,14 +73,30 @@ public class HTMLPrinter { * @param title * @param representation * @param stylesheet + * @param script */ public HTMLPrinter(Writer writer, String title, HTMLRepresentation representation, - String stylesheet) + String stylesheet, String script) { this.printer = new CodePrinter(writer, " "); this.title = title; this.representation = representation; this.stylesheet = stylesheet; + this.script = script; + } + + /** + * Creates a new instance. + * + * @param writer + * @param title + * @param representation + * @param stylesheet + */ + public HTMLPrinter(Writer writer, String title, HTMLRepresentation representation, + String stylesheet) + { + this(writer, title, representation, stylesheet, DEFAULT_JAVASCRIPT); } /** @@ -91,7 +111,8 @@ public class HTMLPrinter { } /** - * Creates a new instance with "HTML 4.01 Transitional" as default document type. + * Creates a new instance with "HTML 4.01 Transitional" as default + * document type. * * @param writer * @param title @@ -404,6 +425,19 @@ public class HTMLPrinter { } /** + * Prints script followed by a new line. + * + * @param script + */ + public HTMLPrinter printlnScript(String script) { + printOTag("script", new XMLAttribute[]{ + new XMLAttribute("type", "text/javascript"), + new XMLAttribute("src", script) + }); + return printlnCTag("script"); + } + + /** * Prints <code>n</code> HTML blank spaces followed by a new line. * * @param n The parameter <code>n</code> gives the number @@ -664,19 +698,6 @@ public class HTMLPrinter { } /** - * Prints a comment with generator name and generation date. - * - * @param generator - */ - protected void printGeneratedBy(String generator) { - if (generator != null) { - SimpleDateFormat df = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy"); - printlnComment("Generated by " + generator + " on " + - df.format(new Date())); - } - } - - /** * Prints HTML meta informations. * * @param metaAttrs @@ -694,18 +715,16 @@ public class HTMLPrinter { } /** - * Prints HTML link information for style sheets. + * Prints HTML link information for the given stylesheet. * - * @param stylesheets A list of stylesheets to be linked + * @param stylesheet The stylesheet to be linked * to the current HTML document */ - protected void printStyles(String[] stylesheets) { - for (int i = 0; i < stylesheets.length; i++) { - printlnLink(new XMLAttribute[]{ - new XMLAttribute("rel", "stylesheet"), - new XMLAttribute("type", "text/css"), - new XMLAttribute("href", stylesheets[i])}); - } + protected void printlnStyle(String stylesheet) { + printlnLink(new XMLAttribute[]{ + new XMLAttribute("rel", "stylesheet"), + new XMLAttribute("type", "text/css"), + new XMLAttribute("href", stylesheet)}); } /** @@ -715,13 +734,19 @@ public class HTMLPrinter { * @param generator * @param stylesheets */ - public void printHeader(XMLAttribute[] metaAttrs, String generator, String[] stylesheets) { + public void printHeader(XMLAttribute[] metaAttrs, + String[] comments, String[] stylesheets, String[] scripts) + { printPreamble(); printlnOTag("head").indent(); printlnTag("title", title); - printGeneratedBy(generator); + for (int i = 0; i < comments.length; i++) + printlnComment(comments[i]); printMetaInfo(metaAttrs); - printStyles(stylesheets); + for (int i = 0; i < stylesheets.length; i++) + printlnStyle(stylesheets[i]); + for (int i = 0; i < scripts.length; i++) + printlnScript(scripts[i]); undent().printlnCTag("head").line(); } @@ -732,8 +757,26 @@ public class HTMLPrinter { * @param generator * @param stylesheet */ - public void printHeader(XMLAttribute[] metaAttrs, String generator, String stylesheet) { - printHeader(metaAttrs, generator, new String[]{ stylesheet }); + public void printHeader(XMLAttribute[] metaAttrs, String comment, + String stylesheet, String script) + { + printHeader(metaAttrs, + new String[]{ comment }, + new String[]{ stylesheet }, + new String[]{ script }); + } + + /** + * Prints HTML header section. + * + * @param metaXMLAttributes + * @param generator + * @param stylesheet + */ + public void printHeader(XMLAttribute[] metaAttrs, String comment, + String stylesheet) + { + printHeader(metaAttrs, comment, stylesheet, script); } /** @@ -742,8 +785,8 @@ public class HTMLPrinter { * @param metaXMLAttributes * @param generator */ - public void printHeader(XMLAttribute[] metaAttrs, String generator) { - printHeader(metaAttrs, generator, stylesheet); + public void printHeader(XMLAttribute[] metaAttrs, String comment) { + printHeader(metaAttrs, comment, stylesheet); } /** @@ -752,22 +795,29 @@ public class HTMLPrinter { * @param metaXMLAttributes */ public void printHeader(XMLAttribute[] metaAttrs) { - printHeader(metaAttrs, null); + printHeader(metaAttrs, null); + } + + /** + * Open the body section of the current page. + */ + public void printOpenBody(XMLAttribute[] attrs) { + printlnOTag("body", attrs).indent(); } /** * Open the body section of the current page. */ public void printOpenBody() { - printlnOTag("body").indent(); + printlnOTag("body").indent(); } /** * Prints the HTML footer of the current page. */ public void printFootpage() { - undent().printlnCTag("body"); - printlnCTag("html"); + undent().printlnCTag("body"); + printlnCTag("html"); } //######################################################################## @@ -821,7 +871,7 @@ public abstract class StringOf { * @param close */ public String array(Object[] objects, String open, String sep, String close) { - return array(objects, open, sep, close, true); + return array(objects, open, sep, close, true); } /** |