1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
|
/* ____ ____ ____ ____ ______ *\
** / __// __ \/ __// __ \/ ____/ SOcos COmpiles Scala **
** __\_ \/ /_/ / /__/ /_/ /\_ \ (c) 2002, LAMP/EPFL **
** /_____/\____/\___/\____/____/ **
\* */
// $Id$
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.
*
* @author Stephane Micheloud
* @version 1.1
*/
public class HTMLPrinter {
public static final String DEFAULT_STYLESHEET = "style.css";
//########################################################################
// Private Constants
/*
* The characters which must be HTML encoded
*/
private static final Character AMP = new Character('&');
private static final Character GT = new Character('>');
private static final Character LT = new Character('<');
private static final Character QUOT = new Character('"');
//########################################################################
// Private Fields
/** The underlying code printer */
private final CodePrinter printer;
/** The document title */
private final String title;
//########################################################################
// Protected Fields
/** The document representation */
protected final HTMLRepresentation representation;
//########################################################################
// Public Constructors
/**
* 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 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 with "iso-8859-1" as default character encoding.
*
* @param writer
* @param title
*/
public HTMLPrinter(Writer writer, String title) {
this(writer, title, "iso-8859-1");
}
//########################################################################
// Public Methods - Getting & Setting
/**
* Returns the underlying code printer.
*/
public CodePrinter getCodePrinter() {
return printer;
}
/**
* Returns the underlying title.
*/
public String getTitle() {
return title;
}
//########################################################################
// Public Methods - Character encoding
/**
* Encodes the special character <code>ch</code> to its
* corresponding HTML encoding.
*
* @param ch
*/
public static String encode(Character ch) {
if (AMP.compareTo(ch) == 0) return "&";
else if (GT.compareTo(ch) == 0) return ">";
else if (LT.compareTo(ch) == 0) return "<";
else if (QUOT.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.
*/
public HTMLPrinter indent() {
printer.indent();
return this;
}
/** Decreases the indentation level by one. */
public HTMLPrinter undent() {
printer.undent();
return this;
}
/** Inserts a new line. */
public HTMLPrinter line() {
printer.line();
return this;
}
/** Inserts a white space. */
public HTMLPrinter space() {
printer.space();
return this;
}
//########################################################################
// Public Methods - Printing simple values followed by a new line
/** Prints a new line. */
public HTMLPrinter println() {
printer.println();
return this;
}
/** Prints the boolean value followed by a new line. */
public HTMLPrinter println(boolean value) {
printer.println(value);
return this;
}
/** Prints the byte value followed by a new line. */
public HTMLPrinter println(byte value) {
printer.println(value);
return this;
}
/** Prints the short value followed by a new line. */
public HTMLPrinter println(short value) {
printer.println(value);
return this;
}
/** Prints the char value followed by a new line. */
public HTMLPrinter println(char value) {
printer.println(value);
return this;
}
/** Prints the int value followed by a new line. */
public HTMLPrinter println(int value) {
printer.println(value);
return this;
}
/** Prints the long value followed by a new line. */
public HTMLPrinter println(long value) {
printer.println(value);
return this;
}
/** Prints the float value followed by a new line. */
public HTMLPrinter println(float value) {
printer.println(value);
return this;
}
/** Prints the double value followed by a new line. */
public HTMLPrinter println(double value) {
printer.println(value);
return this;
}
/** Prints the string followed by a new line. */
public HTMLPrinter println(String value) {
printer.println(value);
return this;
}
/** Prints the opening HTML tag followed by a new line. */
public HTMLPrinter printlnOTag(String label) {
printer.print("<");
printer.print(label);
printer.println(">");
return this;
}
/** Prints the HTML tag with attributes followed by a new line. */
public HTMLPrinter printlnOTag(String label, XMLAttribute[] attrs) {
printer.print("<");
printer.print(label);
printer.print(" ");
printer.print(StringOf.object.array(attrs, "", " ", ""));
printer.println(">");
return this;
}
/** Prints the closing HTML tag followed by a new line. */
public HTMLPrinter printlnCTag(String label) {
printer.print("</");
printer.print(label);
printer.println(">");
return this;
}
/**
* 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 <code>label</code> with attributes <code>attrs</code>
* and contents <code>text</code> followed by a new line.
*
* @param label
* @param attrs
* @param text
*/
public HTMLPrinter printlnTag(String label, XMLAttribute[] attrs, String text) {
printOTag(label, attrs);
print(text);
return printlnCTag(label);
}
/**
* Prints the short HTML tag followed by a new line.
*
* @param label
*/
public HTMLPrinter printlnSTag(String label) {
printer.print("<");
printer.print(label);
printer.println("/>");
return this;
}
/**
* Prints the short HTML tag with attributes <code>attrs</code>
* followed by a new line.
*
* @param label
* @param attrs
*/
public HTMLPrinter printlnSTag(String label, XMLAttribute[] attrs) {
printer.print("<");
printer.print(label);
printer.print(" ");
printer.print(StringOf.object.array(attrs, "", " ", ""));
printer.println("/>");
return this;
}
/**
* Prints <A HREF=link> text </a> 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 <A HREF=dest TARGET=target> text </a> 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),
new XMLAttribute("target", target)});
print(text);
return printlnCTag("a");
}
/**
* Prints <A NAME=name> text </a> 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 <code>text</code> in bold followed by a new line.
*
* @param text
*/
public HTMLPrinter printlnBold(String text) {
return printlnTag("b", text);
}
/**
* Prints text <code>text</code> in color <code>color</code> followed by a new line.
*
* @param color
* @param text
*/
public HTMLPrinter printlnFontColor(String color, String text) {
return printlnTag(
"font",
new XMLAttribute[]{ new XMLAttribute("color", color)},
text);
}
/**
* Prints comment with contents <code>text</code> followed by a new line.
*
* @param text
*/
public HTMLPrinter printlnComment(String text) {
printer.print("<!--");
printer.print(text);
printer.println("-->");
return this;
}
/**
* Prints an horizontal line separator followed by a new line.
*/
public HTMLPrinter printlnHLine() {
return printlnOTag("hr");
}
/**
* Prints an horizontal line separator with attributes <code>attrs</code>.
*
* @param attrs
*/
public HTMLPrinter printlnHLine(XMLAttribute[] attrs) {
return printlnOTag("hr", attrs);
}
/**
* Prints the <meta> tag with attributes 'attrs' followed by a new line.
*
* @param attrs
*/
public HTMLPrinter printlnMeta(XMLAttribute[] attrs) {
return printlnOTag("meta", attrs);
}
/**
* Prints the <link> tag with attributes 'attrs' followed by a new line.
*
* @param attrs
*/
public HTMLPrinter printlnLink(XMLAttribute[] attrs) {
return printlnOTag("link", attrs);
}
//########################################################################
// Public Methods - Printing simple values
/** Prints the boolean value. */
public HTMLPrinter print(boolean value) {
printer.print(value);
return this;
}
/** Prints the byte value. */
public HTMLPrinter print(byte value) {
printer.print(value);
return this;
}
/** Prints the short value. */
public HTMLPrinter print(short value) {
printer.print(value);
return this;
}
/** Prints the char value. */
public HTMLPrinter print(char value) {
printer.print(value);
return this;
}
/** Prints the int value. */
public HTMLPrinter print(int value) {
printer.print(value);
return this;
}
/** Prints the long value. */
public HTMLPrinter print(long value) {
printer.print(value);
return this;
}
/** Prints the float value. */
public HTMLPrinter print(float value) {
printer.print(value);
return this;
}
/** Prints the long value. */
public HTMLPrinter print(double value) {
printer.print(value);
return this;
}
/** Prints the string. */
public HTMLPrinter print(String value) {
printer.print(value);
return this;
}
/** Prints the opening HTML tag. */
public HTMLPrinter printOTag(String label) {
printer.print("<");
printer.print(label);
printer.print(">");
return this;
}
/** Prints the opening HTML tag 'label' with attributes 'attrs'. */
public HTMLPrinter printOTag(String label, XMLAttribute[] attrs) {
printer.print("<");
printer.print(label);
printer.print(" ");
printer.print(StringOf.object.array(attrs, "", " ", ""));
printer.print(">");
return this;
}
/** Prints the closing HTML tag 'label'. */
public HTMLPrinter printCTag(String label) {
printer.print("</");
printer.print(label);
printer.print(">");
return this;
}
/**
* Prints the HTML tag <code>label</code> with contents <code>text</code>.
*
* @param label
* @param text
*/
public HTMLPrinter printTag(String label, String text) {
printOTag(label);
print(text);
return printCTag(label);
}
/** Prints the HTML tag 'label' with attributes 'attrs' and contents 'text'. */
public HTMLPrinter printTag(String label, XMLAttribute[] attrs, String text) {
printOTag(label, attrs);
print(text);
printCTag(label);
return this;
}
/** Prints the short HTML tag 'label'. */
public HTMLPrinter printSTag(String label) {
printer.print("<");
printer.print(label);
printer.print("/>");
return this;
}
/** Prints <A HREF=link> text </a> tag. */
public HTMLPrinter printAhref(String dest, String text) {
printOTag("a", new XMLAttribute[]{ new XMLAttribute("href", dest) });
print(text);
return printCTag("a");
}
/** Prints <A HREF=dest TARGET=target> text </a> tag. */
public HTMLPrinter printAhref(String dest, String target, String text) {
printOTag("a", new XMLAttribute[]{
new XMLAttribute("href", dest),
new XMLAttribute("target", target)});
print(text);
return printCTag("a");
}
/** Prints <A NAME=name> text </a> tag. */
public HTMLPrinter printAname(String anchor, String text) {
printOTag("a", new XMLAttribute[]{ new XMLAttribute("name", anchor) });
print(text);
return printCTag("a");
}
/**
* Prints text <code>text</code> in bold.
*
* @param text
*/
public HTMLPrinter printBold(String text) {
return printTag("b", text);
}
/**
* Prints text <code>text</code> in color <code>color</code>.
*
* @param color
* @param text
*/
public HTMLPrinter printFontColor(String color, String text) {
return printTag("font", new XMLAttribute[]{ new XMLAttribute("color", color) }, text);
}
/**
* Prints comment with contents <code>text</code>.
*
* @param text
*/
public HTMLPrinter printComment(String text) {
printer.print("<!-- ");
printer.print(text);
printer.print(" -->");
return this;
}
/**
* Prints <code>n</code> HTML blank spaces.
*
* @param n The parameter <code>n</code> gives the number
* of printed blank spaces
*/
public HTMLPrinter printNbsp(int n) {
while (n > 0) {
print(" ");
n--;
}
return this;
}
/**
* Prints an horizontal line separator.
*/
public HTMLPrinter printHLine() {
return printOTag("hr");
}
/**
* Prints an horizontal line separator with attributes <code>attrs</code>.
*
* @param attrs
*/
public HTMLPrinter printHLine(XMLAttribute[] attrs) {
return printOTag("hr", attrs);
}
//########################################################################
// Public Methods - Converting
/**
* Returns the string representation of this printer.
*/
public String toString() {
return printer.toString();
}
//########################################################################
/**
* Prints the HTML preamble of the current page.
*/
protected void printPreamble() {
println("<!DOCTYPE html PUBLIC \"-//W3C//DTD " +
representation.getType() + "//" + representation.getLanguage() + "\">");
printlnOTag("html").line();
}
/**
* 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
*/
protected void printMetaInfo(XMLAttribute[] metaAttrs) {
printlnMeta(new XMLAttribute[]{
new XMLAttribute("http-equiv", "content-type"),
new XMLAttribute("content",
"text/html; charset=" + representation.getEncoding())});
for (int i = 0; i < metaAttrs.length; i++) {
printlnMeta(new XMLAttribute[]{
new XMLAttribute("name", metaAttrs[i].name),
new XMLAttribute("content", metaAttrs[i].value)});
}
}
/**
* Prints HTML link information for style sheets.
*
* @param stylesheets A list of stylesheets 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])});
}
}
/**
* Prints HTML header section of the current page.
*
* @param metaAttrs
* @param generator
* @param stylesheets
*/
public void printHeader(XMLAttribute[] metaAttrs, String generator, String[] stylesheets) {
printPreamble();
printlnOTag("head").indent();
printlnTag("title", title);
printGeneratedBy(generator);
printMetaInfo(metaAttrs);
printStyles(stylesheets);
undent().printlnCTag("head").line();
}
/**
* Prints HTML header section.
*
* @param metaXMLAttributes
* @param generator
* @param stylesheet
*/
public void printHeader(XMLAttribute[] metaAttrs, String generator, String stylesheet) {
printHeader(metaAttrs, generator, new String[]{ stylesheet });
}
/**
* Prints HTML header section.
*
* @param metaXMLAttributes
* @param generator
*/
public void printHeader(XMLAttribute[] metaAttrs, String generator) {
printHeader(metaAttrs, generator, DEFAULT_STYLESHEET);
}
/**
* Prints the header section of the current page.
*
* @param metaXMLAttributes
*/
public void printHeader(XMLAttribute[] metaAttrs) {
printHeader(metaAttrs, null);
}
/**
* Open the body section of the current page.
*/
public void printOpenBody() {
printlnOTag("body").indent();
}
/**
* Prints the HTML footer of the current page.
*/
public void printFootpage() {
undent().printlnCTag("body");
printlnCTag("html");
}
//########################################################################
}
/**
* Map from Object to String.
*/
public abstract class StringOf {
/**
* Gives the string representation of an object.
*
* @param o
*/
public abstract String element(Object o);
/**
* Gives the string representation of an array of objects.
* Return delimiters for empty arrays depending on "delimWhenEmpty".
*
* @param objects
* @param open
* @param sep
* @param close
* @param delimWhenEmpty
*/
public String array(Object[] objects, String open, String sep, String close, boolean delimWhenEmpty) {
if ((objects.length == 0) && !delimWhenEmpty)
return "";
else {
StringBuffer str = new StringBuffer();
str.append(open);
if (objects.length > 0) {
str.append(element(objects[0]));
for(int i = 1; i < objects.length; i++)
str.append(sep + element(objects[i]));
}
str.append(close);
return str.toString();
}
}
/**
* Returns always delimiters.
*
* @param objects
* @param open
* @param sep
* @param close
*/
public String array(Object[] objects, String open, String sep, String close) {
return array(objects, open, sep, close, true);
}
/**
* Basic map.
*/
public static StringOf object = new StringOf() {
public String element(Object o) {
return o.toString();
}
};
}
|