/
Namespace.js
464 lines (440 loc) · 16.8 KB
/
Namespace.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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// VIE - Vienna IKS Editables
// (c) 2011 Henri Bergius, IKS Consortium
// (c) 2011 Sebastian Germesin, IKS Consortium
// (c) 2011 Szaby Grünwald, IKS Consortium
// VIE may be freely distributed under the MIT license.
// For all details and documentation:
// http://viejs.org/
if (VIE.prototype.Namespaces) {
throw new Error("ERROR: VIE.Namespaces is already defined. " +
"Please check your VIE installation!");
}
// ## VIE Namespaces
//
// In general, a namespace is a container that provides context for the identifiers.
// Within VIE, namespaces are used to distinguish different ontolgies or vocabularies
// of identifiers, types and attributes. However, because of their verbosity, namespaces
// tend to make their usage pretty circuitous. The ``VIE.Namespaces(...)`` class provides VIE
// with methods to maintain abbreviations (akak **prefixes**) for namespaces in order to
// alleviate their usage. By default, every VIE instance is equipped with a main instance
// of the namespaces in ``myVIE.namespaces``. Furthermore, VIE uses a **base namespace**,
// which is used if no prefix is given (has an empty prefix).
// In the upcoming sections, we will explain the
// methods to add, access and remove prefixes.
// ## VIE.Namespaces(base, namespaces)
// This is the constructor of a VIE.Namespaces. The constructor initially
// needs a *base namespace* and can optionally be initialised with an
// associative array of prefixes and namespaces. The base namespace is used in a way
// that every non-prefixed, non-expanded attribute or type is assumed to be of that
// namespace. This helps, e.g., in an environment where only one namespace is given.
// **Parameters**:
// *{string}* **base** The base namespace.
// *{object}* **namespaces** Initial namespaces to bootstrap the namespaces. (optional)
// **Throws**:
// *{Error}* if the base namespace is missing.
// **Returns**:
// *{VIE.Attribute}* : A **new** VIE.Attribute object.
// **Example usage**:
//
// var ns = new myVIE.Namespaces("http://viejs.org/ns/",
// {
// "foaf": "http://xmlns.com/foaf/0.1/"
// });
VIE.prototype.Namespaces = function (base, namespaces) {
if (!base) {
throw new Error("Please provide a base namespace!");
}
this._base = base;
this._namespaces = (namespaces)? namespaces : {};
if (typeof this._namespaces !== "object" || _.isArray(this._namespaces)) {
throw new Error("If you want to initialise VIE namespace prefixes, " +
"please provide a proper object!");
}
};
// ### base(ns)
// This is a **getter** and **setter** for the base
// namespace. If called like ``base();`` it
// returns the actual base namespace as a string. If provided
// with a string, e.g., ``base("http://viejs.org/ns/");``
// it sets the current base namespace and retuns the namespace object
// for the purpose of chaining. If provided with anything except a string,
// it throws an Error.
// **Parameters**:
// *{string}* **ns** The namespace to be set. (optional)
// **Throws**:
// *{Error}* if the namespace is not of type string.
// **Returns**:
// *{string}* : The current base namespace.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// console.log(namespaces.base()); // <-- "http://base.ns/"
// namespaces.base("http://viejs.org/ns/");
// console.log(namespaces.base()); // <-- "http://viejs.org/ns/"
VIE.prototype.Namespaces.prototype.base = function (ns) {
if (!ns) {
return this._base;
}
else if (typeof ns === "string") {
/* remove another mapping */
this.removeNamespace(ns);
this._base = ns;
return this._base;
} else {
throw new Error("Please provide a valid namespace!");
}
};
// ### add(prefix, namespace)
// This method adds new prefix mappings to the
// current instance. If a prefix or a namespace is already
// present (in order to avoid ambiguities), an Error is thrown.
// ``prefix`` can also be an object in which case, the method
// is called sequentially on all elements.
// **Parameters**:
// *{string|object}* **prefix** The prefix to be set. If it is an object, the
// method will be applied to all key,value pairs sequentially.
// *{string}* **namespace** The namespace to be set.
// **Throws**:
// *{Error}* If a prefix or a namespace is already
// present (in order to avoid ambiguities).
// **Returns**:
// *{VIE.Namespaces}* : The current namespaces instance.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.add("", "http://...");
// // is always equal to
// namespaces.base("http://..."); // <-- setter of base namespace
VIE.prototype.Namespaces.prototype.add = function (prefix, namespace) {
if (typeof prefix === "object") {
for (var k1 in prefix) {
this.add(k1, prefix[k1]);
}
return this;
}
if (prefix === "") {
this.base(namespace);
return this;
}
/* checking if we overwrite existing mappings */
else if (this.contains(prefix) && namespace !== this._namespaces[prefix]) {
throw new Error("ERROR: Trying to register namespace prefix mapping (" + prefix + "," + namespace + ")!" +
"There is already a mapping existing: '(" + prefix + "," + this.get(prefix) + ")'!");
} else {
jQuery.each(this._namespaces, function (k1,v1) {
if (v1 === namespace && k1 !== prefix) {
throw new Error("ERROR: Trying to register namespace prefix mapping (" + prefix + "," + namespace + ")!" +
"There is already a mapping existing: '(" + k1 + "," + namespace + ")'!");
}
});
}
/* if not, just add them */
this._namespaces[prefix] = namespace;
return this;
};
// ### addOrReplace(prefix, namespace)
// This method adds new prefix mappings to the
// current instance. This will overwrite existing mappings.
// **Parameters**:
// *{string|object}* **prefix** The prefix to be set. If it is an object, the
// method will be applied to all key,value pairs sequentially.
// *{string}* **namespace** The namespace to be set.
// **Throws**:
// *nothing*
// **Returns**:
// *{VIE.Namespaces}* : The current namespaces instance.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.addOrReplace("", "http://...");
// // is always equal to
// namespaces.base("http://..."); // <-- setter of base namespace
VIE.prototype.Namespaces.prototype.addOrReplace = function (prefix, namespace) {
if (typeof prefix === "object") {
for (var k1 in prefix) {
this.addOrReplace(k1, prefix[k1]);
}
return this;
}
this.remove(prefix);
this.removeNamespace(namespace);
return this.add(prefix, namespace);
};
// ### get(prefix)
// This method retrieves a namespaces, given a prefix. If the
// prefix is the empty string, the base namespace is returned.
// **Parameters**:
// *{string}* **prefix** The prefix to be retrieved.
// **Throws**:
// *nothing*
// **Returns**:
// *{string|undefined}* : The namespace or ```undefined``` if no namespace could be found.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.addOrReplace("test", "http://test.ns");
// console.log(namespaces.get("test")); // <-- "http://test.ns"
VIE.prototype.Namespaces.prototype.get = function (prefix) {
if (prefix === "") {
return this.base();
}
return this._namespaces[prefix];
};
// ### getPrefix(namespace)
// This method retrieves a prefix, given a namespace.
// **Parameters**:
// *{string}* **namespace** The namespace to be retrieved.
// **Throws**:
// *nothing*
// **Returns**:
// *{string|undefined}* : The prefix or ```undefined``` if no prefix could be found.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.addOrReplace("test", "http://test.ns");
// console.log(namespaces.getPrefix("http://test.ns")); // <-- "test"
VIE.prototype.Namespaces.prototype.getPrefix = function (namespace) {
var prefix;
if (namespace.indexOf('<') === 0) {
namespace = namespace.substring(1, namespace.length - 1);
}
jQuery.each(this._namespaces, function (k1,v1) {
if (namespace.indexOf(v1) === 0) {
prefix = k1;
}
if (namespace.indexOf(k1 + ':') === 0) {
prefix = k1;
}
});
return prefix;
};
// ### contains(prefix)
// This method checks, whether a prefix is stored in the instance.
// **Parameters**:
// *{string}* **prefix** The prefix to be checked.
// **Throws**:
// *nothing*
// **Returns**:
// *{boolean}* : ```true``` if the prefix could be found, ```false``` otherwise.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.addOrReplace("test", "http://test.ns");
// console.log(namespaces.contains("test")); // <-- true
VIE.prototype.Namespaces.prototype.contains = function (prefix) {
return (prefix in this._namespaces);
};
// ### containsNamespace(namespace)
// This method checks, whether a namespace is stored in the instance.
// **Parameters**:
// *{string}* **namespace** The namespace to be checked.
// **Throws**:
// *nothing*
// **Returns**:
// *{boolean}* : ```true``` if the namespace could be found, ```false``` otherwise.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.addOrReplace("test", "http://test.ns");
// console.log(namespaces.containsNamespace("http://test.ns")); // <-- true
VIE.prototype.Namespaces.prototype.containsNamespace = function (namespace) {
return this.getPrefix(namespace) !== undefined;
};
// ### update(prefix, namespace)
// This method overwrites the namespace that is stored under the
// prefix ``prefix`` with the new namespace ``namespace``.
// If a namespace is already bound to another prefix, an Error is thrown.
// **Parameters**:
// *{string}* **prefix** The prefix.
// *{string}* **namespace** The namespace.
// **Throws**:
// *{Error}* If a namespace is already bound to another prefix.
// **Returns**:
// *{VIE.Namespaces}* : The namespace instance.
// **Example usage**:
//
// ...
VIE.prototype.Namespaces.prototype.update = function (prefix, namespace) {
this.remove(prefix);
return this.add(prefix, namespace);
};
// ### updateNamespace(prefix, namespace)
// This method overwrites the prefix that is bound to the
// namespace ``namespace`` with the new prefix ``prefix``. If another namespace is
// already registered with the given ``prefix``, an Error is thrown.
// **Parameters**:
// *{string}* **prefix** The prefix.
// *{string}* **namespace** The namespace.
// **Throws**:
// *nothing*
// **Returns**:
// *{VIE.Namespaces}* : The namespace instance.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.add("test", "http://test.ns");
// namespaces.updateNamespace("test2", "http://test.ns");
// namespaces.get("test2"); // <-- "http://test.ns"
VIE.prototype.Namespaces.prototype.updateNamespace = function (prefix, namespace) {
this.removeNamespace(prefix);
return this.add(prefix, namespace);
};
// ### remove(prefix)
// This method removes the namespace that is stored under the prefix ``prefix``.
// **Parameters**:
// *{string}* **prefix** The prefix to be removed.
// **Throws**:
// *nothing*
// **Returns**:
// *{VIE.Namespaces}* : The namespace instance.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.add("test", "http://test.ns");
// namespaces.get("test"); // <-- "http://test.ns"
// namespaces.remove("test");
// namespaces.get("test"); // <-- undefined
VIE.prototype.Namespaces.prototype.remove = function (prefix) {
if (prefix) {
delete this._namespaces[prefix];
}
return this;
};
// ### removeNamespace(namespace)
// This method removes removes the namespace ``namespace`` from the instance.
// **Parameters**:
// *{string}* **namespace** The namespace to be removed.
// **Throws**:
// *nothing*
// **Returns**:
// *{VIE.Namespaces}* : The namespace instance.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.add("test", "http://test.ns");
// namespaces.get("test"); // <-- "http://test.ns"
// namespaces.removeNamespace("http://test.ns");
// namespaces.get("test"); // <-- undefined
VIE.prototype.Namespaces.prototype.removeNamespace = function (namespace) {
var prefix = this.getPrefix(namespace);
if (prefix) {
delete this._namespaces[prefix];
}
return this;
};
// ### toObj()
// This method serializes the namespace instance into an associative
// array representation. The base namespace is given an empty
// string as key.
// **Parameters**:
// *{boolean}* **omitBase** If set to ```true``` this omits the baseNamespace.
// **Throws**:
// *nothing*
// **Returns**:
// *{object}* : A serialization of the namespaces as an object.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.add("test", "http://test.ns");
// console.log(namespaces.toObj());
// // <-- {"" : "http://base.ns/",
// "test": "http://test.ns"}
// console.log(namespaces.toObj(true));
// // <-- {"test": "http://test.ns"}
VIE.prototype.Namespaces.prototype.toObj = function (omitBase) {
if (omitBase) {
return jQuery.extend({}, this._namespaces);
}
return jQuery.extend({'' : this._base}, this._namespaces);
};
// ### curie(uri, safe)
// This method converts a given
// URI into a CURIE (or SCURIE), based on the given ```VIE.Namespaces``` object.
// If the given uri is already a URI, it is left untouched and directly returned.
// If no prefix could be found, an ```Error``` is thrown.
// **Parameters**:
// *{string}* **uri** The URI to be transformed.
// *{boolean}* **safe** A flag whether to generate CURIEs or SCURIEs.
// **Throws**:
// *{Error}* If no prefix could be found in the passed namespaces.
// **Returns**:
// *{string}* The CURIE or SCURIE.
// **Example usage**:
//
// var ns = new myVIE.Namespaces(
// "http://viejs.org/ns/",
// { "dbp": "http://dbpedia.org/ontology/" }
// );
// var uri = "<http://dbpedia.org/ontology/Person>";
// ns.curie(uri, false); // --> dbp:Person
// ns.curie(uri, true); // --> [dbp:Person]
VIE.prototype.Namespaces.prototype.curie = function(uri, safe){
return VIE.Util.toCurie(uri, safe, this);
};
// ### isCurie(curie)
// This method checks, whether
// the given string is a CURIE and returns ```true``` if so and ```false```otherwise.
// **Parameters**:
// *{string}* **curie** The CURIE (or SCURIE) to be checked.
// **Throws**:
// *nothing*
// **Returns**:
// *{boolean}* ```true``` if the given curie is a CURIE or SCURIE and ```false``` otherwise.
// **Example usage**:
//
// var ns = new myVIE.Namespaces(
// "http://viejs.org/ns/",
// { "dbp": "http://dbpedia.org/ontology/" }
// );
// var uri = "<http://dbpedia.org/ontology/Person>";
// var curie = "dbp:Person";
// var scurie = "[dbp:Person]";
// var text = "This is some text.";
// ns.isCurie(uri); // --> false
// ns.isCurie(curie); // --> true
// ns.isCurie(scurie); // --> true
// ns.isCurie(text); // --> false
VIE.prototype.Namespaces.prototype.isCurie = function (something) {
return VIE.Util.isCurie(something, this);
};
// ### uri(curie)
// This method converts a
// given CURIE (or save CURIE) into a URI, based on the given ```VIE.Namespaces``` object.
// **Parameters**:
// *{string}* **curie** The CURIE to be transformed.
// **Throws**:
// *{Error}* If no URI could be assembled.
// **Returns**:
// *{string}* : A string, representing the URI.
// **Example usage**:
//
// var ns = new myVIE.Namespaces(
// "http://viejs.org/ns/",
// { "dbp": "http://dbpedia.org/ontology/" }
// );
// var curie = "dbp:Person";
// var scurie = "[dbp:Person]";
// ns.uri(curie);
// --> <http://dbpedia.org/ontology/Person>
// ns.uri(scurie);
// --> <http://dbpedia.org/ontology/Person>
VIE.prototype.Namespaces.prototype.uri = function (curie) {
return VIE.Util.toUri(curie, this);
};
// ### isUri(something)
// This method checks, whether the given string is a URI.
// **Parameters**:
// *{string}* **something** : The string to be checked.
// **Throws**:
// *nothing*
// **Returns**:
// *{boolean}* : ```true``` if the string is a URI, ```false``` otherwise.
// **Example usage**:
//
// var namespaces = new vie.Namespaces("http://base.ns/");
// namespaces.addOrReplace("test", "http://test.ns");
// var uri = "<http://test.ns/Person>";
// var curie = "test:Person";
// namespaces.isUri(uri); // --> true
// namespaces.isUri(curie); // --> false
VIE.prototype.Namespaces.prototype.isUri = VIE.Util.isUri;