Search in sources :

Example 1 with IContextAware

use of net.sf.eclipsecs.core.config.configtypes.IContextAware in project eclipse-cs by checkstyle.

the class CheckConfigurationTester method getUnresolvedProperties.

/**
 * Tests a configuration if there are unresolved properties.
 *
 * @return the list of unresolved properties as ResolvableProperty values.
 * @throws CheckstylePluginException
 *           most likely the configuration file could not be found
 */
public List<ResolvableProperty> getUnresolvedProperties() throws CheckstylePluginException {
    CheckstyleConfigurationFile configFile = mCheckConfiguration.getCheckstyleConfiguration();
    PropertyResolver resolver = configFile.getPropertyResolver();
    // context
    if (mContextProject != null && resolver instanceof IContextAware) {
        ((IContextAware) resolver).setProjectContext(mContextProject);
    }
    MissingPropertyCollector collector = new MissingPropertyCollector();
    if (resolver instanceof MultiPropertyResolver) {
        ((MultiPropertyResolver) resolver).addPropertyResolver(collector);
    } else {
        MultiPropertyResolver multiResolver = new MultiPropertyResolver();
        multiResolver.addPropertyResolver(resolver);
        multiResolver.addPropertyResolver(collector);
        resolver = multiResolver;
    }
    InputSource in = null;
    try {
        in = configFile.getCheckConfigFileInputSource();
        ConfigurationLoader.loadConfiguration(in, resolver, false);
    } catch (CheckstyleException e) {
        CheckstylePluginException.rethrow(e);
    } finally {
        Closeables.closeQuietly(in.getByteStream());
    }
    return collector.getUnresolvedProperties();
}
Also used : InputSource(org.xml.sax.InputSource) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver) MultiPropertyResolver(net.sf.eclipsecs.core.config.configtypes.MultiPropertyResolver) MultiPropertyResolver(net.sf.eclipsecs.core.config.configtypes.MultiPropertyResolver) IContextAware(net.sf.eclipsecs.core.config.configtypes.IContextAware)

Example 2 with IContextAware

use of net.sf.eclipsecs.core.config.configtypes.IContextAware in project eclipse-cs by checkstyle.

the class CheckerFactory method createChecker.

/**
 * Creates a checker for a given configuration file.
 *
 * @param config
 *          the check configuration data
 * @param project
 *          the project to create the checker for
 * @return the checker for the given configuration file
 * @throws CheckstyleException
 *           the configuration file had errors
 * @throws CheckstylePluginException
 *           the configuration could not be read
 */
public static Checker createChecker(ICheckConfiguration config, IProject project) throws CheckstyleException, CheckstylePluginException {
    String cacheKey = getCacheKey(config, project);
    CheckstyleConfigurationFile configFileData = config.getCheckstyleConfiguration();
    Checker checker = tryCheckerCache(cacheKey, configFileData.getModificationStamp());
    // clear Checkstyle internal caches upon checker reuse
    if (checker != null) {
        checker.clearCache();
    }
    // no cache hit
    if (checker == null) {
        PropertyResolver resolver = configFileData.getPropertyResolver();
        // context
        if (resolver instanceof IContextAware) {
            ((IContextAware) resolver).setProjectContext(project);
        }
        InputSource in = null;
        try {
            in = configFileData.getCheckConfigFileInputSource();
            checker = createCheckerInternal(in, resolver, project);
        } finally {
            Closeables.closeQuietly(in.getByteStream());
        }
        // store checker in cache
        Long modified = new Long(configFileData.getModificationStamp());
        sCheckerMap.put(cacheKey, checker);
        sModifiedMap.put(cacheKey, modified);
    }
    return checker;
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) InputSource(org.xml.sax.InputSource) CheckstyleConfigurationFile(net.sf.eclipsecs.core.config.CheckstyleConfigurationFile) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver) IContextAware(net.sf.eclipsecs.core.config.configtypes.IContextAware)

Example 3 with IContextAware

use of net.sf.eclipsecs.core.config.configtypes.IContextAware in project eclipse-cs by checkstyle.

the class TransformCheckstyleRulesJob method runInWorkspace.

