JAL-1645 Version-Rel Version 2.9 Year-Rel 2015 Licensing glob
[jalview.git] / src / jalview / ws / rest / RestServiceDescription.java
index aae2255..d97ad07 100644 (file)
@@ -1,25 +1,28 @@
 /*
- * Jalview - A Sequence Alignment Editor and Viewer (Version 2.8.0b1)
- * Copyright (C) 2014 The Jalview Authors
+ * Jalview - A Sequence Alignment Editor and Viewer (Version 2.9)
+ * Copyright (C) 2015 The Jalview Authors
  * 
  * This file is part of Jalview.
  * 
  * Jalview is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License 
- * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
+ * as published by the Free Software Foundation, either version 3
+ * of the License, or (at your option) any later version.
  *  
  * Jalview is distributed in the hope that it will be useful, but 
  * WITHOUT ANY WARRANTY; without even the implied warranty 
  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
  * PURPOSE.  See the GNU General Public License for more details.
  * 
- * You should have received a copy of the GNU General Public License along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
+ * You should have received a copy of the GNU General Public License
+ * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
  * The Jalview Authors are detailed in the 'AUTHORS' file.
  */
 package jalview.ws.rest;
 
 import jalview.datamodel.SequenceI;
 import jalview.io.packed.DataProvider.JvDataType;
+import jalview.util.StringUtils;
 import jalview.ws.rest.params.Alignment;
 import jalview.ws.rest.params.AnnotationFile;
 import jalview.ws.rest.params.SeqGroupIndexVector;
@@ -37,6 +40,9 @@ import java.util.regex.Pattern;
 
 public class RestServiceDescription
 {
+  private static final Pattern PARAM_ENCODED_URL_PATTERN = Pattern
+          .compile("([?&])([A-Za-z0-9_]+)=\\$([^$]+)\\$");
+
   /**
    * create a new rest service description ready to be configured
    */
@@ -75,6 +81,7 @@ public class RestServiceDescription
     this.gapCharacter = gapCharacter;
   }
 
+  @Override
   public boolean equals(Object o)
   {
     if (o == null || !(o instanceof RestServiceDescription))
@@ -85,7 +92,8 @@ public class RestServiceDescription
     boolean diff = (gapCharacter != other.gapCharacter);
     diff |= vseparable != other.vseparable;
     diff |= hseparable != other.hseparable;
-    diff |= !(urlSuffix.equals(other.urlSuffix));
+    diff |= !(urlSuffix == null && other.urlSuffix == null || (urlSuffix != null
+            && other.urlSuffix != null && urlSuffix.equals(other.urlSuffix)));
     // TODO - robust diff that includes constants and reordering of URL
     // diff |= !(postUrl.equals(other.postUrl));
     // diff |= !inputParams.equals(other.inputParams);
@@ -354,131 +362,6 @@ public class RestServiceDescription
     return invalidMessage == null;
   }
 
