001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.tools.ant.taskdefs;
019:
020: import org.apache.tools.ant.Task;
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.taskdefs.condition.Condition;
023: import org.apache.tools.ant.types.Reference;
024: import org.apache.tools.ant.types.Comparison;
025: import org.apache.tools.ant.types.ResourceCollection;
026:
027: /**
028: * Count resources from a ResourceCollection, storing to a property or
029: * writing to the log. Can also be used as a Condition.
030: * @since Ant 1.7
031: */
032: public class ResourceCount extends Task implements Condition {
033:
034: private static final String ONE_NESTED_MESSAGE = "ResourceCount can count resources from exactly one nested ResourceCollection.";
035:
036: private static final String COUNT_REQUIRED = "Use of the ResourceCount condition requires that the count attribute be set.";
037:
038: private ResourceCollection rc;
039: private Comparison when = Comparison.EQUAL;
040: private Integer count;
041: private String property;
042:
043: /**
044: * Add the ResourceCollection to count.
045: * @param r the ResourceCollection to count.
046: * @throws BuildException if already set.
047: */
048: public void add(ResourceCollection r) {
049: if (rc != null) {
050: throw new BuildException(ONE_NESTED_MESSAGE);
051: }
052: rc = r;
053: }
054:
055: /**
056: * Set the ResourceCollection reference.
057: * @param r the Reference.
058: */
059: public void setRefid(Reference r) {
060: Object o = r.getReferencedObject();
061: if (!(o instanceof ResourceCollection)) {
062: throw new BuildException(r.getRefId()
063: + " doesn\'t denote a ResourceCollection");
064: }
065: add((ResourceCollection) o);
066: }
067:
068: /**
069: * Execute as a Task.
070: */
071: public void execute() {
072: if (rc == null) {
073: throw new BuildException(ONE_NESTED_MESSAGE);
074: }
075: if (property == null) {
076: log("resource count = " + rc.size());
077: } else {
078: getProject().setNewProperty(property,
079: Integer.toString(rc.size()));
080: }
081: }
082:
083: /**
084: * Fulfill the condition contract.
085: * @return true if the specified ResourceCollection satisfies the set criteria.
086: * @throws BuildException if an error occurs.
087: */
088: public boolean eval() {
089: if (rc == null) {
090: throw new BuildException(ONE_NESTED_MESSAGE);
091: }
092: if (count == null) {
093: throw new BuildException(COUNT_REQUIRED);
094: }
095: return when.evaluate(new Integer(rc.size()).compareTo(count));
096: }
097:
098: /**
099: * Set the target count number for use as a Condition.
100: * @param c number of Resources as int.
101: */
102: public void setCount(int c) {
103: count = new Integer(c);
104: }
105:
106: /**
107: * Set the comparison for use as a Condition.
108: * @param c Comparison (an EnumeratedAttribute) When.
109: * @see org.apache.tools.ant.types.Comparison
110: */
111: public void setWhen(Comparison c) {
112: when = c;
113: }
114:
115: /**
116: * Set the name of the property to set in task mode.
117: * @param p the property name to set.
118: */
119: public void setProperty(String p) {
120: property = p;
121: }
122:
123: }
|