API Overview API Index Package Overview Direct link to this page
JDK 1.6
  javax.swing.text.html. HTMLWriter View Javadoc
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
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259

/*
 * @(#)HTMLWriter.java	1.40 06/04/07
 *
 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
 * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package javax.swing.text.html;

import javax.swing.text.*;
import java.io.Writer;
import java.util.Stack;
import java.util.Enumeration;
import java.util.Vector;
import java.io.IOException;
import java.util.StringTokenizer;
import java.util.NoSuchElementException;
import java.net.URL;

/**
 * This is a writer for HTMLDocuments.
 *
 * @author  Sunita Mani
 * @version 1.26, 02/02/00
 */


public class HTMLWriter extends AbstractWriter {
    /*
     * Stores all elements for which end tags have to
     * be emitted.
     */
    private Stack blockElementStack = new Stack();
    private boolean inContent = false;
    private boolean inPre = false;
    /** When inPre is true, this will indicate the end offset of the pre
     * element. */
    private int preEndOffset;
    private boolean inTextArea = false;
    private boolean newlineOutputed = false;
    private boolean completeDoc;

    /*
     * Stores all embedded tags. Embedded tags are tags that are
     * stored as attributes in other tags. Generally they're
     * character level attributes.  Examples include
     * <b>, <i>, <font>, and <a>. 
     */
    private Vector tags = new Vector(10);

    /**
     * Values for the tags.
     */
    private Vector tagValues = new Vector(10);

    /**
     * Used when writing out content.
     */
    private Segment segment;

    /*
     * This is used in closeOutUnwantedEmbeddedTags.
     */
    private Vector tagsToRemove = new Vector(10);

    /**
     * Set to true after the head has been output.
     */
    private boolean wroteHead;

    /**
     * Set to true when entities (such as <) should be replaced.
     */
    private boolean replaceEntities;

    /**
     * Temporary buffer.
     */
    private char[] tempChars;


    /**
     * Creates a new HTMLWriter.
     *
     * @param w   a Writer
     * @param doc  an HTMLDocument
     *
     */
    public HTMLWriter(Writer w, HTMLDocument doc) {
	this(w, doc, 0, doc.getLength());
    }

    /**
     * Creates a new HTMLWriter.
     *
     * @param w  a Writer
     * @param doc an HTMLDocument
     * @param pos the document location from which to fetch the content
     * @param len the amount to write out
     */
    public HTMLWriter(Writer w, HTMLDocument doc, int pos, int len) {
	super(w, doc, pos, len);
	completeDoc = (pos == 0 && len == doc.getLength());
	setLineLength(80);
    }

    /**
     * Iterates over the 
     * Element tree and controls the writing out of
     * all the tags and its attributes.
     *
     * @exception IOException on any I/O error
     * @exception BadLocationException if pos represents an invalid
     *            location within the document.
     *
     */
    public void write() throws IOException, BadLocationException {
	ElementIterator it = getElementIterator();
	Element current = null;
	Element next = null;

	wroteHead = false;
	setCurrentLineLength(0);
	replaceEntities = false;
	setCanWrapLines(false);
	if (segment == null) {
	    segment = new Segment();
	}
	inPre = false;
        boolean forcedBody = false;
	while ((next = it.next()) != null) {
	    if (!inRange(next)) {
                if (completeDoc && next.getAttributes().getAttribute(
                        StyleConstants.NameAttribute) == HTML.Tag.BODY) {
                    forcedBody = true;
                }
                else {
                    continue;
                }
	    }
	    if (current != null) {
		
		/*
		  if next is child of current increment indent
		*/

		if (indentNeedsIncrementing(current, next)) {
                    incrIndent();
		} else if (current.getParentElement() != next.getParentElement()) {
		    /*
		       next and current are not siblings
		       so emit end tags for items on the stack until the
		       item on top of the stack, is the parent of the
		       next.
		    */
		    Element top = (Element)blockElementStack.peek();
		    while (top != next.getParentElement()) {
			/*
			   pop() will return top.
			*/
			blockElementStack.pop();
			if (!synthesizedElement(top)) {
                            AttributeSet attrs = top.getAttributes();
			    if (!matchNameAttribute(attrs, HTML.Tag.PRE) &&
                                !isFormElementWithContent(attrs)) {
				decrIndent();
			    }
			    endTag(top);
			}
			top = (Element)blockElementStack.peek();
		    }
		} else if (current.getParentElement() == next.getParentElement()) {
		    /*
		       if next and current are siblings the indent level
		       is correct.  But, we need to make sure that if current is
		       on the stack, we pop it off, and put out its end tag.
		    */
		    Element top = (Element)blockElementStack.peek();
		    if (top == current) {
			blockElementStack.pop();
			endTag(top);
		    }
		}
	    }
	    if (!next.isLeaf() || isFormElementWithContent(next.getAttributes())) {
		blockElementStack.push(next);
		startTag(next);
	    } else {
		emptyTag(next);
	    }
	    current = next;
	}
	/* Emit all remaining end tags */

	/* A null parameter ensures that all embedded tags
	   currently in the tags vector have their
	   corresponding end tags written out.
	*/
	closeOutUnwantedEmbeddedTags(null);

        if (forcedBody) {
            blockElementStack.pop();
            endTag(current);
        }
	while (!blockElementStack.empty()) {
	    current = (Element)blockElementStack.pop();
	    if (!synthesizedElement(current)) {
                AttributeSet attrs = current.getAttributes();
		if (!matchNameAttribute(attrs, HTML.Tag.PRE) &&
                              !isFormElementWithContent(attrs)) {
		    decrIndent();
		}
		endTag(current);
	    }
	}

	if (completeDoc) {
	    writeAdditionalComments();
	}

	segment.array = null;
    }


