PageRenderTime 45ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/umpleonline/scripts/compiler.php

http://umple.googlecode.com/
PHP | 426 lines | 171 code | 25 blank | 230 comment | 50 complexity | 78d337adbee16552cfa16bfbf58d49c7 MD5 | raw file
Possible License(s): MIT, LGPL-2.1, BSD-3-Clause, GPL-2.0
  1. <?php
  2. // Copyright: All contributers to the Umple Project
  3. // This file is made available subject to the open source license found at:
  4. // http://umple.org/license
  5. //
  6. // Invoked on the UmpleOnline server to cause one of the
  7. // two jars to be run with appropriate arguments (umple.jar or umplesync.jar)
  8. //
  9. // Involed as an Ajax call by umple_action.js in the following places:
  10. // Action.ajax - near the end of that file - for most compilation tasks
  11. // Action.loadFile using ajax.js - argument load - load a user saved file
  12. // Action.loadExample - argument exampleCode - one of the pre-canned examples
  13. // Action.saveNewFile using ajax.js - argument save
  14. // Action.pngImage, Action.ymlImageCallback
  15. // Action.uiguCallback - argument asUI
  16. //
  17. // This file also formats the output from the jars for presentation back on
  18. // the UmpleOnline screen
  19. //
  20. // Most of the helper methods are in compiler_config.php, except
  21. // getErrorHtml, which reads the generated error file, and jsonDecode
  22. //
  23. require_once("compiler_config.php");
  24. if (isset($_REQUEST["save"]))
  25. {
  26. $input = $_REQUEST["umpleCode"];
  27. $filename = saveFile($input);
  28. echo $filename;
  29. }
  30. else if (isset($_REQUEST["load"]))
  31. {
  32. $filename = $_REQUEST["filename"];
  33. $outputUmple = readTemporaryFile($filename);
  34. echo $outputUmple;
  35. }
  36. else if (isset($_REQUEST["action"]))
  37. {
  38. handleUmpleTextChange();
  39. }
  40. else if (isset($_REQUEST["umpleCode"]))
  41. {
  42. $input = $_REQUEST["umpleCode"];
  43. $fulllanguage = $_REQUEST["language"];
  44. // If there are suboptions in the language string, parse them out
  45. // Language stringh could be cpp or cpp.opt1.opt2.opt3
  46. $langparts = explode('.',$fulllanguage);
  47. $language = $langparts[0];
  48. $suboptions = "";
  49. for($i=1; $i<count($langparts); $i++){
  50. $suboptions = $suboptions . " -s " . $langparts[$i];
  51. }
  52. $languageStyle = $_REQUEST["languageStyle"];
  53. $outputErr = isset($_REQUEST["error"])?$_REQUEST["error"]:false;
  54. $uigu = False;
  55. $javadoc = false;
  56. $stateDiagram = false;
  57. $classDiagram = false;
  58. $yumlDiagram = false;
  59. $uigu = false;
  60. $htmlContents = false;
  61. if ($language == "javadoc")
  62. {
  63. $language = "Java";
  64. $javadoc = True;
  65. }
  66. else if ($language == "stateDiagram")
  67. {
  68. $language = "GvStateDiagram";
  69. $stateDiagram = True;
  70. }
  71. else if ($language == "classDiagram")
  72. {
  73. $language = "GvClassDiagram";
  74. $classDiagram = True;
  75. }
  76. else if ($language == "yumlDiagram")
  77. {
  78. $language = "Yuml";
  79. $yumlDiagram = True;
  80. }
  81. else if ($language == "uigu")
  82. {
  83. $language = "Uigu";
  84. $uigu = True;
  85. }
  86. if ($languageStyle == "html")
  87. {
  88. $htmlContents = true;
  89. }
  90. if ($language == "Simulate")
  91. {
  92. $filename = saveFile("generate Php \"./\" --override-all;\n" . $input);
  93. executeCommand("java -jar umple.jar {$filename}");
  94. $filename = saveFile("generate Simulate \"./\" --override-all;\n" . $input);
  95. executeCommand("java -jar umple.jar {$filename}");
  96. // Restore file so it doesn't have the 'generate' command in front
  97. saveFile($input);
  98. $modelId = getFilenameId($filename);
  99. echo $modelId;
  100. return;
  101. }
  102. elseif ($uigu)
  103. {
  104. $filename = saveFile($input);
  105. showUserInterface($filename);
  106. /* chdir("JSFProvider");
  107. if (file_exists("JSFProvider/model.ump"))
  108. unlink("JSFProvider/model.ump");
  109. copy("$filename", "JSFProvider/model.ump");
  110. executeCommand("java -cp GUIModel.jar;JSFProvider.jar;GUIGenerator.jar cruise.generator.UIGU UmpleProject.xml model.ump TempDir TempApp");*/
  111. }
  112. else if ($htmlContents) {
  113. $filename = saveFile($input);
  114. $errorFilename = "{$filename}.erroroutput";
  115. $sourceCode = executeCommand("java -jar umplesync.jar -generate {$language} {$filename} 2> {$errorFilename}");
  116. $errors = getErrorHtml($errorFilename, 0);
  117. if($outputErr == "true")
  118. echo $errors;
  119. echo $sourceCode;
  120. return;
  121. } // end html content
  122. elseif (!in_array($language,array("Php","Java","Ruby","RTCpp","Cpp","Sql","GvStateDiagram","GvClassDiagram","Yuml")))
  123. { // If NOT one of the basic languages, then use umplesync.jar
  124. $filename = saveFile($input);
  125. $errorFilename = "{$filename}.erroroutput";
  126. if ($language == "Experimental-Cpp" || $language == "Experimental-Sql") {
  127. $sourceCode = executeCommand("echo \"{$language} is under development. Output is currently only available to developers of Umple\" 2> {$errorFilename}");
  128. }
  129. else {
  130. $sourceCode = executeCommand("java -jar umplesync.jar -generate {$language} {$filename} 2> {$errorFilename}");
  131. }
  132. $errors = getErrorHtml($errorFilename, 0);
  133. if ($language != "Json")
  134. {
  135. $sourceCode = htmlspecialchars($sourceCode);
  136. }
  137. if($outputErr == "true")
  138. echo $errors ."<p>URL_SPLIT";
  139. echo $sourceCode;
  140. return;
  141. }
  142. if (!$uigu)
  143. { // NOTuigu
  144. // Generate the Java, PHP, RTCpp, Ruby, Cpp or Sql and put it into the right directory
  145. $filename = saveFile("generate {$language} \"./{$language}/\" --override-all;\n" . $input);
  146. $outputFilename = "{$filename}.output";
  147. $errorFilename = "{$filename}.erroroutput";
  148. // Clean up any pre-existing java. php, RTCpp, ruby or cpp files
  149. $thedir = dirname($outputFilename);
  150. $toRemove = False;
  151. $rmcommand = "rm -rf ";
  152. foreach (glob("$thedir/{$language}") as $afile) {
  153. $rmcommand = $rmcommand . " " . $afile;
  154. $toRemove = True;
  155. }
  156. if($toRemove) { exec($rmcommand); }
  157. // The following is a hack. The arguments to umplesync need fixing
  158. if (!$stateDiagram && !$classDiagram && !$yumlDiagram) {
  159. $command = "java -jar umplesync.jar -source {$filename} 1> {$outputFilename} 2> {$errorFilename}";
  160. }
  161. else {
  162. // The following is used for outputting diagrams only
  163. $thedir = dirname($outputFilename);
  164. exec("rm -rf " . $thedir . "/modelcd.gv " . $thedir . "/model.gv");
  165. $command = "java -jar umplesync.jar -generate " . $language . " {$filename} " . $suboptions . " 1> {$outputFilename} 2> {$errorFilename}";
  166. }
  167. exec($command);
  168. // Restore file so it doesn't have the 'generate' command in front
  169. saveFile($input);
  170. $sourceCode = readTemporaryFile($outputFilename);
  171. $sourceCode = str_replace("<?php","",$sourceCode);
  172. $sourceCode = str_replace("?>","",$sourceCode);
  173. $sourceCode = htmlspecialchars($sourceCode);
  174. $errhtml = getErrorHtml($errorFilename);
  175. if ($sourceCode == "")
  176. {
  177. if($input == "//$?[End_of_model]$?") {
  178. $html = "
  179. Please create an Umple model textually (on the left side of the screen)
  180. or visually (on the right side of the screen),
  181. and then choose Generate Code again.";
  182. }
  183. else
  184. {
  185. $html = "
  186. An error occurred interpreting your Umple code, please review it and try again.
  187. If the problem persists, please email the Umple code to
  188. the umple-help google group: umple-help@googlegroups.com";
  189. }
  190. echo $errhtml ."<p>URL_SPLIT" . $html;
  191. }
  192. else
  193. {
  194. if ($javadoc)
  195. {
  196. $thedir = dirname($outputFilename);
  197. $theurldir = "ump/" . basename($thedir);
  198. exec("rm -rf " . $thedir . "/javadoc");
  199. $command = "/usr/bin/javadoc -d " . $thedir . "/javadoc -footer \"Generated by Umple\" " ;
  200. foreach (glob("$thedir/{$language}/*/*/*/*.java") as $afile) {
  201. $command = $command . " " . $afile;
  202. }
  203. foreach (glob("$thedir/{$language}/*/*/*.java") as $afile) {
  204. $command = $command . " " . $afile;
  205. }
  206. foreach (glob("$thedir/{$language}/*/*.java") as $afile) {
  207. $command = $command . " " . $afile;
  208. }
  209. foreach (glob("$thedir/{$language}/*.java") as $afile) {
  210. $command = $command . " " . $afile;
  211. }
  212. exec($command);
  213. exec("cd $thedir; rm javadocFromUmple.zip; /usr/bin/zip -r javadocFromUmple javadoc");
  214. $html = "<a href=\"umpleonline/$thedir/javadocFromUmple.zip\">Download the following as a zip file</a>&nbsp;{$errhtml}
  215. <iframe width=100% height=1000 src=\"" . $theurldir . "/javadoc/\">This browser does not
  216. support iframes, so the javadoc cannot be displayed</iframe>
  217. ";
  218. echo $html;
  219. } // end javadoc
  220. else if ($stateDiagram) {
  221. $thedir = dirname($outputFilename);
  222. exec("rm -rf " . $thedir . "/stateDiagram.svg");
  223. $command = "/usr/local/bin/dot -Tsvg -Gdpi=63 " . $thedir . "/model.gv -o " . $thedir . "/stateDiagram.svg";
  224. exec($command);
  225. $svgcode= readTemporaryFile("{$thedir}/stateDiagram.svg");
  226. $html = "<a href=\"umpleonline/$thedir/model.gv\">Download the GraphViz file for the following</a>&nbsp;<a href=\"umpleonline/$thedir/stateDiagram.svg\">Download the SVG file for the following</a>&nbsp;<br/>{$errhtml}&nbsp;
  227. <svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" height=\"2000\" width=\"2000\">";
  228. echo $html;
  229. echo $svgcode;
  230. echo "</svg>";
  231. } // end state diagram
  232. else if ($classDiagram) {
  233. $thedir = dirname($outputFilename);
  234. exec("rm -rf " . $thedir . "/classDiagram.svg");
  235. $command = "/usr/local/bin/dot -Tsvg -Gdpi=63 " . $thedir . "/modelcd.gv -o " . $thedir . "/classDiagram.svg";
  236. exec($command);
  237. $svgcode= readTemporaryFile("{$thedir}/classDiagram.svg");
  238. $html = "<a href=\"umpleonline/$thedir/modelcd.gv\">Download the GraphViz file for the following</a>&nbsp;<a href=\"umpleonline/$thedir/classDiagram.svg\">Download the SVG file for the following</a>&nbsp;<br/>{$errhtml}&nbsp;
  239. <svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" height=\"2000\" width=\"2000\">";
  240. echo $html;
  241. echo $svgcode;
  242. echo "</svg>";
  243. } // end graphViz class diagram
  244. else if ($yumlDiagram) {
  245. $thedir = dirname($outputFilename);
  246. exec("rm -rf " . $thedir . "/yuml.txt");
  247. $command = "cp " . $thedir . "/model.ump.output " . $thedir . "/yuml.txt";
  248. exec($command);
  249. $html = "<a href=\"umpleonline/$thedir/yuml.txt\">Download the Yuml text for the following</a>&nbsp;{$errhtml}";
  250. if ($sourceCode != "null") {
  251. $html = $html . "
  252. <iframe width=100% height=500 src=\"http://yuml.me/diagram/class/" . $sourceCode . "\">This browser does not
  253. support iframes, so the diagram cannot be displayed</iframe> ";
  254. }
  255. echo $html;
  256. } // end yuml diagram
  257. else // This is where the Java, PHP and other output is placed on the screen
  258. {
  259. exec("cd $thedir; rm {$language}FromUmple.zip; /usr/bin/zip -r {$language}FromUmple {$language}");
  260. echo "<a href=\"umpleonline/$thedir/{$language}FromUmple.zip\">Download the following as a zip file</a>&nbsp;{$errhtml}<p>URL_SPLIT";
  261. echo $sourceCode;
  262. }
  263. }
  264. } // end not UIGU
  265. else
  266. { // is UIGU
  267. $thedir = dirname($filename);
  268. $theurldir = "ump/" . basename($thedir);
  269. $errorFilename = "{$filename}.erroroutput";
  270. $errhtml = getErrorHtml($errorFilename);
  271. $rnd=substr(basename($thedir), 3);
  272. $uiguDir=file_get_contents("uigudir.txt");
  273. $html = "<a href=\"$theurldir/umpleUIGU.war\">Download the generated application as web archive (WAR) file.</a>&nbsp;{$errhtml}
  274. <iframe width=100% height=300 src='$theurldir/'></iframe>
  275. ";
  276. echo $html;
  277. }
  278. } // end request has umpleCode
  279. else if (isset($_REQUEST["exampleCode"]))
  280. {
  281. $filename = "../ump/" . $_REQUEST["exampleCode"];
  282. $outputUmple = readTemporaryFile($filename);
  283. echo $outputUmple;
  284. }
  285. else if (isset($_REQUEST["asImage"]))
  286. {
  287. $asImage = $_REQUEST["asImage"];
  288. $json = json_decode($asImage);
  289. showUmlImage($json);
  290. }
  291. else if (isset($_REQUEST["asYuml"]))
  292. {
  293. showYumlImage($_REQUEST["asYuml"]);
  294. }
  295. else if (isset($_REQUEST["asUI"]))
  296. {
  297. showUserInterface($_REQUEST["asUI"]);
  298. }
  299. else
  300. {
  301. echo "Invalid use of compiler";
  302. }
  303. function getErrorHtml($errorFilename, $offset = 1)
  304. {
  305. $errorMessage = readTemporaryFile($errorFilename);
  306. if($errorMessage != "")
  307. {
  308. $errInfo = jsonDecode($errorMessage);
  309. $errhtml = "<a href='#' id='errorClick'>Show/Hide errors and warnings</a>";
  310. $errhtml .= "<div id='errorRow' colspan='3' >"; // style='display:none'
  311. if($errInfo == null)
  312. {
  313. $errhtml .= "Couldn't read results from the Umple compiler!";
  314. }
  315. else
  316. {
  317. $results = $errInfo["results"];
  318. foreach($results as $result)
  319. {
  320. $url = $result["url"];
  321. $line = intval($result["line"]) - $offset;
  322. $errorCode = $result["errorCode"];
  323. $severityInt = intval($result["severity"]);
  324. if($severityInt > 2) {
  325. $severity = "Warning";
  326. $textcolor = "<font color=\"black\">";
  327. }
  328. else
  329. {
  330. $severity = "Error";
  331. $textcolor = "<font color=\"red\">";
  332. }
  333. $msg = htmlspecialchars($result["message"]);
  334. $errhtml .= $textcolor." {$severity} on <a href=\"javascript:Action.setCaretPosition({$line});Action.updateLineNumberDisplay();\">line {$line}</a> : {$msg}.</font> <i><a href=\"{$url}\" target=\"helppage\">More information ({$errorCode})</a></i></br>";
  335. }
  336. }
  337. $errhtml .= "</div>";
  338. $errhtml .= "<script type=\"text/javascript\">jQuery(\"#errorClick\").click(function(a){a.preventDefault();jQuery(\"#errorRow\").toggle();});</script>";
  339. return $errhtml;
  340. }
  341. return "";
  342. }
  343. // taken from http://php.net/manual/en/function.json-decode.php
  344. function jsonDecode ($json)
  345. {
  346. $json = str_replace(array("\\\\", "\\\""), array("&#92;", "&#34;"), $json);
  347. $parts = preg_split("@(\"[^\"]*\")|([\[\]\{\},:])|\s@is", $json, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
  348. foreach ($parts as $index => $part)
  349. {
  350. if (strlen($part) == 1)
  351. {
  352. switch ($part)
  353. {
  354. case "[":
  355. case "{":
  356. $parts[$index] = "array(";
  357. break;
  358. case "]":
  359. case "}":
  360. $parts[$index] = ")";
  361. break;
  362. case ":":
  363. $parts[$index] = "=>";
  364. break;
  365. case ",":
  366. break;
  367. default:
  368. return null;
  369. }
  370. }
  371. else
  372. {
  373. if ((substr($part, 0, 1) != "\"") || (substr($part, -1, 1) != "\""))
  374. {
  375. return null;
  376. }
  377. }
  378. }
  379. $json = str_replace(array("&#92;", "&#34;", "$"), array("\\\\", "\\\"", "\\$"), implode("", $parts));
  380. return eval("return $json;");
  381. }
  382. ?>