File: dereference.js

package info (click to toggle)
virtuoso-opensource 6.1.4%2Bdfsg1-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 245,116 kB
  • sloc: ansic: 639,631; sql: 439,225; xml: 287,085; java: 61,048; sh: 38,723; cpp: 36,889; cs: 25,240; php: 12,562; yacc: 9,036; lex: 7,149; makefile: 6,093; jsp: 4,447; awk: 1,643; perl: 1,017; ruby: 1,003; python: 329
file content (110 lines) | stat: -rw-r--r-- 3,633 bytes parent folder | download | duplicates (2)
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
/*
 *  $Id: dereference.js,v 1.23.2.3 2010/04/06 16:46:11 source Exp $
 *
 *  This file is part of the OpenLink Software Ajax Toolkit (OAT) project.
 *
 *  Copyright (C) 2005-2010 OpenLink Software
 *
 *  See LICENSE file for details.
 */
/*
	OAT.Dereference.go(url, callback, optObj)
*/

OAT.Dereference = {
    options:{
	endpoint:"/about?url=",	 /* endpoint for dereferencing *this* resource */

	pragmas:{},			/* key->value pairs */

	endpointOpts:{
	    virtuoso:true,		/* is virtuoso? */
	    proxyVersion:1		/* normal proxy type - default */
	},

	ajaxOpts:{}
    },

    addParam:function(url, param, value) {
	if (url.match(/.*\?.*/)) {
	    return url + "&" + param + "=" + value;
	}
	return url + "?" + param + "=" + value;
    },

    copy:function(srcObj,dstObj,propObj) {
	if (!propObj) { propObj = srcObj; }
	for (var p in propObj) { dstObj[p] = srcObj[p]; }
    },

    go:function(url,callback,optObj) {
	var ajaxOpts = {};
	var endpointOpts = {};
	var pragmas = {};
	var direct = false;
	var endpoint = "";

	/* deep copy defaults */

	this.copy(this.options.ajaxOpts,ajaxOpts);
	this.copy(this.options.endpointOpts,endpointOpts);
	this.copy(this.options.pragmas,pragmas);
	endpoint = this.options.endpoint;

	/* now the settings */

	if (optObj) {
	    this.copy(optObj.ajaxOpts,ajaxOpts);
	    this.copy(optObj.endpointOpts,endpointOpts);
	    this.copy(optObj.pragmas,pragmas);
	    if (optObj.endpoint) endpoint = optObj.endpoint;
	}

	if (!optObj.direct) {
	    if (url.match(/^http/i) && endpointOpts.virtuoso) { /* Virtuoso proxy: */
		if (endpointOpts.proxyVersion == 1) {
		    var r = url.match(/^(http[s]?:\/\/)([^@\/]+@)?(.*)/);
		    var user = (r[2] ? r[2].substring(0,r[2].length-1) : false);
		    var encoded = encodeURIComponent(r[1] + r[3]);
		    encoded = this.addParam(endpoint + encoded, "force", "rdf");

		    if (user) { encoded = this.addParam(encoded, "login", encodeURIComponent(user)); }
		    if (url.match(/\.n3$/)) { encoded = this.addParam(encoded, "output-format", "n3"); }
		    if (url.match(/\.ttl$/)) { encoded = this.addParam(encoded, "output-format", "ttl"); }
		    for (var p in pragmas) { encoded = this.addParam(encoded, p, pragmas[p]); }
		} else {
		    var r = url.match(/^(http[s]?:\/\/)([^@\/]+@)?(.*)/);
		    var user = (r[2] ? r[2].substring(0,r[2].length-1) : false);
		    var encoded = (endpoint + r[1] + r[3]);
		    ajaxOpts["noSecurityCookie"] = true;
		}
	    } else if ((url.match(/^urn:/i) ||
			url.match(/^doi:/i) ||
			url.match(/^oai:/i) ||
			url.match(/^nodeid:/i)) && endpointOpts.virtuoso) { /* Virtuoso proxy: */
		if (endpointOpts.proxyVersion == 1) {
		    var encoded = encodeURIComponent(url);
		    encoded = this.addParam(endpoint + encoded, "force", "rdf");
		    if (url.match(/\.n3$/)) { encoded = this.addParam(encoded, "output-format", "n3"); }
		    if (url.match(/\.ttl$/)) { encoded = this.addParam(encoded, "output-format", "ttl"); }
		    for (var p in pragmas) { encoded = this.addParam(encoded, p, pragmas[p]); }
		} else {
		    var encoded = endpoint + url;
		    ajaxOpts["noSecurityCookie"] = true;
		}
	    } else if (url.match(/^urn:/i) ||
		       url.match(/^doi:/i) ||
		       url.match(/^oai:/i) ||
		       url.match(/^http/i)) { /* other than Virtuoso: */
		var encoded = endpoint + decodeURIComponent(url);
		ajaxOpts["noSecurityCookie"] = true;
	    } else { /* relative uri */
		var encoded = url;
	    }
	} else { /* Dereference using supplied endpoint and options. Direct SPARQL queries, etc */
	    encoded = endpoint + url;
	}
	var xhr = OAT.AJAX.GET(encoded,false,callback,ajaxOpts);
	return xhr;
    }
}