01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
21: import java.util.Iterator;
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 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 ioe) {
85: throw new BuildException(
86: "when comparing resources " + r1.toString()
87: + " and " + r2.toString(), ioe);
88: }
89: r1 = r2;
90: }
91: }
92: return true;
93: }
94: }
|