Refactor exporter interface along same lines as importer

git-svn-id: http://google-refine.googlecode.com/svn/trunk@1547 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
Tom Morris 2010-10-14 21:33:50 +00:00
parent 9858849198
commit 083abd4329
6 changed files with 59 additions and 29 deletions

View File

@ -15,6 +15,8 @@ import com.google.refine.commands.Command;
import com.google.refine.exporters.CsvExporter;
import com.google.refine.exporters.Exporter;
import com.google.refine.exporters.ExporterRegistry;
import com.google.refine.exporters.StreamExporter;
import com.google.refine.exporters.WriterExporter;
import com.google.refine.model.Project;
public class ExportRowsCommand extends Command {
@ -50,11 +52,15 @@ public class ExportRowsCommand extends Command {
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Type", exporter.getContentType());
if (exporter.takeWriter()) {
if (exporter instanceof WriterExporter) {
PrintWriter writer = response.getWriter();
exporter.export(project, options, engine, writer);
((WriterExporter) exporter).export(project, options, engine, writer);
} else if (exporter instanceof StreamExporter) {
((StreamExporter) exporter).export(project, options, engine, response.getOutputStream());
} else if (exporter instanceof StreamExporter) {
((StreamExporter) exporter).export(project, options, engine, response.getOutputStream());
} else {
exporter.export(project, options, engine, response.getOutputStream());
respondException(response, new RuntimeException("Unknown exporter type"));
}
} catch (Exception e) {
respondException(response, e);

View File

@ -1,7 +1,6 @@
package com.google.refine.exporters;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Properties;
@ -17,7 +16,7 @@ import com.google.refine.model.Column;
import com.google.refine.model.Project;
import com.google.refine.model.Row;
public class CsvExporter implements Exporter{
public class CsvExporter implements WriterExporter{
final static Logger logger = LoggerFactory.getLogger("CsvExporter");
char separator;
@ -30,12 +29,6 @@ public class CsvExporter implements Exporter{
this.separator = separator;
}
@Override
public void export(Project project, Properties options, Engine engine, OutputStream outputStream)
throws IOException {
throw new RuntimeException("Not implemented");
}
@Override
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException {
boolean printColumnHeader = true;
@ -107,9 +100,4 @@ public class CsvExporter implements Exporter{
return "application/x-unknown";
}
@Override
public boolean takeWriter() {
return true;
}
}

View File

@ -1,19 +1,9 @@
package com.google.refine.exporters;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Properties;
import com.google.refine.browsing.Engine;
import com.google.refine.model.Project;
public interface Exporter {
/**
* @return MIME content type handled by exporter
*/
public String getContentType();
public boolean takeWriter();
public void export(Project project, Properties options, Engine engine, OutputStream outputStream) throws IOException;
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException;
}

View File

@ -0,0 +1,15 @@
package com.google.refine.exporters;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import com.google.refine.browsing.Engine;
import com.google.refine.model.Project;
public interface StreamExporter extends Exporter {
public void export(Project project, Properties options, Engine engine, OutputStream outputStream) throws IOException;
}

View File

@ -0,0 +1,16 @@
package com.google.refine.exporters;
import java.io.IOException;
import java.net.URL;
import java.util.Properties;
import com.google.refine.browsing.Engine;
import com.google.refine.model.Project;
public interface UrlExporter extends Exporter {
public void export(Project project, Properties options, Engine engine, URL url) throws IOException;
}

View File

@ -0,0 +1,15 @@
package com.google.refine.exporters;
import java.io.IOException;
import java.io.Writer;
import java.util.Properties;
import com.google.refine.browsing.Engine;
import com.google.refine.model.Project;
public interface WriterExporter extends Exporter {
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException;
}