JAL-3210 JAL-3130 merge changes to JSON processing
authorgmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 31 Mar 2020 10:16:02 +0000 (11:16 +0100)
committergmungoc <g.m.carstairs@dundee.ac.uk>
Tue, 31 Mar 2020 10:16:02 +0000 (11:16 +0100)
src/jalview/ext/paradise/Annotate3D.java

index caff3bb..de1d90c 100644 (file)
  */
 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<Reader> 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<Object> rvals = ((List<Object>) JSONUtils.parse(respons)).iterator();
-      return new Iterator<>()
-      {
-        @Override
-        public boolean hasNext()
-        {
-          return rvals.hasNext();
-        }
-
-        @SuppressWarnings("unchecked")
-               @Override
-        public Reader next()
-        {
-          Map<String, Object> val = (Map<String, Object>) 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<Reader> getRNAMLForPDBId(String pdbid)
@@ -254,3 +185,91 @@ public class Annotate3D
   }
 
 }
+
+class RvalsIterator implements Iterator, AutoCloseable
+{
+  private Iterator<Object> 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<Object>) 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<String, Object> val = (Map<String, Object>) 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();
+    }
+  }
+}