You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2012/02/09 23:36:45 UTC

svn commit: r1242575 - in /commons/proper/vfs/trunk/core/src: main/java/org/apache/commons/vfs2/PatternFileSelector.java test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java

Author: ggregory
Date: Thu Feb  9 22:36:45 2012
New Revision: 1242575

URL: http://svn.apache.org/viewvc?rev=1242575&view=rev
Log:
[VFS-400] Add a FileSelector based on regular expressions.

Added:
    commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java
    commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java

Added: commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java?rev=1242575&view=auto
==============================================================================
--- commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java (added)
+++ commons/proper/vfs/trunk/core/src/main/java/org/apache/commons/vfs2/PatternFileSelector.java Thu Feb  9 22:36:45 2012
@@ -0,0 +1,101 @@
+/*
+ * 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.commons.vfs2;
+
+import java.util.regex.Pattern;
+
+/**
+ * A {@link FileSelector} that selects based on regular expressions matched against base filename.
+ * 
+ * @since 2.1
+ */
+public class PatternFileSelector implements FileSelector
+{
+
+    /**
+     * The extensions to select.
+     */
+    private final Pattern pattern;
+
+    /**
+     * Creates a new selector for the given pattern.
+     * 
+     * @param pattern
+     *            The regular expressed used by this selector.
+     */
+    public PatternFileSelector(Pattern pattern)
+    {
+        this.pattern = pattern;
+    }
+
+    /**
+     * Creates a new selector for the given pattern.
+     * 
+     * @param regex
+     *            The regular expressed used by this selector.
+     */
+    public PatternFileSelector(String regex)
+    {
+        this(Pattern.compile(regex));
+    }
+
+    /**
+     * Creates a new selector for the given Pattern and flags.
+     * 
+     * @param regex
+     *            The expression to be compiled
+     * 
+     * @param flags
+     *            Match flags, a bit mask.
+     * 
+     * @see Pattern#compile(String, int)
+     */
+    public PatternFileSelector(String regex, int flags)
+    {
+        this(Pattern.compile(regex, flags));
+    }
+
+    /**
+     * Determines if a file or folder should be selected.
+     * 
+     * @param fileInfo
+     *            The file selection information.
+     * @return true if the file should be selected, false otherwise.
+     */
+    public boolean includeFile(final FileSelectInfo fileInfo)
+    {
+        return this.pattern.matcher(fileInfo.getFile().getName().getPath()).matches();
+    }
+
+    @Override
+    public String toString()
+    {
+        return this.pattern.toString();
+    }
+
+    /**
+     * Determines whether a folder should be traversed.
+     * 
+     * @param fileInfo
+     *            The file selection information.
+     * @return true if descendents should be traversed, false otherwise.
+     */
+    public boolean traverseDescendents(final FileSelectInfo fileInfo)
+    {
+        return true;
+    }
+}

Added: commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java?rev=1242575&view=auto
==============================================================================
--- commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java (added)
+++ commons/proper/vfs/trunk/core/src/test/java/org/apache/commons/vfs2/PatternFileSelectorTest.java Thu Feb  9 22:36:45 2012
@@ -0,0 +1,142 @@
+/*
+ * 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.commons.vfs2;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Tests FileExtensionSelector.
+ * 
+ * @since 2.1
+ */
+public class PatternFileSelectorTest
+{
+    private static FileObject BaseFolder;
+
+    /**
+     * 9 files and 1 directory = 10
+     */
+    private static final int EntryCount = 10;
+
+    private static final int ExtensionCount = 3;
+
+    private static final int FilesPerExtensionCount = 3;
+
+    /**
+     * Creates a RAM FS.
+     * 
+     * @throws Exception
+     */
+    @BeforeClass
+    public static void setUpClass() throws Exception
+    {
+        BaseFolder = VFS.getManager().resolveFile("ram://" + PatternFileSelectorTest.class.getName());
+        BaseFolder.deleteAll();
+        BaseFolder.createFolder();
+        BaseFolder.resolveFile("a.htm").createFile();
+        BaseFolder.resolveFile("a.html").createFile();
+        BaseFolder.resolveFile("a.xhtml").createFile();
+        BaseFolder.resolveFile("b.htm").createFile();
+        BaseFolder.resolveFile("b.html").createFile();
+        BaseFolder.resolveFile("b.xhtml").createFile();
+        BaseFolder.resolveFile("c.htm").createFile();
+        BaseFolder.resolveFile("c.html").createFile();
+        BaseFolder.resolveFile("c.xhtml").createFile();
+    }
+
+    /**
+     * Deletes RAM FS files.
+     * 
+     * @throws Exception
+     */
+    @AfterClass
+    public static void tearDownClass() throws Exception
+    {
+        if (BaseFolder != null)
+        {
+            BaseFolder.deleteAll();
+        }
+    }
+
+    /**
+     * Tests a null selector.
+     * 
+     * @throws Exception
+     */
+    @Test(expected = NullPointerException.class)
+    public void testNullString() throws Exception
+    {
+        // Yep, this will blow up.
+        new PatternFileSelector((String) null);
+    }
+
+    /**
+     * Tests matching all
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testMatchAll() throws Exception
+    {
+        final FileObject[] list = BaseFolder.findFiles(new PatternFileSelector(".*"));
+        Assert.assertEquals(EntryCount, list.length);
+    }
+
+    /**
+     * Tests a one extension selector.
+     * 
+     * @throws Exception
+     */
+    @Test
+    public void testFileExtensions() throws Exception
+    {
+        final FileObject[] foArray = BaseFolder.findFiles(Selectors.SELECT_FILES);
+        Assert.assertTrue(foArray.length > 0);
+        final String regExPrefix = ".*\\.";
+        // gather file extensions.
+        final Set<String> extensionSet = new HashSet<String>();
+        for (FileObject fo : foArray)
+        {
+            extensionSet.add(regExPrefix + fo.getName().getExtension());
+        }
+        final String message = String.format("Extensions: %s; files: %s", extensionSet.toString(),
+                Arrays.asList(foArray).toString());
+        Assert.assertEquals(message, ExtensionCount, extensionSet.size());
+        // check each extension
+        for (String extension : extensionSet)
+        {
+            final FileSelector selector = new PatternFileSelector(extension);
+            final FileObject[] list = BaseFolder.findFiles(selector);
+            Assert.assertEquals(FilesPerExtensionCount, list.length);
+        }
+        // check each file against itself
+        for (FileObject fo : foArray)
+        {
+            final FileSelector selector = new PatternFileSelector(regExPrefix + fo.getName().getExtension());
+            final FileObject[] list = BaseFolder.findFiles(selector);
+            Assert.assertEquals(FilesPerExtensionCount, list.length);
+        }
+    }
+
+}