Subversion Repositories Applications.papyrus

Rev

Rev 1372 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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