forked from linkeddata/rdflib.js
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.js
391 lines (337 loc) · 12.9 KB
/
util.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/**
* Utility functions for $rdf and the $rdf object itself
*/
if (typeof tabulator != 'undefined' && typeof tabulator.isExtension == 'undefined') tabulator.isExtension = false; // stand-alone library
if( typeof $rdf == 'undefined' ) {
var $rdf = {};
} else {
//dump("Internal error: RDF libray has already been loaded\n");
//dump("Internal error: $rdf type is "+typeof $rdf+"\n");
//dump("Internal error: $rdf.log type is "+typeof $rdf.log+"\n");
//dump("Internal error: $rdf.log.error type is "+typeof $rdf.log.error+"\n");
return $rdf;
throw "Internal error: RDF libray has already been loaded: $rdf already exists";
};
/**
* @class a dummy logger
Note to implement this using the Firefox error console see
https://developer.mozilla.org/en/nsIConsoleService
*/
//dump("@@ rdf/util.js test RESET RDF LOGGER $rdf.log.error)\n");
if($rdf.log != undefined) {
//dump("WTF util.js:" + $rdf.log);
throw "Internal Error: $rdf.log already defined, util.js: " + $rdf.log;
}
$rdf.log = {
'debug':function(x) {return;},
'warn':function(x) {return;},
'info':function(x) {return;},
'error':function(x) {return;},
'success':function(x) {return;},
'msg':function(x) {return;}
}
/**
* @class A utility class
*/
$rdf.Util = {
/** A simple debugging function */
'output': function (o) {
var k = document.createElement('div')
k.textContent = o
document.body.appendChild(k)
},
/**
* A standard way to add callback functionality to an object
**
** Callback functions are indexed by a 'hook' string.
**
** They return true if they want to be called again.
**
*/
'callbackify': function (obj,callbacks) {
obj.callbacks = {}
for (var x=callbacks.length-1; x>=0; x--) {
obj.callbacks[callbacks[x]] = [];
}
obj.addHook = function (hook) {
if (!obj.callbacks[hook])
obj.callbacks[hook] = [];
}
obj.addCallback = function (hook, func) {
obj.callbacks[hook].push(func);
}
obj.removeCallback = function (hook, funcName) {
for (var i=0;i<obj.callbacks[hook].length;i++){
if (obj.callbacks[hook][i].name==funcName){
obj.callbacks[hook].splice(i,1);
return true;
}
}
return false;
}
obj.insertCallback=function (hook,func){
obj.callbacks[hook].unshift(func);
}
obj.fireCallbacks = function (hook, args) {
var newCallbacks = []
var replaceCallbacks = []
var len = obj.callbacks[hook].length
// $rdf.log.info('!@$ Firing '+hook+' call back with length'+len);
for (var x=len-1; x>=0; x--) {
// $rdf.log.info('@@ Firing '+hook+' callback '+ obj.callbacks[hook][x])
if (obj.callbacks[hook][x].apply(obj,args)) {
newCallbacks.push(obj.callbacks[hook][x])
}
}
for (var x=newCallbacks.length-1; x>=0; x--) {
replaceCallbacks.push(newCallbacks[x])
}
for (var x=len; x<obj.callbacks[hook].length; x++) {
replaceCallbacks.push(obj.callbacks[hook][x])
}
obj.callbacks[hook] = replaceCallbacks
}
},
/**
* A standard way to create XMLHttpRequest objects
*/
'XMLHTTPFactory': function () {
if (typeof module != 'undefined' && module && module.exports) { //Node.js
var XMLHttpRequest = require("XMLHttpRequest").XMLHttpRequest;
return new XMLHttpRequest()
}
if (typeof tabulator != 'undefined' && tabulator.isExtension) {
return Components.
classes["@mozilla.org/xmlextras/xmlhttprequest;1"].
createInstance().QueryInterface(Components.interfaces.nsIXMLHttpRequest);
} else if (window.XMLHttpRequest) {
return new window.XMLHttpRequest()
}
else if (window.ActiveXObject) {
try {
return new ActiveXObject("Msxml2.XMLHTTP")
} catch (e) {
return new ActiveXObject("Microsoft.XMLHTTP")
}
}
else {
return false
}
},
'DOMParserFactory': function () {
if(tabulator && tabulator.isExtension) {
return Components.classes["@mozilla.org/xmlextras/domparser;1"]
.getService(Components.interfaces.nsIDOMParser);
} else if ( window.DOMParser ){
return new DOMParser();
} else if ( window.ActiveXObject ) {
return new ActiveXObject( "Microsoft.XMLDOM" );
} else {
return false;
}
},
/**
* Returns a hash of headers and values
**
** @@ Bug: Assumes that each header only occurs once
** Also note that a , in a header value is just the same as having two headers.
*/
'getHTTPHeaders': function (xhr) {
var lines = xhr.getAllResponseHeaders().split("\n")
var headers = {}
var last = undefined
for (var x=0; x<lines.length; x++) {
if (lines[x].length > 0) {
var pair = lines[x].split(': ')
if (typeof pair[1] == "undefined") { // continuation
headers[last] += "\n"+pair[0]
} else {
last = pair[0].toLowerCase()
headers[last] = pair[1]
}
}
}
return headers
},
'dtstamp': function () {
var now = new Date();
var year = now.getYear() + 1900;
var month = now.getMonth() + 1;
var day = now.getDate();
var hour = now.getUTCHours();
var minute = now.getUTCMinutes();
var second = now.getSeconds();
if (month < 10) month = "0" + month;
if (day < 10) day = "0" + day;
if (hour < 10) hour = "0" + hour;
if (minute < 10) minute = "0" + minute;
if (second < 10) second = "0" + second;
return year + "-" + month + "-" + day + "T"
+ hour + ":" + minute + ":" + second + "Z";
},
'enablePrivilege': ((typeof netscape != 'undefined') && (typeof netscape.security.PrivilegeManager != 'undefined') && netscape.security.PrivilegeManager.enablePrivilege) || function() { return; },
'disablePrivilege': ((typeof netscape != 'undefined') && (typeof netscape.security.PrivilegeManager != 'undefined') && netscape.security.PrivilegeManager.disablePrivilege) || function() { return; },
'RDFArrayRemove': function(a, x) { //removes all statements equal to x from a
for(var i=0; i<a.length; i++) {
//TODO: This used to be the following, which didnt always work..why
//if(a[i] == x)
if (a[i].subject.sameTerm( x.subject ) &&
a[i].predicate.sameTerm( x.predicate ) &&
a[i].object.sameTerm( x.object ) &&
a[i].why.sameTerm( x.why )) {
a.splice(i,1);
return;
}
}
throw "RDFArrayRemove: Array did not contain " + x;
},
'string_startswith': function(str, pref) { // missing library routines
return (str.slice(0, pref.length) == pref);
},
// This is the callback from the kb to the fetcher which is used to
// load ontologies of the data we load.
'AJAR_handleNewTerm': function(kb, p, requestedBy) {
var sf = null;
if( typeof kb.sf != 'undefined' ) {
sf = kb.sf;
} else {
return;
}
if (p.termType != 'symbol') return;
var docuri = $rdf.Util.uri.docpart(p.uri);
var fixuri;
if (p.uri.indexOf('#') < 0) { // No hash
// @@ major hack for dbpedia Categories, which spread indefinitely
if ($rdf.Util.string_startswith(p.uri, 'http://dbpedia.org/resource/Category:')) return;
/*
if (string_startswith(p.uri, 'http://xmlns.com/foaf/0.1/')) {
fixuri = "http://dig.csail.mit.edu/2005/ajar/ajaw/test/foaf"
// should give HTTP 303 to ontology -- now is :-)
} else
*/
if ($rdf.Util.string_startswith(p.uri, 'http://purl.org/dc/elements/1.1/')
|| $rdf.Util.string_startswith(p.uri, 'http://purl.org/dc/terms/')) {
fixuri = "http://dublincore.org/2005/06/13/dcq";
//dc fetched multiple times
} else if ($rdf.Util.string_startswith(p.uri, 'http://xmlns.com/wot/0.1/')) {
fixuri = "http://xmlns.com/wot/0.1/index.rdf";
} else if ($rdf.Util.string_startswith(p.uri, 'http://web.resource.org/cc/')) {
// $rdf.log.warn("creative commons links to html instead of rdf. doesn't seem to content-negotiate.");
fixuri = "http://web.resource.org/cc/schema.rdf";
}
}
if (fixuri) {
docuri = fixuri
}
if (sf && sf.getState(docuri) != 'unrequested') return;
if (fixuri) { // only give warning once: else happens too often
$rdf.log.warn("Assuming server still broken, faking redirect of <" + p.uri +
"> to <" + docuri + ">")
}
sf.requestURI(docuri, requestedBy);
}, //AJAR_handleNewTerm
'ArrayIndexOf': function(arr, item, i) {
i || (i = 0);
var length = arr.length;
if (i < 0) i = length + i;
for (; i < length; i++)
if (arr[i] === item) return i;
return -1;
}
};
///////////////////// Parse XML
//
// Returns: A DOM
//
$rdf.Util.parseXML = function(str) {
var dparser;
if ((typeof tabulator != 'undefined' && tabulator.isExtension)) {
dparser = Components.classes["@mozilla.org/xmlextras/domparser;1"].getService(
Components.interfaces.nsIDOMParser);
} else if (typeof module != 'undefined' ){ // Node.js
//var libxmljs = require('libxmljs'); // Was jsdom before 2012-01 then libxmljs but that nonstandard
//return libxmljs.parseXmlString(str);
var jsdom = require('jsdom');
var dom = jsdom.jsdom(str, undefined, {} );// html, level, options
return dom
} else {
dparser = new DOMParser();
}
return dparser.parseFromString(str, 'application/xml');
};
//////////////////////String Utility
// substitutes given terms for occurrnces of %s
// not well named. Used??? - tim
//
$rdf.Util.string = {
//C++, python style %s -> subs
'template': function(base, subs){
var baseA = base.split("%s");
var result = "";
for (var i=0;i<subs.length;i++){
subs[i] += '';
result += baseA[i] + subs[i];
}
return result + baseA.slice(subs.length).join();
}
};
// from http://dev.jquery.com/browser/trunk/jquery/src/core.js
// Overlap with JQuery -- we try to keep the rdflib.js and jquery libraries separate at the moment.
$rdf.Util.extend = function () {
// copy reference to target object
var target = arguments[0] || {},
i = 1,
length = arguments.length,
deep = false,
options, name, src, copy;
// Handle a deep copy situation
if (typeof target === "boolean") {
deep = target;
target = arguments[1] || {};
// skip the boolean and the target
i = 2;
}
// Handle case when target is a string or something (possible in deep copy)
if (typeof target !== "object" && !jQuery.isFunction(target)) {
target = {};
}
// extend jQuery itself if only one argument is passed
if (length === i) {
target = this;
--i;
}
for (; i < length; i++) {
// Only deal with non-null/undefined values
if ((options = arguments[i]) != null) {
// Extend the base object
for (name in options) {
src = target[name];
copy = options[name];
// Prevent never-ending loop
if (target === copy) {
continue;
}
// Recurse if we're merging object values
if (deep && copy && typeof copy === "object" && !copy.nodeType) {
var clone;
if (src) {
clone = src;
} else if (jQuery.isArray(copy)) {
clone = [];
} else if (jQuery.isObject(copy)) {
clone = {};
} else {
clone = copy;
}
// Never move original objects, clone them
target[name] = jQuery.extend(deep, clone, copy);
// Don't bring in undefined values
} else if (copy !== undefined) {
target[name] = copy;
}
}
}
}
// Return the modified object
return target;
};