Source Code Cross Referenced for WildcardFileFilter.java in  » Library » apache-common-IO » org » apache » commons » io » filefilter » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » apache common IO » org.apache.commons.io.filefilter 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.io.filefilter;
018:
019:        import java.io.File;
020:        import java.util.List;
021:
022:        import org.apache.commons.io.FilenameUtils;
023:        import org.apache.commons.io.IOCase;
024:
025:        /**
026:         * Filters files using the supplied wildcards.
027:         * <p>
028:         * This filter selects files and directories based on one or more wildcards.
029:         * Testing is case-sensitive by default, but this can be configured.
030:         * <p>
031:         * The wildcard matcher uses the characters '?' and '*' to represent a
032:         * single or multiple wildcard characters.
033:         * This is the same as often found on Dos/Unix command lines.
034:         * The extension check is case-sensitive by .
035:         * See {@link FilenameUtils#wildcardMatchOnSystem} for more information.
036:         * <p>
037:         * For example:
038:         * <pre>
039:         * File dir = new File(".");
040:         * FileFilter fileFilter = new WildcardFileFilter("*test*.java~*~");
041:         * File[] files = dir.listFiles(fileFilter);
042:         * for (int i = 0; i < files.length; i++) {
043:         *   System.out.println(files[i]);
044:         * }
045:         * </pre>
046:         *
047:         * @author Jason Anderson
048:         * @version $Revision: 155419 $ $Date: 2006-08-28 13:57:00 +0200 (Mo, 28 Aug 2006) $
049:         * @since Commons IO 1.3
050:         */
051:        public class WildcardFileFilter extends AbstractFileFilter {
052:
053:            /** The wildcards that will be used to match filenames. */
054:            private String[] wildcards;
055:            /** Whether the comparison is case sensitive. */
056:            private IOCase caseSensitivity;
057:
058:            /**
059:             * Construct a new case-sensitive wildcard filter for a single wildcard.
060:             *
061:             * @param wildcard  the wildcard to match
062:             * @throws IllegalArgumentException if the pattern is null
063:             */
064:            public WildcardFileFilter(String wildcard) {
065:                this (wildcard, null);
066:            }
067:
068:            /**
069:             * Construct a new wildcard filter for a single wildcard specifying case-sensitivity.
070:             *
071:             * @param wildcard  the wildcard to match, not null
072:             * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
073:             * @throws IllegalArgumentException if the pattern is null
074:             */
075:            public WildcardFileFilter(String wildcard, IOCase caseSensitivity) {
076:                if (wildcard == null) {
077:                    throw new IllegalArgumentException(
078:                            "The wildcard must not be null");
079:                }
080:                this .wildcards = new String[] { wildcard };
081:                this .caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE
082:                        : caseSensitivity);
083:            }
084:
085:            /**
086:             * Construct a new case-sensitive wildcard filter for an array of wildcards.
087:             * <p>
088:             * The array is not cloned, so could be changed after constructing the
089:             * instance. This would be inadvisable however.
090:             *
091:             * @param wildcards  the array of wildcards to match
092:             * @throws IllegalArgumentException if the pattern array is null
093:             */
094:            public WildcardFileFilter(String[] wildcards) {
095:                this (wildcards, null);
096:            }
097:
098:            /**
099:             * Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.
100:             * <p>
101:             * The array is not cloned, so could be changed after constructing the
102:             * instance. This would be inadvisable however.
103:             *
104:             * @param wildcards  the array of wildcards to match, not null
105:             * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
106:             * @throws IllegalArgumentException if the pattern array is null
107:             */
108:            public WildcardFileFilter(String[] wildcards, IOCase caseSensitivity) {
109:                if (wildcards == null) {
110:                    throw new IllegalArgumentException(
111:                            "The wildcard array must not be null");
112:                }
113:                this .wildcards = wildcards;
114:                this .caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE
115:                        : caseSensitivity);
116:            }
117:
118:            /**
119:             * Construct a new case-sensitive wildcard filter for a list of wildcards.
120:             *
121:             * @param wildcards  the list of wildcards to match, not null
122:             * @throws IllegalArgumentException if the pattern list is null
123:             * @throws ClassCastException if the list does not contain Strings
124:             */
125:            public WildcardFileFilter(List wildcards) {
126:                this (wildcards, null);
127:            }
128:
129:            /**
130:             * Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
131:             *
132:             * @param wildcards  the list of wildcards to match, not null
133:             * @param caseSensitivity  how to handle case sensitivity, null means case-sensitive
134:             * @throws IllegalArgumentException if the pattern list is null
135:             * @throws ClassCastException if the list does not contain Strings
136:             */
137:            public WildcardFileFilter(List wildcards, IOCase caseSensitivity) {
138:                if (wildcards == null) {
139:                    throw new IllegalArgumentException(
140:                            "The wildcard list must not be null");
141:                }
142:                this .wildcards = (String[]) wildcards
143:                        .toArray(new String[wildcards.size()]);
144:                this .caseSensitivity = (caseSensitivity == null ? IOCase.SENSITIVE
145:                        : caseSensitivity);
146:            }
147:
148:            //-----------------------------------------------------------------------
149:            /**
150:             * Checks to see if the filename matches one of the wildcards.
151:             *
152:             * @param dir  the file directory
153:             * @param name  the filename
154:             * @return true if the filename matches one of the wildcards
155:             */
156:            public boolean accept(File dir, String name) {
157:                for (int i = 0; i < wildcards.length; i++) {
158:                    if (FilenameUtils.wildcardMatch(name, wildcards[i],
159:                            caseSensitivity)) {
160:                        return true;
161:                    }
162:                }
163:                return false;
164:            }
165:
166:            /**
167:             * Checks to see if the filename matches one of the wildcards.
168:             *
169:             * @param file  the file to check
170:             * @return true if the filename matches one of the wildcards
171:             */
172:            public boolean accept(File file) {
173:                String name = file.getName();
174:                for (int i = 0; i < wildcards.length; i++) {
175:                    if (FilenameUtils.wildcardMatch(name, wildcards[i],
176:                            caseSensitivity)) {
177:                        return true;
178:                    }
179:                }
180:                return false;
181:            }
182:
183:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.