package com.alexrnl.subtitlecorrector.io.subrip;

import java.io.BufferedWriter;
import java.io.IOException;
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.SubtitleWriter;

/**
 * Writer for the SubRip format.<br />
 * @author Alex
 */
public class SubRipWriter extends SubtitleWriter {
	/** Logger */
	private static Logger			lg	= Logger.getLogger(SubRipWriter.class.getName());
	
	/** The date formatter */
	private final SimpleDateFormat	dateFormat;
	/** The subtitle counter (required by SubRip format) */
	private Integer					subtitleCounter;
	
	/**
	 * Constructor #1.<br />
	 */
	public SubRipWriter () {
		super();
		dateFormat = new SimpleDateFormat(SubRip.SUBRIP_DATE_FORMAT);
		subtitleCounter = null;
	}
	
	@Override
	protected void writeHeader (final SubtitleFile file, final BufferedWriter writer) throws IOException {
		if (subtitleCounter != null) {
			throw new IllegalStateException("It seems that the previous file was not finished fully writen");
		}
		subtitleCounter = 0;
	}

	@Override
	protected void writeFooter (final SubtitleFile file, final BufferedWriter writer) throws IOException {
		if (lg.isLoggable(Level.INFO)) {
			lg.info("Successfully writen " + subtitleCounter + " subtitles");
		}
		subtitleCounter = null;
	}

	@Override
	protected void writeSubtitle (final Subtitle subtitle, final BufferedWriter writer) throws IOException {
		writer.write(++subtitleCounter);
		// TODO newline
		writer.write(dateFormat.format(subtitle.getBegin()));
		writer.write(SubRip.SUBRIP_DATE_SEPARATOR);
		writer.write(dateFormat.format(subtitle.getEnd()));
		// TODO newline
		writer.write(subtitle.getContent());
		// TODO newline
	}
}

@@ -1600,39 +1600,61 @@
 Counter);%0A%09%09
-// TODO newline
+writer.write(System.lineSeparator());
 %0A%09%09writer.wr
@@ -1800,31 +1800,53 @@
 ()));%0A%09%09
-// TODO newline
+writer.write(System.lineSeparator());
 %0A%09%09write
@@ -1883,23 +1883,45 @@
 ;%0A%09%09
-// TODO newline
+writer.write(System.lineSeparator());
 %0A%09%7D%0A

Add line in SubRip writer