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    import org.codehaus.plexus.util.StringUtils;
032    
033    /**
034     * @author <a href="mailto:zarars@gmail.com">Zarar Siddiqi</a>
035     * @version $Id: AbstractWritePropertiesMojo.java 8861 2009-01-21 15:35:38Z pgier $
036     */
037    public abstract class AbstractWritePropertiesMojo extends AbstractMojo {
038    
039            /**
040             * @parameter default-value="${project}"
041             * @required
042             * @readonly
043             */
044            MavenProject project;
045    
046            /**
047             * The file that properties will be written to
048             *
049             * @parameter expression="${properties.outputFile}" default-value="${project.build.directory}/properties/project.properties";
050             * @required
051             */
052            File outputFile;
053    
054            /**
055             * Either <code>NORMAL</code> or <code>ENVIRONMENT_VARIABLE</code>. When set to <code>ENVIRONMENT_VARIABLE</code> the keys in the
056             * properties file will all be upper case with periods replaced by underscores.
057             *
058             * @parameter expression="${properties.outputStyle}" default-value="NORMAL"
059             * @required
060             */
061            OutputStyle outputStyle;
062    
063            /**
064             * If supplied property keys are prefixed with this value before being stored.
065             *
066             * @parameter expression="${properties.prefix}"
067             */
068            String prefix;
069    
070            protected void writeProperties(File file, Properties properties, OutputStyle outputStyle, String prefix) throws MojoExecutionException {
071                    Properties prefixed = getPrefixedProperties(properties, prefix);
072                    Properties formatted = getFormattedProperties(prefixed, outputStyle);
073                    Properties sorted = getSortedProperties(formatted);
074                    OutputStream out = null;
075                    try {
076                            out = FileUtils.openOutputStream(file);
077                            sorted.store(out, "Created by the properties-maven-plugin");
078                    } catch (IOException e) {
079                            throw new MojoExecutionException("Error creating properties file", e);
080                    } finally {
081                            IOUtils.closeQuietly(out);
082                    }
083            }
084    
085            protected SortedProperties getSortedProperties(Properties properties) {
086                    SortedProperties sp = new SortedProperties();
087                    sp.putAll(properties);
088                    return sp;
089            }
090    
091            protected Properties getFormattedProperties(Properties properties, OutputStyle style) {
092                    switch (style) {
093                    case NORMAL:
094                            return properties;
095                    case ENVIRONMENT_VARIABLE:
096                            return getEnvironmentVariableProperties(properties);
097                    default:
098                            throw new IllegalArgumentException(outputStyle + " is unknown");
099                    }
100            }
101    
102            protected Properties getPrefixedProperties(Properties properties, String prefix) {
103                    if (StringUtils.isBlank(prefix)) {
104                            return properties;
105                    }
106                    List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
107                    Collections.sort(keys);
108                    Properties newProperties = new Properties();
109                    for (String key : keys) {
110                            String value = properties.getProperty(key);
111                            String newKey = prefix + "." + key;
112                            newProperties.setProperty(newKey, value);
113                    }
114                    return newProperties;
115            }
116    
117            protected Properties getEnvironmentVariableProperties(Properties properties) {
118                    List<String> keys = new ArrayList<String>(properties.stringPropertyNames());
119                    Collections.sort(keys);
120                    Properties newProperties = new Properties();
121                    for (String key : keys) {
122                            String value = properties.getProperty(key);
123                            String newKey = key.toUpperCase().replace(".", "_");
124                            newProperties.setProperty(newKey, value);
125                    }
126                    return newProperties;
127            }
128    }