|
| 1 | +/* |
| 2 | + * Copyright (c) 2011-2024 Qulice.com |
| 3 | + * |
| 4 | + * All rights reserved. |
| 5 | + * |
| 6 | + * Redistribution and use in source and binary forms, with or without |
| 7 | + * modification, are permitted provided that the following conditions |
| 8 | + * are met: 1) Redistributions of source code must retain the above |
| 9 | + * copyright notice, this list of conditions and the following |
| 10 | + * disclaimer. 2) Redistributions in binary form must reproduce the above |
| 11 | + * copyright notice, this list of conditions and the following |
| 12 | + * disclaimer in the documentation and/or other materials provided |
| 13 | + * with the distribution. 3) Neither the name of the Qulice.com nor |
| 14 | + * the names of its contributors may be used to endorse or promote |
| 15 | + * products derived from this software without specific prior written |
| 16 | + * permission. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
| 20 | + * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 | + * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | + * THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 23 | + * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 25 | + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 | + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 28 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 29 | + * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | + */ |
| 31 | +package com.qulice.checkstyle; |
| 32 | + |
| 33 | +import java.util.HashMap; |
| 34 | +import java.util.Map; |
| 35 | +import java.util.regex.Matcher; |
| 36 | +import java.util.regex.Pattern; |
| 37 | + |
| 38 | +/** |
| 39 | + * Check the required JavaDoc tag in the lines. |
| 40 | + * <p>Correct format is the following (of a class javadoc): |
| 41 | + * |
| 42 | + * <pre> |
| 43 | + * /** |
| 44 | + * * This is my new class. |
| 45 | + * * |
| 46 | + * * @since 0.3 |
| 47 | + * */ |
| 48 | + * public final class Foo { |
| 49 | + * /** |
| 50 | + * * This is my other class. |
| 51 | + * * |
| 52 | + * * @since 0.3 |
| 53 | + * */ |
| 54 | + * public final class Boo { |
| 55 | + * // ... |
| 56 | + * </pre> |
| 57 | + * |
| 58 | + * <p>"$Id$" will be replaced by a full text automatically |
| 59 | + * by Subversion as explained in their documentation (see link below). |
| 60 | + * |
| 61 | + * @see <a href="http://svnbook.red-bean.com/en/1.4/svn.advanced.props.special.keywords.html">Keywords substitution in Subversion</a> |
| 62 | +
|
| 63 | + * @since 0.23.1 |
| 64 | + */ |
| 65 | +final class RequiredJavaDocTag { |
| 66 | + /** |
| 67 | + * Tag name. |
| 68 | + */ |
| 69 | + private final String name; |
| 70 | + |
| 71 | + /** |
| 72 | + * Pattern for searching a tag in a string. |
| 73 | + */ |
| 74 | + private final Pattern tag; |
| 75 | + |
| 76 | + /** |
| 77 | + * Pattern for checking the contents of a tag in a string. |
| 78 | + */ |
| 79 | + private final Pattern content; |
| 80 | + |
| 81 | + /** |
| 82 | + * Reference to a method for writing a message to the log. |
| 83 | + */ |
| 84 | + private final LogWriter writer; |
| 85 | + |
| 86 | + /** |
| 87 | + * Ctor. |
| 88 | + * @param name Tag name. |
| 89 | + * @param patt Pattern for checking the contents of a tag in a string. |
| 90 | + * @param lwriter Reference to a method for writing a message to the log. |
| 91 | + */ |
| 92 | + RequiredJavaDocTag(final String name, final Pattern patt, |
| 93 | + final LogWriter lwriter) { |
| 94 | + this( |
| 95 | + name, |
| 96 | + Pattern.compile( |
| 97 | + String.format( |
| 98 | + "(?<name>^ +\\* +@%s)( +)(?<cont>.*)", |
| 99 | + name |
| 100 | + ) |
| 101 | + ), |
| 102 | + patt, |
| 103 | + lwriter |
| 104 | + ); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Ctor. |
| 109 | + * @param cname Tag name. |
| 110 | + * @param ptag Pattern for searching a tag in a string. |
| 111 | + * @param patt Pattern for checking the contents of a tag in a string. |
| 112 | + * @param lwriter Reference to a method for writing a message to the log. |
| 113 | + * @checkstyle ParameterNumberCheck (3 lines) |
| 114 | + */ |
| 115 | + RequiredJavaDocTag(final String cname, final Pattern ptag, |
| 116 | + final Pattern patt, final LogWriter lwriter) { |
| 117 | + this.name = cname; |
| 118 | + this.tag = ptag; |
| 119 | + this.content = patt; |
| 120 | + this.writer = lwriter; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Check if the tag text matches the format from pattern. |
| 125 | + * @param lines List of all lines. |
| 126 | + * @param start Line number where comment starts. |
| 127 | + * @param end Line number where comment ends. |
| 128 | + */ |
| 129 | + public void matchTagFormat(final String[] lines, final int start, |
| 130 | + final int end) { |
| 131 | + final Map<Integer, String> found = new HashMap<>(1); |
| 132 | + for (int pos = start; pos <= end; pos += 1) { |
| 133 | + final String line = lines[pos]; |
| 134 | + final Matcher matcher = this.tag.matcher(line); |
| 135 | + if (matcher.matches() |
| 136 | + && !RequiredJavaDocTag.empty(matcher.group("name")) |
| 137 | + && !RequiredJavaDocTag.empty(matcher.group("cont")) |
| 138 | + ) { |
| 139 | + found.put(pos, matcher.group("cont")); |
| 140 | + break; |
| 141 | + } |
| 142 | + } |
| 143 | + if (found.isEmpty()) { |
| 144 | + this.writer.log( |
| 145 | + start + 1, |
| 146 | + "Missing ''@{0}'' tag in class/interface comment", |
| 147 | + this.name |
| 148 | + ); |
| 149 | + } else { |
| 150 | + for (final Map.Entry<Integer, String> item : found.entrySet()) { |
| 151 | + if (!this.content.matcher(item.getValue()).matches()) { |
| 152 | + this.writer.log( |
| 153 | + item.getKey() + 1, |
| 154 | + "Tag text ''{0}'' does not match the pattern ''{1}''", |
| 155 | + item.getValue(), |
| 156 | + this.content.toString() |
| 157 | + ); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Checks for an empty string. |
| 165 | + * @param str Line to check. |
| 166 | + * @return True if str is empty. |
| 167 | + */ |
| 168 | + private static boolean empty(final String str) { |
| 169 | + return str == null || str.isBlank(); |
| 170 | + } |
| 171 | + |
| 172 | + /** |
| 173 | + * Logger. |
| 174 | + * @see com.puppycrawl.tools.checkstyle.api.AbstractCheck.log |
| 175 | + * @since 0.23.1 |
| 176 | + */ |
| 177 | + interface LogWriter { |
| 178 | + /** |
| 179 | + * Log a message that has no column information. |
| 180 | + * |
| 181 | + * @param line The line number where the audit event was found. |
| 182 | + * @param msg The message that describes the audit event. |
| 183 | + * @param args The details of the message. |
| 184 | + * @see java.text.MessageFormat |
| 185 | + */ |
| 186 | + void log(int line, String msg, Object... args); |
| 187 | + } |
| 188 | +} |
0 commit comments