Search in sources :

Example 6 with IProjectConfiguration

use of net.sf.eclipsecs.core.projectconfig.IProjectConfiguration in project eclipse-cs by checkstyle.

the class CheckFileOnOpenPartListener method isFileAffected.

/**
 * Checks if the given file is affected by the UnOpenedFilesFilter and needs to be handled on
 * editor open/close.
 *
 * @param file
 *          the file to check
 * @return <code>true</code> if the file is affected, <code>false</code> otherwise
 */
private boolean isFileAffected(IFile file) {
    boolean affected = false;
    IProject project = file.getProject();
    try {
        // check if checkstyle is enabled on the project
        if (project.isAccessible() && project.hasNature(CheckstyleNature.NATURE_ID)) {
            IProjectConfiguration config = ProjectConfigurationFactory.getConfiguration(project);
            // now check if the UnOpenedFilesFilter is active
            boolean unOpenedFilesFilterActive = false;
            boolean filtered = false;
            List<IFilter> filters = config.getFilters();
            for (IFilter filter : filters) {
                if (filter instanceof UnOpenedFilesFilter && ((UnOpenedFilesFilter) filter).isEnabled()) {
                    unOpenedFilesFilterActive = true;
                }
                // check if the file would be filtered out
                if (filter.isEnabled() && !(filter instanceof UnOpenedFilesFilter)) {
                    filtered = filtered || !filter.accept(file);
                }
            }
            affected = unOpenedFilesFilterActive && !filtered;
        }
    } catch (CoreException e) {
        // should never happen, since editor cannot be open
        // when project isn't
        CheckstyleLog.log(e);
    } catch (CheckstylePluginException e) {
        CheckstyleLog.log(e);
    }
    return affected;
}
Also used : CoreException(org.eclipse.core.runtime.CoreException) IFilter(net.sf.eclipsecs.core.projectconfig.filters.IFilter) IProjectConfiguration(net.sf.eclipsecs.core.projectconfig.IProjectConfiguration) UnOpenedFilesFilter(net.sf.eclipsecs.core.projectconfig.filters.UnOpenedFilesFilter) CheckstylePluginException(net.sf.eclipsecs.core.util.CheckstylePluginException) IProject(org.eclipse.core.resources.IProject)

Example 7 with IProjectConfiguration

use of net.sf.eclipsecs.core.projectconfig.IProjectConfiguration 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

IProjectConfiguration (net.sf.eclipsecs.core.projectconfig.IProjectConfiguration)7 CoreException (org.eclipse.core.runtime.CoreException)6 CheckstylePluginException (net.sf.eclipsecs.core.util.CheckstylePluginException)5 IProject (org.eclipse.core.resources.IProject)5 ProjectConfigurationWorkingCopy (net.sf.eclipsecs.core.projectconfig.ProjectConfigurationWorkingCopy)3 IStatus (org.eclipse.core.runtime.IStatus)3 Status (org.eclipse.core.runtime.Status)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 FileSet (net.sf.eclipsecs.core.projectconfig.FileSet)2 IFilter (net.sf.eclipsecs.core.projectconfig.filters.IFilter)2 IResource (org.eclipse.core.resources.IResource)2 PropertyResolver (com.puppycrawl.tools.checkstyle.PropertyResolver)1 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)1 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 List (java.util.List)1 Map (java.util.Map)1 CheckstyleBuilder (net.sf.eclipsecs.core.builder.CheckstyleBuilder)1 CheckstyleConfigurationFile (net.sf.eclipsecs.core.config.CheckstyleConfigurationFile)1 ICheckConfiguration (net.sf.eclipsecs.core.config.ICheckConfiguration)1