Search in sources :

Example 1 with Template

use of com.google.refine.templating.Template in project OpenRefine by OpenRefine.

the class TemplatingExporter method export.

@Override
public void export(Project project, Properties options, Engine engine, Writer writer) throws IOException {
    String limitString = options.getProperty("limit");
    int limit = limitString != null ? Integer.parseInt(limitString) : -1;
    JSONObject sortingJson = null;
    try {
        String json = options.getProperty("sorting");
        sortingJson = (json == null) ? null : ParsingUtilities.evaluateJsonStringToObject(json);
    } catch (JSONException e) {
    }
    String templateString = options.getProperty("template");
    String prefixString = options.getProperty("prefix");
    String suffixString = options.getProperty("suffix");
    String separatorString = options.getProperty("separator");
    Template template;
    try {
        template = Parser.parse(templateString);
    } catch (ParsingException e) {
        throw new IOException("Missing or bad template", e);
    }
    template.setPrefix(prefixString);
    template.setSuffix(suffixString);
    template.setSeparator(separatorString);
    if (!"true".equals(options.getProperty("preview"))) {
        StringWriter stringWriter = new StringWriter();
        JSONWriter jsonWriter = new JSONWriter(stringWriter);
        try {
            jsonWriter.object();
            jsonWriter.key("template");
            jsonWriter.value(templateString);
            jsonWriter.key("prefix");
            jsonWriter.value(prefixString);
            jsonWriter.key("suffix");
            jsonWriter.value(suffixString);
            jsonWriter.key("separator");
            jsonWriter.value(separatorString);
            jsonWriter.endObject();
        } catch (JSONException e) {
        // ignore
        }
        project.getMetadata().getPreferenceStore().put("exporters.templating.template", stringWriter.toString());
    }
    if (engine.getMode() == Mode.RowBased) {
        FilteredRows filteredRows = engine.getAllFilteredRows();
        RowVisitor visitor = template.getRowVisitor(writer, limit);
        if (sortingJson != null) {
            try {
                SortingRowVisitor srv = new SortingRowVisitor(visitor);
                srv.initializeFromJSON(project, sortingJson);
                if (srv.hasCriteria()) {
                    visitor = srv;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        filteredRows.accept(project, visitor);
    } else {
        FilteredRecords filteredRecords = engine.getFilteredRecords();
        RecordVisitor visitor = template.getRecordVisitor(writer, limit);
        if (sortingJson != null) {
            try {
                SortingRecordVisitor srv = new SortingRecordVisitor(visitor);
                srv.initializeFromJSON(project, sortingJson);
                if (srv.hasCriteria()) {
                    visitor = srv;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        filteredRecords.accept(project, visitor);
    }
}
Also used : JSONWriter(org.json.JSONWriter) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) JSONException(org.json.JSONException) IOException(java.io.IOException) FilteredRecords(com.google.refine.browsing.FilteredRecords) SortingRecordVisitor(com.google.refine.sorting.SortingRecordVisitor) RecordVisitor(com.google.refine.browsing.RecordVisitor) FilteredRows(com.google.refine.browsing.FilteredRows) Template(com.google.refine.templating.Template) JSONObject(org.json.JSONObject) StringWriter(java.io.StringWriter) ParsingException(com.google.refine.expr.ParsingException) RowVisitor(com.google.refine.browsing.RowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor) SortingRowVisitor(com.google.refine.sorting.SortingRowVisitor)

Aggregations

FilteredRecords (com.google.refine.browsing.FilteredRecords)1 FilteredRows (com.google.refine.browsing.FilteredRows)1 RecordVisitor (com.google.refine.browsing.RecordVisitor)1 RowVisitor (com.google.refine.browsing.RowVisitor)1 ParsingException (com.google.refine.expr.ParsingException)1 SortingRecordVisitor (com.google.refine.sorting.SortingRecordVisitor)1 SortingRowVisitor (com.google.refine.sorting.SortingRowVisitor)1 Template (com.google.refine.templating.Template)1 IOException (java.io.IOException)1 StringWriter (java.io.StringWriter)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JSONWriter (org.json.JSONWriter)1