| 1318 |
alexandre_ |
1 |
/*
|
|
|
2 |
Copyright (c) 2004-2006, The Dojo Foundation
|
|
|
3 |
All Rights Reserved.
|
|
|
4 |
|
|
|
5 |
Licensed under the Academic Free License version 2.1 or above OR the
|
|
|
6 |
modified BSD license. For more information on Dojo licensing, see:
|
|
|
7 |
|
|
|
8 |
http://dojotoolkit.org/community/licensing.shtml
|
|
|
9 |
*/
|
|
|
10 |
|
|
|
11 |
dojo.hostenv.println = function (line) {
|
|
|
12 |
if (arguments.length > 0) {
|
|
|
13 |
print(arguments[0]);
|
|
|
14 |
for (var i = 1; i < arguments.length; i++) {
|
|
|
15 |
var valid = false;
|
|
|
16 |
for (var p in arguments[i]) {
|
|
|
17 |
valid = true;
|
|
|
18 |
break;
|
|
|
19 |
}
|
|
|
20 |
if (valid) {
|
|
|
21 |
dojo.debugShallow(arguments[i]);
|
|
|
22 |
}
|
|
|
23 |
}
|
|
|
24 |
} else {
|
|
|
25 |
print(line);
|
|
|
26 |
}
|
|
|
27 |
};
|
|
|
28 |
dojo.locale = dojo.locale || java.util.Locale.getDefault().toString().replace("_", "-").toLowerCase();
|
|
|
29 |
dojo.render.name = dojo.hostenv.name_ = "rhino";
|
|
|
30 |
dojo.hostenv.getVersion = function () {
|
|
|
31 |
return version();
|
|
|
32 |
};
|
|
|
33 |
if (dj_undef("byId")) {
|
|
|
34 |
dojo.byId = function (id, doc) {
|
|
|
35 |
if (id && (typeof id == "string" || id instanceof String)) {
|
|
|
36 |
if (!doc) {
|
|
|
37 |
doc = document;
|
|
|
38 |
}
|
|
|
39 |
return doc.getElementById(id);
|
|
|
40 |
}
|
|
|
41 |
return id;
|
|
|
42 |
};
|
|
|
43 |
}
|
|
|
44 |
dojo.hostenv.loadUri = function (uri, cb) {
|
|
|
45 |
try {
|
|
|
46 |
var local = (new java.io.File(uri)).exists();
|
|
|
47 |
if (!local) {
|
|
|
48 |
try {
|
|
|
49 |
var stream = (new java.net.URL(uri)).openStream();
|
|
|
50 |
stream.close();
|
|
|
51 |
}
|
|
|
52 |
catch (e) {
|
|
|
53 |
return false;
|
|
|
54 |
}
|
|
|
55 |
}
|
|
|
56 |
if (cb) {
|
|
|
57 |
var contents = (local ? readText : readUri)(uri, "UTF-8");
|
|
|
58 |
cb(eval("(" + contents + ")"));
|
|
|
59 |
} else {
|
|
|
60 |
load(uri);
|
|
|
61 |
}
|
|
|
62 |
return true;
|
|
|
63 |
}
|
|
|
64 |
catch (e) {
|
|
|
65 |
dojo.debug("rhino load('" + uri + "') failed. Exception: " + e);
|
|
|
66 |
return false;
|
|
|
67 |
}
|
|
|
68 |
};
|
|
|
69 |
dojo.hostenv.exit = function (exitcode) {
|
|
|
70 |
quit(exitcode);
|
|
|
71 |
};
|
|
|
72 |
function dj_rhino_current_script_via_java(depth) {
|
|
|
73 |
var optLevel = Packages.org.mozilla.javascript.Context.getCurrentContext().getOptimizationLevel();
|
|
|
74 |
var caw = new java.io.CharArrayWriter();
|
|
|
75 |
var pw = new java.io.PrintWriter(caw);
|
|
|
76 |
var exc = new java.lang.Exception();
|
|
|
77 |
var s = caw.toString();
|
|
|
78 |
var matches = s.match(/[^\(]*\.js\)/gi);
|
|
|
79 |
if (!matches) {
|
|
|
80 |
throw Error("cannot parse printStackTrace output: " + s);
|
|
|
81 |
}
|
|
|
82 |
var fname = ((typeof depth != "undefined") && (depth)) ? matches[depth + 1] : matches[matches.length - 1];
|
|
|
83 |
var fname = matches[3];
|
|
|
84 |
if (!fname) {
|
|
|
85 |
fname = matches[1];
|
|
|
86 |
}
|
|
|
87 |
if (!fname) {
|
|
|
88 |
throw Error("could not find js file in printStackTrace output: " + s);
|
|
|
89 |
}
|
|
|
90 |
return fname;
|
|
|
91 |
}
|
|
|
92 |
function readText(path, encoding) {
|
|
|
93 |
encoding = encoding || "utf-8";
|
|
|
94 |
var jf = new java.io.File(path);
|
|
|
95 |
var is = new java.io.FileInputStream(jf);
|
|
|
96 |
return dj_readInputStream(is, encoding);
|
|
|
97 |
}
|
|
|
98 |
function readUri(uri, encoding) {
|
|
|
99 |
var conn = (new java.net.URL(uri)).openConnection();
|
|
|
100 |
encoding = encoding || conn.getContentEncoding() || "utf-8";
|
|
|
101 |
var is = conn.getInputStream();
|
|
|
102 |
return dj_readInputStream(is, encoding);
|
|
|
103 |
}
|
|
|
104 |
function dj_readInputStream(is, encoding) {
|
|
|
105 |
var input = new java.io.BufferedReader(new java.io.InputStreamReader(is, encoding));
|
|
|
106 |
try {
|
|
|
107 |
var sb = new java.lang.StringBuffer();
|
|
|
108 |
var line = "";
|
|
|
109 |
while ((line = input.readLine()) !== null) {
|
|
|
110 |
sb.append(line);
|
|
|
111 |
sb.append(java.lang.System.getProperty("line.separator"));
|
|
|
112 |
}
|
|
|
113 |
return sb.toString();
|
|
|
114 |
}
|
|
|
115 |
finally {
|
|
|
116 |
input.close();
|
|
|
117 |
}
|
|
|
118 |
}
|
|
|
119 |
if (!djConfig.libraryScriptUri.length) {
|
|
|
120 |
try {
|
|
|
121 |
djConfig.libraryScriptUri = dj_rhino_current_script_via_java(1);
|
|
|
122 |
}
|
|
|
123 |
catch (e) {
|
|
|
124 |
if (djConfig["isDebug"]) {
|
|
|
125 |
print("\n");
|
|
|
126 |
print("we have no idea where Dojo is located.");
|
|
|
127 |
print("Please try loading rhino in a non-interpreted mode or set a");
|
|
|
128 |
print("\n\tdjConfig.libraryScriptUri\n");
|
|
|
129 |
print("Setting the dojo path to './'");
|
|
|
130 |
print("This is probably wrong!");
|
|
|
131 |
print("\n");
|
|
|
132 |
print("Dojo will try to load anyway");
|
|
|
133 |
}
|
|
|
134 |
djConfig.libraryScriptUri = "./";
|
|
|
135 |
}
|
|
|
136 |
}
|
|
|
137 |
dojo.doc = function () {
|
|
|
138 |
return document;
|
|
|
139 |
};
|
|
|
140 |
dojo.body = function () {
|
|
|
141 |
return document.body;
|
|
|
142 |
};
|
|
|
143 |
function setTimeout(func, delay) {
|
|
|
144 |
var def = {sleepTime:delay, hasSlept:false, run:function () {
|
|
|
145 |
if (!this.hasSlept) {
|
|
|
146 |
this.hasSlept = true;
|
|
|
147 |
java.lang.Thread.currentThread().sleep(this.sleepTime);
|
|
|
148 |
}
|
|
|
149 |
try {
|
|
|
150 |
func();
|
|
|
151 |
}
|
|
|
152 |
catch (e) {
|
|
|
153 |
dojo.debug("Error running setTimeout thread:" + e);
|
|
|
154 |
}
|
|
|
155 |
}};
|
|
|
156 |
var runnable = new java.lang.Runnable(def);
|
|
|
157 |
var thread = new java.lang.Thread(runnable);
|
|
|
158 |
thread.start();
|
|
|
159 |
}
|
|
|
160 |
dojo.requireIf((djConfig["isDebug"] || djConfig["debugAtAllCosts"]), "dojo.debug");
|
|
|
161 |
|