at.spardat.xma.boot.IniFileReader Maven / Gradle / Ivy
/*******************************************************************************
* Copyright (c) 2003, 2007 s IT Solutions AT Spardat GmbH .
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* s IT Solutions AT Spardat GmbH - initial API and implementation
*******************************************************************************/
package at.spardat.xma.boot;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/**
* Helper class to read a ini style file.
* Sections and keys are treated case sensitive.
* Lines beginning with ; or # are treated as comment.
* Used by Launcher to read the XmaLauncherServlet response file.
*
* @author s3460
* @since version_number
*/
class IniFileReader{
private Map sections = new LinkedHashMap();
String path;
/**
* constructor reads already complete file
* @param filePath
*/
public IniFileReader(String filePath) {
BufferedReader reader = null;
path = filePath;
try {
reader = new BufferedReader(new FileReader(filePath));
initialize(reader);
} catch (IOException e) {
throw new RuntimeException(e);
} finally{
if(reader != null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
*
* @param section - section without []
* @param key
* @return
* @since version_number
* @author s3460
*/
public String getProperty(String section, String key) {
Map map = getSection(section);
if (map != null) {
String value = (String) map.get(key);
return value;
} else{
return null;
}
}
/**
*
* @param section - section without []
* @param key
* @return
* @since version_number
* @author s3460
*/
public String getProperty(String section, String key, String defaultValue) {
String value = getProperty(section, key);
if(value!=null){
return value;
}else{
return defaultValue;
}
}
/**
*
* @param section - section without []
* @return
* @since version_number
* @author s3460
*/
public Map getSection(String section) {
return (Map) sections.get(section);
}
/**
*
* @return all available sections
* @since version_number
* @author s3460
*/
public String[] getSectionNames(){
String[] names = new String[sections.size()];
int i = 0;
for (Iterator iter = sections.keySet().iterator(); iter.hasNext(); i++) {
String element = (String) iter.next();
names[i] = element;
}
return names;
}
/**
* reads already complete file and stores values in map
* @param r
* @throws IOException
* @since version_number
* @author s3460
*/
private void initialize(BufferedReader r) throws IOException {
String section = null, line;
while ((line = r.readLine()) != null) {
line = line.trim();
if (line.equals("") || line.startsWith(";") || line.startsWith("#")) {
continue;
}
if (line.startsWith("[") && line.endsWith("]")) {
section = line.substring(1, line.length() - 1);
} else if (section == null) {
throw new RuntimeException("Error reading " + path + " [section] header expected");
} else {
int index = line.indexOf('=');
if (index < 0) {
throw new RuntimeException("Error reading " + path + " key/value pair without = ; see line: " + line);
}
String key = line.substring(0, index).trim();
String value = line.substring(index + 1).trim();
Map map = (Map) sections.get(section);
if (map == null) {
sections.put(section, (map = new LinkedHashMap()));
}
map.put(key, value);
}
}
}
}