Ejemplo n.º 1
0
 /// <summary>Main constructor.</summary>
 public BasicFileFilter()
 {
     this.extensions = null;
     this.reason = 0;
     this.filesOnly = false;
     this.dirsOnly = false;
 }
Ejemplo n.º 2
0
 /// <summary>Main constructor.</summary>
 public BasicFileFilter()
 {
     this.extensions = null;
     this.reason     = 0;
     this.filesOnly  = false;
     this.dirsOnly   = false;
 }
Ejemplo n.º 3
0
        /// <summary>Returns true or false if the file is accepted or not.</summary>
        /// <param name="file">File to verify.</param>
        /// <returns>true or false if the file is accepted or not.</returns>
        public bool Accept(IFile file)
        {
            bool valid = false;

            this.reason = 0;

            // Handle files only
            if (file.IsDirectory && this.filesOnly)
                return false;

            // Handle dirs only
            if (file.IsFile && this.dirsOnly)
                return false;

            // Handle include files
            if (file.IsFile && this.includeFiles != null) {
                valid = false;
                foreach (string fileName in this.includeFiles) {
                    if (fileName == file.Name) {
                        this.reason = FilterReason.InvalidFileName;
                        valid = true;
                        break;
                    }
                }

                if (!valid) {
                    this.reason = FilterReason.InvalidFileName;
                    return false;
                }
            }

            // Handle exclude files
            if (file.IsFile && this.excludeFiles != null) {
                foreach (string fileName in this.excludeFiles) {
                    if (fileName == file.Name)
                        return false;
                }
            }

            // Handle include directories
            if (file.IsDirectory && this.includeFolders != null) {
                valid = false;
                foreach (string dirName in this.includeFolders) {
                    if (dirName == file.Name) {
                        valid = true;
                        break;
                    }
                }

                if (!valid) {
                    this.reason = FilterReason.InvalidFileName;
                    return false;
                }
            }

            // Handle exclude directories
            if (file.IsDirectory && this.excludeFolders != null) {
                foreach (string dirName in this.excludeFolders) {
                    if (dirName == file.Name) {
                        this.reason = FilterReason.InvalidFileName;
                        return false;
                    }
                }
            }

            // Handle include file pattern
            if (file.IsFile && this.includeFilePattern != null) {
                Regex pattern = StringUtils.CreateRegex(this.includeFilePattern);

                if (!pattern.IsMatch(file.Name)) {
                    this.reason = FilterReason.InvalidFileName;
                    return false;
                }
            }

            // Handle exclude file pattern
            if (file.IsFile && this.excludeFilePattern != null) {
                Regex pattern = StringUtils.CreateRegex(this.excludeFilePattern);

                if (pattern.IsMatch(file.Name)) {
                    this.reason = FilterReason.InvalidFileName;
                    return false;
                }
            }

            // Handle include directory pattern
            if (file.IsDirectory && this.includeDirectoryPattern != null) {
                Regex pattern = StringUtils.CreateRegex(this.includeDirectoryPattern);

                if (!pattern.IsMatch(file.Name)) {
                    this.reason = FilterReason.InvalidFileName;
                    return false;
                }
            }

            // Handle exclude directory pattern
            if (file.IsDirectory && this.excludeDirectoryPattern != null) {
                Regex pattern = StringUtils.CreateRegex(this.excludeDirectoryPattern);

                if (file.Name != null && pattern.IsMatch(file.Name)) {
                    this.reason = FilterReason.InvalidFileName;
                    return false;
                }
            }

            // Handle extensions
            if (file.IsFile && this.extensions != null) {
                valid = false;
                foreach (string ext in this.extensions) {
                    if (ext == PathUtils.GetExtension(file.AbsolutePath).ToLower()) {
                        valid = true;
                        break;
                    }
                }

                if (!valid) {
                    this.reason = FilterReason.InvalidExtension;
                    return false;
                }
            }

            // Handle include wildcard pattern
            if (this.includeWildcardPattern != null) {
                string patternStr;

                patternStr = this.includeWildcardPattern;
                patternStr = Regex.Replace(patternStr, @"[*.(){}\?:\-!^$|/\\]", @"\$0");
                patternStr = patternStr.Replace(@"\*", @".*");
                patternStr = "^" + patternStr + "$";

                Regex pattern = new Regex(patternStr, RegexOptions.IgnoreCase);

                if (!pattern.IsMatch(file.Name)){
                    this.reason = FilterReason.InvalidFileName;
                    return false;
                }
            }

            return true;
        }
