001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.launcher.types;
019    
020    import java.io.File;
021    
022    import org.apache.tools.ant.ProjectHelper;
023    import org.apache.tools.ant.types.DataType;
024    import org.apache.tools.ant.types.Path;
025    
026    /**
027     * A class that represents nested <sysproperty> or <env> elements. This class
028     * provides the same functionality as the class that represents these same
029     * elements in a "java" task. In addition, this class supports conditional "if" 
030     * and "unless" attributes.
031     *
032     * @author Patrick Luby
033     */
034    public class ConditionalVariable extends DataType {
035    
036        //------------------------------------------------------------------ Fields
037    
038        /**
039         * Cached "if" condition flag.
040         */
041        private String ifCondition = null;
042    
043        /**
044         * Cached key.
045         */
046        private String key = null;
047    
048        /**
049         * Cached "unless" condition flag.
050         */
051        private String unlessCondition = null;
052    
053        /**
054         * Cached value.
055         */
056        private String value = null;
057    
058        //----------------------------------------------------------------- Methods
059    
060        /**
061         * Get the "if" condition flag.
062         *
063         * @return the "if" condition flag
064         */
065        public String getIf() {
066    
067            return ProjectHelper.replaceProperties(project, ifCondition, project.getProperties());
068    
069        }
070    
071        /**
072         * Get the key.
073         *
074         * @return the key for this variable
075         */
076        public String getKey() {
077    
078            return ProjectHelper.replaceProperties(project, key, project.getProperties());
079    
080        }
081    
082        /**
083         * Get the "unless" condition flag.
084         *
085         * @return the "unless" condition flag
086         */
087        public String getUnless() {
088     
089            return ProjectHelper.replaceProperties(project, unlessCondition, project.getProperties());
090    
091        }
092    
093        /**
094         * Get the value.
095         *
096         * @return the value for this variable
097         */
098        public String getValue() {
099    
100            return ProjectHelper.replaceProperties(project, value, project.getProperties());
101    
102        }
103    
104        /**
105         * Set the value to a {@link File}.
106         *
107         * @param file the {@link File} for this variable
108         */
109        public void setFile(File file) {
110    
111            this.value = file.getAbsolutePath();
112    
113        }
114    
115        /**
116         * Set the value to a {@link Path}.
117         *
118         * @param path the {@link Path} for this variable
119         */
120        public void setPath(Path path) {
121    
122            this.value = path.toString();
123    
124        }
125    
126        /**
127         * Set the "if" condition. Tasks that nest this class as an element
128         * should evaluate this flag in their {@link org.apache.tools.ant.Task#execute()} method. If the
129         * following conditions are true, the task should process this element:
130         * <ul>
131         * <ol>The flag is neither null nor a empty string
132         * <ol>The property that the flag resolves to after macro substitution
133         *  is defined
134         * </ul>
135         *
136         * @param property a property name or macro
137         */
138        public void setIf(String property) {
139     
140            this.ifCondition = property;
141    
142        }
143    
144        /**
145         * Set the key.
146         *
147         * @param key the key for this variable
148         */
149        public void setKey(String key) {
150    
151            this.key = key;
152    
153        }
154    
155        /**
156         * Set the value to a {@link Path}.
157         *
158         * @param path the {@link Path} for this variable
159         */
160        public void setFile(Path path) {
161    
162            this.value = path.toString();
163    
164        }
165    
166        /**
167         * Set the "unless" condition. Tasks that nest this class as an element
168         * should evaluate this flag in their {@link org.apache.tools.ant.Task#execute()} method. If the
169         * following conditions are true, the task should ignore this element:
170         * <ul>
171         * <ol>The flag is neither null nor a empty string
172         * <ol>The property that the flag resolves to after macro substitution
173         *  is defined
174         * </ul>
175         *
176         * @param property a property name or macro
177         */
178        public void setUnless(String property) {
179     
180            this.unlessCondition = property;
181    
182        }
183    
184        /**
185         * Set the value.
186         *
187         * @param value the value for this variable
188         */
189        public void setValue(String value) {
190    
191            this.value = value;
192    
193        }
194    
195    }