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.HashMap;
22  import java.util.Map;
23  
24  import org.apache.commons.cli.help.FilterHelpAppendable;
25  import org.apache.commons.cli.help.TableDefinition;
26  import org.apache.commons.cli.help.TextStyle;
27  import org.apache.commons.lang3.StringUtils;
28  import org.apache.commons.text.translate.CharSequenceTranslator;
29  import org.apache.commons.text.translate.LookupTranslator;
30  
31  /**
32   * Appends APT formatted text to an {@link Appendable}.
33   */
34  public class AptHelpAppendable extends FilterHelpAppendable {
35  
36      /**
37       * Translator object for escaping APT codes
38       */
39      public static final CharSequenceTranslator ESCAPE_APT;
40  
41      static {
42          final Map<CharSequence, CharSequence> escapeAptMap = new HashMap<>();
43          escapeAptMap.put("\\", "\\\\");
44          escapeAptMap.put("\"", "\\\"");
45          escapeAptMap.put("*", "\\*");
46          escapeAptMap.put("+", "\\+");
47          escapeAptMap.put("|", "\\|");
48          ESCAPE_APT = new LookupTranslator(escapeAptMap);
49      }
50  
51      /**
52       * Constructs an appendable filter built on top of the specified underlying appendable.
53       *
54       * @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
55       *               without an underlying stream.
56       */
57      public AptHelpAppendable(final Appendable output) {
58          super(output);
59      }
60  
61      @Override
62      public void appendHeader(final int level, final CharSequence text) throws IOException {
63          if (StringUtils.isNotEmpty(text)) {
64              if (level < 1) {
65                  throw new IllegalArgumentException("level must be at least 1");
66              }
67              for (int i = 0; i < level; i++) {
68                  output.append("*");
69              }
70              appendFormat(" %s%n%n", ESCAPE_APT.translate(text));
71          }
72      }
73  
74      @Override
75      public void appendList(final boolean ordered, final Collection<CharSequence> list) throws IOException {
76          if (list != null) {
77              if (ordered) {
78                  int idx = 1;
79                  for (final CharSequence s : list) {
80                      appendFormat("    [[%s]] %s%n", idx++, ESCAPE_APT.translate(s));
81                  }
82              } else {
83                  for (final CharSequence s : list) {
84                      appendFormat("    * %s%n", ESCAPE_APT.translate(s));
85                  }
86              }
87              output.append(System.lineSeparator());
88          }
89      }
90  
91      @Override
92      public void appendParagraph(final CharSequence paragraph) throws IOException {
93          if (StringUtils.isNotEmpty(paragraph)) {
94              appendFormat("  %s%n%n", ESCAPE_APT.translate(paragraph));
95          }
96      }
97  
98      @Override
99      public void appendTable(final TableDefinition table) throws IOException {
100         if (table != null) {
101             // create the row separator string
102             final StringBuilder sb = new StringBuilder("*");
103             for (int i = 0; i < table.headers().size(); i++) {
104                 final String header = table.headers().get(i);
105                 final TextStyle style = table.columnTextStyles().get(i);
106                 sb.append(StringUtils.repeat('-', header.length() + 2));
107                 switch (style.getAlignment()) {
108                 case LEFT:
109                     sb.append("+");
110                     break;
111                 case CENTER:
112                     sb.append("*");
113                     break;
114                 case RIGHT:
115                     sb.append(":");
116                     break;
117                 }
118             }
119             final String rowSeparator = System.lineSeparator() + sb.append(System.lineSeparator());
120             // output the header line.
121             output.append(sb.toString());
122             output.append("|");
123             for (final String header : table.headers()) {
124                 appendFormat(" %s |", ESCAPE_APT.translate(header));
125             }
126             output.append(rowSeparator);
127             // write the table entries
128             for (final Collection<String> row : table.rows()) {
129                 output.append("|");
130                 for (final String cell : row) {
131                     appendFormat(" %s |", ESCAPE_APT.translate(cell));
132                 }
133                 output.append(rowSeparator);
134             }
135             // write the caption
136             if (StringUtils.isNotEmpty(table.caption())) {
137                 appendFormat("%s%n", ESCAPE_APT.translate(table.caption()));
138             }
139             output.append(System.lineSeparator());
140         }
141     }
142 
143     @Override
144     public void appendTitle(final CharSequence title) throws IOException {
145         if (StringUtils.isNotEmpty(title)) {
146             appendFormat("        -----%n        %1$s%n        -----%n%n%1$s%n%n", title);
147         }
148     }
149 }