package it.mice.voila.runtime.oawtemplate.formatters;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.openarchitectureware.xpand2.output.FileHandle;
import org.openarchitectureware.xpand2.output.PostProcessor;

public class BlankLineRemoverPostProcessor implements PostProcessor {
	public void beforeWriteAndClose(FileHandle impl) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		String[] lines = impl.getBuffer().toString().trim().split("\n");
		try {
			for (int i = 0; i < lines.length; i++) {
				if (!lines[i].trim().equals("")) {
					baos.write(lines[i].getBytes());
					baos.write("\n".getBytes());
				}
			}
			impl.setBuffer(baos.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void afterClose(FileHandle impl) {
	}
}
