From 02e939c261d79e9d1c2df779bfe1b787b63dd55c Mon Sep 17 00:00:00 2001 From: Stefan Bodewig Date: Wed, 13 Aug 2008 15:55:01 +0000 Subject: [PATCH] Add readable and writable selectors. Addresses PR 45081. git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@685593 13f79535-47bb-0310-9956-ffa450edef68 --- WHATSNEW | 5 + docs/manual/CoreTypes/resources.html | 4 + docs/manual/CoreTypes/selectors.html | 22 ++++ .../tools/ant/types/AbstractFileSet.java | 10 ++ .../selectors/AbstractSelectorContainer.java | 8 ++ .../selectors/BaseSelectorContainer.java | 8 ++ .../ant/types/selectors/ReadableSelector.java | 47 ++++++++ .../ant/types/selectors/WritableSelector.java | 47 ++++++++ .../ant/types/resources/selectors/antlib.xml | 4 + .../resources/selectors/readwrite-test.xml | 110 ++++++++++++++++++ .../types/selectors/readwrite-test.xml | 104 +++++++++++++++++ 11 files changed, 369 insertions(+) create mode 100644 src/main/org/apache/tools/ant/types/selectors/ReadableSelector.java create mode 100644 src/main/org/apache/tools/ant/types/selectors/WritableSelector.java create mode 100644 src/tests/antunit/types/resources/selectors/readwrite-test.xml create mode 100644 src/tests/antunit/types/selectors/readwrite-test.xml diff --git a/WHATSNEW b/WHATSNEW index 17c7173b5..2ecbcd468 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -252,6 +252,11 @@ Other changes: not an archive earlier if the file is big. Bugzilla Report 45463. + * New file and resource selectors and have + been added that select file which the current process can read or + write. + Bugzilla Report 45081. + Changes from Ant 1.7.0 TO Ant 1.7.1 ============================================= diff --git a/docs/manual/CoreTypes/resources.html b/docs/manual/CoreTypes/resources.html index 69323d155..06ea92df3 100644 --- a/docs/manual/CoreTypes/resources.html +++ b/docs/manual/CoreTypes/resources.html @@ -487,6 +487,10 @@ platforms. resources whose contents match a particular regular expression.
  • compare - select resources based on comparison to other resources.
  • +
  • readable - + Select files (resources must be files) if they are readable.
  • +
  • writable - + Select files (resources must be files) if they are writable.
  • name

    diff --git a/docs/manual/CoreTypes/selectors.html b/docs/manual/CoreTypes/selectors.html index 03826cf99..edbe3871f 100644 --- a/docs/manual/CoreTypes/selectors.html +++ b/docs/manual/CoreTypes/selectors.html @@ -89,6 +89,10 @@ Use a BSF or JSR 223 scripting language to create your own selector +
  • <readable> - + Select files if they are readable.
  • +
  • <writable> - + Select files if they are writable.
  • @@ -969,6 +973,24 @@ + +

    Readable Selector

    + +

    The <readable> selector selects only files + that are readable. Ant only invokes + java.io.File#canRead so if a file is unreadable + but the Java VM cannot detect this state, this selector will + still select the file.

    + + +

    Writable Selector

    + +

    The <writable> selector selects only files + that are writable. Ant only invokes + java.io.File#canWrite so if a file is unwritable + but the Java VM cannot detect this state, this selector will + still select the file.

    +

    Script Selector

    diff --git a/src/main/org/apache/tools/ant/types/AbstractFileSet.java b/src/main/org/apache/tools/ant/types/AbstractFileSet.java index b80054557..bdb178146 100644 --- a/src/main/org/apache/tools/ant/types/AbstractFileSet.java +++ b/src/main/org/apache/tools/ant/types/AbstractFileSet.java @@ -45,6 +45,8 @@ import org.apache.tools.ant.types.selectors.MajoritySelector; import org.apache.tools.ant.types.selectors.DifferentSelector; import org.apache.tools.ant.types.selectors.SelectorContainer; import org.apache.tools.ant.types.selectors.ContainsRegexpSelector; +import org.apache.tools.ant.types.selectors.ReadableSelector; +import org.apache.tools.ant.types.selectors.WritableSelector; import org.apache.tools.ant.types.selectors.modifiedselector.ModifiedSelector; /** @@ -724,6 +726,14 @@ public abstract class AbstractFileSet extends DataType appendSelector(selector); } + public void addReadable(ReadableSelector r) { + appendSelector(r); + } + + public void addWritable(WritableSelector w) { + appendSelector(w); + } + /** * Add an arbitary selector. * @param selector the FileSelector to add. diff --git a/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java b/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java index c118068f4..f0eae3f13 100644 --- a/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java +++ b/src/main/org/apache/tools/ant/types/selectors/AbstractSelectorContainer.java @@ -276,6 +276,14 @@ public abstract class AbstractSelectorContainer extends DataType appendSelector(selector); } + public void addReadable(ReadableSelector r) { + appendSelector(r); + } + + public void addWritable(WritableSelector w) { + appendSelector(w); + } + /** * add an arbitary selector * @param selector the selector to add diff --git a/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java b/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java index 10a44b9bd..e5f7d539d 100644 --- a/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java +++ b/src/main/org/apache/tools/ant/types/selectors/BaseSelectorContainer.java @@ -301,6 +301,14 @@ public abstract class BaseSelectorContainer extends BaseSelector appendSelector(selector); } + public void addReadable(ReadableSelector r) { + appendSelector(r); + } + + public void addWritable(WritableSelector w) { + appendSelector(w); + } + /** * add an arbitary selector * @param selector the selector to add diff --git a/src/main/org/apache/tools/ant/types/selectors/ReadableSelector.java b/src/main/org/apache/tools/ant/types/selectors/ReadableSelector.java new file mode 100644 index 000000000..3eb02ab3a --- /dev/null +++ b/src/main/org/apache/tools/ant/types/selectors/ReadableSelector.java @@ -0,0 +1,47 @@ +/* + * 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.selectors; + +import java.io.File; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.resources.FileProvider; +import org.apache.tools.ant.types.resources.selectors.ResourceSelector; + +/** + * A selector that selects readable files. + * + *

    Readable is definied in terms of java.io.File#canRead, this + * means the selector will accept any file that exists and is readable + * by the application.

    + * + * @since Ant 1.8.0 + */ +public class ReadableSelector implements FileSelector, ResourceSelector { + + public boolean isSelected(File basedir, String filename, File file) { + return file != null && file.canRead(); + } + + public boolean isSelected(Resource r) { + if (r instanceof FileProvider) { + return isSelected(null, null, ((FileProvider) r).getFile()); + } + return false; + } +} \ No newline at end of file diff --git a/src/main/org/apache/tools/ant/types/selectors/WritableSelector.java b/src/main/org/apache/tools/ant/types/selectors/WritableSelector.java new file mode 100644 index 000000000..4dc1096a2 --- /dev/null +++ b/src/main/org/apache/tools/ant/types/selectors/WritableSelector.java @@ -0,0 +1,47 @@ +/* + * 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.selectors; + +import java.io.File; +import org.apache.tools.ant.types.Resource; +import org.apache.tools.ant.types.resources.FileProvider; +import org.apache.tools.ant.types.resources.selectors.ResourceSelector; + +/** + * A selector that selects writable files. + * + *

    Writable is definied in terms of java.io.File#canWrite, this + * means the selector will accept any file that exists and is + * writable by the application.

    + * + * @since Ant 1.8.0 + */ +public class WritableSelector implements FileSelector, ResourceSelector { + + public boolean isSelected(File basedir, String filename, File file) { + return file != null && file.canWrite(); + } + + public boolean isSelected(Resource r) { + if (r instanceof FileProvider) { + return isSelected(null, null, ((FileProvider) r).getFile()); + } + return false; + } +} \ No newline at end of file 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 115e2fbaa..0d0e50248 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 @@ -42,8 +42,12 @@ classname="org.apache.tools.ant.types.resources.selectors.Not" /> + + diff --git a/src/tests/antunit/types/resources/selectors/readwrite-test.xml b/src/tests/antunit/types/resources/selectors/readwrite-test.xml new file mode 100644 index 000000000..e4d6fda5b --- /dev/null +++ b/src/tests/antunit/types/resources/selectors/readwrite-test.xml @@ -0,0 +1,110 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/tests/antunit/types/selectors/readwrite-test.xml b/src/tests/antunit/types/selectors/readwrite-test.xml new file mode 100644 index 000000000..91bd175ad --- /dev/null +++ b/src/tests/antunit/types/selectors/readwrite-test.xml @@ -0,0 +1,104 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +