PageRenderTime 61ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/src/edu/ucsc/cs/mturk/server/topone/StringParser.java

https://bitbucket.org/khuang1024/toponealgorithm
Java | 148 lines | 108 code | 22 blank | 18 comment | 23 complexity | 37b1d43868eb96712cdadc487ade3a52 MD5 | raw file
  1. package edu.ucsc.cs.mturk.server.topone;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.HashMap;
  5. class StringParser {
  6. // Suppress default constructor for noninstantiability.
  7. private StringParser() {
  8. throw new AssertionError();
  9. }
  10. /* Parse the request string.*/
  11. static HashMap<String, String> parseToMap(String str) {
  12. HashMap<String, String> hm = new HashMap<String, String>();
  13. String[] key_value = str.split("&");
  14. for(String kv: key_value){
  15. String[] s = kv.split("=");
  16. if (s.length > 2) {
  17. throw new TopOneServerException("Illegal request string." +
  18. "Please check the format of request string " +
  19. "you sent. It must be in the CGI style.");
  20. }
  21. if (s.length != 1) {
  22. hm.put(s[0], s[1]);
  23. }
  24. }
  25. return hm;
  26. }
  27. /* Extract the questions.*/
  28. static ArrayList<Object> extractQuestions(
  29. HashMap<String, String> hm) {
  30. return extractString(hm, "q");
  31. }
  32. /* Extract the questions.*/
  33. static ArrayList<Object> extractAnswers(
  34. HashMap<String, String> hm) {
  35. return extractString(hm, "a");
  36. }
  37. /* Extract the number of inputs of a HIT.*/
  38. static int extractNumberOfInputs(HashMap<String, String> hm) {
  39. return extractNumber(hm, "nInput");
  40. }
  41. /* Extract the number of outputs of a HIT.*/
  42. static int extractNumberOfOutputs(HashMap<String, String> hm) {
  43. return extractNumber(hm, "nOutput");
  44. }
  45. /* Extract the number of assignments of a normal HIT.*/
  46. static int extractNumberOfAssignments(HashMap<String, String> hm) {
  47. return extractNumber(hm, "nAssignment");
  48. }
  49. /* Extract the number of assignments of a tie-solving HIT.*/
  50. static int extractNumberOfTieAssignments(HashMap<String, String> hm) {
  51. return extractNumber(hm, "nTieAssignment");
  52. }
  53. /* Extract isShuffled. If it is omitted, return true.*/
  54. static boolean extractIsShuffled(HashMap<String, String> hm) {
  55. return extractBoolean(hm, "isShffuled");
  56. }
  57. /* Extract isLogged. If it is omitted, return true.*/
  58. static boolean extractisLogged(HashMap<String, String> hm) {
  59. return extractBoolean(hm, "isLogged");
  60. }
  61. /* Extract the jobId.*/
  62. static String extractJobId(HashMap<String, String> hm) {
  63. String jobId;
  64. if (hm.get("jobId") == null) {
  65. jobId = Integer.toString(new Date().hashCode());
  66. } else {
  67. jobId = hm.get("jobId");
  68. }
  69. return jobId;
  70. }
  71. /* Extract an integer by given key.*/
  72. static private int extractNumber(HashMap<String, String> hm,
  73. String numberName) {
  74. int number = 0;
  75. // Check if the HashMap contains "numberName".
  76. if (hm.get(numberName) == null) {
  77. throw new TopOneServerException("You must indicate " +
  78. numberName + ".");
  79. }
  80. // Check if numberName is a number.
  81. try {
  82. number = Integer.parseInt(hm.get(numberName));
  83. } catch (NumberFormatException e) {
  84. throw new TopOneServerException(numberName +
  85. " must be an integer.");
  86. }
  87. return number;
  88. }
  89. static private ArrayList<Object> extractString(HashMap<String, String> hm, String qa) {
  90. ArrayList<Object> extractedString = new ArrayList<Object>();
  91. if ((!qa.equals("q")) && (!qa.equals("a"))) {
  92. throw new TopOneServerException ("Question or Answer not found!" +
  93. " The parameters related to question must have prefix q." +
  94. " The parameters related to answer must have prefix a.");
  95. }
  96. // Extract qnum/anum.
  97. int num = extractNumber(hm, qa+"num");
  98. // Check if qnum/anum is greater than 0.
  99. if (num <= 0) {
  100. throw new TopOneServerException(qa + "num must be greater than 0.");
  101. }
  102. // Parse the questions/answers.
  103. for (int i = 0; i < num; i++) {
  104. if (hm.get(qa + i) == null) {
  105. throw new TopOneServerException(qa + i + " is not found.");
  106. } else {
  107. extractedString.add(hm.get(qa + i));
  108. }
  109. }
  110. return extractedString;
  111. }
  112. /* Extract a boolean.*/
  113. static private boolean extractBoolean(HashMap<String, String> hm,
  114. String booleanName) {
  115. if (hm.get(booleanName) == null) {
  116. return true;
  117. } else if (hm.get(booleanName).equals("true")) {
  118. return true;
  119. } else if (hm.get(booleanName).equals("false")) {
  120. return false;
  121. } else {
  122. throw new TopOneServerException(booleanName + " must be true or " +
  123. "false or omitted.");
  124. }
  125. }
  126. }