1. /*
  2. * Copyright 2001-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.condition;
  18. import org.apache.tools.ant.BuildException;
  19. /**
  20. * Simple String comparison condition.
  21. *
  22. * @since Ant 1.4
  23. * @version $Revision: 1.10.2.4 $
  24. */
  25. public class Equals implements Condition {
  26. private String arg1, arg2;
  27. private boolean trim = false;
  28. private boolean caseSensitive = true;
  29. /**
  30. * Set the first string
  31. *
  32. * @param a1 the first string
  33. */
  34. public void setArg1(String a1) {
  35. arg1 = a1;
  36. }
  37. /**
  38. * Set the second string
  39. *
  40. * @param a2 the second string
  41. */
  42. public void setArg2(String a2) {
  43. arg2 = a2;
  44. }
  45. /**
  46. * Should we want to trim the arguments before comparing them?
  47. * @param b if true trim the arguments
  48. * @since Ant 1.5
  49. */
  50. public void setTrim(boolean b) {
  51. trim = b;
  52. }
  53. /**
  54. * Should the comparison be case sensitive?
  55. * @param b if true use a case sensitive comparison (this is the
  56. * default)
  57. * @since Ant 1.5
  58. */
  59. public void setCasesensitive(boolean b) {
  60. caseSensitive = b;
  61. }
  62. /**
  63. * @return true if the two strings are equal
  64. * @exception BuildException if the attributes are not set correctly
  65. */
  66. public boolean eval() throws BuildException {
  67. if (arg1 == null || arg2 == null) {
  68. throw new BuildException("both arg1 and arg2 are required in "
  69. + "equals");
  70. }
  71. if (trim) {
  72. arg1 = arg1.trim();
  73. arg2 = arg2.trim();
  74. }
  75. return caseSensitive ? arg1.equals(arg2) : arg1.equalsIgnoreCase(arg2);
  76. }
  77. }