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.tools; 20 21 import org.apache.commons.cli.Option; 22 import org.apache.commons.text.StringEscapeUtils; 23 import org.apache.commons.text.WordUtils; 24 25 import static java.lang.String.format; 26 27 /** 28 * A class that wraps the CLI option and provides Ant specific values. 29 */ 30 public class AntOption extends AbstractOption { 31 32 /** 33 * Constructor. 34 * @param option the option to wrap. 35 */ 36 AntOption(final Option option) { 37 super(option, AntGenerator.createName(option)); 38 } 39 40 /** 41 * Returns {@code true} if the option should be an attribute of the <rat:report> element. 42 * @return {@code true} if the option should be an attribute of the <rat:report> element. 43 */ 44 public boolean isAttribute() { 45 return !option.hasArgs(); 46 } 47 48 /** 49 * Returns {@code true} if the option should be a child element of the <rat:report> element. 50 * 51 * @return {@code true} if the option should be a child element of the <rat:report> element. 52 */ 53 public boolean isElement() { 54 return !isAttribute() || option.getType() != String.class; 55 } 56 57 protected String cleanupName(final Option option) { 58 AntOption antOption = new AntOption(option); 59 String fmt = antOption.isAttribute() ? "%s attribute" : "<%s>"; 60 return format(fmt, AntGenerator.createName(option)); 61 } 62 63 /** 64 * Get the method comment for this option. 65 * 66 * @param addParam if {@code true} the param annotation is added. 67 * @return the Comment block for the function. 68 */ 69 public String getComment(final boolean addParam) { 70 StringBuilder sb = new StringBuilder() 71 .append(format(" /**%n * %s%n", StringEscapeUtils.escapeHtml4(getDescription()))); 72 if (option.isDeprecated()) { 73 sb.append(format(" * %s%n * @deprecated%n", option.getDeprecated())); 74 } 75 if (addParam && option.hasArg()) { 76 sb.append(format(" * @param %s The value to set%n", name)); 77 } 78 return sb.append(format(" */%n")).toString(); 79 } 80 81 /** 82 * Get the signature of the attribute function. 83 * 84 * @return the signature of the attribute function. 85 */ 86 public String getAttributeFunctionName() { 87 return "set" + 88 WordUtils.capitalize(name) + 89 (option.hasArg() ? "(String " : "(boolean ") + 90 name + 91 ")"; 92 } 93 }