Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This method will parse the given String which contains the parameters into proper elements for execution.
- // Pre-cond: cmdID must be a valid value. param != null.
- private static String[] parse(int cmdID, String param) {
- String[] output; // Output variable
- param = param.replaceAll("\"", " \" ").trim(); // Separate all double quotes from other part of the parameter, remove leading and trailing spaces.
- String[] fragments = param.split("\\s+"); // Split the string containing parameters up into fragments. It guarantees no empty string.
- int curr = 0; // Pointer to current String. All String before this pointer are complete names.
- // Note: Complete name is a String between a pair of quote, or a word without spaces and double quotes.
- boolean matched = fragments[curr].matches("[^\"]*"); // Flag to check whether the last element is a complete name or not.
- if (matched) curr++; // Go to next element if already a complete name.
- boolean cont = true; // Continue scanning
- switch (cmdID) { // Since we do first check outside the loop, we must do a check here just in case.
- case 5: // help
- case 7: // numroute
- case 11: // listadjacentlocations
- if (curr == 1) cont = false; // Number of parameters needed: 1
- }
- // This loop will try to augment the fragments to form proper parameters.
- for (int i = 1; i < fragments.length && cont; i++) {
- if (!matched) // If the current String is not complete name.
- fragments[curr] = fragments[curr] + " " + fragments[i]; // Add the next fragment. This method ensures 1 space only between 2 words of a name.
- // Else, the current String is a new fragment.
- if (!fragments[curr].matches("(\"[^\"]*\"|[^\"]*)"))
- matched = false;
- else { // If complete name
- matched = true;
- if (fragments[curr].matches("\"[^\"]*\"")) // If the name is between quotes
- fragments[curr] = fragments[curr].substring(1, fragments[curr].length() - 1).trim(); // Remove quotes and leading and trailing spaces.
- if (fragments[curr].length() != 0) // If the String is not empty
- curr++; // Use next slot. Else, use current slot.
- switch (cmdID) { // This statement will impose the limit on the number of parameters to scan for
- case 2: // addroute
- if (curr == 3) cont = false; break; // Number of parameters needed: 3
- case 3: // distance
- case 9: // deleteroute
- if (curr == 2) cont = false; break; // Number of parameters needed: 2
- case 5: // help
- case 7: // numroute
- case 11: // listadjacentlocations
- if (curr == 1) cont = false; // Number of parameters needed: 1
- // case 1, 8: Match up as many parameters as possible. (addlocation, deletelocation)
- }
- // Assign next fragment, if any, to current slot.
- if (i + 1 < fragments.length)
- fragments[curr] = fragments[i + 1];
- }
- }
- if (matched) { // The last element is a complete name
- switch (cmdID) { // Extra processing for each of the commands (check number of parameters and other extra stuffs)
- case 2: // addroute
- if (curr == 3 && fragments[curr - 1].matches("\\d+")) // There should be 3 parameters and the 3rd parameter must be a number
- return Arrays.copyOf(fragments, curr); break;
- case 3: // distance
- case 9: // deleteroute
- if (curr == 2) // There should be 2 parameters
- return Arrays.copyOf(fragments, curr); break;
- case 5: // help
- case 7: // numroute
- case 11: // listadjacentlocations
- if (curr == 1) // There should be 1 parameters
- return Arrays.copyOf(fragments, curr); break;
- case 1: // addlocation
- case 8: // deletelocation
- if (curr >= 1) // There should be more than or equal to 1 parameters
- return Arrays.copyOf(fragments, curr);
- }
- }
- return null; // Parameter failure (missing parameter, double-quotes do not match up properly).
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement