PageRenderTime 46ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/VisualJavaFX/JavaFxCodeParsers/src/pl/edu/amu/wmi/kino/visualjavafx/javafxcodeparsers/tools/InstructionDivider.java

http://visualjavafx.googlecode.com/
Java | 172 lines | 112 code | 14 blank | 46 comment | 51 complexity | 8d7aec39106ce3725fc62e40cd7d4bfb MD5 | raw file
  1. /*
  2. * To change this template, choose Tools | Templates
  3. * and open the template in the editor.
  4. */
  5. package pl.edu.amu.wmi.kino.visualjavafx.javafxcodeparsers.tools;
  6. import java.util.ArrayList;
  7. /**
  8. * I've created you a method that cam divide document into instructions
  9. * @author psychollek
  10. */
  11. public class InstructionDivider {
  12. int i = 0;
  13. /**
  14. * this method divides you a proper JavaFx (or java !) document into
  15. * instructions, you can have two three of them :
  16. * 1. simple instruction like:
  17. * SomeObject.someMethod(SomeArgument);
  18. * which is represented as string:
  19. * "SomeObject.someMethod(SomeArgument)"
  20. * 2. keword with block of instructions like:
  21. * class SomeClass{
  22. * instruction1;
  23. * instruction2;
  24. * ....
  25. * instructionN;
  26. * }
  27. * which is represented as x = Object[2]
  28. * with x[0] containing String:
  29. * "class SomeClass"
  30. * and x[1] containing array of instructions
  31. * in case the block would be simply standing alone:
  32. * {
  33. * instruction1;
  34. * instruction2;
  35. * ....
  36. * instructionN;
  37. * }
  38. * the x[0] would consist of empty string.
  39. *
  40. * other cases :
  41. * a) the [something] is passed just as it would be a string in " " - if you
  42. * have a need to parse the content of it - simply put it through this
  43. * method - that is for now at least.
  44. * b) same with ( ) pairs - for example :
  45. * for(x;y;z){a;b;c;} will get you {"for(x;y;z)",{"a","b",c"}}
  46. * as this is in fact one instruction !
  47. *
  48. * @param in the String representing the document to be parsed
  49. * @return array of objects - instructions;
  50. */
  51. public synchronized Object[] divide(String in){
  52. int end = in.length();
  53. ArrayList<Object> arr = new ArrayList<Object>();
  54. String tmp = new String();
  55. while (i < end && in.charAt(i) != '}'){
  56. if (in.regionMatches(i, "//", 0, 2)){ // comments are thrown out
  57. while ( i!=end && in.charAt(i) != '\n' ){
  58. i++;
  59. }
  60. }else if(in.regionMatches(i, "/*", 0, 2)){ //multiline comments as well
  61. while ((i+1)!=end && !(in.regionMatches(i, "*/", 0, 2)) ){
  62. i++;
  63. }
  64. i++;
  65. }else if(in.charAt(i) == ';'){ // method divider ! ;]
  66. arr.add(tmp.trim());
  67. tmp = new String();
  68. i++; // the ';' char
  69. }else if(in.charAt(i) == '"'){ // strings are chached
  70. tmp = tmp + in.charAt(i);
  71. i++;
  72. while ( i!=end && in.charAt(i) != '"' ){
  73. tmp = tmp = tmp + in.charAt(i);
  74. i++;
  75. }
  76. tmp = tmp + in.charAt(i);
  77. i++;
  78. }else if(in.charAt(i) == '\''){ // chars/strings are cathed
  79. tmp = tmp + in.charAt(i);
  80. i++;
  81. while ( i!=end && in.charAt(i) != '\'' ){
  82. tmp = tmp = tmp + in.charAt(i);
  83. i++;
  84. }
  85. tmp = tmp + in.charAt(i);
  86. i++;
  87. }else if(in.charAt(i) == '('){ // content in () pairs is catched
  88. tmp = tmp + in.charAt(i);
  89. i++;
  90. int j = 0;
  91. while ( i!=end && (in.charAt(i) != ')' || j>0 ) ){
  92. if (in.charAt(i) == '('){
  93. j++;
  94. }else if(in.charAt(i) == ')'){
  95. j--;
  96. }
  97. tmp = tmp + in.charAt(i);
  98. i++;
  99. }
  100. tmp = tmp + in.charAt(i);
  101. i++;
  102. }else if(in.charAt(i) == '['){ // content in [] pairs is catched
  103. tmp = tmp + in.charAt(i);
  104. i++;
  105. int j = 0;
  106. while ( i!=end && (in.charAt(i) != ']'|| j>0) ){
  107. if (in.charAt(i) == '['){
  108. j++;
  109. }else if(in.charAt(i) == ']'){
  110. j--;
  111. }
  112. tmp = tmp = tmp + in.charAt(i);
  113. i++;
  114. }
  115. tmp = tmp + in.charAt(i);
  116. i++;
  117. }else if(in.charAt(i) == '{'){ // blocks of code are catched and converted
  118. i++; // the '{' char
  119. Object[] x = new Object[2];
  120. x[0] = tmp.trim();
  121. tmp = new String();
  122. x[1] = divide(in);
  123. arr.add(x);
  124. i++; // the '}' char
  125. }else if( // whitespaces trimmer (only spaces or '\n' though)
  126. (in.charAt(i)=='\r')
  127. ||
  128. (
  129. (
  130. (in.charAt(i)==' ')
  131. ||(in.charAt(i)=='\n')
  132. )
  133. && i==0
  134. )
  135. || (
  136. ((in.charAt(i) == ' ')
  137. || (in.charAt(i) == '\n')
  138. )
  139. && (
  140. (in.charAt(i-1) == ' ')
  141. || (in.charAt(i-1)=='\n')
  142. || (in.charAt(i-1)==';') //added to fix ;\n problem
  143. )
  144. )
  145. ){
  146. i++;
  147. }else if(in.charAt(i)=='\n'){ // if encounter '\n' that is int a whitespace - convert it to ' '
  148. tmp = tmp + in.charAt(i);
  149. i++;
  150. }else{ // and finally actual methods are added
  151. tmp = tmp + in.charAt(i);
  152. i++;
  153. }
  154. }
  155. arr.add(tmp.trim()); // in case there would be unterminated instruction in a block
  156. return arr.toArray();
  157. }
  158. }