View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.rat.documentation.options;
20  
21  import java.util.List;
22  import java.util.Map;
23  import java.util.Optional;
24  import java.util.Set;
25  
26  import org.apache.commons.cli.Option;
27  import org.apache.rat.ui.UIOption;
28  import org.apache.rat.ui.UIOptionCollection;
29  
30  import static java.lang.String.format;
31  
32  /**
33   * A class that wraps the CLI option and provides Ant specific values.
34   */
35  public class AntOption extends UIOption<AntOption> {
36  
37      /**
38       * Constructor.
39       *
40       * @param option the option to wrap.
41       */
42      public AntOption(final UIOptionCollection<AntOption> collection, final Option option) {
43          super(collection, option, AntOptionCollection.createName(option));
44      }
45  
46      /**
47       * Returns {@code true} if the option should be an attribute of the &lt;rat:report&gt; element.
48       *
49       * @return {@code true} if the option should be an attribute of the &lt;rat:report&gt; element.
50       */
51      public boolean isAttribute() {
52          return getAntCollection().isAttribute(this);
53      }
54  
55      @Override
56      public String toString() {
57          return getName();
58      }
59  
60      /**
61       * Returns {@code true} if the option should be a child element of the &lt;rat:report&gt; element.
62       *
63       * @return {@code true} if the option should be a child element of the &lt;rat:report&gt; element.
64       */
65      public boolean isElement() {
66          return !isAttribute();
67      }
68  
69      /**
70       * If this option is converted to another option return that option otherwise
71       * return this option.
72       * @return the converted option.
73       */
74      public AntOption getActualAntOption() {
75          return getAntCollection().getActualAntOption(this);
76      }
77  
78      /**
79       * Gets the set of options that are mapped to this option.
80       * @return the set of options that are mapped to this option.
81       */
82      public Set<AntOption> convertedFrom() {
83          return getAntCollection().convertedFrom(this);
84      }
85  
86      @Override
87      public String getText() {
88          return cleanupName(option);
89      }
90  
91      @Override
92      protected String cleanupName(final Option option) {
93          AntOption antOption;
94          if (getOption().equals(option)) {
95              antOption = this;
96          } else {
97              Optional<AntOption> optAntOption = getOptionCollection().getMappedOption(option);
98              if (optAntOption.isPresent()) {
99                  antOption = optAntOption.get();
100             } else {
101                 return "";
102             }
103         }
104         return antOption.cleanupName();
105     }
106 
107     public String cleanupName() {
108         String fmt = isAttribute() ? "%s attribute" : "<%s>";
109         return format(fmt, name);
110     }
111 
112     AntOptionCollection getAntCollection() {
113         return getOptionCollection();
114     }
115 
116     public AntOptionCollection.BuildType buildType() {
117         return getAntCollection().buildType(this.getArgType());
118     }
119 
120     @Override
121     public String getExample() {
122         return new ExampleGenerator().getExample();
123     }
124 
125     /**
126      * An example code generator for this AntOption.
127      */
128     public class ExampleGenerator {
129 
130         /**
131          * The constructor.
132          */
133         public ExampleGenerator() {
134         }
135 
136         /**
137          * Gets an example Ant XML report call using ant option.
138          * @return the example of this ant option.
139          */
140         String getExample() {
141                 return getExample("data", getAntCollection().getRequiredAttributes(getName()), null);
142         }
143 
144         /**
145          * Gets an example Ant XML report call using ant option with the specified attributes and child elements.
146          * @param data The data value for this option.
147          * @param attributes A map of attribute keys and values.
148          * @param childElements a list of child elements for the example
149          * @return example Ant XML report call using ant option with the specified attributes and child elements.
150          */
151         public String getExample(final String data, final Map<String, String> attributes, final List<String> childElements) {
152             return "<rat:report" +
153                     getExampleAttributes(data, attributes) +
154                     "> \n" +
155                     getChildElements(data, childElements) +
156                     "</rat:report>\n";
157         }
158 
159         /**
160          * Creates a string comprising the attributes for the Ant XML report call.
161          * @param data The data value for this option.
162          * @param attributes A map of attribute keys and values.
163          * @return a string comprising all the attribute keys and values for the Ant XML report element.
164          */
165         public String getExampleAttributes(final String data, final Map<String, String> attributes) {
166             AntOption actualOption = getActualAntOption();
167             StringBuilder result = new StringBuilder();
168             if (attributes != null) {
169                 attributes.forEach((k, v) -> result.append(format(" %s=\"%s\"", k, v)));
170             }
171             if (actualOption.isAttribute()) {
172                 result.append(format(" %s=\"%s\"", actualOption.getName(), actualOption.hasArg() ? data : "true"));
173             }
174             return result.toString();
175         }
176 
177         /**
178          * Creates a string comprising the child elements for the Ant XML report call.
179          * @param data the data for this option.
180          * @param childElements additional child elements.
181          * @return A string comprising the child elements for the Ant XML report call.
182          */
183         public String getChildElements(final String data, final List<String> childElements) {
184             AntOption baseOption = AntOption.this;
185             AntOption actualOption = getActualAntOption();
186             StringBuilder result = new StringBuilder();
187             if (!actualOption.isAttribute()) {
188                 String inner = getAntCollection().buildType(getArgType()).getXml(actualOption, baseOption, data);
189                 result.append(inner);
190             }
191             if (childElements != null) {
192                 childElements.forEach(x -> result.append(x).append("\n"));
193             }
194             return result.toString();
195         }
196     }
197 }