001package edu.pdx.cs410J.airlineweb; 002 003import edu.pdx.cs410J.ParserException; 004 005import java.io.BufferedReader; 006import java.io.IOException; 007import java.io.Reader; 008import java.util.HashMap; 009import java.util.Map; 010import java.util.regex.Matcher; 011import java.util.regex.Pattern; 012 013public class TextParser { 014 private final Reader reader; 015 016 public TextParser(Reader reader) { 017 this.reader = reader; 018 } 019 020 public Map<String, String> parse() throws ParserException { 021 Pattern pattern = Pattern.compile("(.*) : (.*)"); 022 023 Map<String, String> map = new HashMap<>(); 024 025 try ( 026 BufferedReader br = new BufferedReader(this.reader) 027 ) { 028 029 for (String line = br.readLine(); line != null; line = br.readLine()) { 030 Matcher matcher = pattern.matcher(line); 031 if (!matcher.find()) { 032 throw new ParserException("Unexpected text: " + line); 033 } 034 035 String word = matcher.group(1); 036 String definition = matcher.group(2); 037 038 map.put(word, definition); 039 } 040 041 } catch (IOException e) { 042 throw new ParserException("While parsing dictionary", e); 043 } 044 045 return map; 046 } 047}