KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > selectors > Compare


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.types.resources.selectors;
19
20 import java.util.Stack JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.apache.tools.ant.Project;
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.types.Comparison;
26 import org.apache.tools.ant.types.DataType;
27 import org.apache.tools.ant.types.Resource;
28 import org.apache.tools.ant.types.Quantifier;
29 import org.apache.tools.ant.types.ResourceCollection;
30 import org.apache.tools.ant.types.resources.Union;
31 import org.apache.tools.ant.types.resources.comparators.ResourceComparator;
32 import org.apache.tools.ant.types.resources.comparators.DelegatedResourceComparator;
33
34 /**
35  * ResourceSelector that compares against "control" Resource(s)
36  * using ResourceComparators.
37  * @since Ant 1.7
38  */

39 public class Compare extends DataType implements ResourceSelector {
40
41     private static final String JavaDoc ONE_CONTROL_MESSAGE
42         = " the <control> element should be specified exactly once.";
43
44     private DelegatedResourceComparator comp = new DelegatedResourceComparator();
45     private Quantifier against = Quantifier.ALL;
46
47     private Comparison when = Comparison.EQUAL;
48
49     private Union control;
50
51     /**
52      * Add a ResourceComparator to this Compare selector.
53      * If multiple ResourceComparators are added, they will be processed in LIFO order.
54      * @param c the ResourceComparator to add.
55      */

56     public synchronized void add(ResourceComparator c) {
57         if (isReference()) {
58             throw noChildrenAllowed();
59         }
60         comp.add(c);
61     }
62
63     /**
64      * Set the quantifier to be used. Default "all".
65      * @param against the Quantifier EnumeratedAttribute to use.
66      */

67     public synchronized void setAgainst(Quantifier against) {
68         if (isReference()) {
69             throw tooManyAttributes();
70         }
71         this.against = against;
72     }
73
74     /**
75      * Set the comparison to be used. Default "equal".
76      * @param when the Comparison EnumeratedAttribute to use.
77      */

78     public synchronized void setWhen(Comparison when) {
79         if (isReference()) {
80             throw tooManyAttributes();
81         }
82         this.when = when;
83     }
84
85     /**
86      * Create the nested control element. These are the
87      * resources to compare against.
88      * @return ResourceCollection.
89      */

90     public synchronized ResourceCollection createControl() {
91         if (isReference()) {
92             throw noChildrenAllowed();
93         }
94         if (control != null) {
95             throw oneControl();
96         }
97         control = new Union();
98         return control;
99     }
100
101     //implement ResourceSelector; inherit doc
102
/** {@inheritDoc} */
103     public synchronized boolean isSelected(Resource r) {
104         if (isReference()) {
105             return ((ResourceSelector) getCheckedRef()).isSelected(r);
106         }
107         if (control == null) {
108             throw oneControl();
109         }
110         int t = 0, f = 0;
111         for (Iterator JavaDoc it = control.iterator(); it.hasNext();) {
112             if (when.evaluate(comp.compare(r, (Resource) it.next()))) {
113                 t++;
114             } else {
115                 f++;
116             }
117         }
118         return against.evaluate(t, f);
119     }
120
121     /**
122      * Overrides the version from DataType
123      * to recurse on nested ResourceComparators.
124      * @param stk the stack of data types to use (recursively).
125      * @param p the project to use to dereference the references.
126      * @throws BuildException on error.
127      */

128     protected synchronized void dieOnCircularReference(Stack JavaDoc stk, Project p)
129         throws BuildException {
130         if (isChecked()) {
131             return;
132         }
133         if (isReference()) {
134             super.dieOnCircularReference(stk, p);
135         } else {
136             if (control != null) {
137                 DataType.invokeCircularReferenceCheck(control, stk, p);
138             }
139             DataType.invokeCircularReferenceCheck(comp, stk, p);
140             setChecked(true);
141         }
142     }
143
144     private BuildException oneControl() {
145         return new BuildException(super.toString() + ONE_CONTROL_MESSAGE);
146     }
147 }
148
Popular Tags