| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package com.puppycrawl.tools.checkstyle.checks.javadoc; |
| 20 | |
|
| 21 | |
import com.google.common.collect.Lists; |
| 22 | |
import com.puppycrawl.tools.checkstyle.api.JavadocTagInfo; |
| 23 | |
import com.puppycrawl.tools.checkstyle.api.TextBlock; |
| 24 | |
import com.puppycrawl.tools.checkstyle.api.Utils; |
| 25 | |
import java.util.List; |
| 26 | |
import java.util.regex.Matcher; |
| 27 | |
import java.util.regex.Pattern; |
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
public final class JavadocUtils |
| 34 | |
{ |
| 35 | |
|
| 36 | |
|
| 37 | |
private JavadocUtils() |
| 38 | 0 | { |
| 39 | 0 | } |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
public static JavadocTags getJavadocTags(TextBlock aCmt, |
| 49 | |
JavadocTagType aTagType) |
| 50 | |
{ |
| 51 | 82 | final String[] text = aCmt.getText(); |
| 52 | 82 | final List<JavadocTag> tags = Lists.newArrayList(); |
| 53 | 82 | final List<InvalidJavadocTag> invalidTags = Lists.newArrayList(); |
| 54 | 82 | Pattern blockTagPattern = |
| 55 | |
Utils.getPattern("/\\*{2,}\\s*@(\\p{Alpha}+)\\s"); |
| 56 | 431 | for (int i = 0; i < text.length; i++) { |
| 57 | 349 | final String s = text[i]; |
| 58 | 349 | final Matcher blockTagMatcher = blockTagPattern.matcher(s); |
| 59 | 349 | if ((aTagType.equals(JavadocTagType.ALL) || aTagType |
| 60 | |
.equals(JavadocTagType.BLOCK)) && blockTagMatcher.find()) |
| 61 | |
{ |
| 62 | 105 | final String tagName = blockTagMatcher.group(1); |
| 63 | 105 | String content = s.substring(blockTagMatcher.end(1)); |
| 64 | 105 | if (content.endsWith("*/")) { |
| 65 | 12 | content = content.substring(0, content.length() - 2); |
| 66 | |
} |
| 67 | 105 | final int line = aCmt.getStartLineNo() + i; |
| 68 | 105 | int col = blockTagMatcher.start(1) - 1; |
| 69 | 105 | if (i == 0) { |
| 70 | 4 | col += aCmt.getStartColNo(); |
| 71 | |
} |
| 72 | 105 | if (JavadocTagInfo.isValidName(tagName)) { |
| 73 | 102 | tags.add( |
| 74 | |
new JavadocTag(line, col, tagName, content.trim())); |
| 75 | |
} |
| 76 | |
else { |
| 77 | 3 | invalidTags.add(new InvalidJavadocTag(line, col, tagName)); |
| 78 | |
} |
| 79 | 105 | } |
| 80 | |
|
| 81 | 244 | else if (aTagType.equals(JavadocTagType.ALL) |
| 82 | |
|| aTagType.equals(JavadocTagType.INLINE)) |
| 83 | |
{ |
| 84 | |
|
| 85 | 30 | final Pattern commentPattern = |
| 86 | |
Utils.getPattern("^\\s*(?:/\\*{2,}|\\*+)\\s*(.*)"); |
| 87 | 30 | final Matcher commentMatcher = commentPattern.matcher(s); |
| 88 | |
final String commentContents; |
| 89 | |
final int commentOffset; |
| 90 | 30 | if (!commentMatcher.find()) { |
| 91 | 2 | commentContents = s; |
| 92 | 2 | commentOffset = 0; |
| 93 | |
} |
| 94 | |
else { |
| 95 | 28 | commentContents = commentMatcher.group(1); |
| 96 | 28 | commentOffset = commentMatcher.start(1) - 1; |
| 97 | |
} |
| 98 | 30 | final Pattern tagPattern = |
| 99 | |
Utils.getPattern(".*?\\{@(\\p{Alpha}+)\\s+(.*?)\\}"); |
| 100 | 30 | final Matcher tagMatcher = tagPattern.matcher(commentContents); |
| 101 | 45 | while (tagMatcher.find()) { |
| 102 | 15 | if (tagMatcher.groupCount() == 2) { |
| 103 | 15 | final String tagName = tagMatcher.group(1); |
| 104 | 15 | final String tagValue = tagMatcher.group(2).trim(); |
| 105 | 15 | final int line = aCmt.getStartLineNo() + i; |
| 106 | 15 | int col = commentOffset + (tagMatcher.start(1) - 1); |
| 107 | 15 | if (i == 0) { |
| 108 | 2 | col += aCmt.getStartColNo(); |
| 109 | |
} |
| 110 | 15 | if (JavadocTagInfo.isValidName(tagName)) { |
| 111 | 14 | tags.add(new JavadocTag(line, col, tagName, |
| 112 | |
tagValue)); |
| 113 | |
} |
| 114 | |
else { |
| 115 | 1 | invalidTags.add(new InvalidJavadocTag(line, col, |
| 116 | |
tagName)); |
| 117 | |
} |
| 118 | 15 | } |
| 119 | |
|
| 120 | |
|
| 121 | |
} |
| 122 | |
} |
| 123 | 349 | blockTagPattern = |
| 124 | |
Utils.getPattern("^\\s*\\**\\s*@(\\p{Alpha}+)\\s"); |
| 125 | |
} |
| 126 | 82 | return new JavadocTags(tags, invalidTags); |
| 127 | |
} |
| 128 | |
|
| 129 | |
|
| 130 | |
|
| 131 | |
|
| 132 | 4 | public enum JavadocTagType |
| 133 | |
{ |
| 134 | |
|
| 135 | 1 | BLOCK, |
| 136 | |
|
| 137 | 1 | INLINE, |
| 138 | |
|
| 139 | 1 | ALL; |
| 140 | |
} |
| 141 | |
} |