-
Notifications
You must be signed in to change notification settings - Fork 7
/
protocol.lisp
322 lines (268 loc) · 11.7 KB
/
protocol.lisp
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
;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
;;; Copyright (c) 2007 Ivan Shvedunov. All rights reserved.
;;; Copyright (c) 2007 David Lichteblau. All rights reserved.
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions
;;; are met:
;;;
;;; * Redistributions of source code must retain the above copyright
;;; notice, this list of conditions and the following disclaimer.
;;;
;;; * Redistributions in binary form must reproduce the above
;;; copyright notice, this list of conditions and the following
;;; disclaimer in the documentation and/or other materials
;;; provided with the distribution.
;;;
;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
(in-package :xpath)
;; generic functions of the XPath protocol
(defparameter *navigator* :default-navigator)
(macrolet ((defprotocol (name &rest extra-args)
(let ((navi-name
(find-symbol
(format nil "~A~A" name :-using-navigator)
(symbol-package name))))
`(progn
(defgeneric ,navi-name (navigator node ,@extra-args))
(defun ,name (node ,@extra-args)
(,navi-name *navigator* node ,@extra-args))))))
(defprotocol xpath-protocol:node-p)
;; NODE-EQUAL and NODE-IDENTITY need to be implemented so that the
;; following equavalence holds (but NODE-EQUAL might be implemented
;; directly instead):
;; (node-equal a b) <=> (equal (hash-key a) (hash-key b))
;; (This is unlike Java's equals() and hashCode(), because HASH-KEY
;; doesn't return a hash code, it returns an object to be used in an
;; EQUAL hash table.)
(defprotocol xpath-protocol:node-equal other)
(defprotocol xpath-protocol:hash-key)
(defprotocol xpath-protocol:child-pipe)
(defprotocol xpath-protocol:attribute-pipe)
(defprotocol xpath-protocol:namespace-pipe)
(defprotocol xpath-protocol:parent-node)
(defprotocol xpath-protocol:node-text)
(defprotocol xpath-protocol:qualified-name)
(defprotocol xpath-protocol:local-name)
(defprotocol xpath-protocol:namespace-prefix)
(defprotocol xpath-protocol:namespace-uri)
(defprotocol xpath-protocol:processing-instruction-target)
(defprotocol xpath-protocol:node-type-p type)
(defprotocol xpath-protocol:base-uri)
(defprotocol xpath-protocol:get-element-by-id id)
(defprotocol xpath-protocol:unparsed-entity-uri name))
(defmacro define-default-method (name (&rest args) &body body)
(let ((navi-name
(find-symbol
(format nil "~A~A" name :-using-navigator)
(symbol-package name))))
`(defmethod ,navi-name ((navigator (eql :default-navigator)) ,@args)
,@body)))
(define-default-method xpath-protocol:node-p ((node t))
nil)
(define-default-method xpath-protocol:node-equal (a b)
(eq a b))
(define-default-method xpath-protocol:hash-key (node)
node)
;; helper functions
(defun parent-pipe (node)
(let ((parent (xpath-protocol:parent-node node)))
(if parent
(list parent)
empty-pipe)))
(defun vector->pipe (vector &optional (start 0))
(if (>= start (length vector))
empty-pipe
(make-pipe (elt vector start)
(vector->pipe vector (1+ start)))))
;; DOM mapping: simple slots
(define-default-method xpath-protocol:node-p ((node dom:node)) t)
(define-default-method xpath-protocol:parent-node ((node dom:attr))
(dom:owner-element node))
(define-default-method xpath-protocol:parent-node ((node dom:node))
(dom:parent-node node))
(define-default-method xpath-protocol:local-name ((node dom:node))
;; fixme?
(or (dom:local-name node) (dom:node-name node)))
(define-default-method xpath-protocol:namespace-prefix ((node dom:node))
(dom:prefix node))
(define-default-method xpath-protocol:namespace-uri ((node dom:node))
(or (dom:namespace-uri node) ""))
(define-default-method xpath-protocol:qualified-name ((node dom:node))
(dom:node-name node))
(define-default-method xpath-protocol:processing-instruction-target
((node dom:node))
(dom:node-value node))
(define-default-method xpath-protocol:base-uri ((node dom:node))
;; fixme
"")
;; DOM mapping: pipes
(define-default-method xpath-protocol:child-pipe ((node dom:node))
empty-pipe)
(define-default-method xpath-protocol:child-pipe ((node dom:document))
(list (dom:document-element node)))
(define-default-method xpath-protocol:child-pipe ((node dom:element))
(vector->pipe (dom:child-nodes node)))
(define-default-method xpath-protocol:attribute-pipe ((node dom:node))
empty-pipe)
(define-default-method xpath-protocol:attribute-pipe ((node dom:element))
(filter-pipe #'(lambda (item)
(not (equal (dom:namespace-uri item)
"http://www.w3.org/2000/xmlns/")))
(vector->pipe (dom:items (dom:attributes node)))))
(define-default-method xpath-protocol:namespace-pipe ((node dom:node))
(when (dom:parent-node node)
(xpath-protocol:namespace-pipe (dom:parent-node node))))
(defstruct (dom-namespace
(:constructor make-dom-namespace (parent prefix uri)))
parent
prefix
uri)
(define-default-method xpath-protocol:node-p ((node dom-namespace))
t)
(define-default-method xpath-protocol:node-equal
((a dom-namespace) (b dom-namespace))
(and (eq (dom-namespace-parent a) (dom-namespace-parent b))
(equal (dom-namespace-prefix a) (dom-namespace-prefix b))))
(define-default-method xpath-protocol:hash-key
((node dom-namespace))
(cons (dom-namespace-parent node) (dom-namespace-prefix node)))
(define-default-method xpath-protocol:child-pipe
((node dom-namespace))
empty-pipe)
(define-default-method xpath-protocol:attribute-pipe
((node dom-namespace))
empty-pipe)
(define-default-method xpath-protocol:namespace-pipe
((node dom-namespace))
empty-pipe)
(define-default-method xpath-protocol:parent-node ((node dom-namespace))
(dom-namespace-parent node))
(define-default-method xpath-protocol:local-name ((node dom-namespace))
(dom-namespace-prefix node))
(define-default-method xpath-protocol:qualified-name ((node dom-namespace))
(dom-namespace-prefix node))
(define-default-method xpath-protocol:namespace-prefix ((node dom-namespace))
nil)
(define-default-method xpath-protocol:namespace-uri ((node dom-namespace))
"")
(define-default-method xpath-protocol:namespace-pipe ((node dom:element))
;; FIXME: completely untested
;; FIXME: rewrite this lazily?
(let ((table (make-hash-table :test 'equal))
(result '()))
(labels ((record* (parent prefix uri)
(unless (or (equal prefix "xmlns")
(gethash prefix table))
(setf (gethash prefix table)
(make-dom-namespace parent prefix uri))))
(record (parent node)
(record* parent
(or (dom:prefix node) "")
(dom:namespace-uri node)))
(recurse (node)
(record node node)
(dolist (attribute (dom:items (dom:attributes node)))
(cond
((equal (dom:namespace-uri attribute)
"http://www.w3.org/2000/xmlns/")
;; record explicitly declared namespaces, which might
;; not be in use anywhere
(record* node
(dom:local-name attribute)
(dom:value attribute)))
((plusp (length (dom:prefix attribute)))
;; record namespaces from DOM 2 slots, which might not
;; be declared in an attribute
(record node attribute))))
(let ((parent (dom:parent-node node)))
(when parent
(recurse parent)))))
(record* nil "xml" "http://www.w3.org/XML/1998/namespace")
(recurse node))
(maphash #'(lambda (prefix nsnode)
(declare (ignore prefix))
(push nsnode result))
table)
result))
(define-default-method xpath-protocol:node-text ((node dom:node))
;; FIXME: support document and document-fragment
(with-output-to-string (s)
(labels ((write-text (node)
(let ((value (dom:node-value node)))
(when value (write-string value s))
(unless (dom:attribute-p node) ;; FIXME: verify CDATA sections
(dom:do-node-list (child (dom:child-nodes node))
(cond ((or (dom:element-p child)
(dom:entity-reference-p child))
(write-text child))
((or (dom:text-node-p child)
(dom:attribute-p child)
(dom:cdata-section-p child))
(write-string (dom:node-value child) s))))))))
(write-text node))))
(define-default-method xpath-protocol:node-text ((node dom-namespace))
(dom-namespace-uri node))
;; currently computed from child-pipe
;;; (defmethod preceding-sibling-pipe ()
;;; (let ((parent (dom:parent-node node)))
;;; (if parent
;;; (let* ((children (dom:child-nodes parent))
;;; (pos (position node children)))
;;; (loop
;;; for i from (1- pos) downto 0
;;; collect (elt children i)))
;;; empty-pipe)))
(define-default-method xpath-protocol:node-type-p ((node dom:node) type)
(declare (ignore type))
nil)
(define-default-method xpath-protocol:node-type-p ((node dom-namespace) type)
(declare (ignore type))
nil)
(macrolet ((deftypemapping (class keyword)
`(define-default-method xpath-protocol:node-type-p
((node ,class) (type (eql ,keyword)))
t)))
(deftypemapping dom:comment :comment)
(deftypemapping dom:processing-instruction :processing-instruction)
(deftypemapping dom:text :text)
(deftypemapping dom:attr :attribute)
(deftypemapping dom:element :element)
(deftypemapping dom-namespace :namespace)
(deftypemapping dom:document :document))
(define-default-method xpath-protocol:get-element-by-id ((node dom:node) id)
(dom:get-element-by-id
(if (dom:document-p node) node (dom:owner-document node)) id))
(define-default-method xpath-protocol:unparsed-entity-uri
((node dom:node) name)
(let ((dtd (rune-dom::dtd (if (dom:document-p node)
node
(dom:owner-document node)))))
(when dtd
(let ((entdef (cdr (gethash name (cxml::dtd-gentities dtd)))))
(when (typep entdef 'cxml::external-entdef)
(let ((uri (cxml::extid-system (cxml::entdef-extid entdef))))
(when uri
(puri:render-uri uri nil))))))))
;; Character data
(define-default-method xpath-protocol:local-name
((node dom:character-data))
"")
(define-default-method xpath-protocol:namespace-prefix
((node dom:character-data))
"")
(define-default-method xpath-protocol:namespace-uri
((node dom:character-data))
"")
(define-default-method xpath-protocol:qualified-name
((node dom:character-data))
"")