/*
 * Copyright 2013, 2014 Deutsche Nationalbibliothek
 *
 * Licensed under the Apache License, Version 2.0 the "License";
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.culturegraph.mf.morph.maps;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Pattern;

import org.culturegraph.mf.morph.api.MorphException;
import org.culturegraph.mf.morph.api.helpers.AbstractReadOnlyMap;
import org.culturegraph.mf.util.ResourceUtil;

/**
 * Provides a {@link Map} based on a file. The file is supposed to be UTF-8
 * encoded. The separator is by default \t. <strong>Important:</strong> Lines
 * that are not split in two parts by the separator are ignored!
 *
 * @author "Markus Michael Geipel"
 *
 */
public final class FileMap extends AbstractReadOnlyMap<String, String> {

	private final Map<String, String> map = new HashMap<String, String>();

	private Pattern split = Pattern.compile("\t", Pattern.LITERAL);

	public void setFiles(final String files) {
		final String[] parts = files.split("\\s*,\\s*");
		for (final String part : parts) {
			setFile(part);
		}
	}

	public void setFile(final String file) {
		final BufferedReader reader;
		try {
			reader = new BufferedReader(new InputStreamReader(
					ResourceUtil.getStream(file), "UTF-8"));

			try {
				String line = null;
				while ((line = reader.readLine()) != null) {
					if (line.isEmpty()) {
						continue;
					}
					final String[] parts = split.split(line);
					if (parts.length == 2) {
						map.put(parts[0], parts[1]);
					}
				}

			} finally {
				reader.close();
			}

		} catch (final UnsupportedEncodingException e) {
			throw new MorphException(e);
		} catch (final FileNotFoundException e) {
			throw new MorphException("resource '" + file + "' not found", e);
		} catch (final IOException e) {
			throw new MorphException(e);
		}

	}

	public void setSeparator(final String delimiter) {
		split = Pattern.compile(delimiter, Pattern.LITERAL);
	}

	@Override
	public String get(final Object key) {
		return map.get(key);
	}

}

@@ -738,32 +738,60 @@
 io.IOException;%0A
+import java.io.InputStream;%0A
 import java.io.I
@@ -824,39 +824,36 @@
 ava.
+n
 io.
-UnsupportedEncodingException
+charset.StandardCharsets
 ;%0Aim
@@ -1783,48 +1783,83 @@
 %7B%0A%09%09
-final BufferedReader reader;%0A%09%09try %7B%0A%09%09%09
+try (InputStream stream = ResourceUtil.getStream(file);%0A%09%09%09%09BufferedReader 
 read
@@ -1882,16 +1882,23 @@
 dReader(
+%0A%09%09%09%09%09%09
 new Inpu
@@ -1915,66 +1915,44 @@
 der(
-%0A%09%09%09%09%09ResourceUtil.getStream(file), %22UTF-8%22));%0A%0A%09%09%09try
+stream, StandardCharsets.UTF_8)))
  %7B%0A
-%09
 %09%09%09S
@@ -1965,21 +1965,13 @@
 line
- = null
 ;%0A%09%09%09
-%09
 whil
@@ -2011,25 +2011,24 @@
 null) %7B%0A%09%09%09%09
-%09
 if (line.isE
@@ -2042,17 +2042,16 @@
  %7B%0A%09%09%09%09%09
-%09
 continue
@@ -2056,20 +2056,18 @@
 ue;%0A%09%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09%09fina
@@ -2108,17 +2108,16 @@
 e);%0A%09%09%09%09
-%09
 if (part
@@ -2129,25 +2129,24 @@
 gth == 2) %7B%0A
-%09
 %09%09%09%09%09map.put
@@ -2175,141 +2175,14 @@
 %09%09%09%09
-%09
 %7D%0A
-%09
 %09%09%09%7D
-%0A%0A%09%09%09%7D finally %7B%0A%09%09%09%09reader.close();%0A%09%09%09%7D%0A%0A%09%09%7D catch (final UnsupportedEncodingException e) %7B%0A%09%09%09throw new MorphException(e);
 %0A%09%09%7D
@@ -2361,17 +2361,16 @@
 e);%0A%09%09%7D%0A
-%0A
 %09%7D%0A%0A%09pub

Fix resource leak in Metamorph file-maps