1 package com.puppycrawl.tools.checkstyle.checks.javadoc; 20 21 import com.puppycrawl.tools.checkstyle.api.Check; 22 import com.puppycrawl.tools.checkstyle.api.DetailAST; 23 import com.puppycrawl.tools.checkstyle.api.FileContents; 24 import com.puppycrawl.tools.checkstyle.api.SeverityLevel; 25 import com.puppycrawl.tools.checkstyle.api.TextBlock; 26 import com.puppycrawl.tools.checkstyle.api.TokenTypes; 27 import com.puppycrawl.tools.checkstyle.api.Utils; 28 import java.util.regex.Matcher ; 29 import java.util.regex.Pattern ; 30 import java.util.regex.PatternSyntaxException ; 31 import org.apache.commons.beanutils.ConversionException; 32 33 67 public class WriteTagCheck 68 extends Check 69 { 70 71 private Pattern mTagRE; 72 73 private Pattern mTagFormatRE; 74 75 76 private String mTag; 77 78 private String mTagFormat; 79 80 private SeverityLevel mTagSeverityLevel = SeverityLevel.INFO; 81 82 87 public void setTag(String aTag) 88 throws ConversionException 89 { 90 try { 91 mTag = aTag; 92 mTagRE = Utils.getPattern(aTag + "\\s+(.*$)"); 93 } 94 catch (final PatternSyntaxException e) { 95 throw new ConversionException("unable to parse " + aTag, e); 96 } 97 } 98 99 104 public void setTagFormat(String aFormat) 105 throws ConversionException 106 { 107 try { 108 mTagFormat = aFormat; 109 mTagFormatRE = Utils.getPattern(aFormat); 110 } 111 catch (final PatternSyntaxException e) { 112 throw new ConversionException("unable to parse " + aFormat, e); 113 } 114 } 115 116 123 public final void setTagSeverity(String aSeverity) 124 { 125 mTagSeverityLevel = SeverityLevel.getInstance(aSeverity); 126 } 127 128 129 public int[] getDefaultTokens() 130 { 131 return new int[] {TokenTypes.INTERFACE_DEF, TokenTypes.CLASS_DEF, }; 132 } 133 134 135 public int[] getAcceptableTokens() 136 { 137 return new int[] {TokenTypes.INTERFACE_DEF, 138 TokenTypes.CLASS_DEF, 139 TokenTypes.METHOD_DEF, 140 }; 141 } 142 143 144 public void visitToken(DetailAST aAST) 145 { 146 final FileContents contents = getFileContents(); 147 final int lineNo = aAST.getLineNo(); 148 final TextBlock cmt = 149 contents.getJavadocBefore(lineNo); 150 if (cmt == null) { 151 log(lineNo, "type.missingTag", mTag); 152 } 153 else { 154 checkTag(lineNo, cmt.getText(), mTag, mTagRE, mTagFormatRE, 155 mTagFormat); 156 } 157 } 158 159 168 private void checkTag( 169 int aLineNo, 170 String [] aCmt, 171 String aTag, 172 Pattern aTagRE, 173 Pattern aFormatRE, 174 String aFormat) 175 { 176 if (aTagRE == null) { 177 return; 178 } 179 180 int tagCount = 0; 181 for (int i = 0; i < aCmt.length; i++) { 182 final String s = aCmt[i]; 183 final Matcher matcher = aTagRE.matcher(s); 184 if (matcher.find()) { 185 tagCount += 1; 186 final int contentStart = matcher.start(1); 187 final String content = s.substring(contentStart); 188 if ((aFormatRE != null) && !aFormatRE.matcher(content).find()) { 189 log(aLineNo + i - aCmt.length, "type.tagFormat", aTag, 190 aFormat); 191 } 192 else { 193 logTag(aLineNo + i - aCmt.length, aTag, content); 194 } 195 196 } 197 } 198 if (tagCount == 0) { 199 log(aLineNo, "type.missingTag", aTag); 200 } 201 202 } 203 204 205 214 protected final void logTag(int aLine, String aTag, String aTagValue) 215 { 216 final String originalSeverity = getSeverity(); 217 setSeverity(mTagSeverityLevel.getName()); 218 219 log(aLine, "javadoc.writeTag", aTag, aTagValue); 220 221 setSeverity(originalSeverity); 222 } 223 } 224 | Popular Tags |