Search in sources :

Example 26 with JSONException

use of org.json.JSONException 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)

Example 27 with JSONException

use of org.json.JSONException in project OpenRefine by OpenRefine.

the class GetModelsCommand method internalRespond.

protected void internalRespond(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Project project = null;
    // This command also supports retrieving rows for an importing job.
    String importingJobID = request.getParameter("importingJobID");
    if (importingJobID != null) {
        long jobID = Long.parseLong(importingJobID);
        ImportingJob job = ImportingManager.getJob(jobID);
        if (job != null) {
            project = job.project;
        }
    }
    if (project == null) {
        project = getProject(request);
    }
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        response.setHeader("Cache-Control", "no-cache");
        Properties options = new Properties();
        JSONWriter writer = new JSONWriter(response.getWriter());
        writer.object();
        writer.key("columnModel");
        project.columnModel.write(writer, options);
        writer.key("recordModel");
        project.recordModel.write(writer, options);
        writer.key("overlayModels");
        writer.object();
        for (String modelName : project.overlayModels.keySet()) {
            OverlayModel overlayModel = project.overlayModels.get(modelName);
            if (overlayModel != null) {
                writer.key(modelName);
                project.overlayModels.get(modelName).write(writer, options);
            }
        }
        writer.endObject();
        writer.key("scripting");
        writer.object();
        for (String languagePrefix : MetaParser.getLanguagePrefixes()) {
            LanguageInfo info = MetaParser.getLanguageInfo(languagePrefix);
            writer.key(languagePrefix);
            writer.object();
            writer.key("name");
            writer.value(info.name);
            writer.key("defaultExpression");
            writer.value(info.defaultExpression);
            writer.endObject();
        }
        writer.endObject();
        writer.endObject();
    } catch (JSONException e) {
        HttpUtilities.respondException(response, e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) Project(com.google.refine.model.Project) LanguageInfo(com.google.refine.expr.MetaParser.LanguageInfo) ImportingJob(com.google.refine.importing.ImportingJob) JSONException(org.json.JSONException) Properties(java.util.Properties) OverlayModel(com.google.refine.model.OverlayModel)

Example 28 with JSONException

use of org.json.JSONException in project OpenRefine by OpenRefine.

the class ApplyOperationsCommand method doPost.

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Project project = getProject(request);
    String jsonString = request.getParameter("operations");
    try {
        JSONArray a = ParsingUtilities.evaluateJsonStringToArray(jsonString);
        int count = a.length();
        for (int i = 0; i < count; i++) {
            JSONObject obj = a.getJSONObject(i);
            reconstructOperation(project, obj);
        }
        if (project.processManager.hasPending()) {
            respond(response, "{ \"code\" : \"pending\" }");
        } else {
            respond(response, "{ \"code\" : \"ok\" }");
        }
    } catch (JSONException e) {
        respondException(response, e);
    }
}
Also used : Project(com.google.refine.model.Project) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException)

Example 29 with JSONException

use of org.json.JSONException in project OpenRefine by OpenRefine.

the class GetOperationsCommand method doGet.

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    Project project = getProject(request);
    try {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("Content-Type", "application/json");
        Properties options = new Properties();
        JSONWriter writer = new JSONWriter(response.getWriter());
        writer.object();
        writer.key("entries");
        writer.array();
        for (HistoryEntry entry : project.history.getLastPastEntries(-1)) {
            writer.object();
            writer.key("description");
            writer.value(entry.description);
            if (entry.operation != null) {
                writer.key("operation");
                entry.operation.write(writer, options);
            }
            writer.endObject();
        }
        writer.endArray();
        writer.endObject();
    } catch (JSONException e) {
        respondException(response, e);
    }
}
Also used : JSONWriter(org.json.JSONWriter) Project(com.google.refine.model.Project) HistoryEntry(com.google.refine.history.HistoryEntry) JSONException(org.json.JSONException) Properties(java.util.Properties)

Example 30 with JSONException

use of org.json.JSONException in project OpenRefine by OpenRefine.

the class Get method call.

