Uploaded image for project: 'Commons VFS'
  1. Commons VFS
  2. VFS-400

Add a FileSelector based on regular expressions

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.1
    • 2.1
    • 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;
          }
      }
      

      Attachments

        1. FileRegexSelector.java.patch
          3 kB
          Rikard Oxenstrand

        Activity

          People

            Unassigned Unassigned
            rikard.oxenstrand Rikard Oxenstrand
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: