]> git.wh0rd.org - tt-rss.git/blob - lib/dojo/_base/_loader/hostenv_rhino.js
remove call-by-reference to comply with php 5.4
[tt-rss.git] / lib / dojo / _base / _loader / hostenv_rhino.js
1 /*
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
5 */
6
7
8 /*
9 * Rhino host environment
10 */
11
12 if(dojo.config["baseUrl"]){
13 dojo.baseUrl = dojo.config["baseUrl"];
14 }else{
15 dojo.baseUrl = "./";
16 }
17
18 dojo.locale = dojo.locale || String(java.util.Locale.getDefault().toString().replace('_','-').toLowerCase());
19 dojo._name = 'rhino';
20 dojo.isRhino = true;
21
22 if(typeof print == "function"){
23 console.debug = print;
24 }
25
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);
31 }
32 return id; // assume it's a node
33 }
34 }
35
36 dojo._isLocalUrl = function(/*String*/ uri) {
37 // summary:
38 // determines if URI is local or not.
39
40 var local = (new java.io.File(uri)).exists();
41 if(!local){
42 var stream;
43 //Try remote URL. Allow this method to throw,
44 //but still do cleanup.
45 try{
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
49 stream.close();
50 }finally{
51 if(stream && stream.close){
52 stream.close();
53 }
54 }
55 }
56 return local;
57 }
58
59 // see comments in spidermonkey loadUri
60 dojo._loadUri = function(uri, cb){
61 if(dojo._loadedUrls[uri]){
62 return true; // Boolean
63 }
64 try{
65 var local;
66 try{
67 local = dojo._isLocalUrl(uri);
68 }catch(e){
69 // no debug output; this failure just means the uri was not found.
70 return false;
71 }
72
73 dojo._loadedUrls[uri] = true;
74 //FIXME: Use Rhino 1.6 native readFile/readUrl if available?
75 if(cb){
76 var contents = (local ? readText : readUri)(uri, "UTF-8");
77
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);
82 })
83 }
84 contents = /^define\(/.test(contents) ? contents : '('+contents+')';
85 cb(eval(contents));
86 }else{
87 load(uri);
88 }
89 dojo._loadedUrls.push(uri);
90 return true;
91 }catch(e){
92 dojo._loadedUrls[uri] = false;
93 console.debug("rhino load('" + uri + "') failed. Exception: " + e);
94 return false;
95 }
96 }
97
98 dojo.exit = function(exitcode){
99 quit(exitcode);
100 }
101
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
107 // want to know
108 var jf = new java.io.File(path);
109 var is = new java.io.FileInputStream(jf);
110 return dj_readInputStream(is, encoding);
111 }
112
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);
118 }
119
120 function dj_readInputStream(is, encoding){
121 var input = new java.io.BufferedReader(new java.io.InputStreamReader(is, encoding));
122 try {
123 var sb = new java.lang.StringBuffer();
124 var line = "";
125 while((line = input.readLine()) !== null){
126 sb.append(line);
127 sb.append(java.lang.System.getProperty("line.separator"));
128 }
129 return sb.toString();
130 } finally {
131 input.close();
132 }
133 }
134
135 dojo._getText = function(/*URI*/ uri, /*Boolean*/ fail_ok){
136 // summary: Read the contents of the specified uri and return those contents.
137 // uri:
138 // A relative or absolute uri.
139 // fail_ok:
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)
144 try{
145 var local = dojo._isLocalUrl(uri);
146 var text = (local ? readText : readUri)(uri, "UTF-8");
147 if(text !== null){
148 //Force JavaScript string.
149 text += "";
150 }
151 return text;
152 }catch(e){
153 if(fail_ok){
154 return null;
155 }else{
156 throw e;
157 }
158 }
159 }
160
161 // summary:
162 // return the document object associated with the dojo.global
163 dojo.doc = typeof document != "undefined" ? document : null;
164
165 dojo.body = function(){
166 return document.body;
167 }
168
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
172 // not the other
173 if(typeof setTimeout == "undefined" || typeof clearTimeout == "undefined"){
174 dojo._timeouts = [];
175 clearTimeout = function(idx){
176 if(!dojo._timeouts[idx]){ return; }
177 dojo._timeouts[idx].stop();
178 }
179
180 setTimeout = function(func, delay){
181 // summary: provides timed callbacks using Java threads
182
183 var def={
184 sleepTime:delay,
185 hasSlept:false,
186
187 run:function(){
188 if(!this.hasSlept){
189 this.hasSlept=true;
190 java.lang.Thread.currentThread().sleep(this.sleepTime);
191 }
192 try{
193 func();
194 }catch(e){
195 console.debug("Error running setTimeout thread:" + e);
196 }
197 }
198 };
199
200 var runnable = new java.lang.Runnable(def);
201 var thread = new java.lang.Thread(runnable);
202 thread.start();
203 return dojo._timeouts.push(thread)-1;
204 }
205 }
206
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]);
213 }
214 }