/*
function FilenameUtils() {
	var UNIX_SEPARATOR = '/';
	var WINDOWS_SEPARATOR = '\\';
	var EXTENSION_SEPARATOR = '.';

	this.getName = function(filename) {
		if (filename == null) {
			 return null;
		}
		index = indexOfLastSeparator(filename);
		return filename.substring(index + 1);
	 };

	this.indexOfLastSeparator = function(filename) {
		if (filename == null) {
            return -1;
        }
        lastUnixPos = filename.lastIndexOf(UNIX_SEPARATOR);
        lastWindowsPos = filename.lastIndexOf(WINDOWS_SEPARATOR);
        return Math.max(lastUnixPos, lastWindowsPos);
	 };
	 
	 this.getExtension = function(filename) {
        if (filename == null) {
            return null;
        }
        index = indexOfExtension(filename);
        if (index == -1) {
            return "";
        } else {
            return filename.substring(index + 1);
        }
    };
	
	this.getBaseName = function(filename) {
        return removeExtension(getName(filename));
    };
	
	this.removeExtension = function(filename) {
        if (filename == null) {
            return null;
        }
        index = indexOfExtension(filename);
        if (index == -1) {
            return filename;
        } else {
            return filename.substring(0, index);
        }
    };
	
	this.indexOfExtension = function(filename) {
        if (filename == null) {
            return -1;
        }
        extensionPos = filename.lastIndexOf(EXTENSION_SEPARATOR);
        lastSeparator = indexOfLastSeparator(filename);
        return (lastSeparator > extensionPos ? -1 : extensionPos);
    };
	
	this.getPath = function(filename) {
        return doGetPath(filename, 1);
    };
	
	this.getPathNoEndSeparator = function(filename) {
        return doGetPath(filename, 0);
    };
	
	this.doGetPath = function(filename, separatorAdd) {
        if (filename == null) {
            return null;
        }
        prefix = 0;
        index = indexOfLastSeparator(filename);
        if (prefix >= filename.length || index < 0) {
            return "";
        }
        return filename.substring(prefix, index + separatorAdd);
    };

	//
	this.wildcardMatch = function(filename, wildcardMatcher, caseSensitivity) {
        if (filename == null && wildcardMatcher == null) {
            return true;
        }
        if (filename == null || wildcardMatcher == null) {
            return false;
        }
        if (caseSensitivity == null) {
            caseSensitivity = true;
        }
		filename = filename.trim();
		wildcardMatcher = wildcardMatcher.trim();
		if(!caseSensitivity) {
			filename = filename.toLowerCase();
			wildcardMatcher = wildcardMatcher.toLowerCase();
		}
        var wcs = splitOnTokens(wildcardMatcher);
		//alert(wcs.length +"\n" +wcs.join(","));
        var anyChars = false;
        var textIdx = 0;
        var wcsIdx = 0;
        var backtrack = new Stack();
        
        // loop around a backtrack stack, to handle complex * matching
        do {
            if (backtrack.size() > 0) {
                var array = backtrack.pop();
                wcsIdx = array[0];
                textIdx = array[1];
                anyChars = true;
            }
            
            // loop whilst tokens and text left to process
            while (wcsIdx < wcs.length) {
      
                if (wcs[wcsIdx]=="?") {
                    // ? so move to next text char
                    textIdx++;
                    anyChars = false;
                    
                } else if (wcs[wcsIdx]=="*") {
                    // set any chars status
                    anyChars = true;
                    if (wcsIdx == wcs.length-1) {
                        textIdx = filename.length;
                    }
                    
                } else {
                    // matching text token
                    if (anyChars) {
                        // any chars then try to locate text token
                        textIdx = filename.indexOf(wcs[wcsIdx], textIdx);
                        if (textIdx == -1) {
                            // token not found
                            break;
                        }
                        var repeat = filename.indexOf(wcs[wcsIdx], textIdx+1);
                        if (repeat >= 0) {
                            //backtrack.push({wcsIdx, repeat});
							tmp = new Array(wcsIdx,repeat);
                            backtrack.push(tmp);
                        }
                    } else {
                        // matching from current position
                        //if (!filename.startsWith(wcs[wcsIdx], textIdx)) {
                        if (filename.indexOf(wcs[wcsIdx]) != textIdx) {
                            // couldnt match token
                            break;
                        }
                    }
      
                    // matched text token, move text index to end of matched token
                    textIdx += wcs[wcsIdx].length;
                    anyChars = false;
                }
      
                wcsIdx++;
            } //end while
            
            // full match
            if (wcsIdx == wcs.length && textIdx == filename.length) {
                return true;
            }
            
        } while (backtrack.size() > 0);  //end do while
  
        return false;
    };

	splitOnTokens = function(text) {

        if (text.indexOf("?") == -1 && text.indexOf("*") == -1) {
            return new Array(text);
        }

        var arr = text;
        var list = new ArrayList();
		var buffer = new StringBuffer();    
		
        for (i = 0; i < arr.length; i++) {
            if (arr.charAt(i) == '?' || arr.charAt(i) == '*') {
                if (buffer.length() != 0) {
                    list.add(buffer.toString());
                    buffer.clear();
                }
                if (arr.charAt(i) == '?') {
                    list.add("?");
                } else if (list.size() == 0 || (i > 0 && list.get(list.size() - 1) !="*")) {
                    list.add("*");
                }
            } else {
                buffer.append(arr.charAt(i));
            }
        }
		if (buffer.length() != 0) {
			list.add(buffer.toString());
		}
		
        return list.toArray();
    };

}
*/

