KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > tigris > scarab > util > build > AntPropertyFileGenerator


1 package org.tigris.scarab.util.build;
2
3 /* ================================================================
4  * Copyright (c) 2000-2002 CollabNet. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in the
15  * documentation and/or other materials provided with the distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowlegement: "This product includes
19  * software developed by Collab.Net <http://www.Collab.Net/>."
20  * Alternately, this acknowlegement may appear in the software itself, if
21  * and wherever such third-party acknowlegements normally appear.
22  *
23  * 4. The hosted project names must not be used to endorse or promote
24  * products derived from this software without prior written
25  * permission. For written permission, please contact info@collab.net.
26  *
27  * 5. Products derived from this software may not use the "Tigris" or
28  * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
29  * prior written permission of Collab.Net.
30  *
31  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
35  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
37  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
39  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
40  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42  *
43  * ====================================================================
44  *
45  * This software consists of voluntary contributions made by many
46  * individuals on behalf of Collab.Net.
47  */

48
49 import java.io.FileNotFoundException JavaDoc;
50 import java.io.IOException JavaDoc;
51
52 import org.apache.tools.ant.BuildException;
53 import org.apache.tools.ant.Task;
54
55 /**
56  * This class is used as ant task backend for the generation
57  * of a property file by use of a template file.
58  *
59  * @author <a HREF="mailto:dabbous@saxess.com">Hussayn Dabbous</a>
60  * @version $Id: AntPropertyFileGenerator.java 9421 2005-02-20 22:32:38Z jorgeuriarte $
61  */

62
63 public class AntPropertyFileGenerator extends Task implements PropertyGetter
64 {
65     
66     /**
67      * Setter: set the path to the template file.
68      * Throws an exception, if the template file does not exist.
69      * @param theTemplatePath
70      */

71     
72     /**
73      * The reusable property file generator
74      */

75     private PropertyFileGenerator generator = new PropertyFileGenerator();
76     
77     public void setTemplate(String JavaDoc theTemplatePath)
78     {
79         boolean status = generator.setTemplate(theTemplatePath);
80         if(!status)
81         {
82            throw new BuildException("the template["
83                 + theTemplatePath
84                 + "] does not exist.");
85         }
86     }
87
88     /**
89      * Setter: set the path to the final property file.
90      * Throws an exception, if the customFile exist,
91      * but can't be overwritten (due to permission settings).
92      * @param theCustomPath
93      */

94     public void setCustom(String JavaDoc theCustomPath)
95     {
96         boolean status = generator.setCustom(theCustomPath);
97         if(!status)
98         {
99             throw new BuildException("custom file ["
100                     + generator.getCustom()
101                     + "] is not writable.");
102
103         }
104     }
105
106     /**
107      * Setter: set the path to the final property file.
108      * Throws an exception, if the customFile exist,
109      * but can't be overwritten (due to permission settings).
110      * @param theCustomPath
111      */

112     public void setProperties(String JavaDoc thePropertyFilePathes)
113     {
114         boolean status = generator.setProperties(thePropertyFilePathes);
115         if(!status)
116         {
117             throw new BuildException("problem with the propretyFilePathlist["
118                     + thePropertyFilePathes
119                     + "] one file is not readable.");
120
121         }
122     }
123
124     
125     /**
126      * Read the templateFile and behave according to
127      * following rule set:
128      * <ul>
129      * <li> rule 1: Copy every line, which does NOT contain
130      * a property verbatim to the customFile.</li>
131      *
132      * <li> rule 2: Retrieve the current online value of each
133      * property found in the templateFile and generate an
134      * appropriate name/value pair in the customFile.</li>
135      *
136      * <li> rule 3: If a property value starts with a "${" in
137      * the templateFile, keep the value as is. By this we
138      * can propagate ${variables} to the customFile, which
139      * will be resolved during startup of Scarab.</li>
140      * </ul>
141      *
142      */

143     public void execute() {
144         log("Create custom file: '" + generator.getCustom() + "'.");
145         log("Using template : '" + generator.getTemplate() );
146         
147         try {
148             generator.execute(this);
149         }
150         catch (FileNotFoundException JavaDoc e)
151         {
152             throw new BuildException( "File not found: " + e.getMessage() );
153         }
154         catch (IOException JavaDoc e)
155         {
156             throw new BuildException("Error during read from ["
157                     + generator.getTemplate()
158                     + "]: "+e.getMessage());
159         }
160
161     }
162
163     /**
164      * This is the method by which the generator can retrieve
165      * property values.
166      * @param name
167      * @return
168      */

169     public Object JavaDoc getProperty(String JavaDoc name, Object JavaDoc def)
170     {
171         String JavaDoc result = null;
172
173         String JavaDoc value = (String JavaDoc) def;
174         if (value==null || !value.startsWith("$"))
175         {
176             result = getProject().getProperty(name);
177         }
178         if(result == null)
179         {
180             result = value;
181         }
182         return result;
183     }
184 }
185
Popular Tags