1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.util.facade;
  18. import java.util.Enumeration;
  19. import java.util.Vector;
  20. /**
  21. * Helper class for facade implementations - encapsulates treatment of
  22. * explicit implementation choices, magic properties and
  23. * implementation specific command line arguments.
  24. *
  25. * @version $Revision: 1.9.2.4 $
  26. *
  27. * @since Ant 1.5
  28. */
  29. public class FacadeTaskHelper {
  30. /**
  31. * Command line arguments.
  32. */
  33. private Vector args = new Vector();
  34. /**
  35. * The explicitly chosen implementation.
  36. */
  37. private String userChoice;
  38. /**
  39. * The magic property to consult.
  40. */
  41. private String magicValue;
  42. /**
  43. * The default value.
  44. */
  45. private String defaultValue;
  46. /**
  47. * @param defaultValue The default value for the implementation.
  48. * Must not be null.
  49. */
  50. public FacadeTaskHelper(String defaultValue) {
  51. this(defaultValue, null);
  52. }
  53. /**
  54. * @param defaultValue The default value for the implementation.
  55. * Must not be null.
  56. * @param magicValue the value of a magic property that may hold a user
  57. * choice. May be null.
  58. */
  59. public FacadeTaskHelper(String defaultValue, String magicValue) {
  60. this.defaultValue = defaultValue;
  61. this.magicValue = magicValue;
  62. }
  63. /**
  64. * Used to set the value of the magic property.
  65. */
  66. public void setMagicValue(String magicValue) {
  67. this.magicValue = magicValue;
  68. }
  69. /**
  70. * Used for explicit user choices.
  71. */
  72. public void setImplementation(String userChoice) {
  73. this.userChoice = userChoice;
  74. }
  75. /**
  76. * Retrieves the implementation.
  77. */
  78. public String getImplementation() {
  79. return userChoice != null ? userChoice
  80. : (magicValue != null ? magicValue
  81. : defaultValue);
  82. }
  83. /**
  84. * Retrieves the explicit user choice
  85. */
  86. public String getExplicitChoice() {
  87. return userChoice;
  88. }
  89. /**
  90. * Command line argument.
  91. */
  92. public void addImplementationArgument(ImplementationSpecificArgument arg) {
  93. args.addElement(arg);
  94. }
  95. /**
  96. * Retrieves the command line arguments enabled for the current
  97. * facade implementation.
  98. */
  99. public String[] getArgs() {
  100. Vector tmp = new Vector(args.size());
  101. for (Enumeration e = args.elements(); e.hasMoreElements();) {
  102. ImplementationSpecificArgument arg =
  103. ((ImplementationSpecificArgument) e.nextElement());
  104. String[] curr = arg.getParts(getImplementation());
  105. for (int i = 0; i < curr.length; i++) {
  106. tmp.addElement(curr[i]);
  107. }
  108. }
  109. String[] res = new String[tmp.size()];
  110. tmp.copyInto(res);
  111. return res;
  112. }
  113. /**
  114. * Tests whether the implementation has been chosen by the user
  115. * (either via a magic property or explicitly.
  116. *
  117. * @since Ant 1.5.2
  118. */
  119. public boolean hasBeenSet() {
  120. return userChoice != null || magicValue != null;
  121. }
  122. }