/[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.1 - (hide annotations) (download) (as text)
Tue Jun 14 11:47:44 2005 UTC (21 years, 1 month ago) by wakaba
Branch: MAIN
File MIME type: application/javascript
New

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.24