View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   * 
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.launcher.types;
19  
20  import java.io.File;
21  
22  import org.apache.tools.ant.ProjectHelper;
23  import org.apache.tools.ant.types.Commandline;
24  import org.apache.tools.ant.types.DataType;
25  import org.apache.tools.ant.types.Path;
26  
27  /**
28   * A class that represents nested <arg> or <jvmarg> elements. This class
29   * provides the same functionality as the class that represents these same
30   * elements in a "java" task. In addition, this class supports conditional "if"
31   * and "unless" attributes.
32   *
33   * @author Patrick Luby
34   */
35  public class ConditionalArgument extends DataType {
36  
37      //------------------------------------------------------------------ Fields
38  
39      /**
40       * Cached "if" condition flag.
41       */
42      private String ifCondition = null;
43  
44      /**
45       * Cached "unless" condition flag.
46       */
47      private String unlessCondition = null;
48  
49      /**
50       * Cached command line arguments.
51       */
52      private String[] parts = null;
53  
54      //----------------------------------------------------------------- Methods
55  
56      /**
57       * Get the "if" condition flag.
58       *
59       * @return the "if" condition flag
60       */
61      public String getIf() {
62   
63          return ProjectHelper.replaceProperties(project, ifCondition, project.getProperties());
64  
65      }
66  
67      /**
68       * Get a single command line argument.
69       *
70       * @return a single command line argument
71       */
72      public String[] getParts() {
73  
74          String[] list = new String[parts.length];
75          for (int i = 0; i < parts.length; i++)
76              list[i] = ProjectHelper.replaceProperties(project, parts[i], project.getProperties());
77          return list;
78  
79      }
80  
81      /**
82       * Get the "unless" condition flag.
83       *
84       * @return the "unless" condition flag
85       */
86      public String getUnless() {
87   
88          return ProjectHelper.replaceProperties(project, unlessCondition, project.getProperties());
89  
90      }
91  
92      /**
93       * Set a single command line argument to the absolute
94       * filename of the specified file.
95       *
96       * @param file a single command line argument
97       */
98      public void setFile(File file) {
99  
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 }