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);
}
}
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;
}
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);
}
}
}
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;
}
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;
}
Aggregations