Search in sources :

Example 1 with CsvExporter

use of com.google.refine.exporters.CsvExporter in project OpenRefine by OpenRefine.

the class CsvExporterTests method SetUp.

@BeforeMethod
public void SetUp() {
    SUT = new CsvExporter();
    writer = new StringWriter();
    project = new Project();
    engine = new Engine(project);
    options = mock(Properties.class);
}
Also used : Project(com.google.refine.model.Project) StringWriter(java.io.StringWriter) CsvExporter(com.google.refine.exporters.CsvExporter) Properties(java.util.Properties) Engine(com.google.refine.browsing.Engine) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 2 with CsvExporter

use of com.google.refine.exporters.CsvExporter in project OpenRefine by OpenRefine.

the class TsvExporterTests method SetUp.

@BeforeMethod
public void SetUp() {
    //new TsvExporter();
    SUT = new CsvExporter('\t');
    writer = new StringWriter();
    project = new Project();
    engine = new Engine(project);
    options = mock(Properties.class);
}
Also used : Project(com.google.refine.model.Project) StringWriter(java.io.StringWriter) CsvExporter(com.google.refine.exporters.CsvExporter) Properties(java.util.Properties) Engine(com.google.refine.browsing.Engine) BeforeMethod(org.testng.annotations.BeforeMethod)

Example 3 with CsvExporter

use of com.google.refine.exporters.CsvExporter in project OpenRefine by OpenRefine.

the class ExportRowsCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ProjectManager.singleton.setBusy(true);
    try {
        Project project = getProject(request);
        Engine engine = getEngine(request, project);
        Properties params = getRequestParameters(request);
        String format = params.getProperty("format");
        Exporter exporter = ExporterRegistry.getExporter(format);
        if (exporter == null) {
            exporter = new CsvExporter('\t');
        }
        String contentType = params.getProperty("contentType");
        if (contentType == null) {
            contentType = exporter.getContentType();
        }
        response.setHeader("Content-Type", contentType);
        String preview = params.getProperty("preview");
        if (!"true".equals(preview)) {
            String path = request.getPathInfo();
            String filename = path.substring(path.lastIndexOf('/') + 1);
            PercentEscaper escaper = new PercentEscaper("", false);
            filename = escaper.escape(filename);
            response.setHeader("Content-Disposition", "attachment; filename=" + filename + "; filename*=utf-8' '" + filename);
        }
        if (exporter instanceof WriterExporter) {
            String encoding = params.getProperty("encoding");
            response.setCharacterEncoding(encoding != null ? encoding : "UTF-8");
            Writer writer = encoding == null ? response.getWriter() : new OutputStreamWriter(response.getOutputStream(), encoding);
            ((WriterExporter) exporter).export(project, params, engine, writer);
            writer.close();
        } else if (exporter instanceof StreamExporter) {
            response.setCharacterEncoding("UTF-8");
            OutputStream stream = response.getOutputStream();
            ((StreamExporter) exporter).export(project, params, engine, stream);
            stream.close();
        // } else if (exporter instanceof UrlExporter) {
        // ((UrlExporter) exporter).export(project, options, engine);
        } else {
            // TODO: Should this use ServletException instead of respondException?
            respondException(response, new RuntimeException("Unknown exporter type"));
        }
    } catch (Exception e) {
        // Use generic error handling rather than our JSON handling
        logger.info("error:{}", e.getMessage());
        if (e instanceof SqlExporterException) {
            response.sendError(HttpStatus.SC_BAD_REQUEST, e.getMessage());
        }
        throw new ServletException(e);
    } finally {
        ProjectManager.singleton.setBusy(false);
    }
}
Also used : WriterExporter(com.google.refine.exporters.WriterExporter) StreamExporter(com.google.refine.exporters.StreamExporter) OutputStream(java.io.OutputStream) Properties(java.util.Properties) WriterExporter(com.google.refine.exporters.WriterExporter) Exporter(com.google.refine.exporters.Exporter) StreamExporter(com.google.refine.exporters.StreamExporter) CsvExporter(com.google.refine.exporters.CsvExporter) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) SqlExporterException(com.google.refine.exporters.sql.SqlExporterException) PercentEscaper(com.google.common.net.PercentEscaper) ServletException(javax.servlet.ServletException) Project(com.google.refine.model.Project) SqlExporterException(com.google.refine.exporters.sql.SqlExporterException) CsvExporter(com.google.refine.exporters.CsvExporter) OutputStreamWriter(java.io.OutputStreamWriter) Engine(com.google.refine.browsing.Engine) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

Engine (com.google.refine.browsing.Engine)3 CsvExporter (com.google.refine.exporters.CsvExporter)3 Project (com.google.refine.model.Project)3 Properties (java.util.Properties)3 StringWriter (java.io.StringWriter)2 BeforeMethod (org.testng.annotations.BeforeMethod)2 PercentEscaper (com.google.common.net.PercentEscaper)1 Exporter (com.google.refine.exporters.Exporter)1 StreamExporter (com.google.refine.exporters.StreamExporter)1 WriterExporter (com.google.refine.exporters.WriterExporter)1 SqlExporterException (com.google.refine.exporters.sql.SqlExporterException)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 ServletException (javax.servlet.ServletException)1