2 Copyright (c) 2004-2011, 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
9 * Rhino host environment
12 if(dojo.config["baseUrl"]){
13 dojo.baseUrl = dojo.config["baseUrl"];
18 dojo.locale = dojo.locale || String(java.util.Locale.getDefault().toString().replace('_','-').toLowerCase());
22 if(typeof print == "function"){
23 console.debug = print;
26 if(!("byId" in dojo)){
27 dojo.byId = function(id, doc){
28 if(id && (typeof id == "string" || id instanceof String)){
29 if(!doc){ doc = document; }
30 return doc.getElementById(id);
32 return id; // assume it's a node
36 dojo._isLocalUrl = function(/*String*/ uri) {
38 // determines if URI is local or not.
40 var local = (new java.io.File(uri)).exists();
43 //Try remote URL. Allow this method to throw,
44 //but still do cleanup.
46 // try it as a file first, URL second
47 stream = (new java.net.URL(uri)).openStream();
48 // close the stream so we don't leak resources
51 if(stream && stream.close){
59 // see comments in spidermonkey loadUri
60 dojo._loadUri = function(uri, cb){
61 if(dojo._loadedUrls[uri]){
62 return true; // Boolean
67 local = dojo._isLocalUrl(uri);
69 // no debug output; this failure just means the uri was not found.
73 dojo._loadedUrls[uri] = true;
74 //FIXME: Use Rhino 1.6 native readFile/readUrl if available?
76 var contents = (local ? readText : readUri)(uri, "UTF-8");
78 // patch up the input to eval until https://bugzilla.mozilla.org/show_bug.cgi?id=471005 is fixed.
79 if(!eval("'\u200f'").length){
80 contents = String(contents).replace(/[\u200E\u200F\u202A-\u202E]/g, function(match){
81 return "\\u" + match.charCodeAt(0).toString(16);
84 contents = /^define\(/.test(contents) ? contents : '('+contents+')';
89 dojo._loadedUrls.push(uri);
92 dojo._loadedUrls[uri] = false;
93 console.debug("rhino load('" + uri + "') failed. Exception: " + e);
98 dojo.exit = function(exitcode){
102 // reading a file from disk in Java is a humiliating experience by any measure.
103 // Lets avoid that and just get the freaking text
104 function readText(path, encoding){
105 encoding = encoding || "utf-8";
106 // NOTE: we intentionally avoid handling exceptions, since the caller will
108 var jf = new java.io.File(path);
109 var is = new java.io.FileInputStream(jf);
110 return dj_readInputStream(is, encoding);
113 function readUri(uri, encoding){
114 var conn = (new java.net.URL(uri)).openConnection();
115 encoding = encoding || conn.getContentEncoding() || "utf-8";
116 var is = conn.getInputStream();
117 return dj_readInputStream(is, encoding);
120 function dj_readInputStream(is, encoding){
121 var input = new java.io.BufferedReader(new java.io.InputStreamReader(is, encoding));
123 var sb = new java.lang.StringBuffer();
125 while((line = input.readLine()) !== null){
127 sb.append(java.lang.System.getProperty("line.separator"));
129 return sb.toString();
135 dojo._getText = function(/*URI*/ uri, /*Boolean*/ fail_ok){
136 // summary: Read the contents of the specified uri and return those contents.
138 // A relative or absolute uri.
140 // Default false. If fail_ok and loading fails, return null
141 // instead of throwing.
142 // returns: The response text. null is returned when there is a
143 // failure and failure is okay (an exception otherwise)
145 var local = dojo._isLocalUrl(uri);
146 var text = (local ? readText : readUri)(uri, "UTF-8");
148 //Force JavaScript string.
162 // return the document object associated with the dojo.global
163 dojo.doc = typeof document != "undefined" ? document : null;
165 dojo.body = function(){
166 return document.body;
169 // Supply setTimeout/clearTimeout implementations if they aren't already there
170 // Note: this assumes that we define both if one is not provided... there might
171 // be a better way to do this if there is a use case where one is defined but
173 if(typeof setTimeout == "undefined" || typeof clearTimeout == "undefined"){
175 clearTimeout = function(idx){
176 if(!dojo._timeouts[idx]){ return; }
177 dojo._timeouts[idx].stop();
180 setTimeout = function(func, delay){
181 // summary: provides timed callbacks using Java threads
190 java.lang.Thread.currentThread().sleep(this.sleepTime);
195 console.debug("Error running setTimeout thread:" + e);
200 var runnable = new java.lang.Runnable(def);
201 var thread = new java.lang.Thread(runnable);
203 return dojo._timeouts.push(thread)-1;
207 //Register any module paths set up in djConfig. Need to do this
208 //in the hostenvs since hostenv_browser can read djConfig from a
209 //script tag's attribute.
210 if(dojo.config["modulePaths"]){
211 for(var param in dojo.config["modulePaths"]){
212 dojo.registerModulePath(param, dojo.config["modulePaths"][param]);