1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli.example;
18
19 import java.io.IOException;
20 import java.util.Collection;
21 import java.util.List;
22 import java.util.Objects;
23
24 import org.apache.commons.cli.help.FilterHelpAppendable;
25 import org.apache.commons.cli.help.TableDefinition;
26 import org.apache.commons.lang3.StringUtils;
27 import org.apache.commons.text.StringEscapeUtils;
28
29
30
31
32 public class XhtmlHelpAppendable extends FilterHelpAppendable {
33
34
35
36
37
38
39
40 public XhtmlHelpAppendable(final Appendable output) {
41 super(output);
42 }
43
44 @Override
45 public void appendHeader(final int level, final CharSequence text) throws IOException {
46 if (StringUtils.isNotEmpty(text)) {
47 if (level < 1) {
48 throw new IllegalArgumentException("level must be at least 1");
49 }
50 appendFormat("<h%s>%s</h%1$s>%n", level, StringEscapeUtils.escapeHtml4(Objects.toString(text)));
51 }
52 }
53
54 @Override
55 public void appendList(final boolean ordered, final Collection<CharSequence> list) throws IOException {
56 if (list != null) {
57 appendFormat("<%sl>%n", ordered ? "o" : "u");
58 for (final CharSequence line : list) {
59 appendFormat(" <li>%s</li>%n", StringEscapeUtils.escapeHtml4(StringUtils.defaultIfEmpty(line, "").toString()));
60 }
61 appendFormat("</%sl>%n", ordered ? "o" : "u");
62 }
63 }
64
65 @Override
66 public void appendParagraph(final CharSequence paragraph) throws IOException {
67 if (StringUtils.isNotEmpty(paragraph)) {
68 appendFormat("<p>%s</p>%n", StringEscapeUtils.escapeHtml4(Objects.toString(paragraph)));
69 }
70 }
71
72 @Override
73 public void appendTable(final TableDefinition table) throws IOException {
74 if (table != null) {
75 appendFormat("<table class='commons_cli_table'>%n");
76 if (StringUtils.isNotEmpty(table.caption())) {
77 appendFormat(" <caption>%s</caption>%n", StringEscapeUtils.escapeHtml4(table.caption()));
78 }
79
80 if (!table.headers().isEmpty()) {
81 appendFormat(" <tr>%n");
82 for (final String header : table.headers()) {
83 appendFormat(" <th>%s</th>%n", StringEscapeUtils.escapeHtml4(header));
84 }
85 appendFormat(" </tr>%n");
86 }
87
88 for (final List<String> row : table.rows()) {
89 appendFormat(" <tr>%n");
90 for (final String column : row) {
91 appendFormat(" <td>%s</td>%n", StringEscapeUtils.escapeHtml4(column));
92 }
93 appendFormat(" </tr>%n");
94 }
95 appendFormat("</table>%n");
96 }
97 }
98
99 @Override
100 public void appendTitle(final CharSequence title) throws IOException {
101 if (StringUtils.isNotEmpty(title)) {
102 appendFormat("<span class='commons_cli_title'>%s</span>%n", StringEscapeUtils.escapeHtml4(Objects.toString(title)));
103 }
104 }
105 }