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