    /**
     * Writes out the attribute set.  Ignores all
     * attributes with a key of type HTML.Tag,
     * attributes with a key of type StyleConstants,
     * and attributes with a key of type
     * HTML.Attribute.ENDTAG.
     *
     * @param attr   an AttributeSet
     * @exception IOException on any I/O error
     *
     */
    protected void writeAttributes(AttributeSet attr) throws IOException {
	// translate css attributes to html
	convAttr.removeAttributes(convAttr);
	convertToHTML32(attr, convAttr);

	Enumeration names = convAttr.getAttributeNames();
	while (names.hasMoreElements()) {
	    Object name = names.nextElement();
	    if (name instanceof HTML.Tag || 
		name instanceof StyleConstants || 
		name == HTML.Attribute.ENDTAG) {
		continue;
	    }
	    write(" " + name + "=\"" + convAttr.getAttribute(name) + "\"");
	}
    }

    /**
     * Writes out all empty elements (all tags that have no
     * corresponding end tag).
     *
     * @param elem   an Element
     * @exception IOException on any I/O error
     * @exception BadLocationException if pos represents an invalid
     *            location within the document.
     */
    protected void emptyTag(Element elem) throws BadLocationException, IOException {

	if (!inContent && !inPre) {
	    indentSmart();
	}

	AttributeSet attr = elem.getAttributes();
	closeOutUnwantedEmbeddedTags(attr);
	writeEmbeddedTags(attr);

	if (matchNameAttribute(attr, HTML.Tag.CONTENT)) {
	    inContent = true;
	    text(elem);
	} else if (matchNameAttribute(attr, HTML.Tag.COMMENT)) {
	    comment(elem);
	}  else {
	    boolean isBlock = isBlockTag(elem.getAttributes());
	    if (inContent && isBlock ) {
		writeLineSeparator();
		indentSmart();
	    }

	    Object nameTag = (attr != null) ? attr.getAttribute
		              (StyleConstants.NameAttribute) : null;
	    Object endTag = (attr != null) ? attr.getAttribute
		              (HTML.Attribute.ENDTAG) : null;

	    boolean outputEndTag = false;
	    // If an instance of an UNKNOWN Tag, or an instance of a 
	    // tag that is only visible during editing
	    //
	    if (nameTag != null && endTag != null &&
		(endTag instanceof String) &&
		((String)endTag).equals("true")) {
		outputEndTag = true;
	    }

	    if (completeDoc && matchNameAttribute(attr, HTML.Tag.HEAD)) {
		if (outputEndTag) {
		    // Write out any styles.
		    writeStyles(((HTMLDocument)getDocument()).getStyleSheet());
		}
		wroteHead = true;
	    }

	    write('<');
	    if (outputEndTag) {
		write('/');
	    }
	    write(elem.getName());
	    writeAttributes(attr);
	    write('>');
	    if (matchNameAttribute(attr, HTML.Tag.TITLE) && !outputEndTag) {
		Document doc = elem.getDocument();
		String title = (String)doc.getProperty(Document.TitleProperty);
		write(title);
	    } else if (!inContent || isBlock) {
		writeLineSeparator();
		if (isBlock && inContent) {
		    indentSmart();
		}
	    }
	}
    }

