Пример #1
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;
  }
Пример #2
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);
    }
  }
  /**
   * @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);
      }
    }
  }