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:
019: package org.apache.tools.ant.util.facade;
020:
021: import java.util.Enumeration;
022: import java.util.Vector;
023:
024: /**
025: * Helper class for facade implementations - encapsulates treatment of
026: * explicit implementation choices, magic properties and
027: * implementation specific command line arguments.
028: *
029: *
030: * @since Ant 1.5
031: */
032: public class FacadeTaskHelper {
033:
034: /**
035: * Command line arguments.
036: */
037: private Vector args = new Vector();
038:
039: /**
040: * The explicitly chosen implementation.
041: */
042: private String userChoice;
043:
044: /**
045: * The magic property to consult.
046: */
047: private String magicValue;
048:
049: /**
050: * The default value.
051: */
052: private String defaultValue;
053:
054: /**
055: * @param defaultValue The default value for the implementation.
056: * Must not be null.
057: */
058: public FacadeTaskHelper(String defaultValue) {
059: this (defaultValue, null);
060: }
061:
062: /**
063: * @param defaultValue The default value for the implementation.
064: * Must not be null.
065: * @param magicValue the value of a magic property that may hold a user.
066: * choice. May be null.
067: */
068: public FacadeTaskHelper(String defaultValue, String magicValue) {
069: this .defaultValue = defaultValue;
070: this .magicValue = magicValue;
071: }
072:
073: /**
074: * Used to set the value of the magic property.
075: * @param magicValue the value of a magic property that may hold a user.
076: */
077: public void setMagicValue(String magicValue) {
078: this .magicValue = magicValue;
079: }
080:
081: /**
082: * Used for explicit user choices.
083: * @param userChoice the explicitly chosen implementation.
084: */
085: public void setImplementation(String userChoice) {
086: this .userChoice = userChoice;
087: }
088:
089: /**
090: * Retrieves the implementation.
091: * @return the implementation.
092: */
093: public String getImplementation() {
094: return userChoice != null ? userChoice
095: : (magicValue != null ? magicValue : defaultValue);
096: }
097:
098: /**
099: * Retrieves the explicit user choice.
100: * @return the explicit user choice.
101: */
102: public String getExplicitChoice() {
103: return userChoice;
104: }
105:
106: /**
107: * Command line argument.
108: * @param arg an argument to add.
109: */
110: public void addImplementationArgument(
111: 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[] getArgs() {
121: Vector tmp = new Vector(args.size());
122: for (Enumeration e = args.elements(); e.hasMoreElements();) {
123: ImplementationSpecificArgument arg = ((ImplementationSpecificArgument) e
124: .nextElement());
125: String[] curr = arg.getParts(getImplementation());
126: for (int i = 0; i < curr.length; i++) {
127: tmp.addElement(curr[i]);
128: }
129: }
130: String[] res = new String[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: }
|