/*
jConfigLib - Library for simple File parsing.
Copyright (C) 2010 Martin Milbret
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package jConfigLib.files;
import jFileLib.text.FileReader;
import jFileLib.text.FileWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;
import java.util.Iterator;
import java.util.Map.Entry;
public class KeyFile extends BaseFile
{
public KeyFile()
{
super();
}
public KeyFile(String newFilePath)
{
super(newFilePath);
readFile();
}
public KeyFile(String newFilePath, String newSeparator)
{
super(newFilePath, newSeparator);
readFile();
}
public KeyFile(String newFilePath, String newSeparator, boolean toLowerCase)
{
super(newFilePath, newSeparator);
readFile(toLowerCase);
}
public KeyFile(String newFilePath, String newSeparator, HashMap<String, String> newPairs)
{
super(newFilePath, newSeparator);
setPairs(newPairs);
}
private HashMap<String, String> pairs = new HashMap<String, String>();
public void setPairs(HashMap<String, String> newPairs)
{
if(newPairs != null)
{
pairs = newPairs;
}
}
public HashMap<String, String> getPairs()
{
return pairs;
}
public boolean readFile(String newFilePath)
{
setFilePath(newFilePath);
return readFile();
}
public boolean readFile()
{
return readFile(false);
}
public boolean readFile(boolean toLowerCase)
{
FileReader reader = new FileReader(getFilePath());
List<String> lines = reader.getFileContent();
if(lines == null)
return false;
String key = null;
String value = null;
for(String line : lines)
{
if(line.isEmpty())
continue;
line = removeAllComments(line);
int separatorPosition = line.indexOf(getSeparator());
if(separatorPosition != -1)
{
//addPair(key, value);
key = line.substring(0, separatorPosition).trim();
value = line.substring(separatorPosition + 1, line.length()).trim();
if(toLowerCase)
{
key = key.toLowerCase();
value = value.toLowerCase();
}
pairs.put(key, value);
}
}
return true;
}
public boolean writeFile(String newFilePath)
{
setFilePath(newFilePath);
return writeFile();
}
public boolean writeFile()
{
if(getPairs().entrySet().isEmpty())
{
return false;
}
List<String> lines = new ArrayList<String>();
Set<Entry<String, String> > set = getPairs().entrySet();
Iterator<Map.Entry<String, String> > i = set.iterator();
while(i.hasNext())
{
Map.Entry<String, String> entry = (Map.Entry<String, String>)i.next();
String key = (String)entry.getKey();
String value = (String)entry.getValue();
String line = key + getSeparator() + value;
lines.add(line);
}
FileWriter writer = new FileWriter(getFilePath());
return writer.writeLinesToFile(lines);
}
public String getValue(String key)
{
if(key == null)
{
return null;
}
Set<Entry<String, String>> set = pairs.entrySet();
Iterator<Entry<String, String>> iterator = set.iterator();
while(iterator.hasNext())
{
Entry<String, String> entry = iterator.next();
if(entry.getKey().equalsIgnoreCase(key))
return entry.getValue();
}
return null;
}
public boolean containsKey(String key)
{
Set<Entry<String, String>> set = pairs.entrySet();
Iterator<Entry<String, String>> iterator = set.iterator();
while(iterator.hasNext())
{
Entry<String, String> entry = iterator.next();
if(entry.getKey().equalsIgnoreCase(key))
return true;
}
return false;
}
}