Search in sources :

Example 1 with PrefixedObjectValueSource

use of org.codehaus.plexus.interpolation.PrefixedObjectValueSource in project maven-plugins by apache.

the class DoapUtil method interpolate.

/**
 * Interpolate a string with project and settings.
 *
 * @param value could be null
 * @param project not null
 * @param settings could be null
 * @return the value trimmed and interpolated or null if the interpolation doesn't work.
 * @since 1.1
 */
public static String interpolate(String value, final MavenProject project, Settings settings) {
    if (project == null) {
        throw new IllegalArgumentException("project is required");
    }
    if (value == null) {
        return value;
    }
    if (!value.contains("${")) {
        return value.trim();
    }
    RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
    try {
        interpolator.addValueSource(new EnvarBasedValueSource());
    } catch (IOException e) {
    // ignore
    }
    interpolator.addValueSource(new PropertiesBasedValueSource(System.getProperties()));
    interpolator.addValueSource(new PropertiesBasedValueSource(project.getProperties()));
    interpolator.addValueSource(new PrefixedObjectValueSource("project", project));
    interpolator.addValueSource(new PrefixedObjectValueSource("pom", project));
    interpolator.addValueSource(new ObjectBasedValueSource(project) {

        @Override
        public Object getValue(String expression) {
            try {
                return ReflectionValueExtractor.evaluate(expression, project, true);
            } catch (Exception e) {
                addFeedback("Failed to extract \'" + expression + "\' from: " + project, e);
            }
            return null;
        }
    });
    if (settings != null) {
        interpolator.addValueSource(new PrefixedObjectValueSource("settings", settings));
    }
    String interpolatedValue = value;
    try {
        interpolatedValue = interpolator.interpolate(value).trim();
    } catch (InterpolationException e) {
    // ignore
    }
    if (interpolatedValue.startsWith("${")) {
        return null;
    }
    return interpolatedValue;
}
Also used : RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) ObjectBasedValueSource(org.codehaus.plexus.interpolation.ObjectBasedValueSource) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) EnvarBasedValueSource(org.codehaus.plexus.interpolation.EnvarBasedValueSource) PropertiesBasedValueSource(org.codehaus.plexus.interpolation.PropertiesBasedValueSource) FileNotFoundException(java.io.FileNotFoundException) SocketTimeoutException(java.net.SocketTimeoutException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) PrefixedObjectValueSource(org.codehaus.plexus.interpolation.PrefixedObjectValueSource)

Example 2 with PrefixedObjectValueSource

use of org.codehaus.plexus.interpolation.PrefixedObjectValueSource in project sling by apache.

the class BundleListUtils method createInterpolator.

public static Interpolator createInterpolator(MavenProject project, MavenSession mavenSession) {
    StringSearchInterpolator interpolator = new StringSearchInterpolator();
    final Properties props = new Properties();
    props.putAll(project.getProperties());
    props.putAll(mavenSession.getSystemProperties());
    props.putAll(mavenSession.getUserProperties());
    interpolator.addValueSource(new PropertiesBasedValueSource(props));
    // add ${project.foo}
    interpolator.addValueSource(new PrefixedObjectValueSource(Arrays.asList("project", "pom"), project, true));
    // add ${session.foo}
    interpolator.addValueSource(new PrefixedObjectValueSource("session", mavenSession));
    // add ${settings.foo}
    final Settings settings = mavenSession.getSettings();
    if (settings != null) {
        interpolator.addValueSource(new PrefixedObjectValueSource("settings", settings));
    }
    return interpolator;
}
Also used : StringSearchInterpolator(org.codehaus.plexus.interpolation.StringSearchInterpolator) Properties(java.util.Properties) PropertiesBasedValueSource(org.codehaus.plexus.interpolation.PropertiesBasedValueSource) Settings(org.apache.maven.settings.Settings) PrefixedObjectValueSource(org.codehaus.plexus.interpolation.PrefixedObjectValueSource)

Aggregations

PrefixedObjectValueSource (org.codehaus.plexus.interpolation.PrefixedObjectValueSource)2 PropertiesBasedValueSource (org.codehaus.plexus.interpolation.PropertiesBasedValueSource)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Properties (java.util.Properties)1 Settings (org.apache.maven.settings.Settings)1 EnvarBasedValueSource (org.codehaus.plexus.interpolation.EnvarBasedValueSource)1 InterpolationException (org.codehaus.plexus.interpolation.InterpolationException)1 ObjectBasedValueSource (org.codehaus.plexus.interpolation.ObjectBasedValueSource)1 RegexBasedInterpolator (org.codehaus.plexus.interpolation.RegexBasedInterpolator)1 StringSearchInterpolator (org.codehaus.plexus.interpolation.StringSearchInterpolator)1