private Reader open() throws IOException { if ("default".equals(config.getEncoding())) { return new FileReader(fileModel.getSourceFile()); } InputStream is = new FileInputStream(fileModel.getSourceFile()); try { return new InputStreamReader(is, config.getEncoding()); } catch (IOException ex) { CloseUtil.close(is); throw ex; } }
/** * Output the file model. * * @param w the output writer. * @throws IOException if there is a problem. */ public void execute(PrintWriter w) throws IOException { this.w = w; w.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); w.println("<file"); w.print(" "); w.println(XMLUtil.toAttribute("name", fileModel.getDisplayName())); w.print(" "); w.println(XMLUtil.toAttribute("last-modified", "" + fileModel.getLastModified())); if (fileModel.getSourceFile() != null) { w.print(" "); w.println(XMLUtil.toAttribute("file", fileModel.getSourceFile().getAbsolutePath())); } w.println(">"); createLimited(); if (fileModel.getSourceFile() != null && fileModel.getSourceFile().exists()) { try { outputContents(); } finally { closeSourceFile(); } } outputSummary(); w.println("</file>"); }
private void createLimited() throws IOException { for (Map.Entry<String, TreeSet<Violation>> e : fileModel.getTypeMap().entrySet()) { String type = e.getKey(); Set<Violation> violations = e.getValue(); LimitedType limitedType = new LimitedType(); limitedType.number = violations.size(); int c = 0; for (Violation v : violations) { limitedType.violations.add(v); addToVMap(v); doViolation(v); c++; if (c >= config.getLimit()) { break; } } limitedMap.put(type, limitedType); } }
private void parseViolations(List<Element> ruleElements) { // searching source files AbsoluteFileFinder finder = new AbsoluteFileFinder(); // add the project path to the search source finder.addSourcePath(this.projectPath.getPath()); // if there's additional paths, add them too if (this.sourcePaths != null) { finder.addSourcePaths(this.sourcePaths); } for (Element ruleElement : ruleElements) { String ruleName = ruleElement.getAttribute("Name"); GendarmeRule rule = this.rules.get(ruleName); String problem = ruleElement.getElementsByTagName("problem").item(0).getTextContent(); String solution = ruleElement.getElementsByTagName("solution").item(0).getTextContent(); List<Element> targetElements = XmlElementUtil.getNamedChildElements(ruleElement, "target"); for (Element targetElement : targetElements) { // String targetName = targetElement.getAttribute("Name"); String targetAssembly = targetElement.getAttribute("Assembly"); DotNetAssembly assembly = new DotNetAssembly(targetAssembly); Element defectElement = (Element) targetElement.getElementsByTagName("defect").item(0); String severityString = defectElement.getAttribute("Severity"); String source = defectElement.getAttribute("Source"); // String location = defectElement.getAttribute("Location"); String confidence = defectElement.getAttribute("Confidence"); String filePath = ""; String fileName = ""; int line = 0; if (rule.getType() == GendarmeRuleType.Method) { Pattern pattern = Pattern.compile("^(.*)\\(.([0-9]*)\\)$"); Matcher matcher = pattern.matcher(source); logger.info("matcher.groupCount() : " + matcher.groupCount()); logger.info("matcher.matches() : " + matcher.matches()); logger.info("source : " + source); if (matcher.matches()) { for (int cpt = 0; cpt < matcher.groupCount(); cpt++) { logger.info("group(" + ((int) (cpt + 1)) + "): " + matcher.group(cpt + 1)); } } if (matcher.matches()) { String fullPath = matcher.group(1); File sourceFile = new File(fullPath); fileName = sourceFile.getName(); filePath = sourceFile.getParent(); line = Integer.parseInt(matcher.group(2)); } } // create the violation Violation violation = new Violation(); // construct the error message StringBuilder messageBuilder = new StringBuilder(); if (rule.getUrl() != null) { messageBuilder.append("<a href=\"").append(rule.getUrl().toString()).append("\">"); messageBuilder.append(rule.getName()); messageBuilder.append("</a>"); } else { messageBuilder.append(rule.getName()); } messageBuilder.append(" - ").append(problem).append("<br/>"); messageBuilder.append("Solution: ").append(solution).append("<br/>"); messageBuilder.append("Confidence: ").append(confidence); violation.setMessage(messageBuilder.toString()); violation.setPopupMessage(problem); // construct the severity if (severityString.equals("Low")) { violation.setSeverityLevel(Severity.LOW_VALUE); violation.setSeverity(Severity.LOW); } else if (severityString.equals("Medium")) { violation.setSeverityLevel(Severity.MEDIUM_VALUE); violation.setSeverity(Severity.MEDIUM); } else if (severityString.equals("High")) { violation.setSeverityLevel(Severity.HIGH_VALUE); violation.setSeverity(Severity.HIGH); } else { violation.setSeverityLevel(Severity.MEDIUM_VALUE); violation.setSeverity(Severity.MEDIUM); } violation.setType(TYPE_NAME); violation.setSource(rule.getName()); // try to get the file // TODO : test it with Linux Master => Windows Slave node. Unix/Windows path could be a // problem. FullFileModel fileModel; if ((filePath.length() > 0) && (fileName.length() > 0)) { violation.setLine(line); // get the display name of the file String displayName = ParseUtil.resolveAbsoluteName( this.projectPath, filePath + File.separatorChar + fileName); // try to get the source file, add it if not already exists fileModel = model.getFileModel(displayName); if (fileModel.getSourceFile() == null) { finder.addSourcePath(filePath); File sourceFile = finder.getFileForName(fileName); if (sourceFile != null && sourceFile.exists()) { fileModel.setSourceFile(sourceFile); fileModel.setLastModified(sourceFile.lastModified()); logger.info( "fileModel.getSourceFile() : " + fileModel.getSourceFile().getAbsolutePath()); } else { logger.info("sourceFile.exists()==false: " + filePath + "," + fileName); } } else { logger.info("fileModel.getSourceFile() != null: " + displayName); } } else { // if there's no source files, just put the assembly name fileModel = model.getFileModel(assembly.getName() + ".dll"); logger.info( "fileModel.getSourceFile() : " + (fileModel.getSourceFile() == null ? "null" : fileModel.getSourceFile().getAbsolutePath())); } logger.info("fileModel.getDisplayName() : " + fileModel.getDisplayName()); logger.info("reportParentFile : " + reportParentFile); logger.info("fileName : " + fileName); logger.info("filePath : " + filePath); // Add the violation to the model fileModel.addViolation(violation); } } }