From 274f5d877fee2ad19d2088dfe35a7be73c0012d9 Mon Sep 17 00:00:00 2001 From: Matthew Jason Benson Date: Wed, 27 Sep 2006 22:37:01 +0000 Subject: [PATCH] add the resource collection; tokenizers graduate to free-standing types git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@450611 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 3 + docs/manual/CoreTypes/resources.html | 32 +++++ .../tools/ant/types/defaults.properties | 6 + .../tools/ant/types/resources/Tokens.java | 129 ++++++++++++++++++ .../antunit/types/resources/tokens-test.xml | 111 +++++++++++++++ src/tests/antunit/types/resources/utf-16.in | Bin 0 -> 26 bytes 6 files changed, 281 insertions(+) create mode 100644 src/main/org/apache/tools/ant/types/resources/Tokens.java create mode 100644 src/tests/antunit/types/resources/tokens-test.xml create mode 100755 src/tests/antunit/types/resources/utf-16.in diff --git a/WHATSNEW b/WHATSNEW index 734ac03c2..27bbd2eb0 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -36,6 +36,9 @@ Other changes: * Extending JAR-Task for SPI. Bugzilla report 31520. +* Added resource collection for convenient creation of string + resources from other resources' content. Inspired by Bugzilla 40504. + Changes from Ant 1.7.0Beta1 to Ant 1.7.0Beta2 ============================================= diff --git a/docs/manual/CoreTypes/resources.html b/docs/manual/CoreTypes/resources.html index 634653ba8..64bf2e083 100644 --- a/docs/manual/CoreTypes/resources.html +++ b/docs/manual/CoreTypes/resources.html @@ -317,6 +317,8 @@ Ant's "legacy" datatypes have been modified to behave as Resource Collections:
  • sort - sorted resource collection
  • first - first n resources from a nested collection
  • +
  • tokens - string tokens + gathered from a nested collection
  • union - set union of nested resource collections
  • intersect - set intersection of nested resource collections
  • @@ -727,6 +729,36 @@ larger collection.

    A single resource collection is required.

    +

    tokens

    +

    Includes the string tokens gathered from a nested + resource collection. Uses the same tokenizers supported by the +TokenFilter. Hint: imaginative + use of this resource collection can implement equivalents for such Unix + functions as sort, grep -c, wc and + wc -l.

    +
    + + + + + + + + + + + + + + + + +
    AttributeDescriptionRequired
    encodingThe encoding of the nested resourcesNo, default is platform default
    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:

    diff --git a/src/main/org/apache/tools/ant/types/defaults.properties b/src/main/org/apache/tools/ant/types/defaults.properties index 81b4d70ee..5e1bcab50 100644 --- a/src/main/org/apache/tools/ant/types/defaults.properties +++ b/src/main/org/apache/tools/ant/types/defaults.properties @@ -56,6 +56,7 @@ sort=org.apache.tools.ant.types.resources.Sort resources=org.apache.tools.ant.types.resources.Resources first=org.apache.tools.ant.types.resources.First tarfileset=org.apache.tools.ant.types.TarFileSet +tokens=org.apache.tools.ant.types.resources.Tokens #Resources (single-element ResourceCollections): resource=org.apache.tools.ant.types.Resource @@ -68,3 +69,8 @@ tarentry=org.apache.tools.ant.types.resources.TarResource gzipresource=org.apache.tools.ant.types.resources.GZipResource bzip2resource=org.apache.tools.ant.types.resources.BZip2Resource javaresource=org.apache.tools.ant.types.resources.JavaResource + +#tokenizer implementations +linetokenizer=org.apache.tools.ant.util.LineTokenizer +stringtokenizer=org.apache.tools.ant.util.StringTokenizer +filetokenizer=org.apache.tools.ant.util.FileTokenizer diff --git a/src/main/org/apache/tools/ant/types/resources/Tokens.java b/src/main/org/apache/tools/ant/types/resources/Tokens.java new file mode 100644 index 000000000..aa8e9c801 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/resources/Tokens.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.InputStreamReader; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.util.Stack; +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; +import org.apache.tools.ant.types.resources.StringResource; +import org.apache.tools.ant.util.ConcatResourceInputStream; +import org.apache.tools.ant.util.LineTokenizer; +import org.apache.tools.ant.util.Tokenizer; + +/** + * ResourceCollection consisting of StringResources gathered from tokenizing + * another ResourceCollection with a Tokenizer implementation. + * @since Ant 1.7 + */ +public class Tokens extends BaseResourceCollectionWrapper { + + private Tokenizer tokenizer; + private String encoding; + + /** + * Sort the contained elements. + * @return a Collection of Resources. + */ + protected synchronized Collection getCollection() { + ResourceCollection rc = getResourceCollection(); + if (rc.size() == 0) { + return Collections.EMPTY_SET; + } + if (tokenizer == null) { + tokenizer = new LineTokenizer(); + } + ConcatResourceInputStream cat = new ConcatResourceInputStream(rc); + cat.setManagingComponent(this); + + InputStreamReader rdr = null; + if (encoding == null) { + rdr = new InputStreamReader(cat); + } else { + try { + rdr = new InputStreamReader(cat, encoding); + } catch (UnsupportedEncodingException e) { + throw new BuildException(e); + } + } + ArrayList result = new ArrayList(); + try { + for (String s = tokenizer.getToken(rdr); s != null; s = tokenizer.getToken(rdr)) { + result.add(new StringResource(s)); + } + } catch (IOException e) { + throw new BuildException("Error reading tokens", e); + } + return result; + } + + /** + * Set the encoding used to create the tokens. + * @param encoding the encoding to use. + */ + public synchronized void setEncoding(String encoding) { + this.encoding = encoding; + } + + /** + * Add the nested Tokenizer to this Tokens ResourceCollection. + * A LineTokenizer will be used by default. + * @param tokenizer the tokenizer to add. + */ + public synchronized void add(Tokenizer tokenizer) { + if (isReference()) { + throw noChildrenAllowed(); + } + if (this.tokenizer != null) { + throw new BuildException("Only one nested tokenizer allowed."); + } + this.tokenizer = tokenizer; + } + + /** + * Overrides the BaseResourceCollectionContainer version + * to check the nested Tokenizer. + * @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 (tokenizer instanceof DataType) { + stk.push(tokenizer); + invokeCircularReferenceCheck((DataType) tokenizer, stk, p); + } + setChecked(true); + } + } + +} diff --git a/src/tests/antunit/types/resources/tokens-test.xml b/src/tests/antunit/types/resources/tokens-test.xml new file mode 100644 index 000000000..5e374526b --- /dev/null +++ b/src/tests/antunit/types/resources/tokens-test.xml @@ -0,0 +1,111 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/tests/antunit/types/resources/utf-16.in b/src/tests/antunit/types/resources/utf-16.in new file mode 100755 index 0000000000000000000000000000000000000000..7c7c2a7806ef59e8f4b3b30cb6dd3d850f193007 GIT binary patch literal 26 acmezOpCOGQ9|*Y^k{A*hilB5A0~Y{e?*-BT literal 0 HcmV?d00001