- Timestamp:
- 02/12/10 13:48:43 (2 years ago)
- Location:
- trunk
- Files:
-
- 2 modified
-
. (modified) (1 prop)
-
src/java/org/jcoderz/phoenix/report/GenericReportReader.java (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:ignore
-
old new 3 3 eclipse 4 4 ${env.CRUISECONTROL_HOME} 5 derbyDB 6 .byecycle
-
- Property svn:ignore
-
trunk/src/java/org/jcoderz/phoenix/report/GenericReportReader.java
r1504 r1606 35 35 import java.io.BufferedReader; 36 36 import java.io.File; 37 import java.io.FileInputStream; 38 import java.io.FileNotFoundException; 37 39 import java.io.FileReader; 38 40 import java.io.IOException; … … 67 69 * Reads reports with format definitions described in the 68 70 * finding-type-format-definition.xds. 69 * 71 * 70 72 * To find the finding type format definition for requested format 71 73 * the following locations are used: 72 * 74 * 73 75 * The name is converted to lower case. 74 * 75 * A file <i>name</i>.xml is searched in the 76 * 77 * A file <i>name</i>.xml is searched in the 76 78 * <code>org.jcoderz.phoenix.report.ftf</code> package. If 77 79 * this is not found the file is searched in the <code>ftf</code> 78 80 * directory. The directory must be available through the classpath. 79 * 80 * 81 * 82 * 81 83 * @author Andreas Mandel 82 84 * … … 88 90 = GenericReportReader.class.getName(); 89 91 private static final Logger logger = Logger.getLogger(CLASSNAME); 90 91 private static final Pattern CODE_LINE_PATTERN 92 93 private static final Pattern CODE_LINE_PATTERN 92 94 = Pattern.compile("^.*$", Pattern.MULTILINE); 93 94 private static final Pattern CARET_LINE_PATTERN 95 96 private static final Pattern CARET_LINE_PATTERN 95 97 = Pattern.compile("^\\s*\\^$", Pattern.MULTILINE); 96 98 97 99 private static final Map<Origin, GenericReportReader> GENERIC_REPORT_TYPES 98 100 = new HashMap<Origin, GenericReportReader>(); 99 101 100 102 101 103 private final List<GenericFindingType> mFindingTypes 102 104 = new ArrayList<GenericFindingType>(); 103 105 104 106 private Map<ResourceInfo, List<Item>> mItems; 105 107 106 108 private SourceFile mSourceFile; 107 109 … … 109 111 private final FindingTypeFormat mFindingTypeFormatDescription; 110 112 111 private final int mTextPos; 113 private final int mTextPos; 112 114 private final Origin mOrigin; 113 115 private final int mFilePos; 114 116 private final int mLineStart; 115 117 private final Severity mDefaultSeverity; 116 118 117 119 private Matcher mRootMatcher = null; 118 120 119 private GenericReportReader (Origin type) 121 private GenericReportReader (Origin type) 120 122 throws JAXBException 121 123 { … … 123 125 mFindingTypeFormatDescription = loadFormatDescription(type); 124 126 initializeFindingTypes(); 125 final FindingDescription root 127 final FindingDescription root 126 128 = mFindingTypeFormatDescription.getRootType(); 127 mMessagePattern 128 = Pattern.compile(root.getPattern(), 129 mMessagePattern 130 = Pattern.compile(root.getPattern(), 129 131 Pattern.MULTILINE); 130 132 mTextPos = Integer.parseInt(root.getTextPos()); 131 133 mFilePos = Integer.parseInt(root.getFilenamePos()); 132 mLineStart = root.isSetLineStartPos() 134 mLineStart = root.isSetLineStartPos() 133 135 ? Integer.parseInt(root.getLineStartPos()) : -1; 134 mDefaultSeverity = root.isSetSeverity() 136 mDefaultSeverity = root.isSetSeverity() 135 137 ? root.getSeverity() : Severity.CODE_STYLE; 136 138 } 137 139 138 140 /** 139 * Initializes the selected finding type. 141 * Initializes the selected finding type. 140 142 * Might return <code>null</code> if the initialization fails. 141 143 * CHECKME: Should return a null object? … … 157 159 { 158 160 // TODO: collect this an add it to the findings map later! 159 logger.log(Level.WARNING, 160 "Could not load finding type for '" + findingType 161 logger.log(Level.WARNING, 162 "Could not load finding type for '" + findingType 161 163 + "' failed with " + ex.getMessage() + ".", ex); 162 164 } … … 175 177 try 176 178 { 177 final String filename 178 = type.toString().toLowerCase(Constants.SYSTEM_LOCALE) 179 final String filename 180 = type.toString().toLowerCase(Constants.SYSTEM_LOCALE) 179 181 + ".xml"; 180 182 in = GenericReportReader.class.getResourceAsStream( … … 185 187 "/ftf/" + filename); 186 188 } 189 if (in == null) 190 { 191 try 192 { 193 in = new FileInputStream(filename); 194 } 195 catch (FileNotFoundException ex) 196 { 197 // in = null; 198 } 199 } 187 200 Assert.notNull(in, "report type description " + type); 188 final UnmarshalResult unmarshal 189 = JaxbUtil.unmarshal(new InputSource(in), 201 final UnmarshalResult unmarshal 202 = JaxbUtil.unmarshal(new InputSource(in), 190 203 "org.jcoderz.phoenix.report.ftf.jaxb"); 191 204 192 205 findingTypeFormatDescription 193 206 = (FindingTypeFormat) unmarshal.getParsedData(); … … 199 212 return findingTypeFormatDescription; 200 213 } 201 214 202 215 /** {@inheritDoc} */ 203 216 public void parse (File f) … … 225 238 } 226 239 } 227 240 228 241 /** 229 242 * Reads the given message and tries to find a matching finding type. … … 231 244 * @return the finding type matching to the message, or null if no such 232 245 * type was found. 233 * @throws JAXBException if item creation fails. 246 * @throws JAXBException if item creation fails. 234 247 */ 235 public Item detectFindingTypeForMessage (String message) 248 public Item detectFindingTypeForMessage (String message) 236 249 throws JAXBException 237 250 { … … 251 264 if (logger.isLoggable(Level.FINE)) 252 265 { 253 logger.fine("For text: '" 266 logger.fine("For text: '" 254 267 + StringUtil.trimLength(message, MAX_DEBUG_TEXT_CHARS) 255 + "' matched finding: " 256 + (result == null ? "null" : result.getFindingType() 268 + "' matched finding: " 269 + (result == null ? "null" : result.getFindingType() 257 270 + "'. End at " + mSourceFile.getPos())); 258 271 } … … 262 275 private void addPositionByCaret (final Item i) 263 276 { 264 final String text 265 = mSourceFile.getContent().substring(mSourceFile.getPos()); 266 final Matcher codeMat 277 final String text 278 = mSourceFile.getContent().substring(mSourceFile.getPos()); 279 final Matcher codeMat 267 280 = CODE_LINE_PATTERN.matcher(text); 268 281 if (codeMat.lookingAt()) 269 282 { 270 final String textAfterCode 283 final String textAfterCode 271 284 = mSourceFile.getContent().substring( 272 mSourceFile.getPos() + codeMat.end() + 1); 273 final Matcher caretMat 285 mSourceFile.getPos() + codeMat.end() + 1); 286 final Matcher caretMat 274 287 = CARET_LINE_PATTERN.matcher(textAfterCode); 275 288 if (caretMat.lookingAt()) … … 277 290 i.setColumn(caretMat.end()); 278 291 mSourceFile.setPos( 279 mSourceFile.getPos() 280 + codeMat.end() + 1 292 mSourceFile.getPos() 293 + codeMat.end() + 1 281 294 + caretMat.end() + 1); 282 295 } 283 296 else 284 297 { 285 logger.fine("Caret defined but not found for '" 298 logger.fine("Caret defined but not found for '" 286 299 + i.getFindingType() 287 + "' Code Line: '" + codeMat + "' caretLine: '" 288 + caretMat + "'. text: '" 300 + "' Code Line: '" + codeMat + "' caretLine: '" 301 + caretMat + "'. text: '" 289 302 + StringUtil.trimLength( 290 303 textAfterCode, MAX_DEBUG_TEXT_CHARS) + "'."); … … 293 306 else 294 307 { 295 logger.fine("Caret defined but not found for '" 308 logger.fine("Caret defined but not found for '" 296 309 + i.getFindingType() 297 + "' Code Line: '" + codeMat + "'. text: '" 310 + "' Code Line: '" + codeMat + "'. text: '" 298 311 + StringUtil.trimLength( 299 312 text, MAX_DEBUG_TEXT_CHARS) + "'."); … … 301 314 } 302 315 303 private void parseNext () 316 private void parseNext () 304 317 throws JAXBException 305 318 { … … 317 330 if (item == null) 318 331 { 319 final int pos 332 final int pos 320 333 = mSourceFile.getContent().indexOf( 321 334 '\n', mSourceFile.getPos()); 322 335 if (pos != -1) 323 336 { 324 mSourceFile.setPos(pos + 1); 337 mSourceFile.setPos(pos + 1); 325 338 } 326 339 else 327 340 { 328 mSourceFile.setPos(mSourceFile.getContent().length()); 341 mSourceFile.setPos(mSourceFile.getContent().length()); 329 342 } 330 343 } … … 336 349 item.setSeverity(mDefaultSeverity); 337 350 } 338 if (!item.isSetLine() && mLineStart != -1 351 if (!item.isSetLine() && mLineStart != -1 339 352 && mRootMatcher.group(mLineStart) != null) 340 353 { … … 366 379 + " '" + StringUtil.trimLength( 367 380 mSourceFile.getContent().substring( 368 mSourceFile.getPos()), 381 mSourceFile.getPos()), 369 382 MAX_DEBUG_TEXT_CHARS)); 370 383 } … … 407 420 else 408 421 { 409 logger.finer("Ignore findings for resource '" 410 + resourceFilename + "' type was " 422 logger.finer("Ignore findings for resource '" 423 + resourceFilename + "' type was " 411 424 + item.getFindingType() + "."); 412 425 } 413 426 } 414 427 415 428 private void initializeFindingTypes () 416 429 { 417 final FindingDescription root 430 final FindingDescription root 418 431 = mFindingTypeFormatDescription.getRootType(); 419 final List<FindingDescription> findingTypes 432 final List<FindingDescription> findingTypes 420 433 = mFindingTypeFormatDescription.getFindingType(); 421 434 for (FindingDescription findingDesc : findingTypes) 422 435 { 423 final GenericFindingType gft 436 final GenericFindingType gft 424 437 = new GenericFindingType(root, findingDesc); 425 438 mFindingTypes.add(gft); … … 428 441 mFindingTypes, new GenericFindingType.OrderByPriority()); 429 442 } 430 431 443 444 432 445 static final class SourceFile 433 446 { … … 435 448 private final String mContent; 436 449 private int mPos; 437 438 public SourceFile (File file) 450 451 public SourceFile (File file) 439 452 throws IOException 440 453 { … … 487 500 return mContent; 488 501 } 489 502 490 503 public boolean readFully () 491 504 {
