1 | /*
|
---|
2 | * Copyright 2001-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 | * Simple String comparison condition.
|
---|
24 | *
|
---|
25 | * @since Ant 1.4
|
---|
26 | */
|
---|
27 | public class Equals implements Condition {
|
---|
28 |
|
---|
29 | private String arg1, arg2;
|
---|
30 | private boolean trim = false;
|
---|
31 | private boolean caseSensitive = true;
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * Set the first string
|
---|
35 | *
|
---|
36 | * @param a1 the first string
|
---|
37 | */
|
---|
38 | public void setArg1(String a1) {
|
---|
39 | arg1 = a1;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Set the second string
|
---|
44 | *
|
---|
45 | * @param a2 the second string
|
---|
46 | */
|
---|
47 | public void setArg2(String a2) {
|
---|
48 | arg2 = a2;
|
---|
49 | }
|
---|
50 |
|
---|
51 | /**
|
---|
52 | * Should we want to trim the arguments before comparing them?
|
---|
53 | * @param b if true trim the arguments
|
---|
54 | * @since Ant 1.5
|
---|
55 | */
|
---|
56 | public void setTrim(boolean b) {
|
---|
57 | trim = b;
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Should the comparison be case sensitive?
|
---|
62 | * @param b if true use a case sensitive comparison (this is the
|
---|
63 | * default)
|
---|
64 | * @since Ant 1.5
|
---|
65 | */
|
---|
66 | public void setCasesensitive(boolean b) {
|
---|
67 | caseSensitive = b;
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * @return true if the two strings are equal
|
---|
72 | * @exception BuildException if the attributes are not set correctly
|
---|
73 | */
|
---|
74 | public boolean eval() throws BuildException {
|
---|
75 | if (arg1 == null || arg2 == null) {
|
---|
76 | throw new BuildException("both arg1 and arg2 are required in "
|
---|
77 | + "equals");
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (trim) {
|
---|
81 | arg1 = arg1.trim();
|
---|
82 | arg2 = arg2.trim();
|
---|
83 | }
|
---|
84 |
|
---|
85 | return caseSensitive ? arg1.equals(arg2) : arg1.equalsIgnoreCase(arg2);
|
---|
86 | }
|
---|
87 | }
|
---|