From 2b1afe846e3b1632538cf8135a20c64339c7b759 Mon Sep 17 00:00:00 2001 From: michelou Date: Tue, 26 Aug 2003 15:42:11 +0000 Subject: - improved javadoc comments - added encode methods --- sources/ch/epfl/lamp/util/HTMLPrinter.java | 211 +++++++++++++++++++++++++---- 1 file changed, 182 insertions(+), 29 deletions(-) diff --git a/sources/ch/epfl/lamp/util/HTMLPrinter.java b/sources/ch/epfl/lamp/util/HTMLPrinter.java index 884b7b2703..04662bb910 100644 --- a/sources/ch/epfl/lamp/util/HTMLPrinter.java +++ b/sources/ch/epfl/lamp/util/HTMLPrinter.java @@ -22,6 +22,15 @@ public class HTMLPrinter { public static final String DEFAULT_STYLESHEET = "style.css"; + //######################################################################## + // Private Constants + + /* + * The characters which must be HTML encoded + */ + private static final Character LT = new Character('<'); + private static final Character GT = new Character('>'); + //######################################################################## // Private Fields @@ -40,21 +49,38 @@ public class HTMLPrinter { //######################################################################## // Public Constructors - /** Creates a new instance */ + /** + * Creates a new instance. + * + * @param writer + * @param title + * @param representation + */ public HTMLPrinter(Writer writer, String title, HTMLRepresentation representation) { this.printer = new CodePrinter(writer, " "); this.title = title; this.representation = representation; } - /** Creates a new instance */ + /** + * Creates a new instance with "HTML 4.01 Transitional" as default document type. + * + * @param writer + * @param title + * @param encoding + */ public HTMLPrinter(Writer writer, String title, String encoding) { this.printer = new CodePrinter(writer, " "); this.title = title; this.representation = new HTMLRepresentation("HTML 4.01 Transitional", encoding); } - /** Creates a new instance */ + /** + * Creates a new instance with "iso-8859-1" as default character encoding. + * + * @param writer + * @param title + */ public HTMLPrinter(Writer writer, String title) { this(writer, title, "iso-8859-1"); } @@ -62,20 +88,50 @@ public class HTMLPrinter { //######################################################################## // Public Methods - Getting & Setting - /** Returns the underlying code printer. */ + /** + * Returns the underlying code printer. + */ public CodePrinter getCodePrinter() { return printer; } - /** Returns the underlying title. */ + /** + * Returns the underlying title. + */ public String getTitle() { return title; } + //######################################################################## + // Public Methods - Character encoding + + /** + * Encodes the special character ch to its + * corresponding HTML encoding. + * + * @param ch + */ + public static String encode(Character ch) { + if (LT.compareTo(ch) == 0) return "<"; + else if (GT.compareTo(ch) == 0) return ">"; + else return ch.toString(); + } + + /** + * Encodes the special characters to their corresponding HTML encodings. + * + * @param text + */ + public static String encode(String text) { + return text; + } + //######################################################################## // Public Methods - Formatting - /** Increases the indentation level by one. */ + /** + * Increases the indentation level by one. + */ public HTMLPrinter indent() { printer.indent(); return this; @@ -188,15 +244,25 @@ public class HTMLPrinter { return this; } - /** Prints HTML tag with label and contents followed by a new line. */ + /** + * Prints HTML tag with label and contents followed by a new line. + * + * @param label + * @param text + */ public HTMLPrinter printlnTag(String label, String text) { printOTag(label); print(text); return printlnCTag(label); } - /** Prints the HTML tag 'label' with attributes 'attrs' and contents 'text' - * followed by a new line. + /** + * Prints the HTML tag label with attributes attrs + * and contents text followed by a new line. + * + * @param label + * @param attrs + * @param text */ public HTMLPrinter printlnTag(String label, XMLAttribute[] attrs, String text) { printOTag(label, attrs); @@ -204,7 +270,9 @@ public class HTMLPrinter { return printlnCTag(label); } - /** Prints the short HTML tag followed by a new line. + /** + * Prints the short HTML tag followed by a new line. + * * @param label */ public HTMLPrinter printlnSTag(String label) { @@ -214,7 +282,13 @@ public class HTMLPrinter { return this; } - /** Prints the short HTML tag with attributes 'attrs' followed by a new line. */ + /** + * Prints the short HTML tag with attributes attrs + * followed by a new line. + * + * @param label + * @param attrs + */ public HTMLPrinter printlnSTag(String label, XMLAttribute[] attrs) { printer.print("<"); printer.print(label); @@ -224,14 +298,25 @@ public class HTMLPrinter { return this; } - /** Prints text tag followed by a new line. */ + /** + * Prints text tag followed by a new line. + * + * @param dest + * @param text + */ public HTMLPrinter printlnAhref(String dest, String text) { printOTag("a", new XMLAttribute[]{ new XMLAttribute("href", dest) }); print(text); return printlnCTag("a"); } - /** Prints text tag followed by a new line. */ + /** + * Prints text tag followed by a new line. + * + * @param dest + * @param target + * @param text + */ public HTMLPrinter printlnAhref(String dest, String target, String text) { printOTag("a", new XMLAttribute[]{ new XMLAttribute("href", dest), @@ -240,26 +325,45 @@ public class HTMLPrinter { return printlnCTag("a"); } - /** Prints text tag followed by a new line. */ + /** + * Prints text tag followed by a new line. + * + * @param anchor + * @param text + */ public HTMLPrinter printlnAname(String anchor, String text) { printOTag("a", new XMLAttribute[]{new XMLAttribute("name", anchor)}); print(text); return printlnCTag("a"); } - /** Prints text 'text' in bold followed by a new line. */ + /** + * Prints text text in bold followed by a new line. + * + * @param text + */ public HTMLPrinter printlnBold(String text) { - printlnTag("b", text); - return this; + return printlnTag("b", text); } - /** Prints text 'text' in color 'color' followed by a new line. */ + /** + * Prints text text in color color followed by a new line. + * + * @param color + * @param text + */ public HTMLPrinter printlnFontColor(String color, String text) { - printlnTag("font", new XMLAttribute[]{new XMLAttribute("color", color)}, text); - return this; + return printlnTag( + "font", + new XMLAttribute[]{ new XMLAttribute("color", color)}, + text); } - /** Prints comment with contents 'text' followed by a new line. */ + /** + * Prints comment with contents text followed by a new line. + * + * @param text + */ public HTMLPrinter printlnComment(String text) { printer.print("