KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > Contains


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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 JavaDoc 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 JavaDoc 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 JavaDoc 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("both string and substring are required "
69                                      + "in contains");
70         }
71
72         return caseSensitive
73             ? string.indexOf(subString) > -1
74             : string.toLowerCase().indexOf(subString.toLowerCase()) > -1;
75     }
76 }
77
Popular Tags