KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.tools.ant.taskdefs.condition;
19
20 import org.apache.tools.ant.BuildException;
21 import org.apache.tools.ant.ProjectComponent;
22 import org.apache.tools.ant.util.regexp.Regexp;
23 import org.apache.tools.ant.types.RegularExpression;
24 import org.apache.tools.ant.util.regexp.RegexpMatcher;
25
26 /**
27  * Simple regular expression condition.
28  *
29  * @since Ant 1.7
30  */

31 public class Matches extends ProjectComponent implements Condition {
32
33     private String JavaDoc string;
34     private boolean caseSensitive = true;
35     private boolean multiLine = false;
36     private boolean singleLine = false;
37     private RegularExpression regularExpression;
38
39     /**
40      * Set the string
41      *
42      * @param string the string to match
43      */

44     public void setString(String JavaDoc string) {
45         this.string = string;
46     }
47
48     /**
49      * Set the regular expression to match against
50      *
51      * @param pattern the regular expression pattern
52      */

53     public void setPattern(String JavaDoc pattern) {
54         if (regularExpression != null) {
55             throw new BuildException(
56                 "Only one regular expression is allowed.");
57         }
58         regularExpression = new RegularExpression();
59         regularExpression.setPattern(pattern);
60     }
61
62     /**
63      * A regular expression.
64      * You can use this element to refer to a previously
65      * defined regular expression datatype instance
66      * @param regularExpression the regular expression object
67      * to be configured as an element
68      */

69     public void addRegexp(RegularExpression regularExpression) {
70         if (this.regularExpression != null) {
71             throw new BuildException(
72                 "Only one regular expression is allowed.");
73         }
74         this.regularExpression = regularExpression;
75     }
76
77     /**
78      * Whether to ignore case or not.
79      * @param b if false, ignore case.
80      * @since Ant 1.7
81      */

82     public void setCasesensitive(boolean b) {
83         caseSensitive = b;
84     }
85
86     /**
87      * Whether to match should be multiline.
88      * @param b the value to set.
89      */

90     public void setMultiline(boolean b) {
91         multiLine = b;
92     }
93
94     /**
95      * Whether to treat input as singleline ('.' matches newline).
96      * Corresponsds to java.util.regex.Pattern.DOTALL.
97      * @param b the value to set.
98      */

99     public void setSingleLine(boolean b) {
100         singleLine = b;
101     }
102
103     /**
104      * @return true if the string matches the regular expression pattern
105      * @exception BuildException if the attributes are not set correctly
106      */

107     public boolean eval() throws BuildException {
108         if (string == null) {
109             throw new BuildException(
110                 "Parameter string is required in matches.");
111         }
112         if (regularExpression == null) {
113             throw new BuildException("Missing pattern in matches.");
114         }
115         int options = RegexpMatcher.MATCH_DEFAULT;
116         if (!caseSensitive) {
117             options = options | RegexpMatcher.MATCH_CASE_INSENSITIVE;
118         }
119         if (multiLine) {
120             options = options | RegexpMatcher.MATCH_MULTILINE;
121         }
122         if (singleLine) {
123             options = options | RegexpMatcher.MATCH_SINGLELINE;
124         }
125         Regexp regexp = regularExpression.getRegexp(getProject());
126         return regexp.matches(string, options);
127     }
128 }
129
Popular Tags