private FilesystemIteratorRule getBestRule ( final String filename ) { String longestPrefix = null ; FilesystemIteratorRule rule = null ; // First search the path and all of its parents for the first regular rule String path = filename ; while ( true ) { // Check the current path for an exact match //System.out.println("DEBUG: Checking "+path); rule = rules . get ( path ) ; if ( rule != null ) { longestPrefix = path ; break ; } // If done, break the loop int pathLen = path . length ( ) ; if ( pathLen == 0 ) break ; int lastSlashPos = path . lastIndexOf ( File . separatorChar ) ; if ( lastSlashPos == - 1 ) { path = "" ; } else if ( lastSlashPos == ( pathLen - 1 ) ) { // If ends with a slash, remove that slash path = path . substring ( 0 , lastSlashPos ) ; } else { // Otherwise, remove and leave the last slash path = path . substring ( 0 , lastSlashPos + 1 ) ; } } if ( prefixRules != null ) { // TODO: If there are many more prefix rules than the length of this filename, it will at some threshold // be faster to do a map lookup for each possible length of the string. // Would also only need to look down to longestPrefix for ( Map . Entry < String , FilesystemIteratorRule > entry : prefixRules . entrySet ( ) ) { String prefix = entry . getKey ( ) ; if ( ( longestPrefix == null || prefix . length ( ) > longestPrefix . length ( ) ) && filename . startsWith ( prefix ) ) { //System.err.println("DEBUG: FilesystemIterator: getBestRule: filename="+filename+", prefix="+prefix+", longestPrefix="+longestPrefix); longestPrefix = prefix ; rule = entry . getValue ( ) ; } } } return rule ; }
Gets the rule that best suits the provided filename . The rule is the longer rule between the regular rules and the prefix rules .