예제 #1
0
  @Override
  public String toString() {

    String ret = "";

    for (int i = 0; i < kloFiles.size(); i++) {
      KloFile kloFile = kloFiles.get(i);
      ret += "Error n�" + (i + 1) + " " + kloFile.toString();
    }

    return ret;
  }
예제 #2
0
  public KloReport parse(final File file) throws IOException {

    if (file == null) {
      throw new IllegalArgumentException("File input is mandatory.");
    }

    if (!file.exists()) {
      throw new IllegalArgumentException("File input " + file.getName() + " must exist.");
    }

    KloReport report = new KloReport();

    List<ValidationError> list = KlocworkModel.OUTPUT_KLOCWORK_9_2.validate(file);
    if (!list.isEmpty()) {
      StringBuilder sb = new StringBuilder("XML Validation failed. See errors below :\n");
      for (ValidationError val : list) {
        sb.append(val.toString()).append("\n");
      }
      throw new IllegalArgumentException(sb.toString());
    }

    try {

      ErrorList errList = getErrorList(file);
      List<KloFile> lowSeverities = new ArrayList<KloFile>();
      List<KloFile> highSeverities = new ArrayList<KloFile>();
      List<KloFile> errors = new ArrayList<KloFile>();
      int i = 0;
      for (Problem problem : errList.getProblem()) {
        KloFile kloFile;
        kloFile = new KloFile();
        kloFile.setKey(i + 1);

        /** Using reflection to get the tags' name and value and to put them in kloFile map */
        for (Field f : problem.getClass().getDeclaredFields()) {
          f.setAccessible(true);
          try {
            String name = f.getName();
            Object value = f.get(problem);
            if (value != null) {
              String valueToString = value.toString();
              // Changing the default value returned by Object.toString() by an empty value
              if (valueToString.startsWith("com.thalesgroup.hudson.plugins.klocwork.model")
                  && valueToString.contains("@")) {
                kloFile.store(name, "");
              } else {
                kloFile.store(name, valueToString);
              }

              // Treating the trace tag
              if (name.equals("trace")) {
                Trace trace = (Trace) value;
                for (TraceBlock tracelt : trace.getTraceBlock()) {

                  kloFile.addTraceBlock(
                      tracelt.getFile(), tracelt.getMethod(), tracelt.getName(), tracelt.getId());

                  for (TraceLine traceLinelt : tracelt.getTraceLine()) {

                    // Element traceLinelt = (Element) listTraceLine.get(k);
                    String refId = null;
                    if (problem.getRefID() != null
                        && (refId = problem.getRefID().toString()) != null) {
                      kloFile.addTraceLine(
                          tracelt.getId(),
                          traceLinelt.getLine(),
                          traceLinelt.getText(),
                          traceLinelt.getType().charAt(0),
                          Integer.parseInt(refId));
                    } else {
                      kloFile.addTraceLine(
                          tracelt.getId(),
                          traceLinelt.getLine(),
                          traceLinelt.getText(),
                          traceLinelt.getType().charAt(0));
                    }
                  }
                }
              }
            }
          } catch (IllegalArgumentException e) {
            e.printStackTrace();
          } catch (IllegalAccessException e) {
            e.printStackTrace();
          }
          f.setAccessible(false);
        }
        // Adding a new entry in the map corresponding to the file name without its path
        String fileName = kloFile.get("file");
        String fileNameWithoutPath = extractFileName(fileName, "\\");
        if (fileName.equals(fileNameWithoutPath)) {
          fileNameWithoutPath = extractFileName(fileName, "/");
        }
        kloFile.store("fileNameOnly", fileNameWithoutPath);

        if (Integer.parseInt((String) kloFile.get("severitylevel")) > 3) {
          highSeverities.add(kloFile);
        } else {
          lowSeverities.add(kloFile);
        }

        errors.add(kloFile);

        agregateMap.put(kloFile.getKey(), kloFile);

        kloFiles.add(kloFile);

        i++;
      }

      if (!lowSeverities.isEmpty()) {
        report.setLowSeverities(lowSeverities);
      }

      if (!highSeverities.isEmpty()) {
        report.setHighSeverities(highSeverities);
      }

      report.setErrors(errors);

    } catch (JAXBException e) {
      e.printStackTrace();
    }

    return report;
  }