-  private static boolean debug = false;
-
-  /**
-   * parse the string into a list
-   * 
-   * @param list
-   * @param separator
-   * @return elements separated by separator
-   */
-  public static String[] separatorListToArray(String list, String separator)
-  {
-    int seplen = separator.length();
-    if (list == null || list.equals("") || list.equals(separator))
-      return null;
-    java.util.ArrayList<String> jv = new ArrayList<String>();
-    int cp = 0, pos, escape;
-    boolean wasescaped = false, wasquoted = false;
-    String lstitem = null;
-    while ((pos = list.indexOf(separator, cp)) >= cp)
-    {
-
-      escape = (pos > 0 && list.charAt(pos - 1) == '\\') ? -1 : 0;
-      if (wasescaped || wasquoted)
-      {
-        // append to previous pos
-        jv.set(jv.size() - 1,
-                lstitem = lstitem + separator
-                        + list.substring(cp, pos + escape));
-
-      }
-      else
-      {
-        jv.add(lstitem = list.substring(cp, pos + escape));
-      }
-      cp = pos + seplen;
-      wasescaped = escape == -1;
-      if (!wasescaped)
-      {
-        // last separator may be in an unmatched quote
-        if (java.util.regex.Pattern.matches("('[^']*')*[^']*'", lstitem))
-        {
-          wasquoted = true;
-        }
-      }
-
-    }
-    if (cp < list.length())
-    {
-      String c = list.substring(cp);
-      if (wasescaped || wasquoted)
-      {
-        // append final separator
-        jv.set(jv.size() - 1, lstitem + separator + c);
-      }
-      else
-      {
-        if (!c.equals(separator))
-        {
-          jv.add(c);
-        }
-      }
-    }
-    if (jv.size() > 0)
-    {
-      String[] v = jv.toArray(new String[jv.size()]);
-      jv.clear();
-      if (debug)
-      {
-        System.err.println("Array from '" + separator
-                + "' separated List:\n" + v.length);
-        for (int i = 0; i < v.length; i++)
-        {
-          System.err.println("item " + i + " '" + v[i] + "'");
-        }
-      }
-      return v;
-    }
-    if (debug)
-    {
-      System.err.println("Empty Array from '" + separator
-              + "' separated List");
-    }
-    return null;
-  }
-
-  /**
-   * concatenate the list with separator
-   * 
-   * @param list
-   * @param separator
-   * @return concatenated string
-   */
-  public static String arrayToSeparatorList(String[] list, String separator)
-  {
-    StringBuffer v = new StringBuffer();
-    if (list != null && list.length > 0)
-    {
-      for (int i = 0, iSize = list.length; i < iSize; i++)
-      {
-        if (list[i] != null)
-        {
-          if (v.length() > 0)
-          {
-            v.append(separator);
-          }
-          // TODO - escape any separator values in list[i]
-          v.append(list[i]);
-        }
-      }
-      if (debug)
-      {
-        System.err.println("Returning '" + separator
-                + "' separated List:\n");
-        System.err.println(v);
-      }
-      return v.toString();
-    }
-    if (debug)
-    {
-      System.err.println("Returning empty '" + separator
-              + "' separated List\n");
-    }
-    return "" + separator;
-  }
-
   /**
    * parse a string containing a list of service properties and configure the
    * service description
@@ -490,7 +373,7 @@ public class RestServiceDescription
   private boolean configureFromServiceInputProperties(String propList,
           StringBuffer warnings)
   {
-    String[] props = separatorListToArray(propList, ",");
+    String[] props = StringUtils.separatorListToArray(propList, ",");
     if (props == null)
     {
       return true;
@@ -608,7 +491,8 @@ public class RestServiceDescription
     ;
     vls.add(new String("gapCharacter='" + gapCharacter + "'"));
     vls.add(new String("returns='" + _genOutputFormatString() + "'"));
-    return arrayToSeparatorList(vls.toArray(new String[0]), ",");
+    return StringUtils
+            .arrayToSeparatorList(vls.toArray(new String[0]), ",");
   }
 
   public String toString()
@@ -651,7 +535,7 @@ public class RestServiceDescription
   public boolean configureFromEncodedString(String encoding,
           StringBuffer warnings)
   {
-    String[] list = separatorListToArray(encoding, "|");
+    String[] list = StringUtils.separatorListToArray(encoding, "|");
 
     int nextpos = parseServiceList(list, warnings, 0);
     if (nextpos > 0)
@@ -752,8 +636,8 @@ public class RestServiceDescription
             url.append("$");
             url.append(param.getValue().getURLtokenPrefix());
             url.append(":");
-            url.append(arrayToSeparatorList(vals.toArray(new String[0]),
-                    ","));
+            url.append(StringUtils.arrayToSeparatorList(
+                    vals.toArray(new String[0]), ","));
             url.append("$");
           }
         }
@@ -779,8 +663,7 @@ public class RestServiceDescription
     boolean valid = true;
     int lastp = 0;
     String url = new String();
-    Matcher prms = Pattern.compile("([?&])([A-Za-z0-9_]+)=\\$([^$]+)\\$")
-            .matcher(ipurl);
+    Matcher prms = PARAM_ENCODED_URL_PATTERN.matcher(ipurl);
     Map<String, InputType> iparams = new Hashtable<String, InputType>();
     InputType jinput;
     while (prms.find())
@@ -822,8 +705,7 @@ public class RestServiceDescription
   public static Class[] getInputTypes()
   {
     // TODO - find a better way of maintaining this classlist
-    return new Class[]
-    { jalview.ws.rest.params.Alignment.class,
+    return new Class[] { jalview.ws.rest.params.Alignment.class,
         jalview.ws.rest.params.AnnotationFile.class,
         SeqGroupIndexVector.class,
         jalview.ws.rest.params.SeqIdVector.class,
@@ -841,11 +723,12 @@ public class RestServiceDescription
     {
       try
       {
-        jinput = (InputType) (type.getConstructor().newInstance(null));
+        jinput = (InputType) (type.getConstructor().newInstance());
         if (iprm.equalsIgnoreCase(jinput.getURLtokenPrefix()))
         {
           ArrayList<String> al = new ArrayList<String>();
-          for (String prprm : separatorListToArray(iprmparams, ","))
+          for (String prprm : StringUtils.separatorListToArray(iprmparams,
+                  ","))
           {
             // hack to ensure that strings like "sep=','" containing unescaped
             // commas as values are concatenated
@@ -996,7 +879,7 @@ public class RestServiceDescription
   public static List<RestServiceDescription> parseDescriptions(
           String services) throws Exception
   {
-    String[] list = separatorListToArray(services, "|");
+    String[] list = StringUtils.separatorListToArray(services, "|");
     List<RestServiceDescription> svcparsed = new ArrayList<RestServiceDescription>();
     int p = 0, lastp = 0;
     StringBuffer warnings = new StringBuffer();