1. /*
  2. * Copyright 2000-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.taskdefs.optional.dotnet;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Task;
  20. import org.apache.tools.ant.Project;
  21. /**
  22. * definitions can be conditional. What .NET conditions can not be
  23. * is in any state other than defined and undefined; you cannot give
  24. * a definition a value.
  25. */
  26. public class DotnetDefine {
  27. private String name;
  28. private String ifCond;
  29. private String unlessCond;
  30. /**
  31. * the name of a property which must be defined for
  32. * the definition to be set. Optional.
  33. * @param condition the name of the property
  34. */
  35. public void setIf(String condition) {
  36. this.ifCond = condition;
  37. }
  38. /**
  39. * the name of a property which must be undefined for
  40. * the definition to be set. Optional.
  41. * @param condition the name of the property
  42. */
  43. public void setUnless(String condition) {
  44. this.unlessCond = condition;
  45. }
  46. public String getName() {
  47. return name;
  48. }
  49. /**
  50. * the name of the definition. Required.
  51. * @param name
  52. */
  53. public void setName(String name) {
  54. this.name = name;
  55. }
  56. /**
  57. * This method gets the value of this definition. Will be null if a condition
  58. * was declared and not met
  59. * @param owner owning task
  60. * @return The value of the definition.
  61. * @throws BuildException
  62. */
  63. public String getValue(Task owner) throws BuildException {
  64. if (name == null) {
  65. throw new BuildException("No name provided for the define element",
  66. owner.getLocation());
  67. }
  68. if (!isSet(owner)) {
  69. return null;
  70. }
  71. return name;
  72. }
  73. /**
  74. * logic taken from patternset
  75. * @param owner
  76. * @return true if the condition is valid
  77. */
  78. public boolean isSet(Task owner) {
  79. Project p = owner.getProject();
  80. if (ifCond != null && p.getProperty(ifCond) == null) {
  81. return false;
  82. } else if (unlessCond != null && p.getProperty(unlessCond) != null) {
  83. return false;
  84. }
  85. return true;
  86. }
  87. }