/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.phenotips.oo;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;

public class OmimSourceParser
{
    private final static String RECORD_MARKER = "*RECORD*";

    private final static String END_MARKER = "*THEEND*";

    private final static String FIELD_MARKER = "*FIELD* ";

    private int counter = 0;

    private RecordData crtTerm = new RecordData();

    private Map<String, RecordData> data = new LinkedHashMap<String, RecordData>();

    private Set<String> fieldSelection;

    public OmimSourceParser(String path, Set<String> fieldSelection)
    {
        BufferedReader in = null;
        try {
            in = new BufferedReader(new InputStreamReader(new URL(path).openConnection().getInputStream()));
        } catch (MalformedURLException uex) {
            try {
                in = new BufferedReader(new FileReader(path));
            } catch (FileNotFoundException fex) {
                // TODO Auto-generated catch block
                fex.printStackTrace();
                return;
            }
        } catch (IOException iex) {
            // TODO Auto-generated catch block
            iex.printStackTrace();
            return;
        }
        try {
            transform(in, fieldSelection);
            if (in != null) {
                in.close();
            }
        } catch (NullPointerException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

    private Map<String, RecordData> transform(BufferedReader in, Set<String> fieldSelection) throws IOException
    {
        this.fieldSelection = fieldSelection;

        String line;
        StringBuilder fieldValue = new StringBuilder();
        String fieldName = "";
        this.counter = 0;
        while ((line = in.readLine()) != null) {
            if (line.trim().equalsIgnoreCase(RECORD_MARKER) || line.trim().equalsIgnoreCase(END_MARKER)) {
                if (this.counter > 0) {
                    storeCrtTerm();
                }
                ++this.counter;
                continue;
            }
            if (line.startsWith(FIELD_MARKER)) {
                if (!StringUtils.isBlank(fieldValue) && !StringUtils.isBlank(fieldName)) {
                    loadField(fieldName, fieldValue.toString().trim());
                }
                fieldValue.delete(0, fieldValue.length());
                fieldName = line.substring(FIELD_MARKER.length());
                if (fieldSelection.size() > 0 && !fieldSelection.contains(fieldName)) {
                    fieldName = "";
                }
            } else {
                if (!StringUtils.isBlank(line) && !StringUtils.isBlank(fieldName)) {
                    fieldValue.append(line.trim()).append(" ");
                }
            }
        }

        return this.data;
    }

    private void storeCrtTerm()
    {
        if (this.crtTerm.getId() != null) {
            this.data.put(this.crtTerm.getId(), this.crtTerm);
        }
        this.crtTerm = new RecordData();
    }

    private boolean isFieldSelected(String name)
    {
        return this.fieldSelection.isEmpty() || this.fieldSelection.contains(name);
    }

    private void loadField(String name, String value)
    {
        if (!(isFieldSelected(name))) {
            return;
        }
        // System.out.println("Adding " + name + " " + value.replaceAll("\"([^\"]+)\".*", "$1"));
        this.crtTerm.addTo(name, value.replaceAll("\"([^\"]+)\".*", "$1"));
    }

    public Map<String, RecordData> getData()
    {
        return this.data;
    }
}

@@ -949,73 +949,8 @@
 er;%0A
-import java.io.FileNotFoundException;%0Aimport java.io.FileReader;%0A
 impo
@@ -1011,47 +1011,8 @@
 er;%0A
-import java.net.MalformedURLException;%0A
 impo
@@ -1653,16 +1653,21 @@
         
+try (
 Buffered
@@ -1681,14 +1681,8 @@
 in =
- null;
 %0A   
@@ -1690,30 +1690,11 @@
     
-try %7B%0A            in =
+   
  new
@@ -1782,622 +1782,85 @@
 am()
-));%0A        %7D catch (MalformedURLException uex) %7B%0A            try %7B%0A                in = new BufferedReader(new FileReader(path));%0A            %7D catch (FileNotFoundException fex) %7B%0A                // TODO Auto-generated catch block%0A                fex.printStackTrace();%0A                return;%0A            %7D%0A        %7D catch (IOException iex) %7B%0A            // TODO Auto-generated catch block%0A            iex.printStackTrace();%0A            return;%0A        %7D%0A        try %7B%0A            transform(in, fieldSelection);%0A            if (in != null) %7B%0A                in.close();%0A            %7D%0A        %7D catch (NullPointer
+, %22UTF-8%22))) %7B%0A            transform(in, fieldSelection);%0A        %7D catch (IO
 Exce
@@ -1914,34 +1914,43 @@
        %7D catch (
-IO
+NullPointer
 Exception ex) %7B%0A

Use try-with-resources