/**
 * OLAT - Online Learning and Training<br>
 * http://www.olat.org
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); <br>
 * you may not use this file except in compliance with the License.<br>
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing,<br>
 * software distributed under the License is distributed on an "AS IS" BASIS, <br>
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. <br>
 * See the License for the specific language governing permissions and <br>
 * limitations under the License.
 * <p>
 * Copyright (c) 2005-2006 by JGS goodsolutions GmbH, Switzerland<br>
 * http://www.goodsolutions.ch All rights reserved.
 * <p>
 */
package org.olat.core.gui.control.generic.folder;

import java.util.HashSet;
import java.util.Locale;
import java.util.StringTokenizer;

import org.olat.core.gui.translator.Translator;
import org.olat.core.util.Util;

/**
 * Description:<br>
 * <P>
 * Initial Date: 22.06.2006 <br>
 * 
 * @author Felix Jost
 */
public class FolderHelper {

	private static final String FILETYPES = "avi bat bmp css doc docx dvi exe gif gz htm html jpeg jpg log midi mov mp3 mpeg mpg pdf png ppt pptx ps ra ram readme rtf tar tgz txt wav xls xlsx xml xsl zip";
	private static HashSet knownFileTypes;

	static {
		// initialize known filetypes for faster access
		FolderHelper.knownFileTypes = new HashSet();
		StringTokenizer st = new StringTokenizer(FILETYPES, " ");
		while (st.hasMoreElements()) {
			FolderHelper.knownFileTypes.add(st.nextElement());
		}
	}

	/**
	 * Extract the type of file based on suffix.
	 * 
	 * @param filePath
	 * @param locale
	 * @return File type based on file extension.
	 */
	public static String extractFileType(String filePath, Locale locale) {
		int lastDot = filePath.lastIndexOf('.');
		if (lastDot > 0) {
			if (lastDot < filePath.length()) return filePath.substring(lastDot + 1).toLowerCase();
		}
		Translator translator = Util.createPackageTranslator(FolderHelper.class, locale);
		return translator.translate("UnknownFile");
	}

	/**
	 * @param fileType
	 * @return True if filetype is known.
	 */
	public static boolean isKnownFileType(String fileType) {
		return FolderHelper.knownFileTypes.contains(fileType);
	}

}