Ejemplo n.º 4
0
        /// <summary>Returns true or false if the file is accepted or not.</summary>
        /// <param name="file">File to verify.</param>
        /// <returns>true or false if the file is accepted or not.</returns>
        public bool Accept(IFile file)
        {
            bool valid = false;

            this.reason = 0;

            // Handle files only
            if (file.IsDirectory && this.filesOnly)
            {
                return(false);
            }

            // Handle dirs only
            if (file.IsFile && this.dirsOnly)
            {
                return(false);
            }

            // Handle include files
            if (file.IsFile && this.includeFiles != null)
            {
                valid = false;
                foreach (string fileName in this.includeFiles)
                {
                    if (fileName == file.Name)
                    {
                        this.reason = FilterReason.InvalidFileName;
                        valid       = true;
                        break;
                    }
                }

                if (!valid)
                {
                    this.reason = FilterReason.InvalidFileName;
                    return(false);
                }
            }

            // Handle exclude files
            if (file.IsFile && this.excludeFiles != null)
            {
                foreach (string fileName in this.excludeFiles)
                {
                    if (fileName == file.Name)
                    {
                        return(false);
                    }
                }
            }

            // Handle include directories
            if (file.IsDirectory && this.includeFolders != null)
            {
                valid = false;
                foreach (string dirName in this.includeFolders)
                {
                    if (dirName == file.Name)
                    {
                        valid = true;
                        break;
                    }
                }

                if (!valid)
                {
                    this.reason = FilterReason.InvalidFileName;
                    return(false);
                }
            }

            // Handle exclude directories
            if (file.IsDirectory && this.excludeFolders != null)
            {
                foreach (string dirName in this.excludeFolders)
                {
                    if (dirName == file.Name)
                    {
                        this.reason = FilterReason.InvalidFileName;
                        return(false);
                    }
                }
            }

            // Handle include file pattern
            if (file.IsFile && this.includeFilePattern != null)
            {
                Regex pattern = StringUtils.CreateRegex(this.includeFilePattern);

                if (!pattern.IsMatch(file.Name))
                {
                    this.reason = FilterReason.InvalidFileName;
                    return(false);
                }
            }

            // Handle exclude file pattern
            if (file.IsFile && this.excludeFilePattern != null)
            {
                Regex pattern = StringUtils.CreateRegex(this.excludeFilePattern);

                if (pattern.IsMatch(file.Name))
                {
                    this.reason = FilterReason.InvalidFileName;
                    return(false);
                }
            }

            // Handle include directory pattern
            if (file.IsDirectory && this.includeDirectoryPattern != null)
            {
                Regex pattern = StringUtils.CreateRegex(this.includeDirectoryPattern);

                if (!pattern.IsMatch(file.Name))
                {
                    this.reason = FilterReason.InvalidFileName;
                    return(false);
                }
            }

            // Handle exclude directory pattern
            if (file.IsDirectory && this.excludeDirectoryPattern != null)
            {
                Regex pattern = StringUtils.CreateRegex(this.excludeDirectoryPattern);

                if (file.Name != null && pattern.IsMatch(file.Name))
                {
                    this.reason = FilterReason.InvalidFileName;
                    return(false);
                }
            }

            // Handle extensions
            if (file.IsFile && this.extensions != null)
            {
                valid = false;
                foreach (string ext in this.extensions)
                {
                    if (ext == PathUtils.GetExtension(file.AbsolutePath).ToLower())
                    {
                        valid = true;
                        break;
                    }
                }

                if (!valid)
                {
                    this.reason = FilterReason.InvalidExtension;
                    return(false);
                }
            }

            // Handle include wildcard pattern
            if (this.includeWildcardPattern != null)
            {
                string patternStr;

                patternStr = this.includeWildcardPattern;
                patternStr = Regex.Replace(patternStr, @"[*.(){}\?:\-!^$|/\\]", @"\$0");
                patternStr = patternStr.Replace(@"\*", @".*");
                patternStr = "^" + patternStr + "$";

                Regex pattern = new Regex(patternStr, RegexOptions.IgnoreCase);

                if (!pattern.IsMatch(file.Name))
                {
                    this.reason = FilterReason.InvalidFileName;
                    return(false);
                }
            }

            return(true);
        }