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.Commandline;
024 import org.apache.tools.ant.types.DataType;
025 import org.apache.tools.ant.types.Path;
026
027 /**
028 * A class that represents nested <arg> or <jvmarg> elements. This class
029 * provides the same functionality as the class that represents these same
030 * elements in a "java" task. In addition, this class supports conditional "if"
031 * and "unless" attributes.
032 *
033 * @author Patrick Luby
034 */
035 public class ConditionalArgument extends DataType {
036
037 //------------------------------------------------------------------ Fields
038
039 /**
040 * Cached "if" condition flag.
041 */
042 private String ifCondition = null;
043
044 /**
045 * Cached "unless" condition flag.
046 */
047 private String unlessCondition = null;
048
049 /**
050 * Cached command line arguments.
051 */
052 private String[] parts = null;
053
054 //----------------------------------------------------------------- Methods
055
056 /**
057 * Get the "if" condition flag.
058 *
059 * @return the "if" condition flag
060 */
061 public String getIf() {
062
063 return ProjectHelper.replaceProperties(project, ifCondition, project.getProperties());
064
065 }
066
067 /**
068 * Get a single command line argument.
069 *
070 * @return a single command line argument
071 */
072 public String[] getParts() {
073
074 String[] list = new String[parts.length];
075 for (int i = 0; i < parts.length; i++)
076 list[i] = ProjectHelper.replaceProperties(project, parts[i], project.getProperties());
077 return list;
078
079 }
080
081 /**
082 * Get the "unless" condition flag.
083 *
084 * @return the "unless" condition flag
085 */
086 public String getUnless() {
087
088 return ProjectHelper.replaceProperties(project, unlessCondition, project.getProperties());
089
090 }
091
092 /**
093 * Set a single command line argument to the absolute
094 * filename of the specified file.
095 *
096 * @param file a single command line argument
097 */
098 public void setFile(File file) {
099
100 this.parts = new String[]{ file.getAbsolutePath() };
101
102 }
103
104 /**
105 * Set the "if" condition. Tasks that nest this class as an element
106 * should evaluate this flag in their {@link org.apache.tools.ant.Task#execute()} method. If the
107 * following conditions are true, the task should process this element:
108 * <ul>
109 * <ol>The flag is neither null nor a empty string
110 * <ol>The property that the flag resolves to after macro substitution
111 * is defined
112 * </ul>
113 *
114 * @param property a property name or macro
115 */
116 public void setIf(String property) {
117
118 this.ifCondition = property;
119
120 }
121
122 /**
123 * Set a line to split into several command line arguments.
124 *
125 * @param line line to split into several commandline arguments
126 */
127 public void setLine(String line) {
128
129 parts = Commandline.translateCommandline(line);
130
131 }
132
133 /**
134 * Set a single command line argument and treat it like a path. The
135 * correct path separator for the platform is used.
136 *
137 * @param path a single command line argument
138 */
139 public void setPath(Path path) {
140
141 this.parts = new String[]{ path.toString() };
142
143 }
144
145 /**
146 * Set the "unless" condition. Tasks that nest this class as an element
147 * should evaluate this flag in their {@link org.apache.tools.ant.Task#execute()} method. If the
148 * following conditions are true, the task should ignore this element:
149 * <ul>
150 * <ol>The flag is neither null nor a empty string
151 * <ol>The property that the flag resolves to after macro substitution
152 * is defined
153 * </ul>
154 *
155 * @param property a property name or macro
156 */
157 public void setUnless(String property) {
158
159 this.unlessCondition = property;
160
161 }
162
163 /**
164 * Set a single command line argument.
165 *
166 * @param value a single command line argument
167 */
168 public void setValue(String value) {
169
170 this.parts = new String[]{ value };
171
172 }
173
174 }