/[suikacvs]/www/2005/uri-resolver/SimpleURIResolver.js
Suika

Contents of /www/2005/uri-resolver/SimpleURIResolver.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations) (download) (as text)
Wed Jun 15 01:07:11 2005 UTC (21 years, 1 month ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +14 -11 lines
File MIME type: application/javascript
Opera 8 support

1 /*
2
3 Class SimpleURIResolver
4
5 This class provides URI resolver function using
6 RDF/XML-based simple mapping tables.
7
8 */
9
10 /* Constructor */
11 function SimpleURIResolver () {
12 }
13
14 /* Namespaces */
15 var NS_RDF ="http://www.w3.org/1999/02/22-rdf-syntax-ns#";
16 var NS_PROP = "http://suika.fam.cx/admin/assign/urn-20050519#DT-";
17 var NS_TYPES = "http://suika.fam.cx/~wakaba/2005/6/uri-table#";
18 var NS_REPLACE = "http://suika.fam.cx/~wakaba/archive/2005/6/replace#";
19
20 /* Node Types */
21 var ELEMENT_NODE = 1;
22 var TEXT_NODE = 3;
23
24 /* Public Methods */
25
26 /*
27
28 resolve
29 Resolves a URI reference into another URI reference.
30
31 Parameters
32 origURI of type DOMString
33 The original absolute URI reference.
34 useSecondary of type boolean
35 Whether secondary resolved URI references should
36 be returned if any or not. If set to false,
37 only primary resolved URI references are returned if any.
38
39 Return Value
40 DOMString A resolved URI reference, or null if no
41 URI reference found.
42
43 No Exception
44
45 */
46 SimpleURIResolver.prototype.resolve = function (origURI, useSecondary) {
47 if (this.ResolveMatchTable.primary[origURI]) {
48 return this.ResolveMatchTable.primary[origURI];
49 } else if (useSecondary && this.ResolveMatchTable.secondary[origURI]) {
50 return this.ResolveMatchTable.secondary[origURI];
51 } else {
52 return this.ResolveByTable (origURI, useSecondary, this.ResolveTable);
53 }
54 };
55
56 /*
57
58 addMappingTableURI
59 Appends an entry of a pair of a URI prefix and a URI reference
60 from which a mapping table might be retrievable to the list
61 of root mapping table.
62
63 Parameters
64 uriPrefix of type DOMString
65 The prefix of URI references. URI references passed
66 to the resolve method are tested against this prefix
67 and if they does match the mapping table referenced by
68 the tableURI is looked.
69 tableURI of type DOMString
70 An absolute URI reference. It is expected that retrieving
71 this URI reference results in an RDF/XML-based simple
72 mapping table supported by this class.
73
74 No Return Value
75 No Exception
76
77 */
78
79 SimpleURIResolver.prototype.addMappingTableURI =
80 function (uriPrefix, tableURI) {
81 this.ResolveTable.push ({prefix: uriPrefix, see: tableURI});
82 };
83
84 /* Internal Cache Tables */
85
86 /* Mapping table constructed from tables added by addMappingTableURI
87 method and tables referred by them. */
88 SimpleURIResolver.prototype.ResolveTable = [];
89
90 /* Mapping table looked up literally (URI reference as string). */
91 SimpleURIResolver.prototype.ResolveMatchTable = {
92 primary: {}, secondary: {}
93 };
94
95 /* List of URI references of tables, used not to open
96 the same table twice and to avoid infinite loops caused
97 by circlular references. */
98 SimpleURIResolver.prototype.TableLoaded = {};
99
100 /* Internal Methods */
101
102 /* Resolving URI reference by using ResolveTable. */
103 SimpleURIResolver.prototype.ResolveByTable =
104 function (origURI, useSecondary, table) {
105 for (var i = 0; i < table.length; i++) {
106 var item = table[i];
107 if ((item.uri && item.uri == origURI) ||
108 (item.prefix &&
109 item.prefix == origURI.substring (0, item.prefix.length))) {
110 if (item.table) {
111 return this.ResolveByTable (origURI, useSecondary, item.table);
112 } else if (item.primary || (useSecondary && item.secondary)) {
113 var repprop = {
114 all: origURI,
115 _: (item.uri ? '' : origURI.substring (item.prefix.length))
116 };
117 if (item.primary) {
118 this.ResolveMatchTable.primary[origURI] =
119 this.ReplaceTemplate (item.primary, repprop);
120 return this.ResolveMatchTable.primary[origURI];
121 } else {
122 this.ResolveMatchTable.primary[origURI] = null;
123 this.ResolveMatchTable.secondary[origURI] =
124 this.ReplaceTemplate (item.secondary, repprop);
125 return this.ResolveMatchTable.secondary[origURI];
126 }
127 } else if (item.see && !this.TableLoaded[item.see]) {
128 item.table = [];
129 this.TableLoaded[item.see] = true;
130 var req = new XMLHttpRequest ();
131 req.open ('GET', item.see, false);
132 req.send (null);
133 if (req.responseXML) {
134 var db = req.responseXML;
135 var dbitems = db.documentElement.childNodes;
136 var dbitemsLength = dbitems.length;
137 for (var j = 0; j < dbitemsLength; j++) {
138 var dbitem = dbitems[j];
139 if ((dbitem.nodeType == ELEMENT_NODE) &&
140 (dbitem.namespaceURI == NS_TYPES)) {
141 var titem = {};
142 if (dbitem.localName == 'Resource') {
143 if (dbitem.hasAttributeNS (NS_RDF, 'about')) {
144 titem.uri = dbitem.getAttributeNS (NS_RDF, 'about');
145 }
146 } else if (dbitem.localName == 'ResourceGroup') {
147 if (dbitem.hasAttributeNS (NS_RDF, 'about')) {
148 titem.prefix = dbitem.getAttributeNS (NS_RDF, 'about');
149 }
150 }
151 var dbitemcs = dbitem.childNodes;
152 var dbitemcsLength = dbitemcs.length;
153 for (var k = 0; k < dbitemcsLength; k++) {
154 var dbitemc = dbitemcs[k];
155 if (dbitemc.namespaceURI == NS_PROP) {
156 if (dbitemc.localName == 'PRIMARY-RESOLVED-URI') {
157 titem.primary = dbitemc;
158 } else if (dbitemc.localName == 'SECONDARY-RESOLVED-URI') {
159 titem.secondary = dbitemc;
160 }
161 } else if (dbitemc.namespaceURI == NS_TYPES) {
162 if (dbitemc.localName == 'see') {
163 titem.see = dbitemc.getAttributeNS (NS_RDF, 'resource');
164 /* TODO: Base URI */
165 }
166 }
167 }
168 item.table.push (titem);
169 }
170 }
171 i--; /* Redo */
172 } else {
173 this.ResolveMatchTable.primary[origURI] = null;
174 if (useSecondary) this.ResolveMatchTable.secondary[origURI] = null;
175 return;
176 }
177 } else {
178 this.ResolveMatchTable.primary[origURI] = null;
179 if (useSecondary) this.ResolveMatchTable.secondary[origURI] = null;
180 return;
181 }
182 }
183 }
184 this.ResolveMatchTable.primary[origURI] = null;
185 if (useSecondary) this.ResolveMatchTable.secondary[origURI] = null;
186 };
187
188 /* Replacing template in mapping tables. */
189 SimpleURIResolver.prototype.ReplaceTemplate =
190 function (template, replaceTable) {
191 var tempFrags = [template];
192 var result = '';
193 while (tempFrags.length > 0) {
194 var tempFrag = tempFrags.pop ();
195 if (tempFrag.nodeType == TEXT_NODE) {
196 result += tempFrag.data;
197 } else if (tempFrag.nodeType == ELEMENT_NODE) {
198 if (tempFrag.namespaceURI == NS_REPLACE) {
199 if (tempFrag.localName == 'Integer') {
200 var rint = '';
201 var digits = tempFrag.getAttributeNS (null, 'min-digits') || 0;
202 rint = '' + parseInt (replaceTable[tempFrag.getAttributeNS
203 (null, 'source') || '_'], 10);
204 while (rint.length < digits) {
205 rint = '0' + rint;
206 }
207 result += rint;
208 } else if (tempFrag.localName == 'String') {
209 result += replaceTable[tempFrag.getAttributeNS
210 (null, 'source') || '_'];
211 /* Should already be escape-encoded */
212 } else if (tempFrag.localName == 'If') {
213 var tf = false;
214 if (tempFrag.hasAttributeNS (null, 'test')) {
215 if (!replaceTable['0']) {
216 var sp = replaceTable['_'].split (/:/);
217 for (var i = 0; i < sp.length; i++) {
218 replaceTable[i] = sp[i];
219 }
220 }
221 var rept = replaceTable[tempFrag.getAttributeNS (null, 'test')];
222 tf = (rept && rept.length > 0);
223 } else {
224 tf = (replaceTable['_'].length > 0);
225 }
226
227 var ifcs = tempFrag.childNodes;
228 var ifcsLength = ifcs.length;
229 for (var i = 0; i < ifcsLength; i++) {
230 var ifc = ifcs[i];
231 if (ifc.nodeType == ELEMENT_NODE &&
232 ifc.namespaceURI == NS_REPLACE) {
233 if ((tf && ifc.localName == 'True') ||
234 (!tf && ifc.localName == 'False')) {
235 var ifccs = ifc.childNodes;
236 var ifccsLength = ifccs.length;
237 for (var j = ifccsLength - 1; j >= 0; j--) {
238 tempFrags.push (ifccs[j]);
239 }
240 }
241 }
242 }
243 }
244 } else {
245 var tempFragcs = tempFrag.childNodes;
246 var tempFragcsLength = tempFragcs.length;
247 for (var i = tempFragcsLength - 1; i >= 0; i--) {
248 tempFrags.push (tempFragcs[i]);
249 }
250 }
251 }
252 }
253 return result;
254 };
255
256 /* ***** BEGIN LICENSE BLOCK *****
257 * Copyright 2005 Wakaba <[email protected]>. All rights reserved.
258 *
259 * This program is free software; you can redistribute it and/or
260 * modify it under the same terms as Perl itself.
261 *
262 * Alternatively, the contents of this file may be used
263 * under the following terms (the "MPL/GPL/LGPL"),
264 * in which case the provisions of the MPL/GPL/LGPL are applicable instead
265 * of those above. If you wish to allow use of your version of this file only
266 * under the terms of the MPL/GPL/LGPL, and not to allow others to
267 * use your version of this file under the terms of the Perl, indicate your
268 * decision by deleting the provisions above and replace them with the notice
269 * and other provisions required by the MPL/GPL/LGPL. If you do not delete
270 * the provisions above, a recipient may use your version of this file under
271 * the terms of any one of the Perl or the MPL/GPL/LGPL.
272 *
273 * "MPL/GPL/LGPL":
274 *
275 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
276 *
277 * The contents of this file are subject to the Mozilla Public License Version
278 * 1.1 (the "License"); you may not use this file except in compliance with
279 * the License. You may obtain a copy of the License at
280 * <http://www.mozilla.org/MPL/>
281 *
282 * Software distributed under the License is distributed on an "AS IS" basis,
283 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
284 * for the specific language governing rights and limitations under the
285 * License.
286 *
287 * The Original Code is SuikaURNResolver code.
288 *
289 * The Initial Developer of the Original Code is Wakaba.
290 * Portions created by the Initial Developer are Copyright (C) 2005
291 * the Initial Developer. All Rights Reserved.
292 *
293 * Contributor(s):
294 * Wakaba <[email protected]>
295 *
296 * Alternatively, the contents of this file may be used under the terms of
297 * either the GNU General Public License Version 2 or later (the "GPL"), or
298 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
299 * in which case the provisions of the GPL or the LGPL are applicable instead
300 * of those above. If you wish to allow use of your version of this file only
301 * under the terms of either the GPL or the LGPL, and not to allow others to
302 * use your version of this file under the terms of the MPL, indicate your
303 * decision by deleting the provisions above and replace them with the notice
304 * and other provisions required by the LGPL or the GPL. If you do not delete
305 * the provisions above, a recipient may use your version of this file under
306 * the terms of any one of the MPL, the GPL or the LGPL.
307 *
308 * ***** END LICENSE BLOCK ***** */

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24