Search in sources :

Example 1 with MapBasedValueSource

use of org.codehaus.plexus.interpolation.MapBasedValueSource 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 MapBasedValueSource

use of org.codehaus.plexus.interpolation.MapBasedValueSource 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 MapBasedValueSource

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

the class DocumentDescriptorReader method readAndFilterDocumentDescriptor.

/**
 * Read and filter the <code>docDescriptor</code> file.
 *
 * @param docDescriptor not null, corresponding to non-localized descriptor file.
 * @return a DocumentModel instance.
 * @throws XmlPullParserException if an error occurs during parsing.
 * @throws IOException if an error occurs during reading.
 */
public DocumentModel readAndFilterDocumentDescriptor(File docDescriptor) throws XmlPullParserException, IOException {
    if (locale != null) {
        String descriptorFilename = docDescriptor.getName();
        String localized = FileUtils.removeExtension(descriptorFilename) + '_' + locale.getLanguage() + '.' + FileUtils.getExtension(descriptorFilename);
        File localizedDocDescriptor = new File(docDescriptor.getParentFile(), localized);
        if (localizedDocDescriptor.exists()) {
            docDescriptor = localizedDocDescriptor;
        }
    }
    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) File(java.io.File) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource) EnvarBasedValueSource(org.codehaus.plexus.interpolation.EnvarBasedValueSource)

Example 4 with MapBasedValueSource

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

the class AbstractInvokerMojo method getInvokerProperties.

/**
 * Gets the (interpolated) invoker properties for an integration test.
 *
 * @param projectDirectory The base directory of the IT project, must not be <code>null</code>.
 * @return The invoker properties, may be empty but never <code>null</code>.
 * @throws org.apache.maven.plugin.MojoExecutionException If an I/O error occurred during reading the properties.
 */
private InvokerProperties getInvokerProperties(final File projectDirectory) throws MojoExecutionException {
    Properties props = new Properties();
    if (invokerPropertiesFile != null) {
        File propertiesFile = new File(projectDirectory, invokerPropertiesFile);
        if (propertiesFile.isFile()) {
            InputStream in = null;
            try {
                in = new FileInputStream(propertiesFile);
                props.load(in);
                in.close();
                in = null;
            } catch (IOException e) {
                throw new MojoExecutionException("Failed to read invoker properties: " + propertiesFile, e);
            } finally {
                IOUtil.close(in);
            }
        }
        Interpolator interpolator = new RegexBasedInterpolator();
        interpolator.addValueSource(new MapBasedValueSource(getInterpolationValueSource(false)));
        // CHECKSTYLE_OFF: LineLength
        for (String key : props.stringPropertyNames()) {
            String value = props.getProperty(key);
            try {
                value = interpolator.interpolate(value, "");
            } catch (InterpolationException e) {
                throw new MojoExecutionException("Failed to interpolate invoker properties: " + propertiesFile, e);
            }
            props.setProperty(key, value);
        }
    // CHECKSTYLE_ON: LineLength
    }
    return new InvokerProperties(props);
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) Interpolator(org.codehaus.plexus.interpolation.Interpolator) RegexBasedInterpolator(org.codehaus.plexus.interpolation.RegexBasedInterpolator) IOException(java.io.IOException) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) Properties(java.util.Properties) File(java.io.File) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource) FileInputStream(java.io.FileInputStream)

Example 5 with MapBasedValueSource

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

the class GenerateApplicationXmlMojo method buildEnvEntries.

/**
 * Builds the env-entries based on the configuration.
 *
 * @return a list of EnvEntry object(s)
 * @throws EarPluginException if the configuration is invalid
 */
private List<EnvEntry> buildEnvEntries() throws EarPluginException {
    final List<EnvEntry> result = new ArrayList<EnvEntry>();
    if (envEntries == null) {
        return result;
    }
    try {
        StringSearchInterpolator ssi = new StringSearchInterpolator();
        ValueSource vs = new MapBasedValueSource(project.getProperties());
        ssi.addValueSource(vs);
        final PlexusConfiguration[] allEnvEntries = envEntries.getChildren(EnvEntry.ENV_ENTRY);
        getLog().debug("buildEnvEntries: allEnvEntries:" + allEnvEntries);
        getLog().debug("buildEnvEntries: allEnvEntries size:" + allEnvEntries.length);
        for (PlexusConfiguration envEntry : allEnvEntries) {
            // CHECKSTYLE_OFF: LineLength
            final String childDescription = interpolate(ssi, envEntry.getChild(EnvEntry.DESCRIPTION).getValue());
            final String childEnvEntryName = interpolate(ssi, envEntry.getChild(EnvEntry.ENV_ENTRY_NAME).getValue());
            final String childEnvEntryType = interpolate(ssi, envEntry.getChild(EnvEntry.ENV_ENTRY_TYPE).getValue());
            final String childEnvEntryValue = interpolate(ssi, envEntry.getChild(EnvEntry.ENV_ENTRY_VALUE).getValue());
            final String childEnvLookupNameValue = interpolate(ssi, envEntry.getChild(EnvEntry.ENV_LOOKUP_NAME).getValue());
            try {
                result.add(new EnvEntry(childDescription, childEnvEntryName, childEnvEntryType, childEnvEntryValue, childEnvLookupNameValue));
            } catch (IllegalArgumentException e) {
                throw new EarPluginException("Invalid env-entry [" + envEntry + "]", e);
            }
        }
        return result;
    } catch (InterpolationException e) {
        throw new EarPluginException("Interpolation exception:", e);
    }
}
Also used : PlexusConfiguration(org.codehaus.plexus.configuration.PlexusConfiguration) StringSearchInterpolator(org.codehaus.plexus.interpolation.StringSearchInterpolator) ValueSource(org.codehaus.plexus.interpolation.ValueSource) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource) ArrayList(java.util.ArrayList) InterpolationException(org.codehaus.plexus.interpolation.InterpolationException) MapBasedValueSource(org.codehaus.plexus.interpolation.MapBasedValueSource)

Aggregations

InterpolationException (org.codehaus.plexus.interpolation.InterpolationException)7 MapBasedValueSource (org.codehaus.plexus.interpolation.MapBasedValueSource)7 IOException (java.io.IOException)4 RegexBasedInterpolator (org.codehaus.plexus.interpolation.RegexBasedInterpolator)4 ArrayList (java.util.ArrayList)3 PlexusConfiguration (org.codehaus.plexus.configuration.PlexusConfiguration)3 EnvarBasedValueSource (org.codehaus.plexus.interpolation.EnvarBasedValueSource)3 StringSearchInterpolator (org.codehaus.plexus.interpolation.StringSearchInterpolator)3 ValueSource (org.codehaus.plexus.interpolation.ValueSource)3 File (java.io.File)2 Properties (java.util.Properties)2 Activation (org.apache.maven.model.Activation)2 ActivationFile (org.apache.maven.model.ActivationFile)2 Interpolator (org.codehaus.plexus.interpolation.Interpolator)2 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 Reader (java.io.Reader)1 StringReader (java.io.StringReader)1 DocumentXpp3Reader (org.apache.maven.doxia.document.io.xpp3.DocumentXpp3Reader)1 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)1