1   ////////////////////////////////////////////////////////////////////////////////
2   // checkstyle: Checks Java source code for adherence to a set of rules.
3   // Copyright (C) 2001-2019 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       * @param lines The text of the comment, as separate lines.
53       * @return The tags extracted from the block.
54       */
55      public static List<TagInfo> extractBlockTags(String... lines) {
56          final List<TagInfo> tags = new ArrayList<>();
57  
58          for (int i = 0; i < lines.length; i++) {
59              // Starting lines of a comment have a different first line pattern.
60              final boolean isFirstLine = i == 0;
61              final Pattern pattern;
62              if (isFirstLine) {
63                  pattern = BLOCK_TAG_PATTERN_FIRST_LINE;
64              }
65              else {
66                  pattern = BLOCK_TAG_PATTERN;
67              }
68  
69              final String line = lines[i];
70              final Matcher tagMatcher = pattern.matcher(line);
71  
72              if (tagMatcher.find()) {
73                  final String tagName = tagMatcher.group(1);
74  
75                  // offset of one for the @ character
76                  final int colNum = tagMatcher.start(1) - 1;
77                  final int lineNum = i + 1;
78  
79                  final String remainder = line.substring(tagMatcher.end(1));
80                  String tagValue = remainder.trim();
81  
82                  // Handle the case where we're on the last line of a Javadoc comment.
83                  if (tagValue.endsWith(JAVADOC_CLOSING_TAG)) {
84                      final int endIndex = tagValue.length() - JAVADOC_CLOSING_TAG.length();
85                      tagValue = tagValue.substring(0, endIndex).trim();
86                  }
87  
88                  final LineColumn position = new LineColumn(lineNum, colNum);
89                  tags.add(new TagInfo(tagName, tagValue, position));
90              }
91          }
92  
93          return tags;
94      }
95  
96  }