001: package org.tigris.scarab.util.build;
002:
003: /* ================================================================
004: * Copyright (c) 2000-2002 CollabNet. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions are
008: * met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in the
015: * documentation and/or other materials provided with the distribution.
016: *
017: * 3. The end-user documentation included with the redistribution, if
018: * any, must include the following acknowlegement: "This product includes
019: * software developed by Collab.Net <http://www.Collab.Net/>."
020: * Alternately, this acknowlegement may appear in the software itself, if
021: * and wherever such third-party acknowlegements normally appear.
022: *
023: * 4. The hosted project names must not be used to endorse or promote
024: * products derived from this software without prior written
025: * permission. For written permission, please contact info@collab.net.
026: *
027: * 5. Products derived from this software may not use the "Tigris" or
028: * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without
029: * prior written permission of Collab.Net.
030: *
031: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
032: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
033: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
034: * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
035: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
036: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
037: * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
038: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
039: * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
040: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
041: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
042: *
043: * ====================================================================
044: *
045: * This software consists of voluntary contributions made by many
046: * individuals on behalf of Collab.Net.
047: */
048:
049: import java.io.FileNotFoundException;
050: import java.io.IOException;
051:
052: import org.apache.tools.ant.BuildException;
053: import org.apache.tools.ant.Task;
054:
055: /**
056: * This class is used as ant task backend for the generation
057: * of a property file by use of a template file.
058: *
059: * @author <a href="mailto:dabbous@saxess.com">Hussayn Dabbous</a>
060: * @version $Id: AntPropertyFileGenerator.java 9421 2005-02-20 22:32:38Z jorgeuriarte $
061: */
062:
063: public class AntPropertyFileGenerator extends Task implements
064: PropertyGetter {
065:
066: /**
067: * Setter: set the path to the template file.
068: * Throws an exception, if the template file does not exist.
069: * @param theTemplatePath
070: */
071:
072: /**
073: * The reusable property file generator
074: */
075: private PropertyFileGenerator generator = new PropertyFileGenerator();
076:
077: public void setTemplate(String theTemplatePath) {
078: boolean status = generator.setTemplate(theTemplatePath);
079: if (!status) {
080: throw new BuildException("the template[" + theTemplatePath
081: + "] does not exist.");
082: }
083: }
084:
085: /**
086: * Setter: set the path to the final property file.
087: * Throws an exception, if the customFile exist,
088: * but can't be overwritten (due to permission settings).
089: * @param theCustomPath
090: */
091: public void setCustom(String theCustomPath) {
092: boolean status = generator.setCustom(theCustomPath);
093: if (!status) {
094: throw new BuildException("custom file ["
095: + generator.getCustom() + "] is not writable.");
096:
097: }
098: }
099:
100: /**
101: * Setter: set the path to the final property file.
102: * Throws an exception, if the customFile exist,
103: * but can't be overwritten (due to permission settings).
104: * @param theCustomPath
105: */
106: public void setProperties(String thePropertyFilePathes) {
107: boolean status = generator.setProperties(thePropertyFilePathes);
108: if (!status) {
109: throw new BuildException(
110: "problem with the propretyFilePathlist["
111: + thePropertyFilePathes
112: + "] one file is not readable.");
113:
114: }
115: }
116:
117: /**
118: * Read the templateFile and behave according to
119: * following rule set:
120: * <ul>
121: * <li> rule 1: Copy every line, which does NOT contain
122: * a property verbatim to the customFile.</li>
123: *
124: * <li> rule 2: Retrieve the current online value of each
125: * property found in the templateFile and generate an
126: * appropriate name/value pair in the customFile.</li>
127: *
128: * <li> rule 3: If a property value starts with a "${" in
129: * the templateFile, keep the value as is. By this we
130: * can propagate ${variables} to the customFile, which
131: * will be resolved during startup of Scarab.</li>
132: * </ul>
133: *
134: */
135: public void execute() {
136: log("Create custom file: '" + generator.getCustom() + "'.");
137: log("Using template : '" + generator.getTemplate());
138:
139: try {
140: generator.execute(this );
141: } catch (FileNotFoundException e) {
142: throw new BuildException("File not found: "
143: + e.getMessage());
144: } catch (IOException e) {
145: throw new BuildException("Error during read from ["
146: + generator.getTemplate() + "]: " + e.getMessage());
147: }
148:
149: }
150:
151: /**
152: * This is the method by which the generator can retrieve
153: * property values.
154: * @param name
155: * @return
156: */
157: public Object getProperty(String name, Object def) {
158: String result = null;
159:
160: String value = (String) def;
161: if (value == null || !value.startsWith("$")) {
162: result = getProject().getProperty(name);
163: }
164: if (result == null) {
165: result = value;
166: }
167: return result;
168: }
169: }
|