KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
21 import java.util.Iterator JavaDoc;
22
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.types.Resource;
25 import org.apache.tools.ant.types.ResourceCollection;
26 import org.apache.tools.ant.types.resources.Union;
27 import org.apache.tools.ant.util.ResourceUtils;
28
29 /**
30  * Compares resources for equality based on size and content.
31  * All resources specified must match; no resource collections
32  * specified is an error condition; if resource collections are
33  * specified, but yield fewer than two elements, the condition
34  * evaluates to <code>true</code>.
35  * @since Ant 1.7
36  */

37 public class ResourcesMatch implements Condition {
38
39     private Union resources = null;
40     private boolean asText = false;
41
42     /**
43      * Set whether to treat resources as if they were text files,
44      * ignoring line endings.
45      * @param asText whether to ignore line endings.
46      */

47     public void setAsText(boolean asText) {
48         this.asText = asText;
49     }
50
51     /**
52      * Add a resource collection.
53      * @param rc the resource collection to add.
54      */

55     public void add(ResourceCollection rc) {
56         if (rc == null) {
57             return;
58         }
59         resources = resources == null ? new Union() : resources;
60         resources.add(rc);
61     }
62
63     /**
64      * Verify that all resources match.
65      * @return true if all resources are equal.
66      * @exception BuildException if there is an error.
67      */

68     public boolean eval() throws BuildException {
69         if (resources == null) {
70             throw new BuildException(
71                 "You must specify one or more nested resource collections");
72         }
73         if (resources.size() > 1) {
74             Iterator JavaDoc i = resources.iterator();
75             Resource r1 = (Resource) i.next();
76             Resource r2 = null;
77
78             while (i.hasNext()) {
79                 r2 = (Resource) i.next();
80                 try {
81                     if (!ResourceUtils.contentEquals(r1, r2, asText)) {
82                         return false;
83                     }
84                 } catch (IOException JavaDoc ioe) {
85                     throw new BuildException("when comparing resources "
86                         + r1.toString() + " and " + r2.toString(), ioe);
87                 }
88                 r1 = r2;
89             }
90         }
91         return true;
92     }
93 }
94
Popular Tags