From: gmungoc Date: Tue, 31 Mar 2020 10:16:02 +0000 (+0100) Subject: JAL-3210 JAL-3130 merge changes to JSON processing X-Git-Tag: Release_2_11_4_0~45^2~18^2~23 X-Git-Url: http://source.jalview.org/gitweb/?a=commitdiff_plain;h=290d1c0638e869b08d5f2729472197873fa06af0;p=jalview.git JAL-3210 JAL-3130 merge changes to JSON processing --- diff --git a/src/jalview/ext/paradise/Annotate3D.java b/src/jalview/ext/paradise/Annotate3D.java index caff3bb..de1d90c 100644 --- a/src/jalview/ext/paradise/Annotate3D.java +++ b/src/jalview/ext/paradise/Annotate3D.java @@ -20,10 +20,6 @@ */ package jalview.ext.paradise; -import jalview.util.JSONUtils; -import jalview.util.MessageManager; -import jalview.ws.HttpClientUtils; - import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; @@ -40,6 +36,10 @@ import org.apache.http.message.BasicNameValuePair; import org.json.simple.parser.ContentHandler; import org.json.simple.parser.ParseException; +import jalview.util.JSONUtils; +import jalview.util.MessageManager; +import jalview.ws.HttpClientUtils; + /** * simple methods for calling the various paradise RNA tools * @@ -151,90 +151,21 @@ public class Annotate3D } - /** - * @param respons - * @return - * @throws Exception - */ public static Iterator processJsonResponseFor(Reader respons) throws Exception { - // BH 2019 never called? + org.json.simple.parser.JSONParser jp = new org.json.simple.parser.JSONParser(); try { - @SuppressWarnings("unchecked") - final Iterator rvals = ((List) JSONUtils.parse(respons)).iterator(); - return new Iterator<>() - { - @Override - public boolean hasNext() - { - return rvals.hasNext(); - } - - @SuppressWarnings("unchecked") - @Override - public Reader next() - { - Map val = (Map) rvals.next(); - - Object sval = null; - try - { - sval = val.get("2D"); - } catch (Exception x) - { - x.printStackTrace(); - } - if (sval == null) - { - System.err.println( - "DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :" - + val.toString()); - - sval = ""; - } - return new StringReader(sval.toString()); - - } - - @Override - public void remove() - { - throw new Error( - MessageManager.getString("error.not_implemented_remove")); - - } - - @Override - protected Object clone() throws CloneNotSupportedException - { - throw new CloneNotSupportedException( - MessageManager.getString("error.not_implemented_clone")); - } - - @Override - public boolean equals(Object obj) - { - return super.equals(obj); - } - - @Override - protected void finalize() throws Throwable - { - while (rvals.hasNext()) - { - rvals.next(); - } - super.finalize(); - } - }; + final RvalsIterator rvals = new RvalsIterator(respons); + return rvals; } catch (Exception foo) { throw new Exception(MessageManager.getString( "exception.couldnt_parse_responde_from_annotated3d_server"), foo); } + } public static Iterator getRNAMLForPDBId(String pdbid) @@ -254,3 +185,91 @@ public class Annotate3D } } + +class RvalsIterator implements Iterator, AutoCloseable +{ + private Iterator rvals; + + @SuppressWarnings("unchecked") + protected RvalsIterator(Reader respons) throws IOException, ParseException + { + /* + * as in 2.11.1 (pre-JalviewJS) + */ + // final JSONArray responses = (JSONArray) jp.parse(respons); + // this.rvals = responses.iterator(); + + /* + * as in JalviewJS (with comment that this code is never called) + */ + this.rvals = ((List) JSONUtils.parse(respons)).iterator(); + } + + @Override + public boolean hasNext() + { + return rvals.hasNext(); + } + + @Override + public Reader next() + { + // JSONObject val = (JSONObject) rvals.next(); // 2.11.1 + @SuppressWarnings("unchecked") + Map val = (Map) rvals.next(); + + Object sval = null; + try + { + sval = val.get("2D"); + } catch (Exception x) + { + x.printStackTrace(); + } + + if (sval == null) + { + System.err.println( + "DEVELOPER WARNING: Annotate3d didn't return a '2D' tag in its response. Consider checking output of server. Response was :" + + val.toString()); + + sval = ""; + } + // 2.11.1: + // return new StringReader( + // (sval instanceof JSONObject) ? ((JSONObject) sval).toString() + // : sval.toString()); + return new StringReader(sval.toString()); + + } + + @Override + public void remove() + { + throw new Error( + MessageManager.getString("error.not_implemented_remove")); + + } + + @Override + protected Object clone() throws CloneNotSupportedException + { + throw new CloneNotSupportedException( + MessageManager.getString("error.not_implemented_clone")); + } + + @Override + public boolean equals(Object obj) + { + return super.equals(obj); + } + + @Override + public void close() + { + while (rvals.hasNext()) + { + rvals.next(); + } + } +}