var FilenameUtils = {
	UNIX_SEPARATOR : '/',
	WINDOWS_SEPARATOR : '\\',
	EXTENSION_SEPARATOR : '.',

	getName : function(filename) {
		if (filename == null) {
			 return null;
		}
		index = this.indexOfLastSeparator(filename);
		return filename.substring(index + 1);
	 },

	indexOfLastSeparator : function(filename) {
		if (filename == null) {
            return -1;
        }
        lastUnixPos = filename.lastIndexOf(this.UNIX_SEPARATOR);
        lastWindowsPos = filename.lastIndexOf(this.WINDOWS_SEPARATOR);
        return Math.max(lastUnixPos, lastWindowsPos);
	 },
	 
	 getExtension : function(filename) {
        if (filename == null) {
            return null;
        }
        index = this.indexOfExtension(filename);
        if (index == -1) {
            return "";
        } else {
            return filename.substring(index + 1);
        }
    },
	
	getBaseName : function(filename) {
        return removeExtension(this.getName(filename));
    },
	
	removeExtension : function(filename) {
        if (filename == null) {
            return null;
        }
        index = this.indexOfExtension(filename);
        if (index == -1) {
            return filename;
        } else {
            return filename.substring(0, index);
        }
    },
	
	indexOfExtension : function(filename) {
        if (filename == null) {
            return -1;
        }
        extensionPos = filename.lastIndexOf(this.EXTENSION_SEPARATOR);
        lastSeparator = this.indexOfLastSeparator(filename);
        return (lastSeparator > extensionPos ? -1 : extensionPos);
    },
	
	getPath : function(filename) {
        return this.doGetPath(filename, 1);
    },
	
	getPathNoEndSeparator : function(filename) {
        return this.doGetPath(filename, 0);
    },
	
	doGetPath : function(filename, separatorAdd) {
        if (filename == null) {
            return null;
        }
        prefix = 0;
        index = this.indexOfLastSeparator(filename);
        if (prefix >= filename.length || index < 0) {
            return "";
        }
        return filename.substring(prefix, index + separatorAdd);
    },

	wildcardMatch : function(filename, wildcardMatcher, caseSensitivity) {
        if (filename == null && wildcardMatcher == null) {
            return true;
        }
        if (filename == null || wildcardMatcher == null) {
            return false;
        }
        if (caseSensitivity == null) {
            caseSensitivity = true;
        }
		filename = filename.trim();
		wildcardMatcher = wildcardMatcher.trim();
		if(!caseSensitivity) {
			filename = filename.toLowerCase();
			wildcardMatcher = wildcardMatcher.toLowerCase();
		}
        var wcs = this.splitOnTokens(wildcardMatcher);
		//alert(wcs.length +"\n" +wcs.join(","));
        var anyChars = false;
        var textIdx = 0;
        var wcsIdx = 0;
        var backtrack = new Stack();
        
        // loop around a backtrack stack, to handle complex * matching
        do {
            if (backtrack.size() > 0) {
                var array = backtrack.pop();
                wcsIdx = array[0];
                textIdx = array[1];
                anyChars = true;
            }
            
            // loop whilst tokens and text left to process
            while (wcsIdx < wcs.length) {
      
                if (wcs[wcsIdx]=="?") {
                    // ? so move to next text char
                    textIdx++;
                    anyChars = false;
                    
                } else if (wcs[wcsIdx]=="*") {
                    // set any chars status
                    anyChars = true;
                    if (wcsIdx == wcs.length-1) {
                        textIdx = filename.length;
                    }
                    
                } else {
                    // matching text token
                    if (anyChars) {
                        // any chars then try to locate text token
                        textIdx = filename.indexOf(wcs[wcsIdx], textIdx);
                        if (textIdx == -1) {
                            // token not found
                            break;
                        }
                        var repeat = filename.indexOf(wcs[wcsIdx], textIdx+1);
                        if (repeat >= 0) {
                            //backtrack.push({wcsIdx, repeat});
							tmp = new Array(wcsIdx,repeat);
                            backtrack.push(tmp);
                        }
                    } else {
                        // matching from current position
                        //if (!filename.startsWith(wcs[wcsIdx], textIdx)) {
                        if (filename.indexOf(wcs[wcsIdx]) != textIdx) {
                            // couldnt match token
                            break;
                        }
                    }
      
                    // matched text token, move text index to end of matched token
                    textIdx += wcs[wcsIdx].length;
                    anyChars = false;
                }
      
                wcsIdx++;
            } //end while
            
            // full match
            if (wcsIdx == wcs.length && textIdx == filename.length) {
                return true;
            }
            
        } while (backtrack.size() > 0);  //end do while
  
        return false;
    },

	splitOnTokens : function(text) {

        if (text.indexOf("?") == -1 && text.indexOf("*") == -1) {
            return new Array(text);
        }

        var arr = text;
        var list = new ArrayList();
		var buffer = new StringBuffer();    
		
        for (i = 0; i < arr.length; i++) {
            if (arr.charAt(i) == '?' || arr.charAt(i) == '*') {
                if (buffer.length() != 0) {
                    list.add(buffer.toString());
                    buffer.clear();
                }
                if (arr.charAt(i) == '?') {
                    list.add("?");
                } else if (list.size() == 0 || (i > 0 && list.get(list.size() - 1) !="*")) {
                    list.add("*");
                }
            } else {
                buffer.append(arr.charAt(i));
            }
        }
		if (buffer.length() != 0) {
			list.add(buffer.toString());
		}
		
        return list.toArray();
    }

}