/**
 * {@inheritDoc}
 */
@Override
public IStatus runInWorkspace(final IProgressMonitor arg0) throws CoreException {
    try {
        final IProjectConfiguration conf = ProjectConfigurationFactory.getConfiguration(mProject);
        final List<Configuration> rules = new ArrayList<Configuration>();
        // collect rules from all configured filesets
        for (FileSet fs : conf.getFileSets()) {
            ICheckConfiguration checkConfig = fs.getCheckConfig();
            CheckstyleConfigurationFile configFile = checkConfig.getCheckstyleConfiguration();
            PropertyResolver resolver = configFile.getPropertyResolver();
            // context
            if (resolver instanceof IContextAware) {
                ((IContextAware) resolver).setProjectContext(mProject);
            }
            InputSource in = null;
            try {
                in = configFile.getCheckConfigFileInputSource();
                Configuration configuration = ConfigurationLoader.loadConfiguration(in, resolver, true);
                // flatten the nested configuration tree into a list
                recurseConfiguration(configuration, rules);
            } finally {
                Closeables.closeQuietly(in.getByteStream());
            }
        }
        if (rules.isEmpty()) {
            return Status.CANCEL_STATUS;
        }
        final CheckstyleTransformer transformer = new CheckstyleTransformer(mProject, rules);
        transformer.transformRules();
    } catch (CheckstyleException e) {
        Status status = new Status(IStatus.ERROR, CheckstylePlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e);
        throw new CoreException(status);
    } catch (CheckstylePluginException e) {
        Status status = new Status(IStatus.ERROR, CheckstylePlugin.PLUGIN_ID, IStatus.ERROR, e.getMessage(), e);
        throw new CoreException(status);
    }
    return Status.OK_STATUS;
}
Also used : Status(org.eclipse.core.runtime.Status) IStatus(org.eclipse.core.runtime.IStatus) InputSource(org.xml.sax.InputSource) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) IProjectConfiguration(net.sf.eclipsecs.core.projectconfig.IProjectConfiguration) ICheckConfiguration(net.sf.eclipsecs.core.config.ICheckConfiguration) FileSet(net.sf.eclipsecs.core.projectconfig.FileSet) ICheckConfiguration(net.sf.eclipsecs.core.config.ICheckConfiguration) IProjectConfiguration(net.sf.eclipsecs.core.projectconfig.IProjectConfiguration) ArrayList(java.util.ArrayList) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver) IContextAware(net.sf.eclipsecs.core.config.configtypes.IContextAware) CoreException(org.eclipse.core.runtime.CoreException) CheckstyleConfigurationFile(net.sf.eclipsecs.core.config.CheckstyleConfigurationFile) CheckstyleTransformer(net.sf.eclipsecs.core.transformer.CheckstyleTransformer) CheckstylePluginException(net.sf.eclipsecs.core.util.CheckstylePluginException)

Aggregations

PropertyResolver (com.puppycrawl.tools.checkstyle.PropertyResolver)3 IContextAware (net.sf.eclipsecs.core.config.configtypes.IContextAware)3 InputSource (org.xml.sax.InputSource)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)2 CheckstyleConfigurationFile (net.sf.eclipsecs.core.config.CheckstyleConfigurationFile)2 Checker (com.puppycrawl.tools.checkstyle.Checker)1 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 ArrayList (java.util.ArrayList)1 ICheckConfiguration (net.sf.eclipsecs.core.config.ICheckConfiguration)1 MultiPropertyResolver (net.sf.eclipsecs.core.config.configtypes.MultiPropertyResolver)1 FileSet (net.sf.eclipsecs.core.projectconfig.FileSet)1 IProjectConfiguration (net.sf.eclipsecs.core.projectconfig.IProjectConfiguration)1 CheckstyleTransformer (net.sf.eclipsecs.core.transformer.CheckstyleTransformer)1 CheckstylePluginException (net.sf.eclipsecs.core.util.CheckstylePluginException)1 CoreException (org.eclipse.core.runtime.CoreException)1 IStatus (org.eclipse.core.runtime.IStatus)1 Status (org.eclipse.core.runtime.Status)1