diff --git a/WHATSNEW b/WHATSNEW index 2a7a0f587..998f5f0a5 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -66,6 +66,9 @@ Other changes: * Added resource collection for convenient creation of string resources from other resources' content. Inspired by Bugzilla 40504. +* Added resource selector to select resources based on the + results of their comparison to other resources. + 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 64bf2e083..bcde85951 100644 --- a/docs/manual/CoreTypes/resources.html +++ b/docs/manual/CoreTypes/resources.html @@ -463,6 +463,8 @@ platforms. containing a particular text string.
  • containsregexp - select resources whose contents match a particular regular expression.
  • +
  • compare - select resources + based on comparison to other resources.
  • name

    @@ -621,12 +623,40 @@ platforms. +

    compare

    +

    Selects a resource based on its comparison to one or more "control" + resources using nested resource comparators.

    + + + + + + + + + + + + + + + + +
    AttributeDescriptionRequired
    whenComparison ("equal"/"eq", "greater"/"gt", "less"/"lt", + "le" (less or equal), "ge" (greater or equal), "ne" (not equal).No, default "equal"
    againstQuantifier ("all"/"each"/"every", "any"/"some", + (exactly) "one", "most"/"majority", "none".No, default "all"
    +

    Parameters specified as nested elements

    +

    The resources against which comparisons will be made must be specified + using the nested <control> element, which denotes a + resources collection.

    +

    sort

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

    + natural order, or by one or more nested resource + comparators:

    @@ -650,7 +680,7 @@ natural order, or by one or more nested resource comparators:

    are available in the internal antliborg.apache.tools.ant.types.resources.comparators:

    - +

    Resource Comparators:

    • name - sort resources by name
    • exists - sort resources by existence
    • diff --git a/src/main/org/apache/tools/ant/types/Quantifier.java b/src/main/org/apache/tools/ant/types/Quantifier.java new file mode 100755 index 000000000..ab9d13fce --- /dev/null +++ b/src/main/org/apache/tools/ant/types/Quantifier.java @@ -0,0 +1,139 @@ +/* + * 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; + +import org.apache.tools.ant.BuildException; + +/** + * EnumeratedAttribute for quantifier comparisons. Evaluates a + * boolean[] or raw true and false + * counts. Accepts the following values:
        + *
      • "all"
      • - none false + *
      • "each"
      • - none false + *
      • "every"
      • - none false + *
      • "any"
      • - at least one true + *
      • "some"
      • - at least one true + *
      • "one"
      • - exactly one true + *
      • "majority"
      • - more true than false + *
      • "most"
      • - more true than false + *
      • "none"
      • - none true + *
      + * @since Ant 1.7 + */ +public class Quantifier extends EnumeratedAttribute { + private static final String[] VALUES + = new String[] {"all", "each", "every", "any", "some", "one", + "majority", "most", "none"}; + + public static final Quantifier ALL = new Quantifier("all"); + public static final Quantifier ANY = new Quantifier("any"); + public static final Quantifier ONE = new Quantifier("one"); + public static final Quantifier MAJORITY = new Quantifier("majority"); + public static final Quantifier NONE = new Quantifier("none"); + + private static abstract class Predicate { + abstract boolean eval(int t, int f); + } + + private static final Predicate ALL_PRED = new Predicate() { + boolean eval(int t, int f) { return f == 0; } + }; + + private static final Predicate ANY_PRED = new Predicate() { + boolean eval(int t, int f) { return t > 0 ; } + }; + + private static final Predicate ONE_PRED = new Predicate() { + boolean eval(int t, int f) { return t == 1; } + }; + + private static final Predicate MAJORITY_PRED = new Predicate() { + boolean eval(int t, int f) { return t > f; } + }; + + private static final Predicate NONE_PRED = new Predicate() { + boolean eval(int t, int f) { return t == 0; } + }; + + private static final Predicate[] PREDS = new Predicate[VALUES.length]; + + static { + PREDS[0] = ALL_PRED; + PREDS[1] = ALL_PRED; + PREDS[2] = ALL_PRED; + PREDS[3] = ANY_PRED; + PREDS[4] = ANY_PRED; + PREDS[5] = ONE_PRED; + PREDS[6] = MAJORITY_PRED; + PREDS[7] = MAJORITY_PRED; + PREDS[8] = NONE_PRED; + } + + /** + * Default constructor. + */ + public Quantifier() { + } + + /** + * Construct a new Quantifier with the specified value. + * @param value the EnumeratedAttribute value. + */ + public Quantifier(String value) { + setValue(value); + } + + /** + * Return the possible values. + * @return String[] of EnumeratedAttribute values. + */ + public String[] getValues() { + return VALUES; + } + + /** + * Evaluate a boolean array. + * @param b the boolean[] to evaluate. + * @return true if the argument fell within the parameters of this Quantifier. + */ + public boolean evaluate(boolean[] b) { + int t = 0; + for (int i = 0; i < b.length; i++) { + if (b[i]) { + t++; + } + } + return evaluate(t, b.length - t); + } + + /** + * Evaluate integer true vs. false counts. + * @param t the number of true values. + * @param f the number of false values. + * @return true if the arguments fell within the parameters of this Quantifier. + */ + public boolean evaluate(int t, int f) { + int index = getIndex(); + if (index == -1) { + throw new BuildException("Quantifier value not set."); + } + return PREDS[index].eval(t, f); + } + +} + diff --git a/src/main/org/apache/tools/ant/types/resources/selectors/Compare.java b/src/main/org/apache/tools/ant/types/resources/selectors/Compare.java new file mode 100644 index 000000000..f75b7c658 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/resources/selectors/Compare.java @@ -0,0 +1,153 @@ +/* + * 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.selectors; + +import java.util.Stack; +import java.util.Vector; +import java.util.TreeMap; +import java.util.Iterator; +import java.util.Collection; +import java.util.Comparator; +import java.util.Collections; +import java.util.AbstractCollection; +import java.util.NoSuchElementException; + +import org.apache.tools.ant.Project; +import org.apache.tools.ant.BuildException; +import org.apache.tools.ant.types.Comparison; +import org.apache.tools.ant.types.DataType; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.Quantifier; +import org.apache.tools.ant.types.ResourceCollection; +import org.apache.tools.ant.types.resources.Union; +import org.apache.tools.ant.types.resources.comparators.ResourceComparator; +import org.apache.tools.ant.types.resources.comparators.DelegatedResourceComparator; + +/** + * ResourceSelector that compares against "control" Resource(s) + * using ResourceComparators. + * @since Ant 1.7 + */ +public class Compare extends DataType implements ResourceSelector { + + private static final String ONE_CONTROL_MESSAGE + = " the element should be specified exactly once."; + + private DelegatedResourceComparator comp = new DelegatedResourceComparator(); + private Quantifier against = Quantifier.ALL; + + private Comparison when = Comparison.EQUAL; + + private Union control; + + /** + * Add a ResourceComparator to this Compare selector. + * If multiple ResourceComparators are added, they will be processed in LIFO order. + * @param c the ResourceComparator to add. + */ + public synchronized void add(ResourceComparator c) { + if (isReference()) { + throw noChildrenAllowed(); + } + comp.add(c); + } + + /** + * Set the quantifier to be used. Default "all". + * @param against the Quantifier EnumeratedAttribute to use. + */ + public synchronized void setAgainst(Quantifier against) { + if (isReference()) { + throw tooManyAttributes(); + } + this.against = against; + } + + /** + * Set the comparison to be used. Default "equal". + * @param when the Comparison EnumeratedAttribute to use. + */ + public synchronized void setWhen(Comparison when) { + if (isReference()) { + throw tooManyAttributes(); + } + this.when = when; + } + + /** + * Create the nested control element. These are the + * resources to compare against. + * @return ResourceCollection. + */ + public synchronized ResourceCollection createControl() { + if (isReference()) { + throw noChildrenAllowed(); + } + if (control != null) { + throw oneControl(); + } + control = new Union(); + return control; + } + + //implement ResourceSelector; inherit doc + public synchronized boolean isSelected(Resource r) { + if (isReference()) { + return ((ResourceSelector) getCheckedRef()).isSelected(r); + } + if (control == null) { + throw oneControl(); + } + int t = 0, f = 0; + for (Iterator it = control.iterator(); it.hasNext();) { + if (when.evaluate(comp.compare(r, (Resource) it.next()))) { + t++; + } else { + f++; + } + } + return against.evaluate(t, f); + } + + /** + * Overrides the version from DataType + * to recurse on nested ResourceComparators. + * @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 (control != null) { + DataType.invokeCircularReferenceCheck(control, stk, p); + } + DataType.invokeCircularReferenceCheck(comp, stk, p); + setChecked(true); + } + } + + private BuildException oneControl() { + return new BuildException(super.toString() + ONE_CONTROL_MESSAGE); + } +} diff --git a/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml b/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml index ebf3d4779..0c890f99f 100755 --- a/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml +++ b/src/resources/org/apache/tools/ant/types/resources/selectors/antlib.xml @@ -1,6 +1,8 @@ + + xmlns:rsel="antlib:org.apache.tools.ant.types.resources.selectors" + xmlns:rcmp="antlib:org.apache.tools.ant.types.resources.comparators"> @@ -99,7 +100,7 @@ - @@ -115,7 +116,7 @@ - + @@ -358,6 +359,68 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + @@ -365,7 +428,7 @@ depends="testand,testor,testnone,testnot,majority" /> + depends="name,testexists,instanceof,testtype,testdate,testsize,testcontains,testcontainsregexp,logical,testcompare" />