コード例 #1
0
  /**
   * Get an <code>CheckConfiguration</code> instance by its name.
   *
   * @param name Name of the requested instance.
   * @return The requested instance or <code>null</code> if the named instance could not be found.
   */
  public static ICheckConfiguration getByName(String name) {

    for (ICheckConfiguration config : sConfigurations) {
      if (config.getName().equals(name)) {
        return config;
      }
    }

    return null;
  }
コード例 #2
0
  /** Load the check configurations from the persistent state storage. */
  private static void loadFromPersistence() throws CheckstylePluginException {

    InputStream inStream = null;

    try {

      IPath configPath = CheckstylePlugin.getDefault().getStateLocation();
      configPath = configPath.append(CHECKSTYLE_CONFIG_FILE);
      File configFile = configPath.toFile();

      //
      // Make sure the files exists, it might not.
      //
      if (!configFile.exists()) {
        return;
      } else {
        inStream = new BufferedInputStream(new FileInputStream(configFile));
      }

      SAXReader reader = new SAXReader();
      Document document = reader.read(inStream);

      Element root = document.getRootElement();

      String version = root.attributeValue(XMLTags.VERSION_TAG);
      if (!CURRENT_CONFIG_FILE_FORMAT_VERSION.equals(version)) {

        // the old (pre 4.0.0) configuration files aren't supported
        // anymore
        CheckstyleLog.log(
            null, "eclipse-cs version 3.x type configuration files are not supported anymore.");
        return;
      }

      String defaultConfigName = root.attributeValue(XMLTags.DEFAULT_CHECK_CONFIG_TAG);

      sConfigurations.addAll(getGlobalCheckConfigurations(root));

      for (ICheckConfiguration config : sConfigurations) {
        if (config.getName().equals(defaultConfigName)) {
          sDefaultCheckConfig = config;
        }
      }
    } catch (IOException e) {
      CheckstylePluginException.rethrow(e, Messages.errorLoadingConfigFile);
    } catch (DocumentException e) {
      CheckstylePluginException.rethrow(e, Messages.errorLoadingConfigFile);
    } finally {
      IOUtils.closeQuietly(inStream);
    }
  }
コード例 #3
0
  /**
   * Copy the checkstyle configuration of a check configuration into another configuration.
   *
   * @param source the source check configuration
   * @param target the target check configuartion
   * @throws CheckstylePluginException Error copying the configuration
   */
  public static void copyConfiguration(ICheckConfiguration source, ICheckConfiguration target)
      throws CheckstylePluginException {
    // use the export function ;-)
    File targetFile = FileUtils.toFile(target.getResolvedConfigurationFileURL());

    File sourceFile = FileUtils.toFile(source.getResolvedConfigurationFileURL());

    // copying from a file to the same file will destroy it.
    if (ObjectUtils.equals(targetFile, sourceFile)) {
      return;
    }

    exportConfiguration(targetFile, source);
  }
コード例 #4
0
  /**
   * Write check configurations to an external file in standard Checkstyle format.
   *
   * @param file File to write too.
   * @param config List of check configurations to write out.
   * @throws CheckstylePluginException Error during export.
   */
  public static void exportConfiguration(File file, ICheckConfiguration config)
      throws CheckstylePluginException {

    InputStream in = null;
    OutputStream out = null;

    try {

      // Just copy the checkstyle configuration
      in = config.getCheckstyleConfiguration().getCheckConfigFileStream();
      out = new BufferedOutputStream(new FileOutputStream(file));

      IOUtils.copy(in, out);
    } catch (Exception e) {
      CheckstylePluginException.rethrow(e);
    } finally {
      IOUtils.closeQuietly(in);
      IOUtils.closeQuietly(out);
    }
  }