Advertisement
Guest User

Parse string with double quote

a guest
May 21st, 2012
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.14 KB | None | 0 0
  1.  // This method will parse the given String which contains the parameters into proper elements for execution.
  2.   // Pre-cond: cmdID must be a valid value. param != null.
  3.   private static String[] parse(int cmdID, String param) {
  4.     String[] output; // Output variable
  5.    
  6.     param = param.replaceAll("\"", " \" ").trim(); // Separate all double quotes from other part of the parameter, remove leading and trailing spaces.
  7.     String[] fragments = param.split("\\s+"); // Split the string containing parameters up into fragments. It guarantees no empty string.
  8.    
  9.     int curr = 0; // Pointer to current String. All String before this pointer are complete names.
  10.     // Note: Complete name is a String between a pair of quote, or a word without spaces and double quotes.
  11.     boolean matched = fragments[curr].matches("[^\"]*"); // Flag to check whether the last element is a complete name or not.
  12.     if (matched) curr++; // Go to next element if already a complete name.
  13.    
  14.     boolean cont = true; // Continue scanning
  15.    
  16.     switch (cmdID) { // Since we do first check outside the loop, we must do a check here just in case.
  17.       case 5:  // help
  18.       case 7:  // numroute
  19.       case 11: // listadjacentlocations
  20.         if (curr == 1) cont = false; // Number of parameters needed: 1
  21.     }
  22.    
  23.     // This loop will try to augment the fragments to form proper parameters.
  24.     for (int i = 1; i < fragments.length && cont; i++) {
  25.       if (!matched) // If the current String is not complete name.
  26.         fragments[curr] = fragments[curr] + " " + fragments[i]; // Add the next fragment. This method ensures 1 space only between 2 words of a name.
  27.       // Else, the current String is a new fragment.
  28.      
  29.       if (!fragments[curr].matches("(\"[^\"]*\"|[^\"]*)"))
  30.         matched = false;
  31.       else { // If complete name
  32.         matched = true;
  33.        
  34.         if (fragments[curr].matches("\"[^\"]*\"")) // If the name is between quotes
  35.           fragments[curr] = fragments[curr].substring(1, fragments[curr].length() - 1).trim(); // Remove quotes and leading and trailing spaces.
  36.        
  37.         if (fragments[curr].length() != 0) // If the String is not empty
  38.           curr++; // Use next slot. Else, use current slot.
  39.        
  40.         switch (cmdID) { // This statement will impose the limit on the number of parameters to scan for
  41.           case 2: // addroute
  42.             if (curr == 3) cont = false; break; // Number of parameters needed: 3
  43.           case 3: // distance
  44.           case 9: // deleteroute
  45.             if (curr == 2) cont = false; break; // Number of parameters needed: 2
  46.           case 5:  // help
  47.           case 7:  // numroute
  48.           case 11: // listadjacentlocations
  49.             if (curr == 1) cont = false; // Number of parameters needed: 1
  50.           // case 1, 8: Match up as many parameters as possible. (addlocation, deletelocation)
  51.         }
  52.        
  53.         // Assign next fragment, if any, to current slot.
  54.         if (i + 1 < fragments.length)
  55.           fragments[curr] = fragments[i + 1];
  56.       }
  57.     }
  58.    
  59.     if (matched) { // The last element is a complete name
  60.       switch (cmdID) { // Extra processing for each of the commands (check number of parameters and other extra stuffs)
  61.         case 2: // addroute
  62.           if (curr == 3 && fragments[curr - 1].matches("\\d+")) // There should be 3 parameters and the 3rd parameter must be a number
  63.           return Arrays.copyOf(fragments, curr); break;
  64.         case 3: // distance
  65.         case 9: // deleteroute
  66.           if (curr == 2) // There should be 2 parameters
  67.           return Arrays.copyOf(fragments, curr); break;
  68.         case 5:  // help
  69.         case 7:  // numroute
  70.         case 11: // listadjacentlocations
  71.           if (curr == 1) // There should be 1 parameters
  72.           return Arrays.copyOf(fragments, curr); break;
  73.         case 1: // addlocation
  74.         case 8: // deletelocation
  75.           if (curr >= 1) // There should be more than or equal to 1 parameters
  76.           return Arrays.copyOf(fragments, curr);
  77.       }
  78.     }
  79.    
  80.     return null; // Parameter failure (missing parameter, double-quotes do not match up properly).
  81.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement