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

          People

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

            Dates

            • Created:
              Updated:
              Resolved:

              Development