hostenv_rhino.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. Copyright (c) 2004-2012, The Dojo Foundation All Rights Reserved.
  3. Available via Academic Free License >= 2.1 OR the modified BSD license.
  4. see: http://dojotoolkit.org/license for details
  5. */
  6. /*
  7. * Rhino host environment
  8. */
  9. if(dojo.config["baseUrl"]){
  10. dojo.baseUrl = dojo.config["baseUrl"];
  11. }else{
  12. dojo.baseUrl = "./";
  13. }
  14. dojo.locale = dojo.locale || String(java.util.Locale.getDefault().toString().replace('_','-').toLowerCase());
  15. dojo._name = 'rhino';
  16. dojo.isRhino = true;
  17. if(typeof print == "function"){
  18. console.debug = print;
  19. }
  20. if(!("byId" in dojo)){
  21. dojo.byId = function(id, doc){
  22. if(id && (typeof id == "string" || id instanceof String)){
  23. if(!doc){ doc = document; }
  24. return doc.getElementById(id);
  25. }
  26. return id; // assume it's a node
  27. }
  28. }
  29. dojo._isLocalUrl = function(/*String*/ uri) {
  30. // summary:
  31. // determines if URI is local or not.
  32. var local = (new java.io.File(uri)).exists();
  33. if(!local){
  34. var stream;
  35. //Try remote URL. Allow this method to throw,
  36. //but still do cleanup.
  37. try{
  38. // try it as a file first, URL second
  39. stream = (new java.net.URL(uri)).openStream();
  40. // close the stream so we don't leak resources
  41. stream.close();
  42. }finally{
  43. if(stream && stream.close){
  44. stream.close();
  45. }
  46. }
  47. }
  48. return local;
  49. }
  50. // see comments in spidermonkey loadUri
  51. dojo._loadUri = function(uri, cb){
  52. if(dojo._loadedUrls[uri]){
  53. return true; // Boolean
  54. }
  55. try{
  56. var local;
  57. try{
  58. local = dojo._isLocalUrl(uri);
  59. }catch(e){
  60. // no debug output; this failure just means the uri was not found.
  61. return false;
  62. }
  63. dojo._loadedUrls[uri] = true;
  64. //FIXME: Use Rhino 1.6 native readFile/readUrl if available?
  65. if(cb){
  66. var contents = (local ? readText : readUri)(uri, "UTF-8");
  67. // patch up the input to eval until https://bugzilla.mozilla.org/show_bug.cgi?id=471005 is fixed.
  68. if(!eval("'\u200f'").length){
  69. contents = String(contents).replace(/[\u200E\u200F\u202A-\u202E]/g, function(match){
  70. return "\\u" + match.charCodeAt(0).toString(16);
  71. })
  72. }
  73. contents = /^define\(/.test(contents) ? contents : '('+contents+')';
  74. cb(eval(contents));
  75. }else{
  76. load(uri);
  77. }
  78. dojo._loadedUrls.push(uri);
  79. return true;
  80. }catch(e){
  81. dojo._loadedUrls[uri] = false;
  82. console.debug("rhino load('" + uri + "') failed. Exception: " + e);
  83. return false;
  84. }
  85. }
  86. dojo.exit = function(exitcode){
  87. quit(exitcode);
  88. }
  89. // reading a file from disk in Java is a humiliating experience by any measure.
  90. // Lets avoid that and just get the freaking text
  91. function readText(path, encoding){
  92. encoding = encoding || "utf-8";
  93. // NOTE: we intentionally avoid handling exceptions, since the caller will
  94. // want to know
  95. var jf = new java.io.File(path);
  96. var is = new java.io.FileInputStream(jf);
  97. return dj_readInputStream(is, encoding);
  98. }
  99. function readUri(uri, encoding){
  100. var conn = (new java.net.URL(uri)).openConnection();
  101. encoding = encoding || conn.getContentEncoding() || "utf-8";
  102. var is = conn.getInputStream();
  103. return dj_readInputStream(is, encoding);
  104. }
  105. function dj_readInputStream(is, encoding){
  106. var input = new java.io.BufferedReader(new java.io.InputStreamReader(is, encoding));
  107. try {
  108. var sb = new java.lang.StringBuffer();
  109. var line = "";
  110. while((line = input.readLine()) !== null){
  111. sb.append(line);
  112. sb.append(java.lang.System.getProperty("line.separator"));
  113. }
  114. return sb.toString();
  115. } finally {
  116. input.close();
  117. }
  118. }
  119. dojo._getText = function(/*URI*/ uri, /*Boolean*/ fail_ok){
  120. // summary: Read the contents of the specified uri and return those contents.
  121. // uri:
  122. // A relative or absolute uri.
  123. // fail_ok:
  124. // Default false. If fail_ok and loading fails, return null
  125. // instead of throwing.
  126. // returns: The response text. null is returned when there is a
  127. // failure and failure is okay (an exception otherwise)
  128. try{
  129. var local = dojo._isLocalUrl(uri);
  130. var text = (local ? readText : readUri)(uri, "UTF-8");
  131. if(text !== null){
  132. //Force JavaScript string.
  133. text += "";
  134. }
  135. return text;
  136. }catch(e){
  137. if(fail_ok){
  138. return null;
  139. }else{
  140. throw e;
  141. }
  142. }
  143. }
  144. // summary:
  145. // return the document object associated with the dojo.global
  146. dojo.doc = typeof document != "undefined" ? document : null;
  147. dojo.body = function(){
  148. return document.body;
  149. }
  150. // Supply setTimeout/clearTimeout implementations if they aren't already there
  151. // Note: this assumes that we define both if one is not provided... there might
  152. // be a better way to do this if there is a use case where one is defined but
  153. // not the other
  154. if(typeof setTimeout == "undefined" || typeof clearTimeout == "undefined"){
  155. dojo._timeouts = [];
  156. clearTimeout = function(idx){
  157. if(!dojo._timeouts[idx]){ return; }
  158. dojo._timeouts[idx].stop();
  159. }
  160. setTimeout = function(func, delay){
  161. // summary: provides timed callbacks using Java threads
  162. var def={
  163. sleepTime:delay,
  164. hasSlept:false,
  165. run:function(){
  166. if(!this.hasSlept){
  167. this.hasSlept=true;
  168. java.lang.Thread.currentThread().sleep(this.sleepTime);
  169. }
  170. try{
  171. func();
  172. }catch(e){
  173. console.debug("Error running setTimeout thread:" + e);
  174. }
  175. }
  176. };
  177. var runnable = new java.lang.Runnable(def);
  178. var thread = new java.lang.Thread(runnable);
  179. thread.start();
  180. return dojo._timeouts.push(thread)-1;
  181. }
  182. }
  183. //Register any module paths set up in djConfig. Need to do this
  184. //in the hostenvs since hostenv_browser can read djConfig from a
  185. //script tag's attribute.
  186. if(dojo.config["modulePaths"]){
  187. for(var param in dojo.config["modulePaths"]){
  188. dojo.registerModulePath(param, dojo.config["modulePaths"][param]);
  189. }
  190. }