Search in sources :

Example 31 with CheckstylePluginException

use of net.sf.eclipsecs.core.util.CheckstylePluginException 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)

Example 32 with CheckstylePluginException

use of net.sf.eclipsecs.core.util.CheckstylePluginException in project eclipse-cs by checkstyle.

the class ProjectConfigurationWorkingCopy method storeToPersistence.

/**
 * Store the audit configurations to the persistent state storage.
 */
private void storeToPersistence(ProjectConfigurationWorkingCopy config) throws CheckstylePluginException {
    try {
        Document docu = writeProjectConfig(config);
        byte[] data = XMLUtil.toByteArray(docu);
        InputStream pipeIn = new ByteArrayInputStream(data);
        // create or overwrite the .checkstyle file
        IProject project = config.getProject();
        IFile file = project.getFile(ProjectConfigurationFactory.PROJECT_CONFIGURATION_FILE);
        if (!file.exists()) {
            file.create(pipeIn, true, null);
            file.refreshLocal(IResource.DEPTH_INFINITE, null);
        } else {
            if (file.isReadOnly()) {
                ResourceAttributes attrs = ResourceAttributes.fromFile(file.getFullPath().toFile());
                attrs.setReadOnly(true);
                file.setResourceAttributes(attrs);
            }
            file.setContents(pipeIn, true, true, null);
        }
        config.getLocalCheckConfigWorkingSet().store();
    } catch (Exception e) {
        CheckstylePluginException.rethrow(e, NLS.bind(Messages.errorWritingCheckConfigurations, e.getLocalizedMessage()));
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(org.dom4j.Document) IProject(org.eclipse.core.resources.IProject) ResourceAttributes(org.eclipse.core.resources.ResourceAttributes) CheckstylePluginException(net.sf.eclipsecs.core.util.CheckstylePluginException)

Example 33 with CheckstylePluginException

use of net.sf.eclipsecs.core.util.CheckstylePluginException in project eclipse-cs by checkstyle.

the class ProjectConfigurationWorkingCopy method writeFileSet.

/**
 * Produces the sax events to write a file set to xml.
 *
 * @param fileSet
 *          the file set
 * @param project
 *          the project
 * @param docRoot
 *          the root element of the project configuration
 */
private void writeFileSet(FileSet fileSet, IProject project, Element docRoot) throws CheckstylePluginException {
    if (fileSet.getCheckConfig() == null) {
        throw new CheckstylePluginException(NLS.bind(Messages.errorFilesetWithoutCheckConfig, fileSet.getName(), project.getName()));
    }
    Element fileSetEl = docRoot.addElement(XMLTags.FILESET_TAG);
    fileSetEl.addAttribute(XMLTags.NAME_TAG, fileSet.getName());
    fileSetEl.addAttribute(XMLTags.ENABLED_TAG, Boolean.toString(fileSet.isEnabled()));
    ICheckConfiguration checkConfig = fileSet.getCheckConfig();
    if (checkConfig != null) {
        fileSetEl.addAttribute(XMLTags.CHECK_CONFIG_NAME_TAG, checkConfig.getName());
        fileSetEl.addAttribute(XMLTags.LOCAL_TAG, Boolean.toString(!checkConfig.isGlobal()));
    }
    // write patterns
    for (FileMatchPattern pattern : fileSet.getFileMatchPatterns()) {
        Element patternEl = fileSetEl.addElement(XMLTags.FILE_MATCH_PATTERN_TAG);
        patternEl.addAttribute(XMLTags.MATCH_PATTERN_TAG, pattern.getMatchPattern() != null ? pattern.getMatchPattern() : "");
        patternEl.addAttribute(XMLTags.INCLUDE_PATTERN_TAG, Boolean.toString(pattern.isIncludePattern()));
    }
}
Also used : ICheckConfiguration(net.sf.eclipsecs.core.config.ICheckConfiguration) Element(org.dom4j.Element) CheckstylePluginException(net.sf.eclipsecs.core.util.CheckstylePluginException)

Example 34 with CheckstylePluginException

use of net.sf.eclipsecs.core.util.CheckstylePluginException in project eclipse-cs by checkstyle.

the class ProjectConfigurationType method isConfigurable.

@Override
public boolean isConfigurable(ICheckConfiguration checkConfiguration) {
    boolean isConfigurable = true;
    boolean isProtected = Boolean.valueOf(checkConfiguration.getAdditionalData().get(KEY_PROTECT_CONFIG)).booleanValue();
    isConfigurable = !isProtected;
    if (!isProtected) {
        // file can is writable
        try {
            File file = URIUtil.toFile(checkConfiguration.getResolvedConfigurationFileURL().toURI());
            isConfigurable = file != null && file.canWrite();
        } catch (CheckstylePluginException | URISyntaxException e) {
            CheckstyleLog.log(e);
            isConfigurable = false;
        }
    }
    return isConfigurable;
}
Also used : CheckstylePluginException(net.sf.eclipsecs.core.util.CheckstylePluginException) URISyntaxException(java.net.URISyntaxException) File(java.io.File)

Aggregations

CheckstylePluginException (net.sf.eclipsecs.core.util.CheckstylePluginException)34 ICheckConfiguration (net.sf.eclipsecs.core.config.ICheckConfiguration)7 IProject (org.eclipse.core.resources.IProject)7 CoreException (org.eclipse.core.runtime.CoreException)7 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)6 IProjectConfiguration (net.sf.eclipsecs.core.projectconfig.IProjectConfiguration)5 GridData (org.eclipse.swt.layout.GridData)5 Composite (org.eclipse.swt.widgets.Composite)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 FileSet (net.sf.eclipsecs.core.projectconfig.FileSet)4 IFilter (net.sf.eclipsecs.core.projectconfig.filters.IFilter)4 Document (org.dom4j.Document)4 IStatus (org.eclipse.core.runtime.IStatus)4 Status (org.eclipse.core.runtime.Status)4 GridLayout (org.eclipse.swt.layout.GridLayout)4 Button (org.eclipse.swt.widgets.Button)4 File (java.io.File)3 IOException (java.io.IOException)3 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)2