    /**
     * Determines if the HTML.Tag associated with the
     * element is a block tag.
     *
     * @param attr  an AttributeSet
     * @return  true if tag is block tag, false otherwise.
     */
    protected boolean isBlockTag(AttributeSet attr) {
	Object o = attr.getAttribute(StyleConstants.NameAttribute);
	if (o instanceof HTML.Tag) {
	    HTML.Tag name = (HTML.Tag) o;
	    return name.isBlock();
	}
	return false;
    }


    /**
     * Writes out a start tag for the element.
     * Ignores all synthesized elements.
     *
     * @param elem   an Element
     * @exception IOException on any I/O error
     */
    protected void startTag(Element elem) throws IOException, BadLocationException {
	
	if (synthesizedElement(elem)) {
	    return;
	}

	// Determine the name, as an HTML.Tag.
	AttributeSet attr = elem.getAttributes();
	Object nameAttribute = attr.getAttribute(StyleConstants.NameAttribute);
	HTML.Tag name;
	if (nameAttribute instanceof HTML.Tag) {
	    name = (HTML.Tag)nameAttribute;
	}
	else {
	    name = null;
	}

	if (name == HTML.Tag.PRE) {
	    inPre = true;
	    preEndOffset = elem.getEndOffset();
	}

	// write out end tags for item on stack
	closeOutUnwantedEmbeddedTags(attr);

	if (inContent) { 
	    writeLineSeparator();
	    inContent = false;
	    newlineOutputed = false;
	}

	if (completeDoc && name == HTML.Tag.BODY && !wroteHead) {
	    // If the head has not been output, output it and the styles.
	    wroteHead = true;
	    indentSmart();
	    write("<head>");
	    writeLineSeparator();
	    incrIndent();
	    writeStyles(((HTMLDocument)getDocument()).getStyleSheet());
	    decrIndent();
	    writeLineSeparator();
	    indentSmart();
	    write("</head>");
	    writeLineSeparator();
	}

	indentSmart();
	write('<');
	write(elem.getName());
	writeAttributes(attr);
	write('>');
	if (name != HTML.Tag.PRE) {
	    writeLineSeparator();
	}

	if (name == HTML.Tag.TEXTAREA) {
	    textAreaContent(elem.getAttributes());
	} else if (name == HTML.Tag.SELECT) {
	    selectContent(elem.getAttributes());
	} else if (completeDoc && name == HTML.Tag.BODY) {
	    // Write out the maps, which is not stored as Elements in
	    // the Document.
	    writeMaps(((HTMLDocument)getDocument()).getMaps());
	}
	else if (name == HTML.Tag.HEAD) {
            HTMLDocument document = (HTMLDocument)getDocument();
	    wroteHead = true;
            incrIndent();
            writeStyles(document.getStyleSheet());
            if (document.hasBaseTag()) {
                indentSmart();
                write("<base href=\"" + document.getBase() + "\">");
                writeLineSeparator();
            }
            decrIndent();
	}
	
    }

    
    /**
     * Writes out text that is contained in a TEXTAREA form
     * element.
     *
     * @param attr  an AttributeSet
     * @exception IOException on any I/O error
     * @exception BadLocationException if pos represents an invalid
     *            location within the document.
     */
    protected void textAreaContent(AttributeSet attr) throws BadLocationException, IOException {
	Document doc = (Document)attr.getAttribute(StyleConstants.ModelAttribute);
	if (doc != null && doc.getLength() > 0) {
	    if (segment == null) {
		segment = new Segment();
	    }
	    doc.getText(0, doc.getLength(), segment);
	    if (segment.count > 0) {
		inTextArea = true;
		incrIndent();
		indentSmart();
		setCanWrapLines(true);
		replaceEntities = true;
		write(segment.array, segment.offset, segment.count);
		replaceEntities = false;
		setCanWrapLines(false);
		writeLineSeparator();
		inTextArea = false;
		decrIndent();
	    }
	}
    }


    /**
     * Writes out text.  If a range is specified when the constructor
     * is invoked, then only the appropriate range of text is written
     * out.
     *
     * @param elem   an Element
     * @exception IOException on any I/O error
     * @exception BadLocationException if pos represents an invalid
     *            location within the document.
     */
    protected void text(Element elem) throws BadLocationException, IOException {
	int start = Math.max(getStartOffset(), elem.getStartOffset());
	int end = Math.min(getEndOffset(), elem.getEndOffset());
	if (start < end) {
	    if (segment == null) {
		segment = new Segment();
	    }
	    getDocument().getText(start, end - start, segment);
	    newlineOutputed = false;
	    if (segment.count > 0) {
		if (segment.array[segment.offset + segment.count - 1] == '\n'){
		    newlineOutputed = true;
		}
		if (inPre && end == preEndOffset) {
		    if (segment.count > 1) {
			segment.count--;
		    }
		    else {
			return;
		    }
		}
		replaceEntities = true;
		setCanWrapLines(!inPre);
		write(segment.array, segment.offset, segment.count);
		setCanWrapLines(false);
		replaceEntities = false;
	    }
	}
    }

