use of com.puppycrawl.tools.checkstyle.PropertyResolver in project eclipse-cs by checkstyle.
the class RemoteConfigurationType method getCheckstyleConfiguration.
/**
* {@inheritDoc}
*/
@Override
public CheckstyleConfigurationFile getCheckstyleConfiguration(ICheckConfiguration checkConfiguration) throws CheckstylePluginException {
boolean useCacheFile = Boolean.valueOf(checkConfiguration.getAdditionalData().get(KEY_CACHE_CONFIG)).booleanValue();
CheckstyleConfigurationFile data = new CheckstyleConfigurationFile();
synchronized (Authenticator.class) {
// $NON-NLS-1$
String currentRedirects = System.getProperty("http.maxRedirects");
Authenticator oldAuthenticator = RemoteConfigAuthenticator.getDefault();
try {
// resolve the true configuration file URL
data.setResolvedConfigFileURL(resolveLocation(checkConfiguration));
Authenticator.setDefault(new RemoteConfigAuthenticator(data.getResolvedConfigFileURL()));
boolean originalFileSuccess = false;
byte[] configurationFileData = null;
try {
// $NON-NLS-1$ //$NON-NLS-2$
System.setProperty("http.maxRedirects", "3");
URLConnection connection = data.getResolvedConfigFileURL().openConnection();
// get the configuration file data
configurationFileData = getBytesFromURLConnection(connection);
// get last modification timestamp
data.setModificationStamp(connection.getLastModified());
originalFileSuccess = true;
} catch (IOException e) {
if (useCacheFile) {
configurationFileData = getBytesFromCacheFile(checkConfiguration);
} else {
throw e;
}
}
data.setCheckConfigFileBytes(configurationFileData);
// get the properties bundle
byte[] additionalPropertiesBytes = null;
if (originalFileSuccess) {
additionalPropertiesBytes = getAdditionPropertiesBundleBytes(data.getResolvedConfigFileURL());
} else if (useCacheFile) {
additionalPropertiesBytes = getBytesFromCacheBundleFile(checkConfiguration);
}
data.setAdditionalPropertyBundleBytes(additionalPropertiesBytes);
// get the property resolver
PropertyResolver resolver = getPropertyResolver(checkConfiguration, data);
data.setPropertyResolver(resolver);
// write to cache file
if (originalFileSuccess && useCacheFile) {
writeToCacheFile(checkConfiguration, configurationFileData, additionalPropertiesBytes);
}
} catch (UnknownHostException e) {
CheckstylePluginException.rethrow(e, NLS.bind(Messages.RemoteConfigurationType_errorUnknownHost, e.getMessage()));
} catch (FileNotFoundException e) {
CheckstylePluginException.rethrow(e, NLS.bind(Messages.RemoteConfigurationType_errorFileNotFound, e.getMessage()));
} catch (IOException | URISyntaxException e) {
CheckstylePluginException.rethrow(e);
} finally {
Authenticator.setDefault(oldAuthenticator);
if (currentRedirects != null) {
// $NON-NLS-1$
System.setProperty("http.maxRedirects", currentRedirects);
} else {
// $NON-NLS-1$
System.getProperties().remove("http.maxRedirects");
}
}
}
return data;
}
use of com.puppycrawl.tools.checkstyle.PropertyResolver 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();
}
use of com.puppycrawl.tools.checkstyle.PropertyResolver in project eclipse-cs by checkstyle.
the class ConfigurationType method getCheckstyleConfiguration.
@Override
public CheckstyleConfigurationFile getCheckstyleConfiguration(ICheckConfiguration checkConfiguration) throws CheckstylePluginException {
CheckstyleConfigurationFile data = new CheckstyleConfigurationFile();
try {
// resolve the true configuration file URL
data.setResolvedConfigFileURL(resolveLocation(checkConfiguration));
URLConnection connection = data.getResolvedConfigFileURL().openConnection();
connection.connect();
// get last modification timestamp
data.setModificationStamp(connection.getLastModified());
// get the configuration file data
byte[] configurationFileData = getBytesFromURLConnection(connection);
data.setCheckConfigFileBytes(configurationFileData);
// get the properties bundle
byte[] additionalPropertiesBytes = getAdditionPropertiesBundleBytes(data.getResolvedConfigFileURL());
data.setAdditionalPropertyBundleBytes(additionalPropertiesBytes);
// get the property resolver
PropertyResolver resolver = getPropertyResolver(checkConfiguration, data);
data.setPropertyResolver(resolver);
} catch (IOException | URISyntaxException e) {
CheckstylePluginException.rethrow(e);
}
return data;
}
use of com.puppycrawl.tools.checkstyle.PropertyResolver 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;
}
use of com.puppycrawl.tools.checkstyle.PropertyResolver 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;
}
Aggregations