Selector based on regular expressions
-------------------------------------
Key: VFS-400
URL: https://issues.apache.org/jira/browse/VFS-400
Project: Commons VFS
Issue Type: New Feature
Affects Versions: 2.1
Reporter: Rikard Oxenstrand
Priority: Minor
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}
--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:
https://issues.apache.org/jira/secure/ContactAdministrators!default.jspa
For more information on JIRA, see: http://www.atlassian.com/software/jira