    /**
     * Writes out the content of the SELECT form element.
     * 
     * @param attr the AttributeSet associated with the form element
     * @exception IOException on any I/O error
     */
    protected void selectContent(AttributeSet attr) throws IOException {
	Object model = attr.getAttribute(StyleConstants.ModelAttribute);
	incrIndent();
	if (model instanceof OptionListModel) {
	    OptionListModel listModel = (OptionListModel)model;
	    int size = listModel.getSize();
	    for (int i = 0; i < size; i++) {
		Option option = (Option)listModel.getElementAt(i);
		writeOption(option);
	    }
	} else if (model instanceof OptionComboBoxModel) {
	    OptionComboBoxModel comboBoxModel = (OptionComboBoxModel)model;
	    int size = comboBoxModel.getSize();
	    for (int i = 0; i < size; i++) {
		Option option = (Option)comboBoxModel.getElementAt(i);
		writeOption(option);
	    }
	}
	decrIndent();
    }


    /**
     * Writes out the content of the Option form element.
     * @param option  an Option
     * @exception IOException on any I/O error
     * 
     */
    protected void writeOption(Option option) throws IOException {
	
	indentSmart();
	write('<');
	write("option");
        // PENDING: should this be changed to check for null first?
        Object value = option.getAttributes().getAttribute
                              (HTML.Attribute.VALUE);
	if (value != null) {
	    write(" value="+ value);
	}
	if (option.isSelected()) {
	    write(" selected");
	}
	write('>');
	if (option.getLabel() != null) {
	    write(option.getLabel());
	}
	writeLineSeparator();
    }

    /**
     * Writes out an end tag for the element.
     *
     * @param elem    an Element
     * @exception IOException on any I/O error
     */
    protected void endTag(Element elem) throws IOException {
	if (synthesizedElement(elem)) {
	    return;
	}

	// write out end tags for item on stack
	closeOutUnwantedEmbeddedTags(elem.getAttributes());
	if (inContent) { 
	    if (!newlineOutputed && !inPre) {
		writeLineSeparator();
	    }
	    newlineOutputed = false;
	    inContent = false;
	}
	if (!inPre) {
	    indentSmart();
	}
	if (matchNameAttribute(elem.getAttributes(), HTML.Tag.PRE)) {
	    inPre = false;
	}
        write('<');
        write('/');
        write(elem.getName());
        write('>');
	writeLineSeparator();
    }



    /**
     * Writes out comments.
     *
     * @param elem    an Element
     * @exception IOException on any I/O error
     * @exception BadLocationException if pos represents an invalid
     *            location within the document.
     */
    protected void comment(Element elem) throws BadLocationException, IOException {
	AttributeSet as = elem.getAttributes();
	if (matchNameAttribute(as, HTML.Tag.COMMENT)) {
	    Object comment = as.getAttribute(HTML.Attribute.COMMENT);
	    if (comment instanceof String) {
		writeComment((String)comment);
	    }
	    else {
		writeComment(null);
	    }
	}
    }


    /**
     * Writes out comment string.
     *
     * @param string   the comment
     * @exception IOException on any I/O error
     * @exception BadLocationException if pos represents an invalid
     *            location within the document.
     */
    void writeComment(String string) throws IOException {
	write("<!--");
	if (string != null) {
	    write(string);
	}
	write("-->");
	writeLineSeparator();
        indentSmart();
    }


    /**
     * Writes out any additional comments (comments outside of the body)
     * stored under the property HTMLDocument.AdditionalComments.
     */
    void writeAdditionalComments() throws IOException {
	Object comments = getDocument().getProperty
	                                (HTMLDocument.AdditionalComments);

	if (comments instanceof Vector) {
	    Vector v = (Vector)comments;
	    for (int counter = 0, maxCounter = v.size(); counter < maxCounter;
		 counter++) {
		writeComment(v.elementAt(counter).toString());
	    }
	}
    }


    /**
     * Returns true if the element is a
     * synthesized element.  Currently we are only testing
     * for the p-implied tag.
     */
    protected boolean synthesizedElement(Element elem) {
	if (matchNameAttribute(elem.getAttributes(), HTML.Tag.IMPLIED)) {
	    return true;
	}
	return false;
    }


