+++ /dev/null
-/**
- * Macaco
- * Copyright (c) 2009-2012 Marco Zanon <info@marcozanon.com>.
- * Released under MIT license (see LICENSE for details).
- */
-
-package com.marcozanon.macaco.configuration;
-
-import com.marcozanon.macaco.MInformation;
-import com.marcozanon.macaco.MObject;
-import com.marcozanon.macaco.text.MText;
-import java.io.FileInputStream;
-import java.io.FileNotFoundException;
-import java.io.InputStreamReader;
-import java.io.IOException;
-import java.io.LineNumberReader;
-import java.io.UnsupportedEncodingException;
-import java.util.LinkedHashMap;
-
-public class MConfiguration extends MObject {
-
- protected LinkedHashMap<String, String> parameters = new LinkedHashMap<String, String>();
-
- /* */
-
- public MConfiguration(String file) throws MFileParsingConfigurationException {
- super();
- //
- this.parseFile(file);
- }
-
- /* Parameters */
-
- protected LinkedHashMap<String, String> getParametersReference() {
- return this.parameters;
- }
-
- /* Strings management */
-
- public void parseFile(String file) throws MFileParsingConfigurationException {
- if (MText.isBlank(file)) {
- throw new IllegalArgumentException("Invalid 'file': null or empty.");
- }
- //
- LineNumberReader buffer = null;
- try {
- buffer = new LineNumberReader(new InputStreamReader(new FileInputStream(file), MInformation.TEXT_ENCODING));
- }
- catch (FileNotFoundException exception) {
- throw new MFileParsingConfigurationException("Could not open file.", exception);
- }
- catch (UnsupportedEncodingException exception) { // cannot happen
- }
- String line = null;
- synchronized (this.getParametersReference()) {
- while (true) {
- try {
- line = buffer.readLine();
- }
- catch (IOException exception) {
- throw new MFileParsingConfigurationException("Could not read file.", exception);
- }
- if (null == line) {
- break;
- }
- line = line.trim();
- if ((line.startsWith("#")) || (line.startsWith(";")) || (MText.isEmpty(line))) {
- continue;
- }
- else {
- int a = line.indexOf("=");
- if (-1 == a) {
- throw new MFileParsingConfigurationException(String.format("Invalid line: %s: string malformed.", buffer.getLineNumber()));
- }
- String key = line.substring(0, a).trim();
- String value = line.substring(a + 1).trim();
- if (this.getParametersReference().containsKey(key)) {
- throw new MFileParsingConfigurationException(String.format("Invalid line: %s: duplicated key: %s.", buffer.getLineNumber(), key));
- }
- this.getParametersReference().put(key, value);
- }
- }
- }
- try {
- buffer.close();
- }
- catch (IOException exception) {
- throw new MFileParsingConfigurationException("Could not close file.", exception);
- }
- }
-
- public void clear() {
- synchronized (this.getParametersReference()) {
- this.getParametersReference().clear();
- }
- }
-
- public String getValue(String key) throws MValueNotFoundConfigurationException {
- if (MText.isBlank(key)) {
- throw new IllegalArgumentException("Invalid 'key': null or empty.");
- }
- //
- if (!this.getParametersReference().containsKey(key)) {
- throw new MValueNotFoundConfigurationException(String.format("Invalid 'key': %s: not available.", key));
- }
- return this.getParametersReference().get(key);
- }
-
-}