1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
31
32
33 public final class BlockTagUtil {
34
35
36 private static final Pattern BLOCK_TAG_PATTERN_FIRST_LINE = Pattern.compile(
37 "/\\*{2,}\\s*@(\\p{Alpha}+)\\s");
38
39
40 private static final Pattern BLOCK_TAG_PATTERN = Pattern.compile(
41 "^\\s*\\**\\s*@(\\p{Alpha}+)\\s");
42
43
44 private static final String JAVADOC_CLOSING_TAG = "*/";
45
46
47 private BlockTagUtil() {
48 }
49
50
51
52
53
54
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
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
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
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 }