    /**
     * Returns true if the StyleConstants.NameAttribute is
     * equal to the tag that is passed in as a parameter.
     */
    protected boolean matchNameAttribute(AttributeSet attr, HTML.Tag tag) {
	Object o = attr.getAttribute(StyleConstants.NameAttribute);
	if (o instanceof HTML.Tag) {
	    HTML.Tag name = (HTML.Tag) o;
	    if (name == tag) {
		return true;
	    }
	}
	return false;
    }

    /**
     * Searches for embedded tags in the AttributeSet
     * and writes them out.  It also stores these tags in a vector
     * so that when appropriate the corresponding end tags can be
     * written out.
     *
     * @exception IOException on any I/O error
     */
    protected void writeEmbeddedTags(AttributeSet attr) throws IOException {
	
	// translate css attributes to html
	attr = convertToHTML(attr, oConvAttr);

	Enumeration names = attr.getAttributeNames();
	while (names.hasMoreElements()) {
	    Object name = names.nextElement();
	    if (name instanceof HTML.Tag) {
		HTML.Tag tag = (HTML.Tag)name;
		if (tag == HTML.Tag.FORM || tags.contains(tag)) {
		    continue;
		}
		write('<');
		write(tag.toString());
		Object o = attr.getAttribute(tag);
		if (o != null && o instanceof AttributeSet) {
		    writeAttributes((AttributeSet)o);
		}
		write('>');
		tags.addElement(tag);
		tagValues.addElement(o);
	    }
	}
    }


    /**
     * Searches the attribute set for a tag, both of which
     * are passed in as a parameter.  Returns true if no match is found
     * and false otherwise.
     */
    private boolean noMatchForTagInAttributes(AttributeSet attr, HTML.Tag t,
					      Object tagValue) {
	if (attr != null && attr.isDefined(t)) {
	    Object newValue = attr.getAttribute(t);

	    if ((tagValue == null) ? (newValue == null) :
		(newValue != null && tagValue.equals(newValue))) {
		return false;
	    }
	}
	return true;
    }


    /**
     * Searches the attribute set and for each tag
     * that is stored in the tag vector.  If the tag isnt found,
     * then the tag is removed from the vector and a corresponding
     * end tag is written out.
     *
     * @exception IOException on any I/O error
     */
    protected void closeOutUnwantedEmbeddedTags(AttributeSet attr) throws IOException {

	tagsToRemove.removeAllElements();

	// translate css attributes to html
	attr = convertToHTML(attr, null);

	HTML.Tag t;
	Object tValue;
	int firstIndex = -1;
	int size = tags.size();
	// First, find all the tags that need to be removed.
	for (int i = size - 1; i >= 0; i--) {
	    t = (HTML.Tag)tags.elementAt(i);
	    tValue = tagValues.elementAt(i);
	    if ((attr == null) || noMatchForTagInAttributes(attr, t, tValue)) {
		firstIndex = i;
		tagsToRemove.addElement(t);
	    }
	}
	if (firstIndex != -1) {
	    // Then close them out.
	    boolean removeAll = ((size - firstIndex) == tagsToRemove.size());
	    for (int i = size - 1; i >= firstIndex; i--) {
		t = (HTML.Tag)tags.elementAt(i);
		if (removeAll || tagsToRemove.contains(t)) {
		    tags.removeElementAt(i);
		    tagValues.removeElementAt(i);
		}
		write('<');
		write('/');
		write(t.toString());
		write('>');
	    }
	    // Have to output any tags after firstIndex that still remaing,
	    // as we closed them out, but they should remain open.
	    size = tags.size();
	    for (int i = firstIndex; i < size; i++) {
		t = (HTML.Tag)tags.elementAt(i);
		write('<');
		write(t.toString());
		Object o = tagValues.elementAt(i);
		if (o != null && o instanceof AttributeSet) {
		    writeAttributes((AttributeSet)o);
		}
		write('>');
	    }
	}
    }


    /**
     * Determines if the element associated with the attributeset
     * is a TEXTAREA or SELECT.  If true, returns true else
     * false
     */
    private boolean isFormElementWithContent(AttributeSet attr) {
	if (matchNameAttribute(attr, HTML.Tag.TEXTAREA) ||
	    matchNameAttribute(attr, HTML.Tag.SELECT)) {
	    return true;
	}
	return false;
    }


