001    /*
002     * $RCSfile: MergeProperties.java,v $
003     * $Revision: 1.2 $ $Date: 2006/11/22 10:50:21 $
004     *
005     * This file is part of ***  M y C o R e  ***
006     * See http://www.mycore.de/ for details.
007     *
008     * This program is free software; you can use it, redistribute it
009     * and / or modify it under the terms of the GNU General Public License
010     * (GPL) as published by the Free Software Foundation; either version 2
011     * of the License or (at your option) any later version.
012     *
013     * This program is distributed in the hope that it will be useful, but
014     * WITHOUT ANY WARRANTY; without even the implied warranty of
015     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
016     * GNU General Public License for more details.
017     *
018     * You should have received a copy of the GNU General Public License
019     * along with this program, in a file called gpl.txt or license.txt.
020     * If not, write to the Free Software Foundation Inc.,
021     * 59 Temple Place - Suite 330, Boston, MA  02111-1307 USA
022     */
023    package org.mycore.buildtools.anttasks;
024    
025    import java.io.FileInputStream;
026    import java.io.FileOutputStream;
027    import java.io.InputStream;
028    import java.io.OutputStream;
029    import java.util.Properties;
030    
031    import org.apache.tools.ant.BuildException;
032    import org.apache.tools.ant.Task;
033    
034    /**
035     * Ant task, that can be used to merges one properties file into another.
036     * New Properties will be added, existing properties overwritten.
037     *  
038     * @author Robert Stephan
039     * 
040     * @version $Revision: 13085 $ $Date: 2008-02-06 18:27:24 +0100 (Mi, 06 Feb 2008) $
041     *  
042     */
043    public class MCRMergePropertiesTask extends Task {
044            private String base, delta;
045    
046            /**
047             * sets the base properties file
048             * @param f the base properties file
049             */
050            public void setBasefile(String f) {
051                    base = f;
052            }
053    
054            /**
055             * sets the delta properties file, containing the new properties, which will
056             * be added or used as a replacement
057             * @param f the delta properties file
058             */
059            public void setDeltafile(String f) {
060                    delta = f;
061            }
062    
063            /* (non-Javadoc)
064             * @see org.apache.tools.ant.Task#execute()
065             */
066            public void execute() throws BuildException {
067                    if (base == null || delta == null) {
068                            throw new BuildException("all parameter must be specified: \n"
069                                            + "basefile -the basic property file \n"
070                                            + "deltafile -the property file, containg the changes \n");
071                    }
072    
073                    try {
074                            Properties baseProps = new Properties();
075                            InputStream isBase =new FileInputStream(base); 
076                            baseProps.load(isBase);
077                            isBase.close();
078                            
079                            Properties deltaProps = new Properties();
080                            InputStream isDelta = new FileInputStream(delta);
081                            deltaProps.load(isDelta);
082                            isDelta.close();
083                            
084                            baseProps.putAll(deltaProps);
085                            
086                            OutputStream propOut = new FileOutputStream(base);
087                            baseProps.store(propOut, "Merged Properties File");
088                            propOut.close();
089                    } catch (Exception e) {
090                            throw new BuildException("Something went wrong at reading or writing a properties file",e);
091                    }
092            }
093            
094            /**
095             * simple testcase for class
096             * @param args unsed default arguments for main method
097             */
098            public static void main(String[] args){
099                    String baseFile="C:\\workspaces\\atlibri\\antmycore\\test\\mergeproperties\\base.properties";
100                    String deltaFile="C:\\workspaces\\atlibri\\antmycore\\test\\mergeproperties\\delta.properties";
101                                            
102                    MCRMergePropertiesTask  mx = new MCRMergePropertiesTask();
103                    mx.setBasefile(baseFile);
104                    mx.setDeltafile(deltaFile);             
105                    
106                    mx.execute();           
107            }
108    }