KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > facade > FacadeTaskHelper


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
19 package org.apache.tools.ant.util.facade;
20
21 import java.util.Enumeration JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 /**
25  * Helper class for facade implementations - encapsulates treatment of
26  * explicit implementation choices, magic properties and
27  * implementation specific command line arguments.
28  *
29  *
30  * @since Ant 1.5
31  */

32 public class FacadeTaskHelper {
33
34     /**
35      * Command line arguments.
36      */

37     private Vector JavaDoc args = new Vector JavaDoc();
38
39     /**
40      * The explicitly chosen implementation.
41      */

42     private String JavaDoc userChoice;
43
44     /**
45      * The magic property to consult.
46      */

47     private String JavaDoc magicValue;
48
49     /**
50      * The default value.
51      */

52     private String JavaDoc defaultValue;
53
54     /**
55      * @param defaultValue The default value for the implementation.
56      * Must not be null.
57      */

58     public FacadeTaskHelper(String JavaDoc defaultValue) {
59         this(defaultValue, null);
60     }
61
62     /**
63      * @param defaultValue The default value for the implementation.
64      * Must not be null.
65      * @param magicValue the value of a magic property that may hold a user.
66      * choice. May be null.
67      */

68     public FacadeTaskHelper(String JavaDoc defaultValue, String JavaDoc magicValue) {
69         this.defaultValue = defaultValue;
70         this.magicValue = magicValue;
71     }
72
73     /**
74      * Used to set the value of the magic property.
75      * @param magicValue the value of a magic property that may hold a user.
76      */

77     public void setMagicValue(String JavaDoc magicValue) {
78         this.magicValue = magicValue;
79     }
80
81     /**
82      * Used for explicit user choices.
83      * @param userChoice the explicitly chosen implementation.
84      */

85     public void setImplementation(String JavaDoc userChoice) {
86         this.userChoice = userChoice;
87     }
88
89     /**
90      * Retrieves the implementation.
91      * @return the implementation.
92      */

93     public String JavaDoc getImplementation() {
94         return userChoice != null ? userChoice
95                                   : (magicValue != null ? magicValue
96                                                         : defaultValue);
97     }
98
99     /**
100      * Retrieves the explicit user choice.
101      * @return the explicit user choice.
102      */

103     public String JavaDoc getExplicitChoice() {
104         return userChoice;
105     }
106
107     /**
108      * Command line argument.
109      * @param arg an argument to add.
110      */

111     public void addImplementationArgument(ImplementationSpecificArgument arg) {
112         args.addElement(arg);
113     }
114
115     /**
116      * Retrieves the command line arguments enabled for the current
117      * facade implementation.
118      * @return an array of command line arguements.
119      */

120     public String JavaDoc[] getArgs() {
121         Vector JavaDoc tmp = new Vector JavaDoc(args.size());
122         for (Enumeration JavaDoc e = args.elements(); e.hasMoreElements();) {
123             ImplementationSpecificArgument arg =
124                 ((ImplementationSpecificArgument) e.nextElement());
125             String JavaDoc[] curr = arg.getParts(getImplementation());
126             for (int i = 0; i < curr.length; i++) {
127                 tmp.addElement(curr[i]);
128             }
129         }
130         String JavaDoc[] res = new String JavaDoc[tmp.size()];
131         tmp.copyInto(res);
132         return res;
133     }
134
135     /**
136      * Tests whether the implementation has been chosen by the user
137      * (either via a magic property or explicitly.
138      * @return true if magic or user choice has be set.
139      * @since Ant 1.5.2
140      */

141     public boolean hasBeenSet() {
142         return userChoice != null || magicValue != null;
143     }
144 }
145
Popular Tags