    /**
     * Determines whether a the indentation needs to be
     * incremented.  Basically, if next is a child of current, and
     * next is NOT a synthesized element, the indent level will be
     * incremented.  If there is a parent-child relationship and "next"
     * is a synthesized element, then its children must be indented.
     * This state is maintained by the indentNext boolean.
     *
     * @return boolean that's true if indent level
     *         needs incrementing.
     */
    private boolean indentNext = false;
    private boolean indentNeedsIncrementing(Element current, Element next) {
	if ((next.getParentElement() == current) && !inPre) {
	    if (indentNext) {
		indentNext = false;
		return true;
	    } else if (synthesizedElement(next)) {
		indentNext = true;
	    } else if (!synthesizedElement(current)){
		return true;
	    }
	}
	return false;
    }

    /**
     * Outputs the maps as elements. Maps are not stored as elements in
     * the document, and as such this is used to output them.
     */
    void writeMaps(Enumeration maps) throws IOException {
	if (maps != null) {
	    while(maps.hasMoreElements()) {
		Map map = (Map)maps.nextElement();
		String name = map.getName();

		incrIndent();
		indentSmart();
		write("<map");
		if (name != null) {
		    write(" name=\"");
		    write(name);
		    write("\">");
		}
		else {
		    write('>');
		}
		writeLineSeparator();
		incrIndent();

		// Output the areas
		AttributeSet[] areas = map.getAreas();
		if (areas != null) {
		    for (int counter = 0, maxCounter = areas.length;
			 counter < maxCounter; counter++) {
			indentSmart();
			write("<area");
			writeAttributes(areas[counter]);
			write("></area>");
			writeLineSeparator();
		    }
		}
		decrIndent();
		indentSmart();
		write("</map>");
		writeLineSeparator();
		decrIndent();
	    }
	}
    }

    /**
     * Outputs the styles as a single element. Styles are not stored as
     * elements, but part of the document. For the time being styles are
     * written out as a comment, inside a style tag.
     */
    void writeStyles(StyleSheet sheet) throws IOException {
	if (sheet != null) {
	    Enumeration styles = sheet.getStyleNames();
	    if (styles != null) {
		boolean outputStyle = false;
		while (styles.hasMoreElements()) {
		    String name = (String)styles.nextElement();
		    // Don't write out the default style.
		    if (!StyleContext.DEFAULT_STYLE.equals(name) &&
			writeStyle(name, sheet.getStyle(name), outputStyle)) {
			outputStyle = true;
		    }
		}
		if (outputStyle) {
		    writeStyleEndTag();
		}
	    }
	}
    }

    /**
     * Outputs the named style. <code>outputStyle</code> indicates
     * whether or not a style has been output yet. This will return
     * true if a style is written.
     */
    boolean writeStyle(String name, Style style, boolean outputStyle)
	         throws IOException{
	boolean didOutputStyle = false;
	Enumeration attributes = style.getAttributeNames();
	if (attributes != null) {
	    while (attributes.hasMoreElements()) {
		Object attribute = attributes.nextElement();
		if (attribute instanceof CSS.Attribute) {
		    String value = style.getAttribute(attribute).toString();
		    if (value != null) {
			if (!outputStyle) {
			    writeStyleStartTag();
			    outputStyle = true;
			}
			if (!didOutputStyle) {
			    didOutputStyle = true;
			    indentSmart();
			    write(name);
			    write(" {");
			}
			else {
			    write(";");
			}
			write(' ');
			write(attribute.toString());
			write(": ");
			write(value);
		    }
		}
	    }
	}
	if (didOutputStyle) {
	    write(" }");
	    writeLineSeparator();
	}
	return didOutputStyle;
    }

    void writeStyleStartTag() throws IOException {
	indentSmart();
	write("<style type=\"text/css\">");
	incrIndent();
	writeLineSeparator();
	indentSmart();
	write("<!--");
	incrIndent();
	writeLineSeparator();
    }

    void writeStyleEndTag() throws IOException {
	decrIndent();
	indentSmart();
	write("-->");
	writeLineSeparator();
	decrIndent();
	indentSmart();
	write("</style>");
	writeLineSeparator();
	indentSmart();
    }

    // --- conversion support ---------------------------

    /**
     * Convert the give set of attributes to be html for
     * the purpose of writing them out.  Any keys that
     * have been converted will not appear in the resultant
     * set.  Any keys not converted will appear in the 
     * resultant set the same as the received set.<p>
     * This will put the converted values into <code>to</code>, unless
     * it is null in which case a temporary AttributeSet will be returned.
     */
    AttributeSet convertToHTML(AttributeSet from, MutableAttributeSet to) {
	if (to == null) {
	    to = convAttr;
	}
	to.removeAttributes(to);
	if (writeCSS) {
	    convertToHTML40(from, to);
	} else {
	    convertToHTML32(from, to);
	}
	return to;
    }

