com.puppycrawl.tools.checkstyle.PropertiesExpander Java Examples

The following examples show how to use com.puppycrawl.tools.checkstyle.PropertiesExpander. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NoHttpCheckITest.java    From nohttp with Apache License 2.0 5 votes vote down vote up
private Configuration loadConfiguration() throws Exception {
	Properties properties = this.parameter.getProperties();
	try (InputStream inputStream = this.parameter.getConfig()) {
		Configuration configuration = ConfigurationLoader.loadConfiguration(
				new InputSource(inputStream),
				new PropertiesExpander(properties),
				ConfigurationLoader.IgnoredModulesOptions.EXECUTE,
				ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
		return configuration;
	}
}
 
Example #2
Source File: SpringChecks.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
@Override
public void finishLocalSetup() {
	ModuleFactory moduleFactory = new FilteredModuleFactory(this.moduleFactory, this.excludes);
	DefaultContext context = new DefaultContext();
	context.add("classLoader", this.classLoader);
	context.add("severity", getSeverity());
	context.add("tabWidth", String.valueOf(getTabWidth()));
	context.add("moduleFactory", moduleFactory);
	Properties properties = new Properties();
	put(properties, "headerType", this.headerType);
	put(properties, "headerCopyrightPattern", this.headerCopyrightPattern);
	put(properties, "headerFile", this.headerFile);
	put(properties, "projectRootPackage", this.projectRootPackage);
	this.checks = new SpringConfigurationLoader(context, moduleFactory).load(new PropertiesExpander(properties));
}
 
Example #3
Source File: SpringChecksTests.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private Configuration loadConfiguration() throws Exception {
	try (InputStream inputStream = new FileInputStream(this.parameter.getConfigFile())) {
		Configuration configuration = ConfigurationLoader.loadConfiguration(new InputSource(inputStream),
				new PropertiesExpander(new Properties()), IgnoredModulesOptions.EXECUTE,
				ThreadModeSettings.SINGLE_THREAD_MODE_INSTANCE);
		return configuration;
	}
}
 
Example #4
Source File: SpringConfigurationLoaderTests.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private PropertyResolver getPropertyResolver() {
	Properties properties = new Properties();
	properties.put("headerType", SpringHeaderCheck.DEFAULT_HEADER_TYPE);
	properties.put("headerFile", "");
	properties.put("headerCopyrightPattern", SpringHeaderCheck.DEFAULT_HEADER_COPYRIGHT_PATTERN);
	properties.put("projectRootPackage", SpringImportOrderCheck.DEFAULT_PROJECT_ROOT_PACKAGE);
	return new PropertiesExpander(properties);
}
 
Example #5
Source File: CheckerService.java    From vscode-checkstyle with GNU Lesser General Public License v3.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void setConfiguration(Map<String, Object> config) throws IOException, CheckstyleException {
    final String configurationFsPath = (String) config.get("path");
    final Map<String, String> properties = (Map<String, String>) config.get("properties");
    final Properties checkstyleProperties = new Properties();
    checkstyleProperties.putAll(properties);
    checker.configure(ConfigurationLoader.loadConfiguration(
        configurationFsPath,
        new PropertiesExpander(checkstyleProperties)
    ));
}
 
Example #6
Source File: CheckstyleProcessor.java    From sputnik with Apache License 2.0 5 votes vote down vote up
@NotNull
private Checker createChecker(@NotNull AuditListener auditListener) {
    try {
        Checker checker = new Checker();
        ClassLoader moduleClassLoader = Checker.class.getClassLoader();
        String configurationFile = getConfigurationFilename();
        Properties properties = System.getProperties();// loadProperties(new File(System.getProperty(CHECKSTYLE_PROPERTIES_FILE)));
        checker.setModuleClassLoader(moduleClassLoader);
        checker.configure(ConfigurationLoader.loadConfiguration(configurationFile, new PropertiesExpander(properties)));
        checker.addListener(auditListener);
        return checker;
    } catch (CheckstyleException e) {
        throw new ReviewException("Unable to create Checkstyle checker", e);
    }
}
 
Example #7
Source File: CheckStyleExecutor.java    From repositoryminer with Apache License 2.0 5 votes vote down vote up
public Map<String, List<StyleProblem>> execute() throws CheckstyleException {
	Properties properties;
	if (propertiesFile == null) {
		properties = System.getProperties();
	} else {
		properties = loadProperties(new File(propertiesFile));
	}

	if (configFile == null) {
		configFile = "/google_checks.xml";
	}
	
	// create configurations
	Configuration config = ConfigurationLoader.loadConfiguration(configFile, new PropertiesExpander(properties));

	// create our custom audit listener
	RepositoryMinerAudit listener = new RepositoryMinerAudit();
	listener.setRepositoryPathLength(repository.length());

	ClassLoader moduleClassLoader = Checker.class.getClassLoader();
	RootModule rootModule = getRootModule(config.getName(), moduleClassLoader);

	rootModule.setModuleClassLoader(moduleClassLoader);
	rootModule.configure(config);
	rootModule.addListener(listener);

	// executes checkstyle
	rootModule.process((List<File>) FileUtils.listFiles(new File(repository), EXTENSION_FILE_FILTER, true));
	rootModule.destroy();

	return listener.getFileErrors();
}
 
Example #8
Source File: Main.java    From diff-check with GNU Lesser General Public License v2.1 4 votes vote down vote up
/**
 * Executes required Checkstyle actions based on passed parameters.
 * @param cliOptions
 *        pojo object that contains all options
 * @return number of violations of ERROR level
 * @throws IOException
 *         when output file could not be found
 * @throws CheckstyleException
 *         when properties file could not be loaded
 */
private static int runCheckstyle(CliOptions cliOptions)
        throws CheckstyleException, IOException {
    // setup the properties
    final Properties props;

    if (cliOptions.propertiesLocation == null) {
        props = System.getProperties();
    }
    else {
        props = loadProperties(new File(cliOptions.propertiesLocation));
    }

    // create a configuration
    final ThreadModeSettings multiThreadModeSettings =
            new ThreadModeSettings(
                    cliOptions.checkerThreadsNumber, cliOptions.treeWalkerThreadsNumber);

    final ConfigurationLoader.IgnoredModulesOptions ignoredModulesOptions;
    if (cliOptions.executeIgnoredModules) {
        ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.EXECUTE;
    }
    else {
        ignoredModulesOptions = ConfigurationLoader.IgnoredModulesOptions.OMIT;
    }

    final Configuration config = ConfigurationLoader.loadConfiguration(
            cliOptions.configLocation, new PropertiesExpander(props),
            ignoredModulesOptions, multiThreadModeSettings);

    // create RootModule object and run it
    final int errorCounter;
    final ClassLoader moduleClassLoader = Checker.class.getClassLoader();
    final RootModule rootModule = getRootModule(config.getName(), moduleClassLoader);

    try {
        final AuditListener listener;
        if (cliOptions.generateXpathSuppressionsFile) {
            // create filter to print generated xpath suppressions file
            final Configuration treeWalkerConfig = getTreeWalkerConfig(config);
            if (treeWalkerConfig != null) {
                final DefaultConfiguration moduleConfig =
                        new DefaultConfiguration(
                                XpathFileGeneratorAstFilter.class.getName());
                moduleConfig.addAttribute(OPTION_TAB_WIDTH_NAME,
                        Integer.toString(cliOptions.tabWidth));
                ((DefaultConfiguration) treeWalkerConfig).addChild(moduleConfig);
            }

            listener = new XpathFileGeneratorAuditListener(System.out,
                    AutomaticBean.OutputStreamOptions.NONE);
        }
        else {
            listener = createListener(cliOptions.format,
                    cliOptions.outputLocation);
        }

        rootModule.setModuleClassLoader(moduleClassLoader);
        rootModule.configure(config);
        rootModule.addListener(listener);

        if (CollectionUtils.isNotEmpty(DIFF_ENTRY_LIST) && rootModule instanceof Checker) {
            Checker checker = (Checker) rootModule;
            checker.addFilter(new DiffLineFilter(DIFF_ENTRY_LIST));
        }

        // run RootModule
        errorCounter = rootModule.process(cliOptions.files);
    }
    finally {
        rootModule.destroy();
    }

    return errorCounter;
}
 
Example #9
Source File: CheckstyleConfiguration.java    From sonar-checkstyle with GNU Lesser General Public License v3.0 4 votes vote down vote up
@VisibleForTesting
static Configuration toCheckstyleConfiguration(File xmlConfig) throws CheckstyleException {
    return ConfigurationLoader.loadConfiguration(xmlConfig.getAbsolutePath(),
            new PropertiesExpander(new Properties()));
}