urllength limit and more efficient url construction
authorjprocter <Jim Procter>
Fri, 2 Apr 2010 10:33:54 +0000 (10:33 +0000)
committerjprocter <Jim Procter>
Fri, 2 Apr 2010 10:33:54 +0000 (10:33 +0000)
src/jalview/util/GroupUrlLink.java

index 55e3bd9..0401bb2 100644 (file)
@@ -410,6 +410,8 @@ public class GroupUrlLink
    */\r
   protected Object[] makeUrlsIf(boolean createFullUrl, Hashtable repstrings, boolean onlyIfMatches)\r
   {\r
+    int pass = 0;\r
+    \r
     // prepare string arrays in correct order to be assembled into URL input\r
     String[][] idseq = new String[mtch.length][]; // indexed by pass\r
     int mins = 0, maxs = 0; // allowed two values, 1 or n-sequences.\r
@@ -444,7 +446,6 @@ public class GroupUrlLink
                 "Cannot have zero length vector of replacement strings - either 1 value or n values.");\r
       }\r
     }\r
-    int pass = 0;\r
     // iterate through input, collating segments to be inserted into url\r
     StringBuffer matched[] = new StringBuffer[idseq.length];\r
     // and precompile regexes\r
@@ -462,7 +463,14 @@ public class GroupUrlLink
         rgxs[pass] = null;\r
       }\r
     }\r
-    // record which of the input sequences were actually used to generate the\r
+    // tot up the invariant lengths for this url\r
+    int urllength = url_prefix.length();\r
+    for (pass=0;pass<matched.length; pass++)\r
+    {\r
+      urllength+=url_suffix[pass].length();\r
+    }\r
+\r
+    // flags to record which of the input sequences were actually used to generate the\r
     // url\r
     boolean[] thismatched = new boolean[maxs];\r
     int seqsmatched = 0;\r
@@ -470,10 +478,10 @@ public class GroupUrlLink
     {\r
       // initialise flag for match\r
       thismatched[sq] = false;\r
-      String[] thematches = new String[rgxs.length];\r
+      StringBuffer[] thematches = new StringBuffer[rgxs.length];\r
       for (pass = 0; pass < rgxs.length; pass++)\r
       {\r
-        thematches[pass] = ""; // initialise - in case there are no more\r
+        thematches[pass] = new StringBuffer(); // initialise - in case there are no more\r
         // matches.\r
         // if a regex is provided, then it must match for all sequences in all\r
         // tokens for it to be considered.\r
@@ -491,6 +499,7 @@ public class GroupUrlLink
           {\r
             rematchat = rg.matchedTo();\r
             thismatched[sq] |= true;\r
+            urllength+=rg.charsMatched(); // count length\r
             if (!createFullUrl)\r
             {\r
               continue; // don't bother making the URL replacement text.\r
@@ -499,7 +508,7 @@ public class GroupUrlLink
             int ns = rg.numSubs();\r
             if (ns == 0)\r
             {\r
-              thematches[pass] += rg.stringMatched();// take whole regex\r
+              thematches[pass].append(rg.stringMatched());// take whole regex\r
             }\r
             /*\r
              * else if (ns==1) { // take only subgroup match return new String[]\r
@@ -513,12 +522,12 @@ public class GroupUrlLink
             else\r
             {\r
               // debug\r
-              for (int s = 0; s <= rg.numSubs(); s++)\r
+              /*for (int s = 0; s <= rg.numSubs(); s++)\r
               {\r
                 System.err.println("Sub " + s + " : " + rg.matchedFrom(s)\r
                         + " : " + rg.matchedTo(s) + " : '"\r
                         + rg.stringMatched(s) + "'");\r
-              }\r
+              }*/\r
               // try to collate subgroup matches\r
               StringBuffer subs = new StringBuffer();\r
               // have to loop through submatches, collating them at top level\r
@@ -533,12 +542,12 @@ public class GroupUrlLink
                   // s is top level submatch. search for submatches enclosed by\r
                   // this one\r
                   int r = s + 1;\r
-                  String rmtch = "";\r
+                  StringBuffer rmtch = new StringBuffer();\r
                   while (r <= ns && rg.matchedTo(r) <= rg.matchedTo(s))\r
                   {\r
                     if (rg.matchedFrom(r) > -1)\r
                     {\r
-                      rmtch += rg.stringMatched(r);\r
+                      rmtch.append(rg.stringMatched(r));\r
                     }\r
                     r++;\r
                   }\r
@@ -557,7 +566,7 @@ public class GroupUrlLink
                   s++;\r
                 }\r
               }\r
-              thematches[pass] += subs.toString();\r
+              thematches[pass].append(subs);\r
             }\r
           }\r
         }\r
@@ -567,9 +576,10 @@ public class GroupUrlLink
           if (!onlyIfMatches)\r
           {\r
             thismatched[sq] |= true;\r
+            urllength+=idseq[pass][sq].length(); // tot up length\r
             if (createFullUrl)\r
             {\r
-            thematches[pass] = idseq[pass][sq]; // take whole string -\r
+            thematches[pass] = new StringBuffer(idseq[pass][sq]); // take whole string -\r
             // regardless - probably not a\r
             // good idea!\r
             /*\r
@@ -609,6 +619,12 @@ public class GroupUrlLink
       // no matches - no url generated\r
       return null;\r
     }\r
+    // check if we are beyond the feasible command line string limit for this platform\r
+    if ((urllength+32)>Platform.getMaxCommandLineLength())\r
+    {\r
+      System.err.println("URL estimated to be too long "+urllength);\r
+      return null;\r
+    }\r
     if (!createFullUrl)\r
     {\r
       // just return the essential info about what the URL would be generated from\r
@@ -617,6 +633,7 @@ public class GroupUrlLink
                         { seqsmatched }, thismatched};\r
     }\r
     // otherwise, create the URL completely.\r
+    \r
     StringBuffer submiturl = new StringBuffer();\r
     submiturl.append(url_prefix);\r
     for (pass = 0; pass < matched.length; pass++)\r