@Override
public Object call(Properties bindings, Object[] args) {
    if (args.length > 1 && args.length <= 3) {
        Object v = args[0];
        Object from = args[1];
        Object to = (args.length == 3) ? args[2] : null;
        if (v != null && from != null) {
            if (v instanceof HasFields && from instanceof String) {
                return ((HasFields) v).getField((String) from, bindings);
            } else if (v instanceof JSONObject && from instanceof String) {
                try {
                    return ((JSONObject) v).get((String) from);
                } catch (JSONException e) {
                // ignore; will return null
                }
            } else {
                if (from instanceof Number && (to == null || to instanceof Number)) {
                    if (v.getClass().isArray() || v instanceof List<?> || v instanceof HasFieldsList || v instanceof JSONArray) {
                        int length = 0;
                        if (v.getClass().isArray()) {
                            length = ((Object[]) v).length;
                        } else if (v instanceof HasFieldsList) {
                            length = ((HasFieldsList) v).length();
                        } else if (v instanceof JSONArray) {
                            length = ((JSONArray) v).length();
                        } else {
                            length = ExpressionUtils.toObjectList(v).size();
                        }
                        int start = ((Number) from).intValue();
                        if (start < 0) {
                            start = length + start;
                        }
                        start = Math.min(length, Math.max(0, start));
                        if (to == null) {
                            if (v.getClass().isArray()) {
                                return ((Object[]) v)[start];
                            } else if (v instanceof HasFieldsList) {
                                return ((HasFieldsList) v).get(start);
                            } else if (v instanceof JSONArray) {
                                try {
                                    return ((JSONArray) v).get(start);
                                } catch (JSONException e) {
                                // ignore; will return null
                                }
                            } else {
                                return ExpressionUtils.toObjectList(v).get(start);
                            }
                        } else {
                            int end = ((Number) to).intValue();
                            if (end < 0) {
                                end = length + end;
                            }
                            end = Math.min(length, Math.max(start, end));
                            if (end > start) {
                                if (v.getClass().isArray()) {
                                    Object[] a2 = new Object[end - start];
                                    System.arraycopy(v, start, a2, 0, end - start);
                                    return a2;
                                } else if (v instanceof HasFieldsList) {
                                    return ((HasFieldsList) v).getSubList(start, end);
                                } else if (v instanceof JSONArray) {
                                    JSONArray a = (JSONArray) v;
                                    Object[] a2 = new Object[end - start];
                                    for (int i = 0; i < a2.length; i++) {
                                        try {
                                            a2[i] = a.get(start + i);
                                        } catch (JSONException e) {
                                        // ignore
                                        }
                                    }
                                    return a2;
                                } else {
                                    return ExpressionUtils.toObjectList(v).subList(start, end);
                                }
                            }
                        }
                    } else {
                        String s = (v instanceof String) ? (String) v : v.toString();
                        int start = ((Number) from).intValue();
                        if (start < 0) {
                            start = s.length() + start;
                        }
                        start = Math.min(s.length(), Math.max(0, start));
                        if (to != null) {
                            int end = ((Number) to).intValue();
                            if (end < 0) {
                                end = s.length() + end;
                            }
                            end = Math.min(s.length(), Math.max(start, end));
                            return s.substring(start, end);
                        } else {
                            return s.substring(start, start + 1);
                        }
                    }
                }
            }
        }
    }
    return null;
}
Also used : HasFieldsList(com.google.refine.expr.HasFieldsList) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) List(java.util.List) HasFieldsList(com.google.refine.expr.HasFieldsList) HasFields(com.google.refine.expr.HasFields)

Aggregations

JSONException (org.json.JSONException)1760 JSONObject (org.json.JSONObject)1346 JSONArray (org.json.JSONArray)593 IOException (java.io.IOException)315 ArrayList (java.util.ArrayList)194 HashMap (java.util.HashMap)139 Test (org.junit.Test)108 ClientException (edu.umass.cs.gnscommon.exceptions.client.ClientException)98 Bundle (android.os.Bundle)67 VolleyError (com.android.volley.VolleyError)66 File (java.io.File)63 DefaultGNSTest (edu.umass.cs.gnsserver.utils.DefaultGNSTest)62 Response (com.android.volley.Response)58 URL (java.net.URL)54 Map (java.util.Map)45 RandomString (edu.umass.cs.gnscommon.utils.RandomString)44 MalformedURLException (java.net.MalformedURLException)43 UnsupportedEncodingException (java.io.UnsupportedEncodingException)41 Intent (android.content.Intent)40 JsonObjectRequest (com.android.volley.toolbox.JsonObjectRequest)40