Commons VFS
  1. Commons VFS
  2. VFS-400

Add a FileSelector based on regular expressions

    Details

    • Type: New Feature New Feature
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 2.1
    • Fix Version/s: 2.1
    • Labels:
      None

      Description

      In the long todo list there was a post about adding a file selector based on regular expressions. I had need for that for a specific project so I built a simple class that seems to work. I'm kind of new to open source contribution though so I'm not sure if i should just commit it to trunk. Here is the code:

      FileRegexSelector.java
      /*
       * 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.Matcher;
      import java.util.regex.Pattern;
      
      /**
       * A {@link FileSelector} that selects based on regular expressions matched against base filename.
       * 
       * @since 2.1
       */
      public class FileRegexSelector implements FileSelector
      {
      
          /**
           * The extensions to select.
           */
          private Pattern pattern = null;
      
          /**
           * Creates a new selector for the given extensions.
           * 
           * @param extensions
           *            The extensions to be included by this selector.
           */
          public FileRegexSelector(String regex)
          {
          	this.pattern = Pattern.compile(regex);
          }
      
          /**
           * 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)
          {
              if (this.pattern == null)
              {
                  return false;
              }
          	Matcher matcher = this.pattern.matcher(fileInfo.getFile().getName().getBaseName());
              return matcher.matches();
          }
      
          /**
           * Determines whether a folder should be traversed.
           * 
           * @param fileInfo
           *            The file selection information.
           * @return true if descendents should be traversed, fase otherwise.
           */
          public boolean traverseDescendents(final FileSelectInfo fileInfo)
          {
              return true;
          }
      }
      

        Activity

        Rikard Oxenstrand created issue -
        Rikard Oxenstrand made changes -
        Field Original Value New Value
        Description In the long todo list there was a post about adding a file selector based on regular expressions. I had need for that for a specific project so I built a simple class that seems to work. I'm kind of new to open source contribution though so I'm not sure if i should just commit it to trunk. Here is the code:
        {code:title=Bar.java|borderStyle=solid}
        /*
         * 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.Matcher;
        import java.util.regex.Pattern;

        /**
         * A {@link FileSelector} that selects based on regular expressions matched against base filename.
         *
         * @since 2.1
         */
        public class FileRegexSelector implements FileSelector
        {

            /**
             * The extensions to select.
             */
            private Pattern pattern = null;

            /**
             * Creates a new selector for the given extensions.
             *
             * @param extensions
             * The extensions to be included by this selector.
             */
            public FileRegexSelector(String regex)
            {
             this.pattern = Pattern.compile(regex);
            }

            /**
             * 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)
            {
                if (this.pattern == null)
                {
                    return false;
                }
             Matcher matcher = this.pattern.matcher(fileInfo.getFile().getName().getBaseName());
                return matcher.matches();
            }

            /**
             * Determines whether a folder should be traversed.
             *
             * @param fileInfo
             * The file selection information.
             * @return true if descendents should be traversed, fase otherwise.
             */
            public boolean traverseDescendents(final FileSelectInfo fileInfo)
            {
                return true;
            }
        }
        {code}
        In the long todo list there was a post about adding a file selector based on regular expressions. I had need for that for a specific project so I built a simple class that seems to work. I'm kind of new to open source contribution though so I'm not sure if i should just commit it to trunk. Here is the code:
        {code:title=FileRegexSelector.java|borderStyle=solid}
        /*
         * 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.Matcher;
        import java.util.regex.Pattern;

        /**
         * A {@link FileSelector} that selects based on regular expressions matched against base filename.
         *
         * @since 2.1
         */
        public class FileRegexSelector implements FileSelector
        {

            /**
             * The extensions to select.
             */
            private Pattern pattern = null;

            /**
             * Creates a new selector for the given extensions.
             *
             * @param extensions
             * The extensions to be included by this selector.
             */
            public FileRegexSelector(String regex)
            {
             this.pattern = Pattern.compile(regex);
            }

            /**
             * 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)
            {
                if (this.pattern == null)
                {
                    return false;
                }
             Matcher matcher = this.pattern.matcher(fileInfo.getFile().getName().getBaseName());
                return matcher.matches();
            }

            /**
             * Determines whether a folder should be traversed.
             *
             * @param fileInfo
             * The file selection information.
             * @return true if descendents should be traversed, fase otherwise.
             */
            public boolean traverseDescendents(final FileSelectInfo fileInfo)
            {
                return true;
            }
        }
        {code}
        Hide
        Adrian Crum added a comment -

        Please convert your code to a patch file and attach the patch file to this issue. When attaching your file, be sure to grant the ASF permission to use your code.

        Show
        Adrian Crum added a comment - Please convert your code to a patch file and attach the patch file to this issue. When attaching your file, be sure to grant the ASF permission to use your code.
        Hide
        Rikard Oxenstrand added a comment - - edited

        Draft implementation of the function. Uploaded as attachment.

        Show
        Rikard Oxenstrand added a comment - - edited Draft implementation of the function. Uploaded as attachment.
        Rikard Oxenstrand made changes -
        Attachment FileRegexSelector.java.patch [ 12513421 ]
        Hide
        Ralph Goers added a comment -

        Sorry to be a pain but do you happen to have a unit test that can verify the functionality? That will save us from having to develop one.

        Show
        Ralph Goers added a comment - Sorry to be a pain but do you happen to have a unit test that can verify the functionality? That will save us from having to develop one.
        Hide
        Rikard Oxenstrand added a comment -

        No, I have no experience with unit tests. The class is quite similar to FileExtensionSelector so there is probably not so much work if you copy and modify the FileExtensionSelectorTest class. I don't have the time right now but in the beginning of march I can probably read up and do it.

        Show
        Rikard Oxenstrand added a comment - No, I have no experience with unit tests. The class is quite similar to FileExtensionSelector so there is probably not so much work if you copy and modify the FileExtensionSelectorTest class. I don't have the time right now but in the beginning of march I can probably read up and do it.
        Hide
        Gary Gregory added a comment -

        This impl works on base file names only.

        Why not match against the whole URI string?

        Or, should we have a BaseFileNameRegExFileSelector (as the one supplied) and a RegExFileSelector?

        Show
        Gary Gregory added a comment - This impl works on base file names only. Why not match against the whole URI string? Or, should we have a BaseFileNameRegExFileSelector (as the one supplied) and a RegExFileSelector?
        Hide
        Rikard Oxenstrand added a comment -

        My thought was to use this to filter out files based on some naming standard (Prefixed/suffixed file names) and didn't want to involve directories since the directory/server names could include the filter criteria if you change location of the files. But I guess that it could be useful to filter out certain sub-directories in some scenarios.

        How about using getPath instead (fileInfo.getFile().getName().getPath())? Then you can filter on both directories and files while still being able to move the files to a different location without any need to change the filter expression. The part of the URI that is before the path is already filtered when you resolve the file.

        Show
        Rikard Oxenstrand added a comment - My thought was to use this to filter out files based on some naming standard (Prefixed/suffixed file names) and didn't want to involve directories since the directory/server names could include the filter criteria if you change location of the files. But I guess that it could be useful to filter out certain sub-directories in some scenarios. How about using getPath instead (fileInfo.getFile().getName().getPath())? Then you can filter on both directories and files while still being able to move the files to a different location without any need to change the filter expression. The part of the URI that is before the path is already filtered when you resolve the file.
        Gary Gregory made changes -
        Summary Selector based on regular expressions Add a FileSelector based on regular expressions
        Hide
        Gary Gregory added a comment -

        I committed a first cut called PatternFileSelector with the unit test PatternFileSelectorTest

        Show
        Gary Gregory added a comment - I committed a first cut called PatternFileSelector with the unit test PatternFileSelectorTest
        Gary Gregory made changes -
        Status Open [ 1 ] Resolved [ 5 ]
        Fix Version/s 2.1 [ 12318076 ]
        Resolution Fixed [ 1 ]
        Gary Gregory made changes -
        Comment [ Implemented as org.apache.commons.vfs2.PatternFileSelector. ]
        Transition Time In Source Status Execution Times Last Executer Last Execution Date
        Open Open Resolved Resolved
        100d 6h 32m 1 Gary Gregory 16/May/12 17:05

          People

          • Assignee:
            Unassigned
            Reporter:
            Rikard Oxenstrand
          • Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development