01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18:
19: package org.apache.tools.ant.taskdefs.condition;
20:
21: import org.apache.tools.ant.BuildException;
22:
23: /**
24: * Is one string part of another string?
25: *
26: *
27: * @since Ant 1.5
28: */
29: public class Contains implements Condition {
30:
31: private String string, subString;
32: private boolean caseSensitive = true;
33:
34: /**
35: * The string to search in.
36: * @param string the string to search in
37: * @since Ant 1.5
38: */
39: public void setString(String string) {
40: this .string = string;
41: }
42:
43: /**
44: * The string to search for.
45: * @param subString the string to search for
46: * @since Ant 1.5
47: */
48: public void setSubstring(String subString) {
49: this .subString = subString;
50: }
51:
52: /**
53: * Whether to search ignoring case or not.
54: * @param b if false, ignore case
55: * @since Ant 1.5
56: */
57: public void setCasesensitive(boolean b) {
58: caseSensitive = b;
59: }
60:
61: /**
62: * @since Ant 1.5
63: * @return true if the substring is within the string
64: * @exception BuildException if the attributes are not set correctly
65: */
66: public boolean eval() throws BuildException {
67: if (string == null || subString == null) {
68: throw new BuildException(
69: "both string and substring are required "
70: + "in contains");
71: }
72:
73: return caseSensitive ? string.indexOf(subString) > -1 : string
74: .toLowerCase().indexOf(subString.toLowerCase()) > -1;
75: }
76: }
|