1 package com.puppycrawl.tools.checkstyle.api; 20 21 import java.util.ArrayList ; 22 import java.util.Collections ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.HashMap ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 29 import java.util.regex.Pattern ; 30 31 import com.puppycrawl.tools.checkstyle.grammars.CommentListener; 32 33 39 public final class FileContents implements CommentListener 40 { 41 45 private static final String MATCH_SINGLELINE_COMMENT_PAT = 46 "^\\s*//.*$"; 47 48 private static final Pattern MATCH_SINGLELINE_COMMENT = 49 Pattern.compile(MATCH_SINGLELINE_COMMENT_PAT); 50 51 52 private final String mFilename; 53 54 55 private final String [] mLines; 56 57 60 private final Map mJavadocComments = new HashMap (); 61 62 63 private final Map mCPlusPlusComments = new HashMap (); 64 68 private final Map mCComments = new HashMap (); 69 70 76 public FileContents(String aFilename, String [] aLines) 77 { 78 mFilename = aFilename; 79 mLines = aLines; 80 } 81 82 83 public void reportSingleLineComment(String aType, 84 int aStartLineNo, int aStartColNo) 85 { 86 reportCppComment(aStartLineNo, aStartColNo); 87 } 88 89 90 public void reportBlockComment(String aType, 91 int aStartLineNo, int aStartColNo, 92 int aEndLineNo, int aEndColNo) 93 { 94 reportCComment(aStartLineNo, aStartColNo, aEndLineNo, aEndColNo); 95 } 96 97 102 public void reportCppComment(int aStartLineNo, int aStartColNo) 103 { 104 final String line = mLines[aStartLineNo - 1]; 105 final String [] txt = new String [] {line.substring(aStartColNo)}; 106 final Comment comment = 107 new Comment(txt, aStartColNo, aStartLineNo, line.length() - 1); 108 mCPlusPlusComments.put(new Integer (aStartLineNo), comment); 109 } 110 111 116 public Map getCppComments() 117 { 118 return Collections.unmodifiableMap(mCPlusPlusComments); 119 } 120 121 128 public void reportCComment(int aStartLineNo, int aStartColNo, 129 int aEndLineNo, int aEndColNo) 130 { 131 final String [] cc = extractCComment(aStartLineNo, aStartColNo, 132 aEndLineNo, aEndColNo); 133 final Comment comment = new Comment(cc, aStartColNo, aEndLineNo, 134 aEndColNo); 135 136 final Integer key = new Integer (aStartLineNo); 138 if (mCComments.containsKey(key)) { 139 final List entries = (List ) mCComments.get(key); 140 entries.add(comment); 141 } 142 else { 143 final List entries = new ArrayList (); 144 entries.add(comment); 145 mCComments.put(key, entries); 146 } 147 148 if (mLines[aStartLineNo - 1].indexOf("/**", aStartColNo) != -1) { 150 mJavadocComments.put(new Integer (aEndLineNo - 1), comment); 151 } 152 } 153 154 160 public Map getCComments() 161 { 162 return Collections.unmodifiableMap(mCComments); 163 } 164 165 173 private String [] extractCComment(int aStartLineNo, int aStartColNo, 174 int aEndLineNo, int aEndColNo) 175 { 176 String [] retVal; 177 if (aStartLineNo == aEndLineNo) { 178 retVal = new String [1]; 179 retVal[0] = mLines[aStartLineNo - 1].substring(aStartColNo, 180 aEndColNo + 1); 181 } 182 else { 183 retVal = new String [aEndLineNo - aStartLineNo + 1]; 184 retVal[0] = mLines[aStartLineNo - 1].substring(aStartColNo); 185 for (int i = aStartLineNo; i < aEndLineNo; i++) { 186 retVal[i - aStartLineNo + 1] = mLines[i]; 187 } 188 retVal[retVal.length - 1] = 189 mLines[aEndLineNo - 1].substring(0, aEndColNo + 1); 190 } 191 return retVal; 192 } 193 194 200 public TextBlock getJavadocBefore(int aLineNo) 201 { 202 int lineNo = aLineNo - 2; 204 205 while ((lineNo > 0) && (lineIsBlank(lineNo) || lineIsComment(lineNo))) { 207 lineNo--; 208 } 209 210 return (TextBlock) mJavadocComments.get(new Integer (lineNo)); 211 } 212 213 214 public String [] getLines() 215 { 216 return mLines; 217 } 218 219 220 public String getFilename() 221 { 222 return mFilename; 223 } 224 225 230 public boolean lineIsBlank(int aLineNo) 231 { 232 return "".equals(mLines[aLineNo].trim()); 234 } 235 236 242 public boolean lineIsComment(int aLineNo) 243 { 244 return MATCH_SINGLELINE_COMMENT.matcher(mLines[aLineNo]).matches(); 245 } 246 247 255 public boolean hasIntersectionWithComment( 256 int aStartLineNo, int aStartColNo, int aEndLineNo, int aEndColNo) 257 { 258 final Collection values = mCComments.values(); 260 261 final Iterator it = values.iterator(); 262 while (it.hasNext()) { 263 final List row = (List ) it.next(); 264 final Iterator rowIterator = row.iterator(); 265 while (rowIterator.hasNext()) { 266 final TextBlock comment = (TextBlock) rowIterator.next(); 267 if (comment.intersects( 268 aStartLineNo, aStartColNo, aEndLineNo, aEndColNo)) 269 { 270 return true; 271 } 272 } 273 } 274 275 for (int lineNumber = aStartLineNo; lineNumber <= aEndLineNo; 277 lineNumber++) 278 { 279 final TextBlock comment = 280 (TextBlock) mCPlusPlusComments.get(new Integer (lineNumber)); 281 if ((comment != null) 282 && comment.intersects(aStartLineNo, aStartColNo, 283 aEndLineNo, aEndColNo)) 284 { 285 return true; 286 } 287 } 288 return false; 289 } 290 291 } 292 | Popular Tags |