From 68db901f8923a6ac7ef0706616701561e6de0db9 Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Fri, 5 Aug 2005 17:27:14 +0000 Subject: [PATCH] Add the resource collection, plus its parent, BaseResourceCollectionWrapper. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@278491 13f79535-47bb-0310-9956-ffa450edef68 --- docs/manual/CoreTypes/resources.html | 64 +++++- src/etc/testcases/types/resources/build.xml | 46 +++- .../tools/ant/types/defaults.properties | 1 + .../BaseResourceCollectionWrapper.java | 203 ++++++++++++++++++ .../tools/ant/types/resources/First.java | 72 +++++++ .../ant/types/ResourceCollectionsTest.java | 8 + 6 files changed, 390 insertions(+), 4 deletions(-) create mode 100755 src/main/org/apache/tools/ant/types/resources/BaseResourceCollectionWrapper.java create mode 100755 src/main/org/apache/tools/ant/types/resources/First.java diff --git a/docs/manual/CoreTypes/resources.html b/docs/manual/CoreTypes/resources.html index 8651d58ca..b077c3a06 100644 --- a/docs/manual/CoreTypes/resources.html +++ b/docs/manual/CoreTypes/resources.html @@ -222,6 +222,8 @@ Ant's "legacy" datatypes have been modified to behave as Resource Collections:
  • restrict - restrict a resource collection to include only resources meeting specified criteria
  • sort - sorted resource collection
  • +
  • first - first n resources from a + nested collection
  • union - set union of nested resource collections
  • intersect - set intersection of nested resource collections
  • @@ -514,6 +516,19 @@ platforms.

    Sorts another nested resource collection according to the resources' natural order, or by one or more nested resource comparators:

    + + + + + + + + + + + +
    AttributeDescriptionRequired
    cacheWhether to cache results; disabling + may seriously impact performanceNo, default true

    Parameters specified as nested elements

    A single resource collection is required.

    The sort can be controlled and customized by specifying one or more @@ -532,7 +547,7 @@ natural order, or by one or more nested resource comparators:

  • size - sort resources by size
  • content - sort resources by content
  • reverse - reverse the natural sort order, - or a single nested resource comparator
  • + or that of a single nested resource comparator

    name

    @@ -570,10 +585,38 @@ natural order, or by one or more nested resource comparators:

    reverse

    -

    Reverse the natural sort order, or a single nested comparator.

    +

    Reverse the natural sort order, or that of a single nested comparator.

    +

    first

    +

    Includes the first count resources from a nested resource collection. +This can be used in conjunction with the sort collection, +for example, to select the first few oldest, largest, etc. resources from a +larger collection.

    +
    + + + + + + + + + + + + + + + + +
    AttributeDescriptionRequired
    countThe number of resources to includeNo, default 1
    cacheWhether to cache results; disabling + may seriously impact performanceNo, default true
    +

    Parameters specified as nested elements

    +

    A single resource collection is required.

    +
    +

    Set operations

    The following resource collections implement set operations:

    @@ -592,6 +635,21 @@ natural order, or by one or more nested resource comparators:

    difference

    Difference of nested resource collections.

    +

    The following attributes apply to all set-operation resource collections: +

    + + + + + + + + + + + +
    AttributeDescriptionRequired
    cacheWhether to cache results; disabling + may seriously impact performanceNo, default true

    @@ -599,4 +657,4 @@ natural order, or by one or more nested resource comparators:

    Reserved.

    - \ No newline at end of file + diff --git a/src/etc/testcases/types/resources/build.xml b/src/etc/testcases/types/resources/build.xml index c93c24abe..ee0b16175 100755 --- a/src/etc/testcases/types/resources/build.xml +++ b/src/etc/testcases/types/resources/build.xml @@ -445,6 +445,50 @@ depends="testfileurl,testfileurlref,testhttpurl1,testhttpurl2,testjarurl,testres - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index 05ee6c4d9..f0beebb2f 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -54,6 +54,7 @@ difference=org.apache.tools.ant.types.resources.Difference intersect=org.apache.tools.ant.types.resources.Intersect sort=org.apache.tools.ant.types.resources.Sort resources=org.apache.tools.ant.types.resources.Resources +first=org.apache.tools.ant.types.resources.First #Resources (single-element ResourceCollections): resource=org.apache.tools.ant.types.Resource diff --git a/src/main/org/apache/tools/ant/types/resources/BaseResourceCollectionWrapper.java b/src/main/org/apache/tools/ant/types/resources/BaseResourceCollectionWrapper.java new file mode 100755 index 000000000..852b84dc5 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/resources/BaseResourceCollectionWrapper.java @@ -0,0 +1,203 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.ant.types.resources; + +import java.io.File; +import java.util.List; +import java.util.Stack; +import java.util.Iterator; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +import org.apache.tools.ant.Project; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.types.DataType; +import org.apache.tools.ant.types.ResourceCollection; + +/** + * Base class for a ResourceCollection that wraps a single nested + * ResourceCollection. + * @since Ant 1.7 + */ +public abstract class BaseResourceCollectionWrapper + extends DataType implements ResourceCollection, Cloneable { + private static final String ONE_NESTED_MESSAGE + = " expects exactly one nested resource collection."; + + private ResourceCollection rc; + private Collection coll = null; + private boolean cache = true; + + /** + * Set whether to cache collections. + * @param b boolean cache flag. + */ + public synchronized void setCache(boolean b) { + cache = b; + } + + /** + * Learn whether to cache collections. Default is true. + * @return boolean cache flag. + */ + public synchronized boolean isCache() { + return cache; + } + + /** + * Add a ResourceCollection to the container. + * @param c the ResourceCollection to add. + * @throws BuildException on error. + */ + public synchronized void add(ResourceCollection c) throws BuildException { + if (isReference()) { + throw noChildrenAllowed(); + } + if (c == null) { + return; + } + if (rc != null) { + throwOneNested(); + } + rc = c; + setChecked(false); + } + + /** + * Fulfill the ResourceCollection contract. + * @return an Iterator of Resources. + */ + public synchronized final Iterator iterator() { + if (isReference()) { + return ((BaseResourceCollectionWrapper) getCheckedRef()).iterator(); + } + dieOnCircularReference(); + return cacheCollection().iterator(); + } + + /** + * Fulfill the ResourceCollection contract. + * @return number of elements as int. + */ + public synchronized int size() { + if (isReference()) { + return ((BaseResourceCollectionWrapper) getCheckedRef()).size(); + } + dieOnCircularReference(); + return cacheCollection().size(); + } + + /** + * Fulfill the ResourceCollection contract. + * @return whether this is a filesystem-only resource collection. + */ + public synchronized boolean isFilesystemOnly() { + if (isReference()) { + return ((BaseResourceCollectionContainer) getCheckedRef()).isFilesystemOnly(); + } + dieOnCircularReference(); + + if (rc == null || rc.isFilesystemOnly()) { + return true; + } + /* now check each Resource in case the child only + lets through files from any children IT may have: */ + for (Iterator i = cacheCollection().iterator(); i.hasNext();) { + if (!(i.next() instanceof FileResource)) { + return false; + } + } + return true; + } + + /** + * Overrides the version of DataType to recurse on all DataType + * child elements that may have been added. + * @param stk the stack of data types to use (recursively). + * @param p the project to use to dereference the references. + * @throws BuildException on error. + */ + protected synchronized void dieOnCircularReference(Stack stk, Project p) + throws BuildException { + if (isChecked()) { + return; + } + if (isReference()) { + super.dieOnCircularReference(stk, p); + } else { + if (rc instanceof DataType) { + stk.push(rc); + invokeCircularReferenceCheck((DataType) rc, stk, p); + stk.pop(); + } + setChecked(true); + } + } + + /** + * Get the nested ResourceCollection. + * @return a ResourceCollection. + * @throws BuildException if no nested ResourceCollection has been provided. + */ + protected synchronized final ResourceCollection getResourceCollection() { + dieOnCircularReference(); + if (rc == null) { + throwOneNested(); + } + return rc; + } + + /** + * Template method for subclasses to return a Collection of Resources. + * @return Collection. + */ + protected abstract Collection getCollection(); + + /** + * Format this BaseResourceCollectionWrapper as a String. + * @return a descriptive String. + */ + public synchronized String toString() { + if (isReference()) { + return getCheckedRef().toString(); + } + if (cacheCollection().size() == 0) { + return ""; + } + StringBuffer sb = new StringBuffer(); + for (Iterator i = coll.iterator(); i.hasNext();) { + if (sb.length() > 0) { + sb.append(File.pathSeparatorChar); + } + sb.append(i.next()); + } + return sb.toString(); + } + + private synchronized Collection cacheCollection() { + if (coll == null || !isCache()) { + coll = getCollection(); + } + return coll; + } + + private void throwOneNested() throws BuildException { + throw new BuildException(super.toString() + ONE_NESTED_MESSAGE); + } + +} diff --git a/src/main/org/apache/tools/ant/types/resources/First.java b/src/main/org/apache/tools/ant/types/resources/First.java new file mode 100755 index 000000000..18505377d --- /dev/null +++ b/src/main/org/apache/tools/ant/types/resources/First.java @@ -0,0 +1,72 @@ +/* + * Copyright 2005 The Apache Software Foundation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +package org.apache.tools.ant.types.resources; + +import java.util.List; +import java.util.Iterator; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; + +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.types.ResourceCollection; + +/** + * ResourceCollection that contains the first count elements of + * another ResourceCollection. + * @since Ant 1.7 + */ +public class First extends BaseResourceCollectionWrapper { + private static final String BAD_COUNT + = "count of first resources should be set to an int >= 0"; + + private int count = 1; + + /** + * Set the number of resources to be included. + * @param i the count as int. + */ + public synchronized void setCount(int i) { + count = i; + } + + /** + * Get the number of resources to be included. Default is 1. + * @return the count as int. + */ + public synchronized int getCount() { + return count; + } + + /** + * Take the first count elements. + * @return a Collection of Resources. + */ + protected Collection getCollection() { + int ct = getCount(); + if (ct < 0) { + throw new BuildException(BAD_COUNT); + } + Iterator iter = getResourceCollection().iterator(); + ArrayList al = new ArrayList(ct); + for (int i = 0; i < ct && iter.hasNext(); i++) { + al.add(iter.next()); + } + return al; + } + +} diff --git a/src/testcases/org/apache/tools/ant/types/ResourceCollectionsTest.java b/src/testcases/org/apache/tools/ant/types/ResourceCollectionsTest.java index 96ff76d0e..6727a2383 100755 --- a/src/testcases/org/apache/tools/ant/types/ResourceCollectionsTest.java +++ b/src/testcases/org/apache/tools/ant/types/ResourceCollectionsTest.java @@ -73,6 +73,14 @@ public class ResourceCollectionsTest extends BuildFileTest { executeTarget("testfileurlref"); } + public void testfirst1() { + executeTarget("testfirst1"); + } + + public void testfirst2() { + executeTarget("testfirst2"); + } + public void testhttpurl1() { executeTarget("testhttpurl1"); }