You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by mb...@apache.org on 2005/08/05 19:27:15 UTC

cvs commit: ant/docs/manual/CoreTypes resources.html

mbenson     2005/08/05 10:27:15

  Modified:    src/main/org/apache/tools/ant/types defaults.properties
               src/etc/testcases/types/resources build.xml
               src/testcases/org/apache/tools/ant/types
                        ResourceCollectionsTest.java
               docs/manual/CoreTypes resources.html
  Added:       src/main/org/apache/tools/ant/types/resources First.java
                        BaseResourceCollectionWrapper.java
  Log:
  Add the <first> resource collection, plus its parent,
  BaseResourceCollectionWrapper.
  
  Revision  Changes    Path
  1.1                  ant/src/main/org/apache/tools/ant/types/resources/First.java
  
  Index: First.java
  ===================================================================
  /*
   * 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 <code>count</code> 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 <code>int</count>.
       */
      public synchronized void setCount(int i) {
          count = i;
      }
  
      /**
       * Get the number of resources to be included. Default is 1.
       * @return the count as <code>int</count>.
       */
      public synchronized int getCount() {
          return count;
      }
  
      /**
       * Take the first <code>count</code> 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;
      }
  
  }
  
  
  
  1.1                  ant/src/main/org/apache/tools/ant/types/resources/BaseResourceCollectionWrapper.java
  
  Index: BaseResourceCollectionWrapper.java
  ===================================================================
  /*
   * 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 <code>true</code>.
       * @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 <code>String</code>.
       */
      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);
      }
  
  }
  
  
  
  1.43      +1 -0      ant/src/main/org/apache/tools/ant/types/defaults.properties
  
  Index: defaults.properties
  ===================================================================
  RCS file: /home/cvs/ant/src/main/org/apache/tools/ant/types/defaults.properties,v
  retrieving revision 1.42
  retrieving revision 1.43
  diff -u -r1.42 -r1.43
  --- defaults.properties	23 May 2005 19:51:57 -0000	1.42
  +++ defaults.properties	5 Aug 2005 17:27:14 -0000	1.43
  @@ -54,6 +54,7 @@
   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
  
  
  
  1.2       +45 -1     ant/src/etc/testcases/types/resources/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/ant/src/etc/testcases/types/resources/build.xml,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- build.xml	23 May 2005 19:51:58 -0000	1.1
  +++ build.xml	5 Aug 2005 17:27:14 -0000	1.2
  @@ -445,6 +445,50 @@
     <target name="single"
       depends="testresource,url,testfile,string,testzipentry,testproperty" />
   
  -  <target name="all" depends="legacy,files,resources,setlogic,single" />
  +  <target name="testfirst0">
  +    <fail>
  +      <condition>
  +        <not>
  +          <resourcecount count="0">
  +            <first count="0">
  +              <filelist dir="${dir}" files="1,2,3,4,5" />
  +            </first>
  +          </resourcecount>
  +        </not>
  +      </condition>
  +    </fail>
  +  </target>
  +
  +  <target name="testfirst1">
  +    <fail>
  +      <condition>
  +        <not>
  +          <resourcecount count="1">
  +            <first>
  +              <filelist dir="${dir}" files="1,2,3,4,5" />
  +            </first>
  +          </resourcecount>
  +        </not>
  +      </condition>
  +    </fail>
  +  </target>
  +
  +  <target name="testfirst2">
  +    <fail>
  +      <condition>
  +        <not>
  +          <resourcecount count="2">
  +            <first count="2">
  +              <filelist dir="${dir}" files="1,2,3,4,5" />
  +            </first>
  +          </resourcecount>
  +        </not>
  +      </condition>
  +    </fail>
  +  </target>
  +
  +  <target name="first" depends="testfirst0,testfirst1,testfirst2" />
  +
  +  <target name="all" depends="legacy,files,resources,setlogic,single,first" />
   
   </project>
  
  
  
  1.2       +8 -0      ant/src/testcases/org/apache/tools/ant/types/ResourceCollectionsTest.java
  
  Index: ResourceCollectionsTest.java
  ===================================================================
  RCS file: /home/cvs/ant/src/testcases/org/apache/tools/ant/types/ResourceCollectionsTest.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ResourceCollectionsTest.java	23 May 2005 19:51:58 -0000	1.1
  +++ ResourceCollectionsTest.java	5 Aug 2005 17:27:14 -0000	1.2
  @@ -73,6 +73,14 @@
           executeTarget("testfileurlref");
       }
   
  +    public void testfirst1() {
  +        executeTarget("testfirst1");
  +    }
  +
  +    public void testfirst2() {
  +        executeTarget("testfirst2");
  +    }
  +
       public void testhttpurl1() {
           executeTarget("testhttpurl1");
       }
  
  
  
  1.4       +61 -3     ant/docs/manual/CoreTypes/resources.html
  
  Index: resources.html
  ===================================================================
  RCS file: /home/cvs/ant/docs/manual/CoreTypes/resources.html,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- resources.html	20 Jul 2005 06:45:30 -0000	1.3
  +++ resources.html	5 Aug 2005 17:27:14 -0000	1.4
  @@ -222,6 +222,8 @@
     <li><a href="#restrict">restrict</a> - restrict a resource collection
       to include only resources meeting specified criteria</li>
     <li><a href="#sort">sort</a> - sorted resource collection</li>
  +  <li><a href="#first">first</a> - first <i>n</i> resources from a
  +    nested collection</li>
     <li><a href="#union">union</a> - set union of nested resource collections</li>
     <li><a href="#intersect">intersect</a> - set intersection
       of nested resource collections</li>
  @@ -514,6 +516,19 @@
   <p>Sorts another nested resource collection according to the resources'
   natural order, or by one or more nested resource comparators:</p>
   <blockquote>
  +  <table border="1" cellpadding="2" cellspacing="0">
  +    <tr>
  +      <td valign="top"><b>Attribute</b></td>
  +      <td valign="top"><b>Description</b></td>
  +      <td align="center" valign="top"><b>Required</b></td>
  +    </tr>
  +    <tr>
  +      <td valign="top">cache</td>
  +      <td valign="top">Whether to cache results; disabling
  +        may seriously impact performance</td>
  +      <td valign="top" align="center">No, default <i>true</i></td>
  +    </tr>
  +  </table>
     <h4>Parameters specified as nested elements</h4>
     <p>A single resource collection is required.</p>
     <p>The sort can be controlled and customized by specifying one or more
  @@ -532,7 +547,7 @@
       <li><a href="#rcmp.size">size</a> - sort resources by size</li>
       <li><a href="#rcmp.content">content</a> - sort resources by content</li>
       <li><a href="#rcmp.reverse">reverse</a> - reverse the natural sort order,
  -      or a single nested resource comparator</li>
  +      or that of a single nested resource comparator</li>
     </ul>
   
     <h4><a name="rcmp.name">name</a></h4>
  @@ -570,10 +585,38 @@
     </table>
   
     <h4><a name="rcmp.reverse">reverse</a></h4>
  -  <p>Reverse the natural sort order, or a single nested comparator.</p>
  +  <p>Reverse the natural sort order, or that of a single nested comparator.</p>
   
   </blockquote>
   
  +<h4><a name="first">first</a></h4>
  +<p>Includes the first <i>count</i> resources from a nested resource collection.
  +This can be used in conjunction with the <a href="#sort">sort</a> collection,
  +for example, to select the first few oldest, largest, etc. resources from a
  +larger collection.</p>
  +<blockquote>
  +  <table border="1" cellpadding="2" cellspacing="0">
  +    <tr>
  +      <td valign="top"><b>Attribute</b></td>
  +      <td valign="top"><b>Description</b></td>
  +      <td align="center" valign="top"><b>Required</b></td>
  +    </tr>
  +    <tr>
  +      <td valign="top">count</td>
  +      <td valign="top">The number of resources to include</td>
  +      <td valign="top" align="center">No, default 1</td>
  +    </tr>
  +    <tr>
  +      <td valign="top">cache</td>
  +      <td valign="top">Whether to cache results; disabling
  +        may seriously impact performance</td>
  +      <td valign="top" align="center">No, default <i>true</i></td>
  +    </tr>
  +  </table>
  +  <h4>Parameters specified as nested elements</h4>
  +  <p>A single resource collection is required.</p>
  +</blockquote>
  +
   <h4><a name="setlogic">Set operations</a></h4>
   <blockquote>
     <p>The following resource collections implement set operations:</p>
  @@ -592,6 +635,21 @@
     <h4><a name="difference">difference</a></h4>
     <p>Difference of nested resource collections.</p>
   
  +  <p>The following attributes apply to all set-operation resource collections:
  +  </p>
  +  <table border="1" cellpadding="2" cellspacing="0">
  +    <tr>
  +      <td valign="top"><b>Attribute</b></td>
  +      <td valign="top"><b>Description</b></td>
  +      <td align="center" valign="top"><b>Required</b></td>
  +    </tr>
  +    <tr>
  +      <td valign="top">cache</td>
  +      <td valign="top">Whether to cache results; disabling
  +        may seriously impact performance</td>
  +      <td valign="top" align="center">No, default <i>true</i></td>
  +    </tr>
  +  </table>
   </blockquote>
   
   <hr>
  @@ -599,4 +657,4 @@
   Reserved.</p>
   
   </body>
  -</html>
  \ No newline at end of file
  +</html>
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org