1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.doxia.module.xhtml5;
20
21 import javax.swing.text.MutableAttributeSet;
22 import javax.swing.text.html.HTML.Attribute;
23
24 import java.io.Writer;
25
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.maven.doxia.sink.SinkEventAttributes;
28 import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
29 import org.apache.maven.doxia.sink.impl.Xhtml5BaseSink;
30 import org.apache.maven.doxia.util.HtmlTools;
31
32
33
34
35 public class Xhtml5Sink extends Xhtml5BaseSink implements Xhtml5Markup {
36
37
38
39
40 private String encoding;
41
42 private String languageId;
43
44
45 private boolean headTitleFlag;
46
47
48
49
50
51
52
53
54
55
56 protected Xhtml5Sink(Writer writer) {
57 super(writer);
58 }
59
60
61
62
63
64
65
66
67 protected Xhtml5Sink(Writer writer, String encoding) {
68 super(writer);
69
70 this.encoding = encoding;
71 }
72
73
74
75
76
77
78
79
80
81
82
83 protected Xhtml5Sink(Writer writer, String encoding, String languageId) {
84 this(writer, encoding);
85
86 this.languageId = languageId;
87 }
88
89 @Override
90 public void head(SinkEventAttributes attributes) {
91 init();
92
93 setHeadFlag(true);
94
95 write("<!DOCTYPE html>");
96
97 MutableAttributeSet atts = new SinkEventAttributeSet();
98 if (attributes != null) {
99 atts.addAttributes(attributes);
100 }
101 atts.addAttribute("xmlns", XHTML5_NAMESPACE);
102
103 if (languageId != null) {
104 atts.addAttribute(Attribute.LANG.toString(), languageId);
105 atts.addAttribute("xml:lang", languageId);
106 }
107
108 writeStartTag(HTML, atts);
109
110 writeStartTag(HEAD);
111 }
112
113
114
115
116 public void head_() {
117 if (!isHeadTitleFlag()) {
118
119
120
121
122
123 writeStartTag(TITLE);
124 writeEndTag(TITLE);
125 }
126
127 setHeadFlag(false);
128 setHeadTitleFlag(false);
129
130 if (encoding != null) {
131 write("<meta charset=\"" + encoding + "\"/>");
132 }
133
134 writeEndTag(HEAD);
135 }
136
137
138
139
140
141
142 public void title(SinkEventAttributes attributes) {
143 setHeadTitleFlag(true);
144
145 writeStartTag(TITLE, attributes);
146 }
147
148
149
150
151
152
153 public void title_() {
154 content(getTextBuffer().toString());
155
156 writeEndTag(TITLE);
157
158 resetTextBuffer();
159 }
160
161
162
163
164
165
166 public void author_() {
167 if (getTextBuffer().length() > 0) {
168 MutableAttributeSet att = new SinkEventAttributeSet();
169 att.addAttribute(Attribute.NAME, "author");
170 String text = HtmlTools.escapeHTML(getTextBuffer().toString());
171
172
173 text = StringUtils.replace(text, "&#", "&#");
174 att.addAttribute(Attribute.CONTENT, text);
175
176 writeSimpleTag(META, att);
177
178 resetTextBuffer();
179 }
180 }
181
182
183
184
185
186
187 public void date_() {
188 if (getTextBuffer().length() > 0) {
189 MutableAttributeSet att = new SinkEventAttributeSet();
190 att.addAttribute(Attribute.NAME, "date");
191 att.addAttribute(Attribute.CONTENT, getTextBuffer().toString());
192
193 writeSimpleTag(META, att);
194
195 resetTextBuffer();
196 }
197 }
198
199
200
201
202
203
204 @Override
205 public void body(SinkEventAttributes attributes) {
206 writeStartTag(BODY, attributes);
207 }
208
209
210
211
212
213
214
215 public void body_() {
216 writeEndTag(BODY);
217
218 writeEndTag(HTML);
219
220 flush();
221
222 init();
223 }
224
225
226
227
228
229
230
231
232
233
234
235 protected void setHeadTitleFlag(boolean headTitleFlag) {
236 this.headTitleFlag = headTitleFlag;
237 }
238
239
240
241
242
243
244
245 protected boolean isHeadTitleFlag() {
246 return this.headTitleFlag;
247 }
248 }