KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > SPartFileFilter


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10
11 package org.mmbase.util;
12
13 import java.io.File JavaDoc;
14 import java.io.FilenameFilter JavaDoc;
15
16 /**
17  * Support utility for MMObjectBuilder.getSMartPath
18  * This filter filters files with the specified
19  * number in its name.
20  *
21  * @todo move this code to a SmartPathFunction class?
22  * @author Wilbert Hengst
23  * @version $Id: SPartFileFilter.java,v 1.6 2004/09/30 17:19:49 pierre Exp $
24  */

25 public class SPartFileFilter implements FilenameFilter JavaDoc {
26
27     /**
28      * The number to check on.
29      * Note: Should be a number, but this is not enforced.
30      */

31     private String JavaDoc nodeNumber;
32
33     /**
34      * Creates the file filter.
35      * @param nodeNumber the number to filter on.
36      */

37     public SPartFileFilter(String JavaDoc nodeNumber) {
38         this.nodeNumber = nodeNumber;
39     }
40
41     /**
42      * Checks whether a file has the node number in its file path.
43      * This checks on the exact number - so if the number to search on is '100',
44      * If the path contains a number such as '1001' or '1100' it will return <code>false</code>.
45      * @param dir The directory as a File (unused in this filter)
46      * @param name The file name to check
47      * @return <code>true</code> if the number is in the path, <code>false</code> otherwise.
48      */

49     public boolean accept(File JavaDoc dir, String JavaDoc name) {
50         int pos = name.indexOf(nodeNumber);
51         if (pos<0) return false;
52         // Check char before found number, if digit return false
53
int c;
54         if (pos>0) {
55             c = name.charAt(pos-1);
56             if ((c>='0') && (c<='9')) return false;
57         }
58         // Check char after found number, if digit return false
59
pos+=nodeNumber.length();
60         if (pos<name.length()) {
61             c = name.charAt(pos);
62             if ((c>='0') && (c<='9')) return false;
63         }
64         return true;
65     }
66 }
67
Popular Tags