    /**
     * If true, the writer will emit CSS attributes in preference
     * to HTML tags/attributes (i.e. It will emit an HTML 4.0
     * style).
     */
    private boolean writeCSS = false;

    /**
     * Buffer for the purpose of attribute conversion
     */
    private MutableAttributeSet convAttr = new SimpleAttributeSet();

    /**
     * Buffer for the purpose of attribute conversion. This can be
     * used if convAttr is being used.
     */
    private MutableAttributeSet oConvAttr = new SimpleAttributeSet();

    /**
     * Create an older style of HTML attributes.  This will 
     * convert character level attributes that have a StyleConstants
     * mapping over to an HTML tag/attribute.  Other CSS attributes
     * will be placed in an HTML style attribute.
     */
    private static void convertToHTML32(AttributeSet from, MutableAttributeSet to) {
	if (from == null) {
	    return;
	}
	Enumeration keys = from.getAttributeNames();
	String value = "";
	while (keys.hasMoreElements()) {
	    Object key = keys.nextElement();
	    if (key instanceof CSS.Attribute) {
		if ((key == CSS.Attribute.FONT_FAMILY) ||
		    (key == CSS.Attribute.FONT_SIZE) ||
		    (key == CSS.Attribute.COLOR)) {
		    
		    createFontAttribute((CSS.Attribute)key, from, to);
		} else if (key == CSS.Attribute.FONT_WEIGHT) {
		    // add a bold tag is weight is bold
		    CSS.FontWeight weightValue = (CSS.FontWeight) 
			from.getAttribute(CSS.Attribute.FONT_WEIGHT);
		    if ((weightValue != null) && (weightValue.getValue() > 400)) {
			addAttribute(to, HTML.Tag.B, SimpleAttributeSet.EMPTY);
		    }
		} else if (key == CSS.Attribute.FONT_STYLE) {
		    String s = from.getAttribute(key).toString();
		    if (s.indexOf("italic") >= 0) {
			addAttribute(to, HTML.Tag.I, SimpleAttributeSet.EMPTY);
		    }
		} else if (key == CSS.Attribute.TEXT_DECORATION) {
		    String decor = from.getAttribute(key).toString();
		    if (decor.indexOf("underline") >= 0) {
			addAttribute(to, HTML.Tag.U, SimpleAttributeSet.EMPTY);
		    }
		    if (decor.indexOf("line-through") >= 0) {
			addAttribute(to, HTML.Tag.STRIKE, SimpleAttributeSet.EMPTY);
		    }
		} else if (key == CSS.Attribute.VERTICAL_ALIGN) {
		    String vAlign = from.getAttribute(key).toString();
		    if (vAlign.indexOf("sup") >= 0) {
			addAttribute(to, HTML.Tag.SUP, SimpleAttributeSet.EMPTY);
		    }
		    if (vAlign.indexOf("sub") >= 0) {
			addAttribute(to, HTML.Tag.SUB, SimpleAttributeSet.EMPTY);
		    }
		} else if (key == CSS.Attribute.TEXT_ALIGN) {
		    addAttribute(to, HTML.Attribute.ALIGN, 
				    from.getAttribute(key).toString());
		} else {
		    // default is to store in a HTML style attribute
		    if (value.length() > 0) {
			value = value + "; ";
		    }
		    value = value + key + ": " + from.getAttribute(key);
		}
	    } else {
		Object attr = from.getAttribute(key);
		if (attr instanceof AttributeSet) {
		    attr = ((AttributeSet)attr).copyAttributes();
		} 
		addAttribute(to, key, attr);
	    }
	}
	if (value.length() > 0) {
	    to.addAttribute(HTML.Attribute.STYLE, value);
	}
    }

    /**
     * Add an attribute only if it doesn't exist so that we don't
     * loose information replacing it with SimpleAttributeSet.EMPTY
     */
    private static void addAttribute(MutableAttributeSet to, Object key, Object value) {
	Object attr = to.getAttribute(key);
	if (attr == null || attr == SimpleAttributeSet.EMPTY) {
            to.addAttribute(key, value);
	} else {
	    if (attr instanceof MutableAttributeSet &&
		value instanceof AttributeSet) {
		((MutableAttributeSet)attr).addAttributes((AttributeSet)value);
	    }
	}
    }

