View Javadoc
1   ///////////////////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code and other text files for adherence to a set of rules.
3   // Copyright (C) 2001-2024 the original author or authors.
4   //
5   // This library is free software; you can redistribute it and/or
6   // modify it under the terms of the GNU Lesser General Public
7   // License as published by the Free Software Foundation; either
8   // version 2.1 of the License, or (at your option) any later version.
9   //
10  // This library is distributed in the hope that it will be useful,
11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // Lesser General Public License for more details.
14  //
15  // You should have received a copy of the GNU Lesser General Public
16  // License along with this library; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  ///////////////////////////////////////////////////////////////////////////////////////////////
19  
20  package com.puppycrawl.tools.checkstyle.checks.javadoc.utils;
21  
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.regex.Matcher;
25  import java.util.regex.Pattern;
26  
27  import com.puppycrawl.tools.checkstyle.api.LineColumn;
28  
29  /**
30   * Tools for parsing block tags from a Javadoc comment.
31   *
32   */
33  public final class BlockTagUtil {
34  
35      /** Block tag pattern for a first line. */
36      private static final Pattern BLOCK_TAG_PATTERN_FIRST_LINE = Pattern.compile(
37          "/\\*{2,}\\s*@(\\p{Alpha}+)\\s");
38  
39      /** Block tag pattern. */
40      private static final Pattern BLOCK_TAG_PATTERN = Pattern.compile(
41          "^\\s*\\**\\s*@(\\p{Alpha}+)\\s");
42  
43      /** Closing tag. */
44      private static final String JAVADOC_CLOSING_TAG = "*/";
45  
46      /** Prevent instantiation. */
47      private BlockTagUtil() {
48      }
49  
50      /**
51       * Extract the block tags from a Javadoc comment.
52       *
53       * @param lines The text of the comment, as separate lines.
54       * @return The tags extracted from the block.
55       */
56      public static List<TagInfo> extractBlockTags(String... lines) {
57          final List<TagInfo> tags = new ArrayList<>();
58  
59          for (int i = 0; i < lines.length; i++) {
60              // Starting lines of a comment have a different first line pattern.
61              final boolean isFirstLine = i == 0;
62              final Pattern pattern;
63              if (isFirstLine) {
64                  pattern = BLOCK_TAG_PATTERN_FIRST_LINE;
65              }
66              else {
67                  pattern = BLOCK_TAG_PATTERN;
68              }
69  
70              final String line = lines[i];
71              final Matcher tagMatcher = pattern.matcher(line);
72  
73              if (tagMatcher.find()) {
74                  final String tagName = tagMatcher.group(1);
75  
76                  // offset of one for the @ character
77                  final int colNum = tagMatcher.start(1) - 1;
78                  final int lineNum = i + 1;
79  
80                  final String remainder = line.substring(tagMatcher.end(1));
81                  String tagValue = remainder.trim();
82  
83                  // Handle the case where we're on the last line of a Javadoc comment.
84                  if (tagValue.endsWith(JAVADOC_CLOSING_TAG)) {
85                      final int endIndex = tagValue.length() - JAVADOC_CLOSING_TAG.length();
86                      tagValue = tagValue.substring(0, endIndex).trim();
87                  }
88  
89                  final LineColumn position = new LineColumn(lineNum, colNum);
90                  tags.add(new TagInfo(tagName, tagValue, position));
91              }
92          }
93  
94          return tags;
95      }
96  
97  }