/*
* Copyright (C) 2001, 2002 Robert MacGrogan
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
* $Archive: SourceJammer$
* $FileName: HistoryTypeMapper.java$
* $FileID: 3956$
*
* Last change:
* $AuthorName: Rob MacGrogan$
* $Date: 8/12/03 12:04 AM$
* $Comment: Allow modification and saving of mappings.$
*/
package org.sourcejammer.client;
import org.sourcejammer.util.AppConfig;
import java.util.Properties;
import java.util.Hashtable;
import java.io.FileInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.StringTokenizer;
import java.util.Vector;
import java.util.Iterator;
/**
* Title: $FileName: HistoryTypeMapper.java$<br>
* @author $AuthorName: Rob MacGrogan$<br>
* @version $VerNum: 4$<br><br>
*
* $Description: $<br>
* $KeyWordsOff: $
*/
public class HistoryTypeMapper {
public static final String FILE_HIST_PROPS_NAME = "filehist.props";
public static final String TEXT_LIST = "text";
public static final String BIN_COMPRESSED_LIST = "bin_comp";
public static final String BIN_FULL_LIST = "bin_full";
public static final String BIN_DIFF_LIST = "bin_diff";
public static final String BIN_CURR_ONLY_LIST = "bin_curr_only";
public static final String IGNORE_LIST = "ignore";
private static final String DEFAULT = "default";
private static final String SEPARATOR = " ";
public static final String NO_EXT_VALUE = "[no-ext]";
private Hashtable fileFilterList = new Hashtable();
private HashMap ignoreList = new HashMap();
private String defaultType = BIN_COMPRESSED_LIST;
private static HistoryTypeMapper instance = null;
private HistoryTypeMapper()
throws IOException{
String confDir = AppConfig.getInstance().getConfigFilePath();
File flProps = new File(confDir, FILE_HIST_PROPS_NAME);
FileInputStream inStr = new FileInputStream(flProps);
Properties props = new Properties();
try{
props.load(inStr);
}
finally{
inStr.close();
}
//First, we set a default type for no extension.
fileFilterList.put(NO_EXT_VALUE, TEXT_LIST);
//We need to build a hashtable for each filter as filter/type.
addListToHashtable(TEXT_LIST, props);
addListToHashtable(BIN_COMPRESSED_LIST, props);
addListToHashtable(BIN_CURR_ONLY_LIST, props);
addListToHashtable(BIN_DIFF_LIST, props);
addListToHashtable(BIN_FULL_LIST, props);
addListToHashtable(IGNORE_LIST, props);
String defType = props.getProperty(DEFAULT);
if (defType != null){
defaultType = defType;
}
}
public static HistoryTypeMapper getInstance(){
return instance;
}
public static HistoryTypeMapper initializeInstance()
throws IOException{
instance = new HistoryTypeMapper();
return instance;
}
public HashMap getIgnoreList(){
return ignoreList;
}
/**
* Return default file type/storage type for file.
*/
public String getDefaultHistoryTypeForFile(String fileName){
String ext = getExtension(fileName);
//Set to lower case to match how extensions are stored.
ext = ext.toLowerCase();
String type = null;
if (ext != null){
type = (String)fileFilterList.get(ext);
}
if (type == null){
type = defaultType;
}
return type;
}
public static String getExtension(String fileName){
String ext = null;
int iDotLocation = fileName.lastIndexOf(".");
if (iDotLocation >= 0){
ext = fileName.substring(iDotLocation + 1);
}
else{
ext = NO_EXT_VALUE;
}
return ext;
}
/**
* Must be a hashtable of objects where toString() is space separated string of filters.
*/
public void setFiltersForAllTypes(Hashtable allFilters){
//Dump old values.
fileFilterList = new Hashtable();
ignoreList = new HashMap();
Enumeration enm = allFilters.keys();
while(enm.hasMoreElements()){
String type = (String)enm.nextElement();
String list = allFilters.get(type).toString();
Iterator itrList = parseList(list);
addFilters(itrList, type);
}
}
/**
* Inefficient. Use getFiltersForAllTypes() insead.
*/
public String getFilterList(String type){
StringBuffer str = new StringBuffer();
if (type.equals(IGNORE_LIST)){
Iterator ignore = ignoreList.keySet().iterator();
boolean firstPass = true;
while(ignore.hasNext()){
String filter = (String)ignore.next();
if(! firstPass){
str.append(SEPARATOR);
}
firstPass = false;
str.append(filter);
}
}
else{
Enumeration allFilters = fileFilterList.keys();
boolean firstPass = true;
while (allFilters.hasMoreElements()){
String filter = (String)allFilters.nextElement();
String histType = (String)fileFilterList.get(filter);
if (histType.equals(type)){
if(! firstPass){
str.append(SEPARATOR);
}
str.append(filter);
}
}
}
return str.toString();
}
private void addListToHashtable(String listIdentifier, Properties props){
String list = props.getProperty(listIdentifier);
if (list != null){
//Parse list and add to hashtable.
Iterator itr = parseList(list);
addFilters(itr, listIdentifier);
}
}
/**
* Parse space separated list to Iterator.
*/
private Iterator parseList(String list){
StringTokenizer tokenizer = new StringTokenizer(list, SEPARATOR, false);
Vector vec = new Vector();
while (tokenizer.hasMoreTokens()){
String s = tokenizer.nextToken();
vec.addElement(s);
}//end while
return vec.iterator();
}
private void addFilters(Iterator itr, String histType){
while(itr.hasNext()){
String s = (String)itr.next();
//Store all as lower case.
s = s.toLowerCase();
fileFilterList.put(s, histType);
if (histType == IGNORE_LIST){
ignoreList.put(s, "placeholder");
}
}
}
public void save() throws IOException{
Properties props = addHashtableValuesToProperties();
//Add default type:
props.setProperty(DEFAULT, defaultType);
String confDir = AppConfig.getInstance().getConfigFilePath();
File flProps = new File(confDir, FILE_HIST_PROPS_NAME);
FileOutputStream stm = new FileOutputStream(flProps);
try{
props.store(stm, "File History Type File Filters");
}
finally{
stm.close();
}
}
/**
* Hashtable keys are types. Values are StringBuffers of each
* type's filters (extensions).
*/
public Hashtable getFiltersForAllTypes(){
Enumeration allFilters = fileFilterList.keys();
//We have to look up the type of each filter in the hashtable.
Hashtable strBufferTable = new Hashtable();
while (allFilters.hasMoreElements()){
String filter = (String)allFilters.nextElement();
String histType = (String)fileFilterList.get(filter);
StringBuffer str = (StringBuffer)strBufferTable.get(histType);
if (str == null){
str = new StringBuffer();
strBufferTable.put(histType, str);
str.append(filter);
}
else{
str.append(SEPARATOR).append(filter);
}
}
//At this point strBufferTable contains a StringBuffer for each hist type.
return strBufferTable;
}
private Properties addHashtableValuesToProperties(){
Hashtable strings = getFiltersForAllTypes();
Enumeration types = strings.keys();
Properties props = new Properties();
while (types.hasMoreElements()){
String type = (String)types.nextElement();
StringBuffer str = (StringBuffer)strings.get(type);
String prop = str.toString();
props.setProperty(type, prop);
}
return props;
}
/**
* Returns the defaultType.
* @return String
*/
public String getDefaultType() {
return defaultType;
}
/**
* Sets the defaultType.
* @param defaultType The defaultType to set
*/
public void setDefaultType(String defaultType) {
this.defaultType = defaultType;
}
}
|