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.xdoc;
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.SinkUtils;
30 import org.apache.maven.doxia.sink.impl.Xhtml5BaseSink;
31 import org.apache.maven.doxia.util.HtmlTools;
32
33
34
35
36
37
38
39
40
41
42 public class XdocSink extends Xhtml5BaseSink implements XdocMarkup {
43
44
45
46
47 private String encoding;
48
49 private String languageId;
50
51
52
53
54
55
56
57
58
59
60
61 protected XdocSink(Writer writer) {
62 super(writer);
63 }
64
65
66
67
68
69
70
71
72
73 protected XdocSink(Writer writer, String encoding) {
74 this(writer);
75 this.encoding = encoding;
76 }
77
78
79
80
81
82
83
84
85
86
87
88
89 protected XdocSink(Writer writer, String encoding, String languageId) {
90 this(writer, encoding);
91
92 this.languageId = languageId;
93 }
94
95
96
97
98
99
100
101
102 protected void init() {
103 super.init();
104 }
105
106
107
108
109
110
111 public void head(SinkEventAttributes attributes) {
112 init();
113
114 setHeadFlag(true);
115
116 write("<?xml version=\"1.0\"");
117 if (encoding != null) {
118 write(" encoding=\"" + encoding + "\"");
119 }
120 write("?>");
121
122 MutableAttributeSet atts = new SinkEventAttributeSet();
123 atts.addAttribute("xmlns", XDOC_NAMESPACE);
124 atts.addAttribute("xmlns:xsi", XML_NAMESPACE);
125 atts.addAttribute("xsi:schemaLocation", XDOC_NAMESPACE + " " + XDOC_SYSTEM_ID);
126
127 if (languageId != null) {
128 atts.addAttribute(Attribute.LANG.toString(), languageId);
129 atts.addAttribute("xml:lang", languageId);
130 }
131
132 if (attributes != null) {
133 atts.addAttributes(attributes);
134 }
135
136 writeStartTag(DOCUMENT_TAG, atts);
137
138 writeStartTag(PROPERTIES_TAG);
139 }
140
141
142
143
144
145
146
147 public void head_() {
148 setHeadFlag(false);
149
150 writeEndTag(PROPERTIES_TAG);
151 }
152
153
154
155
156
157
158 @Override
159 public void title(SinkEventAttributes attributes) {
160 writeStartTag(TITLE);
161 }
162
163
164
165
166
167
168 public void title_() {
169 content(getTextBuffer().toString());
170
171 writeEndTag(TITLE);
172
173 resetTextBuffer();
174 }
175
176
177
178
179
180
181 public void author_() {
182 if (getTextBuffer().length() > 0) {
183 writeStartTag(AUTHOR_TAG);
184 String text = HtmlTools.escapeHTML(getTextBuffer().toString());
185
186
187 text = StringUtils.replace(text, "&#", "&#");
188 write(text);
189 writeEndTag(AUTHOR_TAG);
190 resetTextBuffer();
191 }
192 }
193
194
195
196
197
198
199 public void date_() {
200 if (getTextBuffer().length() > 0) {
201 writeStartTag(DATE_TAG);
202 content(getTextBuffer().toString());
203 writeEndTag(DATE_TAG);
204 resetTextBuffer();
205 }
206 }
207
208
209
210
211
212 public void body(SinkEventAttributes attributes) {
213 writeStartTag(BODY, attributes);
214 }
215
216
217
218
219
220
221
222 public void body_() {
223 writeEndTag(BODY);
224
225 writeEndTag(DOCUMENT_TAG);
226
227 flush();
228
229 init();
230 }
231
232
233
234
235
236
237
238
239
240
241
242
243 protected void onSection(int depth, SinkEventAttributes attributes) {
244 if (depth == SECTION_LEVEL_1) {
245 write(LESS_THAN
246 + SECTION_TAG.toString()
247 + SinkUtils.getAttributeString(
248 SinkUtils.filterAttributes(attributes, SinkUtils.SINK_BASE_ATTRIBUTES))
249 + SPACE
250 + Attribute.NAME
251 + EQUAL
252 + QUOTE);
253 } else if (depth == SECTION_LEVEL_2) {
254 write(LESS_THAN
255 + SUBSECTION_TAG.toString()
256 + SinkUtils.getAttributeString(
257 SinkUtils.filterAttributes(attributes, SinkUtils.SINK_BASE_ATTRIBUTES))
258 + SPACE
259 + Attribute.NAME
260 + EQUAL
261 + QUOTE);
262 }
263 }
264
265
266
267
268
269
270
271
272 protected void onSection_(int depth) {
273 if (depth == SECTION_LEVEL_1) {
274 writeEndTag(SECTION_TAG);
275 } else if (depth == SECTION_LEVEL_2) {
276 writeEndTag(SUBSECTION_TAG);
277 }
278 }
279
280
281
282
283
284
285
286
287
288
289 protected void onSectionTitle(int depth, SinkEventAttributes attributes) {
290 MutableAttributeSet atts = SinkUtils.filterAttributes(attributes, SinkUtils.SINK_SECTION_ATTRIBUTES);
291
292 if (depth == SECTION_LEVEL_3) {
293 writeStartTag(H3, atts);
294 } else if (depth == SECTION_LEVEL_4) {
295 writeStartTag(H4, atts);
296 } else if (depth == SECTION_LEVEL_5) {
297 writeStartTag(H5, atts);
298 } else if (depth == SECTION_LEVEL_6) {
299 writeStartTag(H6, atts);
300 }
301 }
302
303
304
305
306
307
308
309
310
311
312 protected void onSectionTitle_(int depth) {
313 if (depth == SECTION_LEVEL_1 || depth == SECTION_LEVEL_2) {
314 write(String.valueOf(QUOTE) + GREATER_THAN);
315 } else if (depth == SECTION_LEVEL_3) {
316 writeEndTag(H3);
317 } else if (depth == SECTION_LEVEL_4) {
318 writeEndTag(H4);
319 } else if (depth == SECTION_LEVEL_5) {
320 writeEndTag(H5);
321 } else if (depth == SECTION_LEVEL_6) {
322 writeEndTag(H6);
323 }
324 }
325
326
327
328
329
330
331
332
333
334
335
336
337 public void verbatim(SinkEventAttributes attributes) {
338
339 MutableAttributeSet atts = SinkUtils.filterAttributes(attributes, SinkUtils.SINK_VERBATIM_ATTRIBUTES);
340
341 if (atts == null) {
342 atts = new SinkEventAttributeSet();
343 }
344
345 this.setVerbatimMode(VerbatimMode.ON);
346 if (atts.isDefined(SinkEventAttributes.DECORATION)) {
347 if ("source".equals(atts.getAttribute(SinkEventAttributes.DECORATION))) {
348 this.setVerbatimMode(VerbatimMode.ON_WITH_CODE);
349 }
350 }
351
352 atts.removeAttribute(SinkEventAttributes.DECORATION);
353
354 if (getVerbatimMode() == VerbatimMode.ON_WITH_CODE) {
355 writeStartTag(SOURCE_TAG, atts);
356 } else {
357 writeStartTag(PRE, atts);
358 }
359 }
360
361
362
363
364
365
366
367 public void verbatim_() {
368 if (getVerbatimMode() == VerbatimMode.ON_WITH_CODE) {
369 writeEndTag(SOURCE_TAG);
370 } else {
371 writeEndTag(PRE);
372 }
373
374 this.setVerbatimMode(VerbatimMode.OFF);
375 }
376
377
378
379
380
381
382 public void tableRows(int[] justification, boolean grid) {
383
384
385 setCellJustif(justification);
386
387 MutableAttributeSet att = new SinkEventAttributeSet();
388
389 if (!tableAttributes.isDefined(Attribute.BORDER.toString())) {
390 att.addAttribute(Attribute.BORDER, (grid ? "1" : "0"));
391 }
392
393 att.addAttributes(tableAttributes);
394
395 tableAttributes.removeAttributes(tableAttributes);
396
397 writeStartTag(TABLE, att);
398 }
399
400
401
402
403
404
405 @Override
406 public void tableRow(SinkEventAttributes attributes) {
407
408 writeStartTag(TR, attributes);
409
410 setCellCount(0);
411 }
412
413
414
415
416 public void close() {
417 super.close();
418
419 init();
420 }
421
422
423
424
425
426 }