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.taskdefs.condition;
  18. import org.apache.tools.ant.BuildException;
  19. /**
  20. * Is one string part of another string?
  21. *
  22. * @version $Revision: 1.6.2.4 $
  23. *
  24. * @since Ant 1.5
  25. */
  26. public class Contains implements Condition {
  27. private String string, subString;
  28. private boolean caseSensitive = true;
  29. /**
  30. * The string to search in.
  31. * @param string the string to search in
  32. * @since Ant 1.5
  33. */
  34. public void setString(String string) {
  35. this.string = string;
  36. }
  37. /**
  38. * The string to search for.
  39. * @param subString the string to search for
  40. * @since Ant 1.5
  41. */
  42. public void setSubstring(String subString) {
  43. this.subString = subString;
  44. }
  45. /**
  46. * Whether to search ignoring case or not.
  47. * @param b if true, ignore case
  48. * @since Ant 1.5
  49. */
  50. public void setCasesensitive(boolean b) {
  51. caseSensitive = b;
  52. }
  53. /**
  54. * @since Ant 1.5
  55. * @return true if the substring is within the string
  56. * @exception BuildException if the attributes are not set correctly
  57. */
  58. public boolean eval() throws BuildException {
  59. if (string == null || subString == null) {
  60. throw new BuildException("both string and substring are required "
  61. + "in contains");
  62. }
  63. return caseSensitive
  64. ? string.indexOf(subString) > -1
  65. : string.toLowerCase().indexOf(subString.toLowerCase()) > -1;
  66. }
  67. }