View Javadoc
1   /*
2     Licensed to the Apache Software Foundation (ASF) under one or more
3     contributor license agreements.  See the NOTICE file distributed with
4     this work for additional information regarding copyright ownership.
5     The ASF licenses this file to You under the Apache License, Version 2.0
6     (the "License"); you may not use this file except in compliance with
7     the License.  You may obtain a copy of the License at
8   
9         https://www.apache.org/licenses/LICENSE-2.0
10  
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
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   * Appends XHTML formatted text to an {@link Appendable}.
31   */
32  public class XhtmlHelpAppendable extends FilterHelpAppendable {
33  
34      /**
35       * Constructs an appendable filter built on top of the specified underlying appendable.
36       *
37       * @param output the underlying appendable to be assigned to the field {@code this.output} for later use, or {@code null} if this instance is to be created
38       *               without an underlying stream.
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              // write the headers
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              // write the data
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 }