]> git.wh0rd.org - tt-rss.git/blame - lib/dojo/_base/_loader/hostenv_rhino.js
build custom layer of Dojo to speed up loading of tt-rss (refs #293)
[tt-rss.git] / lib / dojo / _base / _loader / hostenv_rhino.js
CommitLineData
2f01fe57
AD
1/*
2 Copyright (c) 2004-2010, 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
a089699c
AD
8/*
9* Rhino host environment
10*/
11
2f01fe57 12if(dojo.config["baseUrl"]){
a089699c 13 dojo.baseUrl = dojo.config["baseUrl"];
2f01fe57 14}else{
a089699c 15 dojo.baseUrl = "./";
2f01fe57 16}
a089699c
AD
17
18dojo.locale = dojo.locale || String(java.util.Locale.getDefault().toString().replace('_','-').toLowerCase());
19dojo._name = 'rhino';
20dojo.isRhino = true;
21
22if(typeof print == "function"){
23 console.debug = print;
2f01fe57 24}
a089699c 25
2f01fe57 26if(!("byId" in dojo)){
a089699c
AD
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 }
2f01fe57 34}
a089699c
AD
35
36dojo._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;
2f01fe57 57}
a089699c
AD
58
59// see comments in spidermonkey loadUri
60dojo._loadUri = function(uri, cb){
61 try{
62 var local;
63 try{
64 local = dojo._isLocalUrl(uri);
65 }catch(e){
66 // no debug output; this failure just means the uri was not found.
67 return false;
68 }
69
70 //FIXME: Use Rhino 1.6 native readFile/readUrl if available?
71 if(cb){
72 var contents = (local ? readText : readUri)(uri, "UTF-8");
73
74 // patch up the input to eval until https://bugzilla.mozilla.org/show_bug.cgi?id=471005 is fixed.
75 if(!eval("'\u200f'").length){
76 contents = String(contents).replace(/[\u200E\u200F\u202A-\u202E]/g, function(match){
77 return "\\u" + match.charCodeAt(0).toString(16);
78 })
79 }
80
81 cb(eval('('+contents+')'));
82 }else{
83 load(uri);
84 }
85 return true;
86 }catch(e){
87 console.debug("rhino load('" + uri + "') failed. Exception: " + e);
88 return false;
89 }
2f01fe57 90}
a089699c
AD
91
92dojo.exit = function(exitcode){
93 quit(exitcode);
2f01fe57 94}
a089699c
AD
95
96// reading a file from disk in Java is a humiliating experience by any measure.
97// Lets avoid that and just get the freaking text
98function readText(path, encoding){
99 encoding = encoding || "utf-8";
100 // NOTE: we intentionally avoid handling exceptions, since the caller will
101 // want to know
102 var jf = new java.io.File(path);
103 var is = new java.io.FileInputStream(jf);
104 return dj_readInputStream(is, encoding);
2f01fe57 105}
a089699c
AD
106
107function readUri(uri, encoding){
108 var conn = (new java.net.URL(uri)).openConnection();
109 encoding = encoding || conn.getContentEncoding() || "utf-8";
110 var is = conn.getInputStream();
111 return dj_readInputStream(is, encoding);
2f01fe57 112}
a089699c
AD
113
114function dj_readInputStream(is, encoding){
115 var input = new java.io.BufferedReader(new java.io.InputStreamReader(is, encoding));
116 try {
117 var sb = new java.lang.StringBuffer();
118 var line = "";
119 while((line = input.readLine()) !== null){
120 sb.append(line);
121 sb.append(java.lang.System.getProperty("line.separator"));
122 }
123 return sb.toString();
124 } finally {
125 input.close();
126 }
2f01fe57 127}
a089699c
AD
128
129dojo._getText = function(/*URI*/ uri, /*Boolean*/ fail_ok){
130 // summary: Read the contents of the specified uri and return those contents.
131 // uri:
132 // A relative or absolute uri.
133 // fail_ok:
134 // Default false. If fail_ok and loading fails, return null
135 // instead of throwing.
136 // returns: The response text. null is returned when there is a
137 // failure and failure is okay (an exception otherwise)
138 try{
139 var local = dojo._isLocalUrl(uri);
140 var text = (local ? readText : readUri)(uri, "UTF-8");
141 if(text !== null){
142 //Force JavaScript string.
143 text += "";
144 }
145 return text;
146 }catch(e){
147 if(fail_ok){
148 return null;
149 }else{
150 throw e;
151 }
152 }
2f01fe57 153}
a089699c
AD
154
155// summary:
156// return the document object associated with the dojo.global
157dojo.doc = typeof document != "undefined" ? document : null;
158
159dojo.body = function(){
160 return document.body;
2f01fe57 161}
a089699c
AD
162
163// Supply setTimeout/clearTimeout implementations if they aren't already there
164// Note: this assumes that we define both if one is not provided... there might
165// be a better way to do this if there is a use case where one is defined but
166// not the other
167if(typeof setTimeout == "undefined" || typeof clearTimeout == "undefined"){
168 dojo._timeouts = [];
169 clearTimeout = function(idx){
170 if(!dojo._timeouts[idx]){ return; }
171 dojo._timeouts[idx].stop();
172 }
173
174 setTimeout = function(func, delay){
175 // summary: provides timed callbacks using Java threads
176
177 var def={
178 sleepTime:delay,
179 hasSlept:false,
180
181 run:function(){
182 if(!this.hasSlept){
183 this.hasSlept=true;
184 java.lang.Thread.currentThread().sleep(this.sleepTime);
185 }
186 try{
187 func();
188 }catch(e){
189 console.debug("Error running setTimeout thread:" + e);
190 }
191 }
192 };
193
194 var runnable = new java.lang.Runnable(def);
195 var thread = new java.lang.Thread(runnable);
196 thread.start();
197 return dojo._timeouts.push(thread)-1;
198 }
2f01fe57 199}
a089699c
AD
200
201//Register any module paths set up in djConfig. Need to do this
202//in the hostenvs since hostenv_browser can read djConfig from a
203//script tag's attribute.
2f01fe57 204if(dojo.config["modulePaths"]){
a089699c
AD
205 for(var param in dojo.config["modulePaths"]){
206 dojo.registerModulePath(param, dojo.config["modulePaths"][param]);
207 }
2f01fe57 208}