package com.alexrnl.subtitlecorrector.io.subrip;

import java.io.BufferedReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.alexrnl.subtitlecorrector.common.Subtitle;
import com.alexrnl.subtitlecorrector.common.SubtitleFile;
import com.alexrnl.subtitlecorrector.io.SubtitleReader;

/**
 * Reader for the SubRip format.<br />
 * @author Alex
 */
public class SubRipReader extends SubtitleReader {
	/** Logger */
	private static Logger			lg	= Logger.getLogger(SubRipReader.class.getName());
	
	/** The date formatter */
	private final SimpleDateFormat	dateFormatter;
	
	/**
	 * Constructor #1.<br />
	 */
	public SubRipReader () {
		super();
		dateFormatter = new SimpleDateFormat(SubRip.SUBRIP_DATE_FORMAT);
	}



	@Override
	protected Subtitle readSubtitle (final SubtitleFile subtitleFile, final BufferedReader reader)
			throws IOException {
		final long begin;
		final long end;
		final StringBuilder content = new StringBuilder();
		String currentLine;
		
		// Removing empty lines
		do {
			currentLine = reader.readLine().trim();
		} while (!currentLine.isEmpty());
		// Get the subtitle number
		final int subtitleIndex = Integer.parseInt(currentLine);
		if (lg.isLoggable(Level.INFO)) {
			lg.info("Reading subtitle#" + subtitleIndex);
		}
		
		// Get the begin and end dates of the subtitle
		currentLine = reader.readLine().trim();
		final String[] dates = currentLine.split(SubRip.SUBRIP_DATE_SEPARATOR);
		if (dates.length < 2) {
			throw new IOException("Could not parse " + currentLine + " as a valid line in the subrip format");
		}
		try {
			begin = dateFormatter.parse(dates[0]).getTime();
			end = dateFormatter.parse(dates[1]).getTime();
		} catch (final ParseException e) {
			lg.warning("Could not parse either " + dates[0] + " or " + dates[1] + " as a date");
			throw new IOException("Problem while parsing a date", e);
		}
		
		// Get the content of the subtitle
		do {
			currentLine = reader.readLine().trim();
			content.append(currentLine);
		} while (!currentLine.isEmpty());
		
		return new Subtitle(begin, end, content.toString());
	}
}

@@ -74,16 +74,45 @@
 Reader;%0A
+import java.io.EOFException;%0A
 import j
@@ -199,16 +199,39 @@
 Format;%0A
+import java.util.Date;%0A
 import j
@@ -288,16 +288,65 @@
 ogger;%0A%0A
+import com.alexrnl.commons.error.ExceptionUtils;%0A
 import c
@@ -1243,32 +1243,194 @@
 eader.readLine()
+;%0A%09%09%09if (currentLine == null) %7B%0A%09%09%09%09throw new EOFException(%22End of file was reach, current line is null%22);%0A%09%09%09%7D%0A%09%09%09currentLine = currentLine.replace(%22%5Cufeff%22, %22%22)
 .trim();%0A%09%09%7D whi
@@ -1425,33 +1425,32 @@
 m();%0A%09%09%7D while (
-!
 currentLine.isEm
@@ -1492,18 +1492,38 @@
 r%0A%09%09
-f
 in
-al int 
+t subtitleIndex;%0A%09%09try %7B%0A%09%09%09
 subt
@@ -1565,16 +1565,237 @@
 tLine);%0A
+%09%09%7D catch (final NumberFormatException e) %7B%0A%09%09%09lg.warning(%22Could not parse '%22 + currentLine + %22' as a number: %22 + ExceptionUtils.display(e));%0A%09%09%09throw new IOException(%22Could not parse a subtitle number properly%22, e);%0A%09%09%7D%0A
 %09%09if (lg
@@ -2280,24 +2280,197 @@
 .getTime();%0A
+%09%09%09if (lg.isLoggable(Level.FINE)) %7B%0A%09%09%09%09lg.fine(%22Begin date=%22 + dateFormatter.format(new Date(begin)));%0A%09%09%09%09lg.fine(%22End date=%22 + dateFormatter.format(new Date(end)));%0A%09%09%09%7D%0A
 %09%09%7D catch (f
@@ -2763,24 +2763,55 @@
 currentLine)
+.append(System.lineSeparator())
 ;%0A%09%09%7D while 

Fix some bug when reading subtitles