Search in sources :

Example 86 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class ConfigurationLoader method loadConfiguration.

/**
     * Returns the module configurations from a specified input source.
     * Note that if the source does wrap an open byte or character
     * stream, clients are required to close that stream by themselves
     *
     * @param configSource the input stream to the Checkstyle configuration
     * @param overridePropsResolver overriding properties
     * @param omitIgnoredModules {@code true} if modules with severity
     *            'ignore' should be omitted, {@code false} otherwise
     * @return the check configurations
     * @throws CheckstyleException if an error occurs
     */
public static Configuration loadConfiguration(InputSource configSource, PropertyResolver overridePropsResolver, boolean omitIgnoredModules) throws CheckstyleException {
    try {
        final ConfigurationLoader loader = new ConfigurationLoader(overridePropsResolver, omitIgnoredModules);
        loader.parseInputSource(configSource);
        return loader.configuration;
    } catch (final SAXParseException ex) {
        final String message = String.format(Locale.ROOT, "%s - %s:%s:%s", UNABLE_TO_PARSE_EXCEPTION_PREFIX, ex.getMessage(), ex.getLineNumber(), ex.getColumnNumber());
        throw new CheckstyleException(message, ex);
    } catch (final ParserConfigurationException | IOException | SAXException ex) {
        throw new CheckstyleException(UNABLE_TO_PARSE_EXCEPTION_PREFIX, ex);
    }
}
Also used : SAXParseException(org.xml.sax.SAXParseException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 87 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project checkstyle by checkstyle.

the class BaseCheckTestSupport method getCheckConfig.

/**
     * Returns {@link Configuration} instance for the given check name.
     * This implementation uses {@link BaseCheckTestSupport#getConfiguration()} method inside.
     * @param checkName check name.
     * @return {@link Configuration} instance for the given check name.
     * @throws CheckstyleException if exception occurs during configuration loading.
     */
protected static Configuration getCheckConfig(String checkName, String checkId) throws CheckstyleException {
    final Configuration result;
    final List<Configuration> configs = getCheckConfigs(checkName);
    if (configs.size() == 1) {
        result = configs.get(0);
    } else {
        result = configs.stream().filter(conf -> {
            try {
                return conf.getAttribute("id").equals(checkId);
            } catch (CheckstyleException ex) {
                throw new IllegalStateException("problem to get ID attribute from " + conf, ex);
            }
        }).findFirst().orElseGet(null);
    }
    return result;
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) CommonUtils(com.puppycrawl.tools.checkstyle.utils.CommonUtils) MessageFormat(java.text.MessageFormat) ArrayList(java.util.ArrayList) AbstractViolationReporter(com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter) ByteArrayInputStream(java.io.ByteArrayInputStream) BriefUtLogger(com.puppycrawl.tools.checkstyle.BriefUtLogger) Locale(java.util.Locale) Map(java.util.Map) ConfigurationLoader(com.puppycrawl.tools.checkstyle.ConfigurationLoader) Properties(java.util.Properties) PropertiesExpander(com.puppycrawl.tools.checkstyle.PropertiesExpander) Assert.assertTrue(org.junit.Assert.assertTrue) IOException(java.io.IOException) LineNumberReader(java.io.LineNumberReader) FileInputStream(java.io.FileInputStream) InputStreamReader(java.io.InputStreamReader) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) List(java.util.List) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) Checker(com.puppycrawl.tools.checkstyle.Checker) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) TreeWalker(com.puppycrawl.tools.checkstyle.TreeWalker) BufferedReader(java.io.BufferedReader) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) Configuration(com.puppycrawl.tools.checkstyle.api.Configuration) DefaultConfiguration(com.puppycrawl.tools.checkstyle.DefaultConfiguration) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException)

Example 88 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project eclipse-cs by checkstyle.

the class Auditor method runAudit.

/**
 * Runs the audit on the files associated with the auditor.
 *
 * @param project
 *          the project is needed to build the correct classpath for the checker
 * @param monitor
 *          the progress monitor
 * @throws CheckstylePluginException
 *           error processing the audit
 */
public void runAudit(IProject project, IProgressMonitor monitor) throws CheckstylePluginException {
    // skip if there are no files to check
    if (mFiles.isEmpty() || project == null) {
        return;
    }
    mMonitor = monitor;
    Checker checker = null;
    CheckstyleAuditListener listener = null;
    try {
        List<File> filesToAudit = getFilesList();
        // begin task
        monitor.beginTask(NLS.bind(Messages.Auditor_msgCheckingConfig, mCheckConfiguration.getName()), filesToAudit.size());
        // create checker
        checker = CheckerFactory.createChecker(mCheckConfiguration, project);
        // get the additional data
        ConfigurationReader.AdditionalConfigData additionalData = CheckerFactory.getAdditionalData(mCheckConfiguration, project);
        // create and add listener
        listener = new CheckstyleAuditListener(project, additionalData);
        checker.addListener(listener);
        // project
        if (project.hasNature(JavaCore.NATURE_ID)) {
            CheckerFactory.getSharedClassLoader().intializeWithProject(project);
        }
        // run the files through the checker
        checker.process(filesToAudit);
    } catch (CheckstyleException e) {
        if (e.getCause() instanceof OperationCanceledException) {
        // user requested cancellation, keep silent
        } else {
            handleCheckstyleFailure(project, e);
        }
    } catch (CoreException e) {
        CheckstylePluginException.rethrow(e);
    } catch (RuntimeException e) {
        if (listener != null) {
            listener.cleanup();
        }
        throw e;
    } finally {
        monitor.done();
        // Cleanup listener and filter
        if (checker != null) {
            checker.removeListener(listener);
        }
    }
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) CoreException(org.eclipse.core.runtime.CoreException) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) IFile(org.eclipse.core.resources.IFile) File(java.io.File) ConfigurationReader(net.sf.eclipsecs.core.config.ConfigurationReader)

Example 89 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException in project eclipse-cs by checkstyle.

the class MultiPropertyResolver method resolve.

@Override
public String resolve(String property) {
    String value = null;
    for (int i = 0, size = mChildResolver.size(); i < size; i++) {
        PropertyResolver aChildResolver = mChildResolver.get(i);
        value = aChildResolver.resolve(property);
        if (value != null) {
            break;
        }
    }
    try {
        // property chaining - might recurse internally
        while (PropertyUtil.hasUnresolvedProperties(value)) {
            value = PropertyUtil.replaceProperties(value, this);
        }
    } catch (CheckstyleException e) {
        throw new RuntimeException(e);
    }
    return value;
}
Also used : CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver)

Example 90 with CheckstyleException

use of com.puppycrawl.tools.checkstyle.api.CheckstyleException 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

CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)146 Test (org.junit.jupiter.api.Test)58 DefaultConfiguration (com.puppycrawl.tools.checkstyle.DefaultConfiguration)56 File (java.io.File)32 IOException (java.io.IOException)27 Test (org.junit.Test)27 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)18 Properties (java.util.Properties)15 ArrayList (java.util.ArrayList)14 URL (java.net.URL)11 Violation (com.puppycrawl.tools.checkstyle.api.Violation)10 FileText (com.puppycrawl.tools.checkstyle.api.FileText)8 PropertiesExpander (com.puppycrawl.tools.checkstyle.PropertiesExpander)7 InputStream (java.io.InputStream)7 SAXException (org.xml.sax.SAXException)7 FileContents (com.puppycrawl.tools.checkstyle.api.FileContents)6 URI (java.net.URI)6 Set (java.util.Set)6 Checker (com.puppycrawl.tools.checkstyle.Checker)5 MalformedURLException (java.net.MalformedURLException)5