/*******************************************************************************
 * Copyright (c) 2012 - 2015 Signal Iduna Corporation and others.
 * 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:
 * Signal Iduna Corporation - initial API and implementation
 * akquinet AG
 *******************************************************************************/

package org.testeditor.fixture.core.elementlist;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.testeditor.fixture.core.exceptions.ElementKeyNotFoundException;

/**
 * This service provides access to the key-value pairs of an element list. <br />
 * 
 * Each FitNesse project which uses any Test-Editor fixtures should have an
 * element list stored inside the FitNesse project (see for example
 * .testeditor/DemoWebTests/FitNesseRoot/DemoWebTests/ElementListe/content.txt).
 * This list is just a wiki page with key-value pairs.<br />
 * 
 * Keys are used inside any test-cases in human readable type. The fixture
 * implementation on the other translates the keys into the values via this
 * service. Values are the technical addresses of any items (e.g. the x-path to
 * a HTML object).
 */
public final class ElementListService {
	private static final Logger LOGGER = Logger.getLogger(ElementListService.class);

	/**
	 * ElementListService exists only once per element list file.
	 */
	private static Map<String, ElementListService> instances = new HashMap<String, ElementListService>();

	private Properties properties;

	/**
	 * Initializes the singleton.
	 * 
	 * @param file
	 *            the element list file
	 */
	private ElementListService(File file) {
		FileInputStream fileInputStream = null;

		try {
			fileInputStream = new FileInputStream(file);
			properties = new Properties();
			properties.load(fileInputStream);
		} catch (Exception e) {
			LOGGER.error("ElementListService :: FAILED - systemException: " + e.getMessage());
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					LOGGER.error("ElementListService :: FAILED - systemException: " + e.getMessage());
				}
			}
		}
	}

	/**
	 * Returns the singleton instance of the service.
	 * 
	 * @param filePath
	 *            path to the element list
	 * @return singleton
	 */
	public static ElementListService instanceFor(String filePath) {
		File file = new File(filePath);
		String canonicalPath = null;
		try {
			canonicalPath = file.getCanonicalPath();
		} catch (IOException e) {
			throw new IllegalArgumentException("ElementListService :: FAILED - systemException: ", e);
		}

		if (!instances.containsKey(canonicalPath)) {
			instances.put(canonicalPath, new ElementListService(file));
		}
		return instances.get(canonicalPath);
	}

	/**
	 * Returns the value for a given key.
	 * 
	 * @param key
	 *            key (e.g. inputUsername)
	 * @return value (e.g. x-path to a HTML object)
	 * @throws ElementKeyNotFoundException
	 *             is thrown if the element key doesn't exist
	 */
	public String getValue(String key) throws ElementKeyNotFoundException {

		if (key.contains(" ")) {
			key = key.replaceAll("\\s", "");
		}
		String property = properties.getProperty(key);

		if (property == null) {
			throw new ElementKeyNotFoundException(key);
		} else {
			property = property.trim();
		}

		return property;
	}
}

@@ -696,16 +696,50 @@
 eption;%0A
+import java.io.InputStreamReader;%0A
 import j
@@ -2010,24 +2010,30 @@
 Service(
-File fil
+String fileNam
 e) %7B%0A%09%09F
@@ -2125,16 +2125,20 @@
 eam(file
+Name
 );%0A%09%09%09pr
@@ -2185,16 +2185,38 @@
 es.load(
+new InputStreamReader(
 fileInpu
@@ -2222,16 +2222,26 @@
 utStream
+, %22UTF-8%22)
 );%0A%09%09%7D c
@@ -3135,16 +3135,20 @@
 ice(file
+Path
 ));%0A%09%09%7D%0A
@@ -3660,24 +3660,92 @@
  == null) %7B%0A
+%09%09%09LOGGER.info(key + %22 was not found in properties %22 + properties);%0A
 %09%09%09throw new
@@ -3848,10 +3848,11 @@
 rty;%0A%09%7D%0A
+%0A
 %7D%0A

Read element-list as UTF-8