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 InlineTagUtil {
34
35
36
37
38 private static final Pattern INLINE_TAG_PATTERN = Pattern.compile(
39 "\\{@(\\p{Alpha}+)\\b(.*?)}", Pattern.DOTALL);
40
41
42 private static final Pattern JAVADOC_PREFIX_PATTERN = Pattern.compile(
43 "^\\s*\\*", Pattern.MULTILINE);
44
45
46 private static final Pattern WHITESPACE_PATTERN = Pattern.compile("\\s+");
47
48
49 private static final Pattern NEWLINE_PATTERN = Pattern.compile("\\n");
50
51
52 private static final char LINE_FEED = '\n';
53
54
55 private static final char CARRIAGE_RETURN = '\r';
56
57
58 private InlineTagUtil() {
59 }
60
61
62
63
64
65
66
67
68 public static List<TagInfo> extractInlineTags(String... lines) {
69 for (String line : lines) {
70 if (line.indexOf(LINE_FEED) != -1 || line.indexOf(CARRIAGE_RETURN) != -1) {
71 throw new IllegalArgumentException("comment lines cannot contain newlines");
72 }
73 }
74
75 final String commentText = convertLinesToString(lines);
76 final Matcher inlineTagMatcher = INLINE_TAG_PATTERN.matcher(commentText);
77
78 final List<TagInfo> tags = new ArrayList<>();
79
80 while (inlineTagMatcher.find()) {
81 final String tagName = inlineTagMatcher.group(1);
82
83
84
85 String matchedTagValue = inlineTagMatcher.group(2);
86 matchedTagValue = removeLeadingJavaDoc(matchedTagValue);
87 matchedTagValue = collapseWhitespace(matchedTagValue);
88
89 final String tagValue = matchedTagValue;
90
91 final int startIndex = inlineTagMatcher.start(1);
92 final LineColumn position = getLineColumnOfIndex(commentText,
93
94 startIndex - 1);
95
96 tags.add(new TagInfo(tagName, tagValue, position));
97 }
98
99 return tags;
100 }
101
102
103
104
105
106
107
108 private static String convertLinesToString(String... lines) {
109 final StringBuilder builder = new StringBuilder(1024);
110 for (String line : lines) {
111 builder.append(line);
112 builder.append(LINE_FEED);
113 }
114 return builder.toString();
115 }
116
117
118
119
120
121
122
123
124 private static LineColumn getLineColumnOfIndex(String source, int index) {
125 final String precedingText = source.subSequence(0, index).toString();
126 final String[] precedingLines = NEWLINE_PATTERN.split(precedingText);
127 final String lastLine = precedingLines[precedingLines.length - 1];
128 return new LineColumn(precedingLines.length, lastLine.length());
129 }
130
131
132
133
134
135
136
137 private static String collapseWhitespace(String str) {
138 final Matcher matcher = WHITESPACE_PATTERN.matcher(str);
139 return matcher.replaceAll(" ").trim();
140 }
141
142
143
144
145
146
147
148 private static String removeLeadingJavaDoc(String source) {
149 final Matcher matcher = JAVADOC_PREFIX_PATTERN.matcher(source);
150 return matcher.replaceAll("");
151 }
152
153 }