package com.natpryce.pearlfish.formats;

import com.google.common.io.Resources;
import com.natpryce.pearlfish.Format;
import com.natpryce.pearlfish.FormatType;
import com.samskivert.mustache.Escaping;
import com.samskivert.mustache.Mustache;
import com.samskivert.mustache.Template;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.nio.charset.Charset;

public class TemplatedTextFormat implements Format<Object> {
    private final Template template;
    private final TextFilter postTemplateFilter;
    private final String fileExtension;
    private final FormatType fileType;

    public TemplatedTextFormat(Template template, TextFilter postTemplateFilter, String fileExtension, FormatType fileType) {
        this.template = template;
        this.postTemplateFilter = postTemplateFilter;
        this.fileExtension = fileExtension;
        this.fileType = fileType;
    }

    public static TemplatedTextFormat fromResource(Class<?> relativeToClass, String baseName, Charset charset, Escaping valueEscaping, TextFilter postTemplateFilter, String fileExtension, final FormatType fileType) {
        final String resourceName = relativeToClass.getSimpleName() + "." + baseName + fileExtension + ".template";

        return new TemplatedTextFormat(
                loadTemplate(relativeToClass, resourceName, charset, valueEscaping),
                postTemplateFilter,
                fileExtension,
                fileType);

    }

    @Override
    public String fileExtension() {
        return fileExtension;
    }

    @Override
    public FormatType fileType() {
        return fileType;
    }

    @Override
    public void write(Object value, OutputStream output) throws IOException {
        OutputStreamWriter writer = new OutputStreamWriter(output);
        writer.write(postTemplateFilter.filter(template.execute(value)));
        writer.flush();
    }

    private static Template loadTemplate(Class<?> testClass, String templateName, Charset charset, Escaping valueEscaping) {
        try {
            URL resource = testClass.getResource(templateName);
            if (resource == null) {
                throw new MissingTemplateException(templateName);
            }

            return Mustache.compiler()
                    .escaping(valueEscaping)
                    .compile(Resources.toString(resource, charset));
        } catch (IOException e) {
            throw new IllegalArgumentException("invalid template " + templateName, e);
        }
    }
}

@@ -38,47 +38,8 @@
 s;%0A%0A
-import com.google.common.io.Resources;%0A
 impo
@@ -118,90 +118,8 @@
 pe;%0A
-import com.samskivert.mustache.Escaping;%0Aimport com.samskivert.mustache.Mustache;%0A
 impo
@@ -251,62 +251,8 @@
 ter;
-%0Aimport java.net.URL;%0Aimport java.nio.charset.Charset;
 %0A%0Apu
@@ -783,1601 +783,429 @@
     
-public static TemplatedTextFormat fromResource(Class%3C?%3E relativeToClass, String baseName, Charset charset, Escaping valueEscaping, TextFilter postTemplateFilter, String fileExtension, final FormatType fileType) %7B%0A        final String resourceName = relativeToClass.getSimpleName() + %22.%22 + baseName + fileExtension + %22.template%22;%0A%0A        return new TemplatedTextFormat(%0A                loadTemplate(relativeToClass, resourceName, charset, valueEscaping),%0A                postTemplateFilter,%0A                fileExtension,%0A                fileType);%0A%0A    %7D%0A%0A    @Override%0A    public String fileExtension() %7B%0A        return fileExtension;%0A    %7D%0A%0A    @Override%0A    public FormatType fileType() %7B%0A        return fileType;%0A    %7D%0A%0A    @Override%0A    public void write(Object value, OutputStream output) throws IOException %7B%0A        OutputStreamWriter writer = new OutputStreamWriter(output);%0A        writer.write(postTemplateFilter.filter(template.execute(value)));%0A        writer.flush();%0A    %7D%0A%0A    private static Template loadTemplate(Class%3C?%3E testClass, String templateName, Charset charset, Escaping valueEscaping) %7B%0A        try %7B%0A            URL resource = testClass.getResource(templateName);%0A            if (resource == null) %7B%0A                throw new MissingTemplateException(templateName);%0A            %7D%0A%0A            return Mustache.compiler()%0A                    .escaping(valueEscaping)%0A                    .compile(Resources.toString(resource, charset));%0A        %7D catch (IOException e) %7B%0A            throw new IllegalArgumentException(%22invalid template %22 + templateName, e);%0A        %7D
+@Override%0A    public String fileExtension() %7B%0A        return fileExtension;%0A    %7D%0A%0A    @Override%0A    public FormatType fileType() %7B%0A        return fileType;%0A    %7D%0A%0A    @Override%0A    public void write(Object value, OutputStream output) throws IOException %7B%0A        OutputStreamWriter writer = new OutputStreamWriter(output);%0A        writer.write(postTemplateFilter.filter(template.execute(value)));%0A        writer.flush();
 %0A   

Remove unused methods