Search in sources :

Example 1 with PropertyResolver

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;
}
Also used : UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver) HttpURLConnection(java.net.HttpURLConnection) URLConnection(java.net.URLConnection) CheckstyleConfigurationFile(net.sf.eclipsecs.core.config.CheckstyleConfigurationFile) Authenticator(java.net.Authenticator)

Example 2 with PropertyResolver

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();
}
Also used : InputSource(org.xml.sax.InputSource) CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver) MultiPropertyResolver(net.sf.eclipsecs.core.config.configtypes.MultiPropertyResolver) MultiPropertyResolver(net.sf.eclipsecs.core.config.configtypes.MultiPropertyResolver) IContextAware(net.sf.eclipsecs.core.config.configtypes.IContextAware)

Example 3 with PropertyResolver

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;
}
Also used : CheckstyleConfigurationFile(net.sf.eclipsecs.core.config.CheckstyleConfigurationFile) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver) URLConnection(java.net.URLConnection)

Example 4 with PropertyResolver

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;
}
Also used : Checker(com.puppycrawl.tools.checkstyle.Checker) InputSource(org.xml.sax.InputSource) CheckstyleConfigurationFile(net.sf.eclipsecs.core.config.CheckstyleConfigurationFile) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver) IContextAware(net.sf.eclipsecs.core.config.configtypes.IContextAware)

Example 5 with PropertyResolver

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;
}
Also used : CheckstyleException(com.puppycrawl.tools.checkstyle.api.CheckstyleException) PropertyResolver(com.puppycrawl.tools.checkstyle.PropertyResolver)

Aggregations

PropertyResolver (com.puppycrawl.tools.checkstyle.PropertyResolver)6 CheckstyleConfigurationFile (net.sf.eclipsecs.core.config.CheckstyleConfigurationFile)4 CheckstyleException (com.puppycrawl.tools.checkstyle.api.CheckstyleException)3 IContextAware (net.sf.eclipsecs.core.config.configtypes.IContextAware)3 InputSource (org.xml.sax.InputSource)3 IOException (java.io.IOException)2 URISyntaxException (java.net.URISyntaxException)2 URLConnection (java.net.URLConnection)2 Checker (com.puppycrawl.tools.checkstyle.Checker)1 Configuration (com.puppycrawl.tools.checkstyle.api.Configuration)1 FileNotFoundException (java.io.FileNotFoundException)1 Authenticator (java.net.Authenticator)1 HttpURLConnection (java.net.HttpURLConnection)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 ICheckConfiguration (net.sf.eclipsecs.core.config.ICheckConfiguration)1 MultiPropertyResolver (net.sf.eclipsecs.core.config.configtypes.MultiPropertyResolver)1 FileSet (net.sf.eclipsecs.core.projectconfig.FileSet)1 IProjectConfiguration (net.sf.eclipsecs.core.projectconfig.IProjectConfiguration)1 CheckstyleTransformer (net.sf.eclipsecs.core.transformer.CheckstyleTransformer)1