KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > ixenon > free > nodes > SimpleFilenameFilter


1 /* $Id$
2  *
3  * Copyright (c) 1999 Xenonsoft Limited. All Rights Reserved.
4  *
5  * This software is protected by copyright. You are hereby notified from
6  * now by reading this message. This software is also the confidential
7  * and proprietary information of Xenonsoft Limited. ("Confidential
8  * Information").
9  *
10  * This software is distributed under the Xenonsoft Public end user
11  * License ("XPeL"), where the machine-readable source code is provided
12  * under the "Open Source" model.
13  * For more information, please read the file "LICENSE-XPL.txt"
14  */

15
16 // SimpleFilenameFilter.java
17

18 // Description:
19
// Installer frame for free installer application
20
//
21
// Author:
22
// Peter Pilgrim
23
// Mon Jan 11 23:50:46 GMT 1999
24
//
25
// RCS HEADER
26
//
27
// $Author$
28
// $Date$
29
// $Source$
30
// $Revision$ $State$ $Locker$
31
//
32
// History
33
// ================================================================================
34
// $Log$
35

36 package ixenon.free.nodes;
37
38 import java.io.*;
39 import java.util.*;
40
41 public class SimpleFilenameFilter implements FilenameFilter
42 {
43     /** enumerated value which denotes matching the beginning
44      * of a filename string */

45     public final static int MATCH_BEGINNING = 1;
46
47     /** enumerated value which denotes matching the middle
48      * of a filename string */

49     public final static int MATCH_MIDDLE = 2;
50
51     /** enumerated value which denotes matching the end
52      * of a filename string */

53     public final static int MATCH_END = 3;
54     
55
56     // Matching list of strings
57
private int matchType;
58     private Vector matchList;
59
60     /**
61      * Create a simple filename filter with one match
62      * @param matchString the single match string
63      * Matches the end of the filename string by default.
64      */

65     public SimpleFilenameFilter( String JavaDoc matchString )
66     {
67     this( matchString, MATCH_END );
68     }
69
70     /**
71      * Create a simple filename filter with one match
72      * @param matchString the single match string
73      * @param matchType how to perform a match
74      *
75      * @see MATCH_BEGINNING
76      * @see MATCH_MIDDLE
77      * @see MATCH_END
78      */

79     public SimpleFilenameFilter( String JavaDoc matchString, int matchType )
80     {
81     this( new String JavaDoc [] { matchString }, matchType );
82     }
83     
84     /**
85      * Create a simple filename filter with one match
86      * @param matches the list of matching string
87      * Matches the end of the filename string by default.
88      */

89     public SimpleFilenameFilter( String JavaDoc [] matches )
90     {
91     this( matches, MATCH_END );
92     }
93
94     /**
95      * Create a simple filename filter with one match
96      * @param matches the list of matching string
97      * @param matchType how to perform a match
98      *
99      * @see MATCH_BEGINNING
100      * @see MATCH_MIDDLE
101      * @see MATCH_END
102      */

103     public SimpleFilenameFilter( String JavaDoc [] matches, int matchType ) {
104     this.matchType = matchType;
105     matchList = new Vector();
106     
107     for (int k=0; k<matches.length; ++k)
108         if ( matches[k] != null )
109         matchList.addElement( matches[k] );
110     }
111     
112     /** The <B>accept</B> method which determine if filename
113      * is reasonable or not.
114      * @param dir the <code>File</code> representing the directory
115      * @param name the filename
116      */

117     public boolean accept(File dir, String JavaDoc name) {
118     File file = new File( dir, name );
119     if (!file.isFile() )
120         return (false);
121
122     for (int k=0; k<matchList.size(); ++k) {
123         String JavaDoc matchString = (String JavaDoc)matchList.elementAt(k);
124         switch (matchType) {
125           case MATCH_BEGINNING:
126           // System.out.println( "filename:`"+ file.getPath() + "' " + ( name.startsWith(matchString) ? "*MATCH BEGIN*":"skip" ));
127
if (name.startsWith(matchString))
128               return (true);
129           
130           case MATCH_MIDDLE:
131           // System.out.println( "filename:`"+ file.getPath() + "' " + ( name.indexOf(matchString)>=0 ? "*MATCH MIDDLE*":"skip" ));
132
if (name.indexOf(matchString) >= 0)
133               return (true);
134           
135           case MATCH_END:
136           // System.out.println( "filename:`"+ file.getPath() + "' " + ( name.endsWith(matchString) ? "*MATCH END*":"skip" ));
137
if (name.endsWith(matchString))
138               return (true);
139         }
140     }
141     
142     return (false);
143     }
144 }
145
146 // fini
147
Popular Tags