/[suikacvs]/www/2005/xpointer-js/xpointer.js
Suika

Contents of /www/2005/xpointer-js/xpointer.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations) (download) (as text)
Sun Mar 13 10:24:13 2005 UTC (19 years, 8 months ago) by wakaba
Branch: MAIN
CVS Tags: HEAD
File MIME type: application/javascript
New

1 wakaba 1.1 /* xpointer.js - XPointer implementation written in JavaScript */
2    
3    
4     /* Regular Expressions */
5     /* BUG: These expressions are less strict than the formal definition of XPointer */
6     var ncname = '[A-Za-z_\u00C0-\u02FF\u0370-\u203E\u2041-\uFFFD]' +
7     '[A-Za-z_\u00C0-\uFFFD.0-9\u00B7-]*';
8     var isNCName = new RegExp ('^' + ncname + '$');
9     var pointerPart = new RegExp ('^(' + ncname + ')(?:' + ':(' + ncname + '))?' +
10     '\\(' + '(' + '[^()^]*(?:[^()^]|\\^[()^]' +
11     '|\\([^()^]*(?:[^()^]|\\^[()^]' +
12     '|\\([^()^]*(?:[^()^]|\\^[()^]' +
13     '|\\([^()^]*(?:[^()^]|\\^[()^]' +
14     //
15     ')*\\)' +
16     ')*\\)' +
17     ')*\\)' +
18     ')*' + ')' + '\\)' +
19     '[\x09\x0A\x0D\x20]*');
20    
21     /** Implemenetations of XPointer schemes */
22     function XPointerSchemeProcessor (evaluateFunction) {
23     this.evaluate = evaluateFunction;
24     }
25    
26     /** XPointer Evaluation Engine */
27     function XPointerEvaluator () {
28     return this;
29     }
30    
31     /** Evaluates an XPointer pointer in the context of a document.
32    
33     @param aDocument A document in which the pointer is evaluated.
34     @param aExpression An XPointer pointer.
35     @raise XPointerShorthandNoMatchError
36     A shorthand pointer is specified but it does not
37     identify any element.
38     @raise XPointerSchemeBasedNoMatchError
39     A scheme-based pointer is specified but it does not
40     identify any subresources.
41     @raise XPointerSyntaxError
42     An illegal pointer is specified.
43     @return XPointerResult that contains subresources identified by the poitner.
44     */
45     XPointerEvaluator.prototype.evaluate = function (aDocument, aExpression) {
46     var ptr = aExpression;
47     var aContext = new XPointerSchemeContext ();
48     if (isNCName.test (aExpression)) {
49     var result = this.getXPointerSchemeProcessor (null, null)
50     .evaluate (aDocument, aContext, ptr);
51     if (!result.hasSubresources) {
52     throw new XPointerShorthandNoMatchError (aExpression);
53     }
54     return result;
55     } else {
56     while (true) {
57     var nptr = ptr.replace (pointerPart, '');
58     if (ptr != nptr) {
59     var schemePrefix = RegExp.$1;
60     var schemeLocalName = RegExp.$2;
61     var schemeData = RegExp.$3;
62     schemeData.replace (/^([()^])/, '$1');
63     var schemeNamespaceURI;
64     ptr = nptr;
65     if (!schemeLocalName) {
66     schemeLocalName = schemePrefix;
67     schemePrefix = null;
68     schemeNamespaceURI = null;
69     } else {
70     schemeNamespaceURI = aContext.lookupNamespaceURI (schemePrefix);
71     if (!schemeNamespaceURI) {
72     continue;
73     }
74     }
75     var sp = this.getXPointerSchemeProcessor
76     (schemeNamespaceURI, schemeLocalName);
77     if (!sp) {
78     continue;
79     }
80     var result = sp.evaluate (aDocument, aContext, schemeData);
81     if (result.hasSubresources) {
82     return result;
83     }
84     } else { /* Don't match */
85     break;
86     }
87     }
88     if (ptr.length > 0) {
89     throw new XPointerSyntaxError (aExpression, ptr);
90     }
91     throw new XPointerSchemeBasedNoMatchError (aExpression);
92     }
93     };
94     XPointerEvaluator.prototype._xpointerSchemeProcessor = {};
95    
96     /** Returns a XPointer scheme processor.
97    
98     @param nsuri The namespace URI of the scheme name, or null if
99     the scheme does not belong to any namespace.
100     @param ln The local name of the scheme.
101     @return XPointerSchemeProcessor object for the scheme, if any, or
102     null otherwise. If both nsuri and ln is null, then
103     a processor for shothand pointers is returned.
104     */
105     XPointerEvaluator.prototype.getXPointerSchemeProcessor = function (nsuri, ln) {
106     var ns = nsuri ? nsuri : '';
107     return this._xpointerSchemeProcessor[ns]
108     ? this._xpointerSchemeProcessor[ns][ln ? ln : '']
109     : null;
110     };
111    
112     /** Registers an XPointer scheme processor.
113    
114     @param nsuri Namespace URI of the scheme, or null if no namespace.
115     @param ln Local name of the scheme.
116     @param sp The scheme processor to register.
117     */
118     XPointerEvaluator.prototype.setXPointerSchemeProcessor = function (nsuri, ln, sp) {
119     var ns = nsuri ? nsuri : '';
120     if (!this._xpointerSchemeProcessor[ns]) this._xpointerSchemeProcessor[ns] = {};
121     this._xpointerSchemeProcessor[ns][ln] = sp;
122     };
123    
124     XPointerEvaluator.prototype._xpointerSchemeProcessor[''] = {
125     /* element() scheme defined by W3C Recommendation */
126     element: new XPointerSchemeProcessor (function (aDocument, aContext, aData) {
127     var cs = aData.split (/\x2F/);
128     var el;
129     if (cs[0].length > 0) {
130     el = document.getElementById (cs[0]);
131     } else {
132     el = aDocument;
133     }
134     if (el) {
135     CS: for (var i = 1; cs.length > i; i++) {
136     var index = parseInt (cs[i]);
137     var elChild = el.childNodes;
138     for (var j = 0; elChild.length > j; j++) {
139     var elc = elChild[j];
140     if (elc.nodeType == elc.ELEMENT_NODE) {
141     if (index-- == 1) {
142     el = elc;
143     continue CS;
144     }
145     }
146     }
147     el = null;
148     break CS;
149     }
150     }
151     return (el && (el != aDocument))
152     ? XPointerResult.createNodeXPointerResult (el)
153     : XPointerResult.createEmptyXPointerResult ();
154     }),
155    
156     /* xmlns() scheme defined by W3C Recomemndation */
157     xmlns: new XPointerSchemeProcessor (function (aDocument, aContext, aData) {
158     var ns = aData.split (/[\x09\x0A\x0D\x20]*=[\x09\x0A\x0D\x20]*/, 2);
159     if (ns[1] != null) {
160     aContext.addNamespaceBinding (ns[0], ns[1]);
161     }
162     return XPointerResult.createEmptyXPointerResult ();
163     })
164     };
165    
166     /* Processor for shorthand pointers */
167     XPointerEvaluator.prototype._xpointerSchemeProcessor['']['']
168     = new XPointerSchemeProcessor (function (aDocument, aContext, aData) {
169     var el = aDocument.getElementById (aData);
170     return XPointerResult.createNodeXPointerResult (el);
171     });
172    
173     /** Scheme context objects, which provide namespace binding context
174    
175     NOTE: Future version of this class might provide more "context"
176     information for scheme processors.
177     */
178     function XPointerSchemeContext () {
179     this.namespaceBinding = {
180     xml: 'http://www.w3.org/XML/1998/namespace'
181     };
182     }
183    
184     /** Resolves a namespace prefix into the namespace URI associated to it.
185    
186     @param prefix Namespace prefix to lookup, or null for the default (unprefixed).
187     @return Namespace URI or null if unbound.
188     */
189     XPointerSchemeContext.prototype.lookupNamespaceURI = function (prefix) {
190     if (prefix) {
191     return this.namespaceBinding[prefix];
192     } else {
193     return this.namespaceBinding[''];
194     }
195     };
196    
197     /** Binds a namespace prefix to a namespace URI. If the prefix is already
198     bound to another namespace URI, that binding us overridden.
199    
200     @param prefix Namespace prefix or null for default (unprefixed).
201     @param nsuri Namespace URI or null to unbind.
202     */
203     XPointerSchemeContext.prototype.addNamespaceBinding = function (prefix, nsuri) {
204     if (prefix) {
205     if ((prefix == 'xml') || (prefix == 'xmlns') ||
206     (nsuri == 'http://www.w3.org/XML/1998/namespace') ||
207     (nsuri == 'http://www.w3.org/2000/xmlns/')) {
208     //
209     } else {
210     this.namespaceBinding[prefix] = nsuri;
211     }
212     } else {
213     this.namespaceBinding[''] = nsuri;
214     }
215     };
216    
217     /** Result objects returned by evaluator.
218    
219     NOTE: Current interface and implementation is experimental.
220    
221     See also the XPathResult interface of DOM Level 3 XPath.
222     */
223     function XPointerResult () {}
224     XPointerResult.prototype = {
225     UNORDERED_NODE_ITERATOR_TYPE: 4,
226     ORDERED_NODE_ITERATOR_TYPE: 5,
227     UNORDERED_NODE_SNAPSHOT_TYPE: 6,
228     ORDERED_NODE_SNAPSHOT_TYPE: 7,
229     ANY_ORDERED_NODE_TYPE: 8,
230     FIRST_ORDERED_NODE_TYPE: 9,
231     /** Any subresources are identified or not. */
232     hasSubresources: false
233     };
234    
235     /** Result types. */
236     XPointerResult.prototype.getResultType = function () {
237     return this._xpResultType;
238     };
239    
240     /** Result node, if the result is a node.
241    
242     @raise XPathException(TYPE_ERR) if resultType differs from ANY_ORDERED_NODE_TYPE
243     or FIRST_ORDERED_NODE_TYPE.
244     @return Result node object.
245     */
246     XPointerResult.prototype.getSingleNodeValue = function () {
247     if (!(this._xpResultType == this.ANY_ORDERED_NODE_TYPE) &&
248     !(this._xpResultType == this.FIRST_ORDERED_NODE_TYPE)) {
249     throw new XPathException (XPathException.prototype.TYPE_ERR);
250     }
251     return this._xpSingleNodeValue;
252     };
253    
254     /** The number of nodes, if the result is a node list snapshot.
255     */
256     XPointerResult.prototype.getSnapshotLength = function () {
257     if (!(this._xpResultType == this.UNORDERED_NODE_SNAPSHOT_TYPE) &&
258     !(this._xpResultType == this.ORDERED_NODE_SNAPSHOT_TYPE)) {
259     throw new XPathException (XPathException.prototype.TYPE_ERR);
260     }
261     return this._xpNodeSnapshot.length;
262     };
263    
264     /** A node item, if the result is the node list snapshot.
265     */
266     XPointerResult.prototype.snapshotItem = function (index) {
267     if (!(this._xpResultType == this.UNORDERED_NODE_SNAPSHOT_TYPE) &&
268     !(this._xpResultType == this.ORDERED_NODE_SNAPSHOT_TYPE)) {
269     throw new XPathException (XPathException.prototype.TYPE_ERR);
270     }
271     return this._xpNodeSnapshot[index];
272     };
273    
274     XPointerResult.createNodeXPointerResult = function (el) {
275     var result = new XPointerResult ();
276     result._xpResultType = XPointerResult.prototype.ANY_ORDERED_NODE_TYPE;
277     if (el) {
278     result._xpSingleNodeValue = result;
279     result.hasSubresources = true;
280     }
281     return result;
282     }
283    
284     XPointerResult.createEmptyXPointerResult = function () {
285     var result = new XPointerResult ();
286     result._xpResultType = XPointerResult.prototype.ANY_ORDERED_NODE_TYPE;
287     return result;
288     }
289    
290     XPointerResult.createNodeSnapshotXPointerResult = function () {
291     var result = new XPointerResult ();
292     result._xpResultType = XPointerResult.prototype.UNORDERED_NODE_SNAPSHOT_TYPE;
293     result.hasSubresources = true;
294     return result;
295     }
296    
297     /* XPointer Framework Errors */
298    
299     function XPointerSyntaxError (aExpression, ptr) {
300     this.pointer = aExpression;
301     this.illegalPointerFragment = ptr;
302     return this;
303     }
304     XPointerSyntaxError.prototype.toString = function () {
305     return 'Broken XPointer: "' + this.illegalPointerFragment + '"';
306     };
307    
308     function XPointerShorthandNoMatchError (aExpression) {
309     this.pointer = aExpression;
310     return this;
311     }
312     XPointerShorthandNoMatchError.prototype.toString = function () {
313     return 'There is no element with ID "' + this.pointer + '"';
314     };
315    
316     function XPointerSchemeBasedNoMatchError (aExpression) {
317     this.pointer = aExpression;
318     return this;
319     }
320     XPointerSchemeBasedNoMatchError.prototype.toString = function () {
321     return 'Pointer "' + this.pointer + '" failed to identify subresources';
322     };
323    
324    
325     /* DOM Level 3 XPath */
326     function XPathException (code) {
327     this.code = code;
328     }
329     XPathException.prototype.INVALID_EXPRESSION_ERR = 51;
330     XPathException.prototype.TYPE_ERR = 52;
331     XPathException.prototype.toString = function () {
332     if (this.code == this.INVALID_EXPRESSION_ERR) {
333     return "The expression cannot be converted to return the specified type";
334     } else if (this.code == this.TYPE_ERR) {
335     return "The expression has a syntax error or contains specialized " +
336     "extension functions or variables not supported by this implementation";
337     } else {
338     return "Unknown error: " + this.code;
339     }
340     };
341    
342    
343     /* ***** BEGIN LICENSE BLOCK *****
344     * Copyright 2005 Wakaba <w@suika.fam.cx>. All rights reserved.
345     *
346     * This program is free software; you can redistribute it and/or
347     * modify it under the same terms as Perl itself.
348     *
349     * Alternatively, the contents of this file may be used
350     * under the following terms (the "MPL/GPL/LGPL"),
351     * in which case the provisions of the MPL/GPL/LGPL are applicable instead
352     * of those above. If you wish to allow use of your version of this file only
353     * under the terms of the MPL/GPL/LGPL, and not to allow others to
354     * use your version of this file under the terms of the Perl, indicate your
355     * decision by deleting the provisions above and replace them with the notice
356     * and other provisions required by the MPL/GPL/LGPL. If you do not delete
357     * the provisions above, a recipient may use your version of this file under
358     * the terms of any one of the Perl or the MPL/GPL/LGPL.
359     *
360     * "MPL/GPL/LGPL":
361     *
362     * Version: MPL 1.1/GPL 2.0/LGPL 2.1
363     *
364     * The contents of this file are subject to the Mozilla Public License Version
365     * 1.1 (the "License"); you may not use this file except in compliance with
366     * the License. You may obtain a copy of the License at
367     * <http://www.mozilla.org/MPL/>
368     *
369     * Software distributed under the License is distributed on an "AS IS" basis,
370     * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
371     * for the specific language governing rights and limitations under the
372     * License.
373     *
374     * The Original Code is xpointer-js code.
375     *
376     * The Initial Developer of the Original Code is Wakaba.
377     * Portions created by the Initial Developer are Copyright (C) 2005
378     * the Initial Developer. All Rights Reserved.
379     *
380     * Contributor(s):
381     * Wakaba <w@suika.fam.cx>
382     *
383     * Alternatively, the contents of this file may be used under the terms of
384     * either the GNU General Public License Version 2 or later (the "GPL"), or
385     * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
386     * in which case the provisions of the GPL or the LGPL are applicable instead
387     * of those above. If you wish to allow use of your version of this file only
388     * under the terms of either the GPL or the LGPL, and not to allow others to
389     * use your version of this file under the terms of the MPL, indicate your
390     * decision by deleting the provisions above and replace them with the notice
391     * and other provisions required by the LGPL or the GPL. If you do not delete
392     * the provisions above, a recipient may use your version of this file under
393     * the terms of any one of the MPL, the GPL or the LGPL.
394     *
395     * ***** END LICENSE BLOCK ***** */

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24