Beispiel #1
0
  /**
   * Save the properties of all renderers to the specified filename.
   *
   * @param propertyFilename String
   */
  private static void saveReportProperties(String propertyFilename) {

    Properties props = new Properties();

    for (Renderer renderer : ReportManager.instance.allRenderers()) {
      Map<PropertyDescriptor<?>, Object> valuesByProp =
          renderer.getPropertiesByPropertyDescriptor();
      for (Map.Entry<PropertyDescriptor<?>, Object> entry : valuesByProp.entrySet()) {
        PropertyDescriptor desc = entry.getKey();
        props.put(keyOf(renderer, desc), desc.asDelimitedString(entry.getValue()));
      }
    }

    FileOutputStream fos = null;

    try {
      fos = new FileOutputStream(propertyFilename);
      props.storeToXML(fos, "asdf");

    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      IOUtil.closeQuietly(fos);
    }
  }
Beispiel #2
0
  /**
   * Load the properties for all renderers from the specified filename. Return whether we succeeded
   * or not.
   *
   * @param propertyFilename String
   * @return boolean
   */
  private static boolean loadReportProperties(String propertyFilename) {

    Properties props = new Properties();
    FileInputStream fis = null;
    try {
      fis = new FileInputStream(propertyFilename);
      props.loadFromXML(fis);
    } catch (Exception e) {
      return false;
    } finally {
      IOUtil.closeQuietly(fis);
    }

    for (Renderer renderer : ReportManager.instance.allRenderers()) {

      for (PropertyDescriptor pDesc : renderer.getPropertyDescriptors()) {
        String key = keyOf(renderer, pDesc);
        if (props.containsKey(key)) {
          Object value = pDesc.valueFrom((String) props.get(key));
          renderer.setProperty(pDesc, value);
        }
      }
    }

    return true;
  }
Beispiel #3
0
 /**
  * Create a Renderer instance based upon the configured reporting options. If withReportWriter
  * then we'll configure it with a writer for the reportFile specified.
  *
  * @param withReportWriter whether to configure a writer or not
  * @return A Renderer instance.
  */
 public Renderer createRenderer(boolean withReportWriter) {
   Renderer renderer = RendererFactory.createRenderer(reportFormat, reportProperties);
   renderer.setShowSuppressedViolations(showSuppressedViolations);
   if (withReportWriter) {
     renderer.setWriter(IOUtil.createWriter(reportFile));
   }
   return renderer;
 }
  /**
   * @param languageVersion LanguageVersion
   * @param ruleSet RuleSet
   * @param dataSources List<DataSource>
   * @param results Set<RuleDuration>
   * @param debug boolean
   * @throws PMDException
   * @throws IOException
   */
  private static void stress(
      LanguageVersion languageVersion,
      RuleSet ruleSet,
      List<DataSource> dataSources,
      Set<RuleDuration> results,
      boolean debug)
      throws PMDException, IOException {

    for (Rule rule : ruleSet.getRules()) {
      if (debug) {
        System.out.println("Starting " + rule.getName());
      }

      RuleSet working = new RuleSet();
      working.addRule(rule);
      RuleSets ruleSets = new RuleSets(working);

      PMDConfiguration config = new PMDConfiguration();
      config.setDefaultLanguageVersion(languageVersion);

      RuleContext ctx = new RuleContext();
      long start = System.currentTimeMillis();
      Reader reader = null;
      for (DataSource dataSource : dataSources) {
        reader = new InputStreamReader(dataSource.getInputStream());
        ctx.setSourceCodeFilename(dataSource.getNiceFileName(false, null));
        new SourceCodeProcessor(config).processSourceCode(reader, ruleSets, ctx);
        IOUtil.closeQuietly(reader);
      }
      long end = System.currentTimeMillis();
      long elapsed = end - start;
      results.add(new RuleDuration(elapsed, rule));
      if (debug) {
        System.out.println("Done timing " + rule.getName() + "; elapsed time was " + elapsed);
      }
    }
  }
  public static void doPMD(PMDConfiguration configuration) {

    // Load the RuleSets
    long startLoadRules = System.nanoTime();
    RuleSetFactory ruleSetFactory = RulesetsFactoryUtils.getRulesetFactory(configuration);

    RuleSets ruleSets =
        RulesetsFactoryUtils.getRuleSets(
            configuration.getRuleSets(), ruleSetFactory, startLoadRules);
    if (ruleSets == null) return;

    Set<Language> languages = getApplicableLanguages(configuration, ruleSets);
    List<DataSource> files = getApplicableFiles(configuration, languages);

    long reportStart = System.nanoTime();
    try {
      Renderer renderer = configuration.createRenderer();
      List<Renderer> renderers = new LinkedList<Renderer>();
      renderers.add(renderer);

      renderer.setWriter(IOUtil.createWriter(configuration.getReportFile()));
      renderer.start();

      Benchmarker.mark(Benchmark.Reporting, System.nanoTime() - reportStart, 0);

      RuleContext ctx = new RuleContext();

      processFiles(configuration, ruleSetFactory, files, ctx, renderers);

      reportStart = System.nanoTime();
      renderer.end();
      renderer.flush();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }