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 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
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
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
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 }