com.puppycrawl.tools.checkstyle.PropertyResolver Java Examples

The following examples show how to use com.puppycrawl.tools.checkstyle.PropertyResolver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConfigurationType.java    From eclipse-cs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Gets the property resolver for this configuration type used to expand property values within
 * the checkstyle configuration.
 *
 * @param checkConfiguration
 *          the actual check configuration
 * @return the property resolver
 * @throws IOException
 *           error creating the property resolver
 * @throws URISyntaxException
 *           if configuration file URL cannot be resolved
 */
protected PropertyResolver getPropertyResolver(ICheckConfiguration config,
        CheckstyleConfigurationFile configFile) throws IOException, URISyntaxException {

  MultiPropertyResolver multiResolver = new MultiPropertyResolver();
  multiResolver.addPropertyResolver(new ResolvablePropertyResolver(config));

  File f = URIUtil.toFile(configFile.getResolvedConfigFileURL().toURI());
  if (f != null) {
    multiResolver.addPropertyResolver(new StandardPropertyResolver(f.toString()));
  } else {
    multiResolver.addPropertyResolver(
            new StandardPropertyResolver(configFile.getResolvedConfigFileURL().toString()));
  }

  multiResolver.addPropertyResolver(new ClasspathVariableResolver());
  multiResolver.addPropertyResolver(new SystemPropertyResolver());

  if (configFile.getAdditionalPropertiesBundleStream() != null) {
    ResourceBundle bundle = new PropertyResourceBundle(
            configFile.getAdditionalPropertiesBundleStream());
    multiResolver.addPropertyResolver(new ResourceBundlePropertyResolver(bundle));
  }

  return multiResolver;
}
 
Example #2
Source File: SpringConfigurationLoader.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private Configuration loadConfiguration(InputStream inputStream, PropertyResolver propertyResolver) {
	try {
		InputSource inputSource = new InputSource(inputStream);
		return ConfigurationLoader.loadConfiguration(inputSource, propertyResolver, IgnoredModulesOptions.EXECUTE);
	}
	catch (CheckstyleException ex) {
		throw new IllegalStateException(ex);
	}
}
 
Example #3
Source File: SpringConfigurationLoaderTests.java    From spring-javaformat with Apache License 2.0 5 votes vote down vote up
private PropertyResolver getPropertyResolver() {
	Properties properties = new Properties();
	properties.put("headerType", SpringHeaderCheck.DEFAULT_HEADER_TYPE);
	properties.put("headerFile", "");
	properties.put("headerCopyrightPattern", SpringHeaderCheck.DEFAULT_HEADER_COPYRIGHT_PATTERN);
	properties.put("projectRootPackage", SpringImportOrderCheck.DEFAULT_PROJECT_ROOT_PACKAGE);
	return new PropertiesExpander(properties);
}
 
Example #4
Source File: CheckConfigurationTester.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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();

  // set the project context if the property resolver needs the
  // 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, IgnoredModulesOptions.EXECUTE);
  } catch (CheckstyleException e) {
    CheckstylePluginException.rethrow(e);
  } finally {
    Closeables.closeQuietly(in.getByteStream());
  }

  return collector.getUnresolvedProperties();
}
 
Example #5
Source File: MultiPropertyResolver.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void setProjectContext(IProject project) {

  // propagate context to the childs
  for (int i = 0, size = mChildResolver.size(); i < size; i++) {
    PropertyResolver aChildResolver = mChildResolver.get(i);
    if (aChildResolver instanceof IContextAware) {
      ((IContextAware) aChildResolver).setProjectContext(project);
    }
  }
}
 
Example #6
Source File: MultiPropertyResolver.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
}
 
Example #7
Source File: BuiltInConfigurationType.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected PropertyResolver getPropertyResolver(ICheckConfiguration config,
        CheckstyleConfigurationFile configFile) {
  MultiPropertyResolver resolver = new MultiPropertyResolver();
  resolver.addPropertyResolver(new ResolvablePropertyResolver(config));
  resolver.addPropertyResolver(
          new BuiltInFilePropertyResolver(resolveLocation(config).toString()));

  return resolver;
}
 
Example #8
Source File: ConfigurationType.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@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;
}
 
Example #9
Source File: PropertyUtil.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Replaces <code>${xxx}</code> style constructions in the given value with the string value of
 * the corresponding data types. The method is package visible to facilitate testing.
 *
 * @param aValue
 *          The string to be scanned for property references. May be <code>null</code>, in which
 *          case this method returns immediately with no effect.
 * @param aProps
 *          Mapping (String to String) of property names to their values. Must not be
 *          <code>null</code>.
 * @return the original string with the properties replaced, or <code>null</code> if the original
 *         string is <code>null</code>. Code copied from ant -
 *         http://cvs.apache.org/viewcvs/jakarta-ant/src/main/org/apache/tools/ant/ProjectHelper.java
 * @throws CheckstyleException
 *           if the string contains an opening <code>${</code> without a closing <code>}</code>
 */
public static String replaceProperties(String aValue, PropertyResolver aProps)
        throws CheckstyleException {
  if (aValue == null) {
    return null;
  }

  final List<String> fragments = new ArrayList<>();
  final List<String> propertyRefs = new ArrayList<>();
  parsePropertyString(aValue, fragments, propertyRefs);

  final StringBuffer sb = new StringBuffer();
  final Iterator<String> i = fragments.iterator();
  final Iterator<String> j = propertyRefs.iterator();
  while (i.hasNext()) {
    String fragment = i.next();
    if (fragment == null) {
      final String propertyName = j.next();
      fragment = aProps.resolve(propertyName);
      if (fragment == null) {
        throw new CheckstyleException("Property ${" + propertyName //$NON-NLS-1$
                + "} has not been set"); //$NON-NLS-1$
      }
    }
    sb.append(fragment);
  }

  return sb.toString();
}
 
Example #10
Source File: CheckerFactory.java    From eclipse-cs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 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();

    // set the project context if the property resolver needs the
    // 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 = Long.valueOf(configFileData.getModificationStamp());
    sCheckerMap.put(cacheKey, checker);
    sModifiedMap.put(cacheKey, modified);
  }

  return checker;
}
 
Example #11
Source File: SpringConfigurationLoader.java    From spring-javaformat with Apache License 2.0 4 votes vote down vote up
public Collection<FileSetCheck> load(PropertyResolver propertyResolver) {
	Configuration config = loadConfiguration(getClass().getResourceAsStream("spring-checkstyle.xml"),
			propertyResolver);
	return Arrays.stream(config.getChildren()).map(this::load).collect(Collectors.toList());
}
 
Example #12
Source File: CheckstyleConfigurationFile.java    From eclipse-cs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Returns the property resolver or <code>null</code> if none has been set.
 * 
 * @return the property resolver
 */
public PropertyResolver getPropertyResolver() {
  return mPropertyResolver;
}
 
Example #13
Source File: CheckstyleConfigurationFile.java    From eclipse-cs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Sets the property resolver for this Checkstyle configuration.
 * 
 * @param propertyResolver
 *          the property resolver
 */
public void setPropertyResolver(PropertyResolver propertyResolver) {
  mPropertyResolver = propertyResolver;
}
 
Example #14
Source File: MultiPropertyResolver.java    From eclipse-cs with GNU Lesser General Public License v2.1 2 votes vote down vote up
/**
 * Adds a PropertyResolver to this aggregation property resolver.
 *
 * @param resolver
 *          the PropertyResolver to add
 */
public void addPropertyResolver(PropertyResolver resolver) {
  mChildResolver.add(resolver);
}