001    /**
002     * Copyright 2009-2012 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.codehaus.mojo.properties;
017    
018    import java.io.File;
019    import java.io.IOException;
020    import java.io.OutputStream;
021    import java.util.ArrayList;
022    import java.util.Collections;
023    import java.util.List;
024    import java.util.Properties;
025    
026    import org.apache.commons.io.FileUtils;
027    import org.apache.commons.io.IOUtils;
028    import org.apache.maven.plugin.AbstractMojo;
029    import org.apache.maven.plugin.MojoExecutionException;
030    import org.apache.maven.project.MavenProject;
031    
032    /**
033     * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
034     * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
035     */
036    public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
037    
038            /**
039             * @parameter default-value="${project}"
040             * @required
041             * @readonly
042             */
043            MavenProject project;
044    
045            /**
046             * The file that properties will be written to
047             *
048             * @parameter expression="${properties.outputFile}" default-value="${project.build.directory}/properties/project.properties";
049             * @required
050             */
051            File outputFile;
052    
053            /**
054             * Either <code>NORMAL</code> or <code>ENVIRONMENT_VARIABLE</code>. When set to <code>ENVIRONMENT_VARIABLE</code> the keys in the
055             * properties file will all be upper case with periods replaced by underscores.
056             *
057             * @parameter expression="${properties.outputStyle}" default-value="NORMAL";
058             * @required
059             */
060            OutputStyle outputStyle;
061    
062            protected void writeProperties(File file, Properties properties, OutputStyle outputStyle) throws MojoExecutionException {
063                    Properties formatted = getProperties(properties, outputStyle);
064                    Properties sorted = getSortedProperties(formatted);
065                    OutputStream out = null;
066                    try {
067                            out = FileUtils.openOutputStream(file);
068                            sorted.store(out, "Created by the properties-maven-plugin");
069                    } catch (IOException e) {
070                            throw new MojoExecutionException("Error creating properties file", e);
071                    } finally {
072                            IOUtils.closeQuietly(out);
073                    }
074            }
075    
076            protected SortedProperties getSortedProperties(Properties properties) {
077                    SortedProperties sp = new SortedProperties();
078                    sp.putAll(properties);
079                    return sp;
080            }
081    
082            protected Properties getProperties(Properties properties, OutputStyle style) {
083                    switch (style) {
084                    case NORMAL:
085                            return properties;
086                    case ENVIRONMENT_VARIABLE:
087                            return getEnvironmentVariableProperties(properties);
088                    default:
089                            throw new IllegalArgumentException(outputStyle + " is unknown");
090                    }
091            }
092    
093            protected Properties getEnvironmentVariableProperties(Properties properties) {
094                    List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
095                    Collections.sort(keys);
096                    Properties newProperties = new Properties();
097                    for (String key : keys) {
098                            String value = properties.getProperty(key);
099                            String newKey = key.toUpperCase().replace(".", "_");
100                            newProperties.setProperty(newKey, value);
101                    }
102                    return newProperties;
103            }
104    }