    /**
     * Create/update an HTML &lt;font&gt; tag attribute.  The
     * value of the attribute should be a MutableAttributeSet so
     * that the attributes can be updated as they are discovered.
     */
    private static void createFontAttribute(CSS.Attribute a, AttributeSet from, 
				    MutableAttributeSet to) {
	MutableAttributeSet fontAttr = (MutableAttributeSet) 
	    to.getAttribute(HTML.Tag.FONT);
	if (fontAttr == null) {
	    fontAttr = new SimpleAttributeSet();
	    to.addAttribute(HTML.Tag.FONT, fontAttr);
	}
	// edit the parameters to the font tag
	String htmlValue = from.getAttribute(a).toString();
	if (a == CSS.Attribute.FONT_FAMILY) {
	    fontAttr.addAttribute(HTML.Attribute.FACE, htmlValue);
	} else if (a == CSS.Attribute.FONT_SIZE) {
	    fontAttr.addAttribute(HTML.Attribute.SIZE, htmlValue);
	} else if (a == CSS.Attribute.COLOR) {
	    fontAttr.addAttribute(HTML.Attribute.COLOR, htmlValue);
	}
    }
	
    /**
     * Copies the given AttributeSet to a new set, converting
     * any CSS attributes found to arguments of an HTML style
     * attribute.
     */
    private static void convertToHTML40(AttributeSet from, MutableAttributeSet to) {
	Enumeration keys = from.getAttributeNames();
	String value = "";
	while (keys.hasMoreElements()) {
	    Object key = keys.nextElement();
	    if (key instanceof CSS.Attribute) {
		value = value + " " + key + "=" + from.getAttribute(key) + ";";
	    } else {
		to.addAttribute(key, from.getAttribute(key));
	    }
	}
	if (value.length() > 0) {
	    to.addAttribute(HTML.Attribute.STYLE, value);
	}
    }

    //
    // Overrides the writing methods to only break a string when
    // canBreakString is true.
    // In a future release it is likely AbstractWriter will get this
    // functionality.
    //

    /**
     * Writes the line separator. This is overriden to make sure we don't
     * replace the newline content in case it is outside normal ascii.
     * @since 1.3
     */
    protected void writeLineSeparator() throws IOException {
	boolean oldReplace = replaceEntities;
	replaceEntities = false;
	super.writeLineSeparator();
	replaceEntities = oldReplace;
        indented = false;
    }

    /**
     * This method is overriden to map any character entities, such as
     * &lt; to &amp;lt;. <code>super.output</code> will be invoked to
     * write the content.
     * @since 1.3
     */
    protected void output(char[] chars, int start, int length)
	           throws IOException {
	if (!replaceEntities) {
	    super.output(chars, start, length);
	    return;
	}
	int last = start;
	length += start;
	for (int counter = start; counter < length; counter++) {
	    // This will change, we need better support character level
	    // entities.
	    switch(chars[counter]) {
		// Character level entities.
	    case '<':
		if (counter > last) {
		    super.output(chars, last, counter - last);
		}
		last = counter + 1;
		output("&lt;");
		break;
	    case '>':
		if (counter > last) {
		    super.output(chars, last, counter - last);
		}
		last = counter + 1;
		output("&gt;");
		break;
	    case '&':
		if (counter > last) {
		    super.output(chars, last, counter - last);
		}
		last = counter + 1;
		output("&amp;");
		break;
	    case '"':
		if (counter > last) {
		    super.output(chars, last, counter - last);
		}
		last = counter + 1;
		output("&quot;");
		break;
		// Special characters
	    case '\n':
	    case '\t':
	    case '\r':
		break;
	    default:
		if (chars[counter] < ' ' || chars[counter] > 127) {
		    if (counter > last) {
			super.output(chars, last, counter - last);
		    }
		    last = counter + 1;
		    // If the character is outside of ascii, write the
		    // numeric value.
		    output("&#");
		    output(String.valueOf((int)chars[counter]));
		    output(";");
		}
		break;
	    }
	}
	if (last < length) {
	    super.output(chars, last, length - last);
	}
    }

    /**
     * This directly invokes super's <code>output</code> after converting
     * <code>string</code> to a char[].
     */
    private void output(String string) throws IOException {
	int length = string.length();
	if (tempChars == null || tempChars.length < length) {
	    tempChars = new char[length];
	}
	string.getChars(0, length, tempChars, 0);
	super.output(tempChars, 0, length);
    }

    private boolean indented = false;

    /**
     * Writes indent only once per line.
     */
    private void indentSmart() throws IOException {
        if (!indented) {
            indent();
            indented = true;
        }
    }
}

Generated By: JavaOnTracks Doclet 0.1.4     ©Thibaut Colar