Search in sources :

Example 1 with EnvarBasedValueSource

use of org.codehaus.plexus.interpolation.EnvarBasedValueSource in project intellij-community by JetBrains.

the class MyFileProfileActivator method isActive.

public boolean isActive(Profile profile) {
    Activation activation = profile.getActivation();
    ActivationFile actFile = activation.getFile();
    if (actFile != null) {
        // check if the file exists, if it does then the profile will be active
        String fileString = actFile.getExists();
        RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
        try {
            interpolator.addValueSource(new EnvarBasedValueSource());
        } catch (IOException e) {
        // ignored
        }
        interpolator.addValueSource(new MapBasedValueSource(System.getProperties()));
        try {
            if (StringUtils.isNotEmpty(fileString)) {
                fileString = StringUtils.replace(interpolator.interpolate(fileString, ""), "\\", "/");
                return fileExists(fileString);
            }
            // check if the file is missing, if it is then the profile will be active
            fileString = actFile.getMissing();
            if (StringUtils.isNotEmpty(fileString)) {
                fileString = StringUtils.replace(interpolator.interpolate(fileString, ""), "\\", "/");
                return !fileExists(fileString);
            }
        } catch (InterpolationException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to interpolate missing file location for profile activator: " + fileString, e);
            } else {
                logger.warn("Failed to interpolate missing file location for profile activator: " + fileString + ". Run in debug mode (-X) for more information.");
            }
        }
    }
    return false;
}
Also used : ActivationFile(org.apache.maven.model.ActivationFile) RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) Activation(org.apache.maven.model.Activation) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) EnvarBasedValueSource(org.codehaus.plexus.interpolation.EnvarBasedValueSource) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource)

Example 2 with EnvarBasedValueSource

use of org.codehaus.plexus.interpolation.EnvarBasedValueSource in project intellij-community by JetBrains.

the class MyFileProfileActivator method isActive.

public boolean isActive(Profile profile) {
    Activation activation = profile.getActivation();
    ActivationFile actFile = activation.getFile();
    if (actFile != null) {
        // check if the file exists, if it does then the profile will be active
        String fileString = actFile.getExists();
        RegexBasedInterpolator interpolator = new RegexBasedInterpolator();
        try {
            interpolator.addValueSource(new EnvarBasedValueSource());
        } catch (IOException e) {
        // ignored
        }
        interpolator.addValueSource(new MapBasedValueSource(System.getProperties()));
        try {
            if (StringUtils.isNotEmpty(fileString)) {
                fileString = StringUtils.replace(interpolator.interpolate(fileString, ""), "\\", "/");
                return fileExists(fileString);
            }
            // check if the file is missing, if it is then the profile will be active
            fileString = actFile.getMissing();
            if (StringUtils.isNotEmpty(fileString)) {
                fileString = StringUtils.replace(interpolator.interpolate(fileString, ""), "\\", "/");
                return !fileExists(fileString);
            }
        } catch (InterpolationException e) {
            if (logger.isDebugEnabled()) {
                logger.debug("Failed to interpolate missing file location for profile activator: " + fileString, e);
            } else {
                logger.warn("Failed to interpolate missing file location for profile activator: " + fileString + ". Run in debug mode (-X) for more information.");
            }
        }
    }
    return false;
}
Also used : ActivationFile(org.apache.maven.model.ActivationFile) RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) Activation(org.apache.maven.model.Activation) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) EnvarBasedValueSource(org.codehaus.plexus.interpolation.EnvarBasedValueSource) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource)

Example 3 with EnvarBasedValueSource

use of org.codehaus.plexus.interpolation.EnvarBasedValueSource 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 4 with EnvarBasedValueSource

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

the class DocumentDescriptorReader method readAndFilterDocumentDescriptor.

/**
     * Read and filter the <code>docDescriptor</code> file.
     *
     * @param docDescriptor not null.
     * @return a DocumentModel instance.
     * @throws XmlPullParserException if an error occurs during parsing.
     * @throws IOException if an error occurs during reading.
     */
public DocumentModel readAndFilterDocumentDescriptor(final File docDescriptor) throws XmlPullParserException, IOException {
    Reader reader = null;
    try {
        // System properties
        final Properties filterProperties = System.getProperties();
        // Project properties
        if (project != null && project.getProperties() != null) {
            filterProperties.putAll(project.getProperties());
        }
        final Interpolator interpolator = new RegexBasedInterpolator();
        interpolator.addValueSource(new MapBasedValueSource(filterProperties));
        interpolator.addValueSource(new EnvarBasedValueSource());
        interpolator.addValueSource(new ObjectBasedValueSource(project) {

            /** {@inheritDoc} */
            public Object getValue(final String expression) {
                try {
                    return ReflectionValueExtractor.evaluate(expression, project);
                } catch (Exception e) {
                    addFeedback("Failed to extract \'" + expression + "\' from: " + project, e);
                }
                return null;
            }
        });
        final DateBean bean = new DateBean();
        interpolator.addValueSource(new ObjectBasedValueSource(bean));
        reader = ReaderFactory.newXmlReader(docDescriptor);
        final String interpolatedDoc = interpolator.interpolate(IOUtil.toString(reader));
        reader.close();
        reader = null;
        if (log != null && log.isDebugEnabled()) {
            log.debug("Interpolated document descriptor (" + docDescriptor.getAbsolutePath() + ")\n" + interpolatedDoc);
        }
        // No Strict
        return new DocumentXpp3Reader().read(new StringReader(interpolatedDoc), false);
    } catch (InterpolationException e) {
        final IOException io = new IOException("Error interpolating document descriptor");
        io.initCause(e);
        throw io;
    } finally {
        IOUtil.close(reader);
    }
}
Also used : RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) DocumentXpp3Reader(org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader) Reader(java.io.Reader) StringReader(java.io.StringReader) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) IOException(java.io.IOException) Properties(java.util.Properties) XmlPullParserException(org.codehaus.plexus.util.xml.pull.XmlPullParserException) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) DocumentXpp3Reader(org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader) StringReader(java.io.StringReader) Interpolator(org.codehaus.plexus.interpolation.Interpolator) RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) ObjectBasedValueSource(org.codehaus.plexus.interpolation.ObjectBasedValueSource) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource) EnvarBasedValueSource(org.codehaus.plexus.interpolation.EnvarBasedValueSource)

Aggregations

IOException (java.io.IOException)4 EnvarBasedValueSource (org.codehaus.plexus.interpolation.EnvarBasedValueSource)4 InterpolationException (org.codehaus.plexus.interpolation.InterpolationException)4 RegexBasedInterpolator (org.codehaus.plexus.interpolation.RegexBasedInterpolator)4 MapBasedValueSource (org.codehaus.plexus.interpolation.MapBasedValueSource)3 Activation (org.apache.maven.model.Activation)2 ActivationFile (org.apache.maven.model.ActivationFile)2 ObjectBasedValueSource (org.codehaus.plexus.interpolation.ObjectBasedValueSource)2 FileNotFoundException (java.io.FileNotFoundException)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 MalformedURLException (java.net.MalformedURLException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 Properties (java.util.Properties)1 DocumentXpp3Reader (org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader)1 Interpolator (org.codehaus.plexus.interpolation.Interpolator)1 PrefixedObjectValueSource (org.codehaus.plexus.interpolation.PrefixedObjectValueSource)1 PropertiesBasedValueSource (org.codehaus.plexus.interpolation.PropertiesBasedValueSource)1 XmlPullParserException (org.codehaus.plexus.util.xml.pull.XmlPullParserException)1