JAL-715 - allow rest service attributes to be exported and imported as a | separated...
[jalview.git] / src / jalview / ws / rest / InputType.java
index 9a4f297..28bfda0 100644 (file)
@@ -8,6 +8,9 @@ import java.io.StringWriter;
 import java.io.UnsupportedEncodingException;
 import java.nio.charset.Charset;
 import java.util.ArrayList;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.http.entity.mime.content.ContentBody;
 import org.apache.http.entity.mime.content.StringBody;
@@ -100,4 +103,70 @@ public abstract class InputType {
   {
     return (inputData==null || inputData.size()==0);
   }
+  /**
+   * return a url encoded version of this parameter's value, or an empty string if the parameter has no ='value' content.
+   * @return
+   */
+  public abstract List<String> getURLEncodedParameter();
+  
+  /**
+   * set the property known as tok, possibly by assigning it with a given val
+   * @param tok
+   * @param val (may be empty or null)
+   * @param warnings place where parse warnings are reported
+   * @return true if property was set
+   */
+  public abstract boolean configureProperty(String tok, String val, StringBuffer warnings);
+  
+  /**
+   * Get unique key for this type of parameter in a URL encoding.
+   * @return the string that prefixes an input parameter of InputType<T> type in the string returned from getURLEncodedParameter
+   */
+  public abstract String getURLtokenPrefix();
+  /**
+   * parse the given token String and set InputParameter properties appropriately
+   * @param tokenstring - urlencoded parameter string as returned from getURLEncodedParameter
+   * @param warnings - place where any warning messages about bad property values are written
+   * @return true if configuration succeeded, false otherwise.
+   */
+  public boolean configureFromURLtokenString(List<String> tokenstring, StringBuffer warnings) {
+      boolean valid=true;
+      for (String tok:tokenstring)
+      {
+        Matcher mtch = Pattern.compile("^([^=]+)=?'?([^']*)?'?").matcher(tok);
+        if (mtch.find()) {
+          try {
+            if (mtch.group(1).equals("min"))
+            {
+              min = Integer.parseInt(mtch.group(2));
+              continue;
+              
+            } else 
+            if (mtch.group(1).equals("max"))
+            {
+                max = Integer.parseInt(mtch.group(2));
+                continue;
+              }
+            }
+          catch (NumberFormatException x)
+              {
+                valid=false;
+                warnings.append("Invalid value for parameter "+mtch.group(1).toLowerCase()+" '"+mtch.group(2)+"' (expected an integer)\n");
+            }
+          
+          valid = valid && configureProperty(mtch.group(1), mtch.group(2), warnings);
+        }
+        }
+      return valid;
+  }
+  public void addBaseParams(ArrayList<String> prms)
+  {
+    // todo : check if replaceids should be a global for the service, rather than for a specific parameter.
+    if (min!=1) {
+      prms.add("min='"+min+"'");
+    }
+    if (max!=0) {
+      prms.add("min='"+max+"'");
+    }
+  }
 }
\ No newline at end of file