-
Notifications
You must be signed in to change notification settings - Fork 2
/
xpath.lisp
311 lines (254 loc) · 10.3 KB
/
xpath.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
;;; -*- show-trailing-whitespace: t; indent-tabs: 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 :cxml-stp-impl)
(defun vector->pipe (vector &optional (start 0))
(if (>= start (length vector))
nil
(xpath::make-pipe (elt vector start)
(vector->pipe vector (1+ start)))))
;;;; XPath protocol implementation for STP
;;;; FIXME: xpath-protocol:child-pipe destructively normalizes the STP tree!
(define-default-method xpath-protocol:local-name ((node stp:node))
(local-name node))
(define-default-method xpath-protocol:namespace-prefix ((node stp:node))
(namespace-prefix node))
(define-default-method xpath-protocol:parent-node ((node stp:node))
(stp:parent node))
(define-default-method xpath-protocol:namespace-uri ((node stp:node))
(namespace-uri node))
(define-default-method xpath-protocol:qualified-name ((node stp:node))
(qualified-name node))
(define-default-method xpath-protocol:base-uri ((node stp:element))
(stp:base-uri node))
(define-default-method xpath-protocol:base-uri ((node stp:document))
(stp:base-uri node))
(define-default-method xpath-protocol:base-uri ((node stp:node))
nil)
(define-default-method xpath-protocol:child-pipe ((node stp:node))
nil)
(define-default-method xpath-protocol:child-pipe ((node stp:document))
(filter-children (alexandria:of-type '(not document-type)) node))
(define-default-method xpath-protocol:child-pipe ((node stp:element))
(normalize-text-nodes! node)
(vector->pipe (%children node)))
(define-default-method xpath-protocol:attribute-pipe ((node stp:node))
nil)
(define-default-method xpath-protocol:attribute-pipe ((node stp:element))
(list-attributes node))
(define-default-method xpath-protocol:namespace-pipe ((node stp:node))
(when (stp:parent node)
(xpath-protocol:namespace-pipe (stp:parent node))))
(defstruct (stp-namespace
(:constructor make-stp-namespace (parent prefix uri)))
parent
prefix
uri)
(define-default-method xpath-protocol:node-equal
((a stp-namespace) (b stp-namespace))
(and (eq (stp-namespace-parent a) (stp-namespace-parent b))
(equal (stp-namespace-prefix a) (stp-namespace-prefix b))))
(define-default-method xpath-protocol:hash-key
((node stp-namespace))
(cons (stp-namespace-parent node) (stp-namespace-prefix node)))
(define-default-method xpath-protocol:base-uri ((node stp-namespace))
nil)
(define-default-method xpath-protocol:node-p ((node stp-namespace))
t)
(define-default-method xpath-protocol:child-pipe ((node stp-namespace)) nil)
(define-default-method xpath-protocol:attribute-pipe ((node stp-namespace)) nil)
(define-default-method xpath-protocol:namespace-pipe ((node stp-namespace)) nil)
(define-default-method xpath-protocol:parent-node ((node stp-namespace))
(stp-namespace-parent node))
(define-default-method xpath-protocol:local-name ((node stp-namespace))
(stp-namespace-prefix node))
(define-default-method xpath-protocol:qualified-name ((node stp-namespace))
(stp-namespace-prefix node))
(define-default-method xpath-protocol:namespace-uri ((node stp-namespace))
"")
(define-default-method xpath-protocol:namespace-pipe
((original-node stp:element))
(let ((node original-node)
(table (make-hash-table :test 'equal))
(current '()))
(labels ((yield (prefix uri)
(unless (gethash prefix table)
(let ((nsnode (make-stp-namespace original-node prefix uri)))
(setf (gethash prefix table) nsnode)
(push nsnode current))))
(iterate ()
(if current
(cons (pop current) #'iterate)
(recurse)))
(recurse ()
(etypecase node
(null)
(stp:element
(let ((parent (stp:parent node)))
(map-extra-namespaces #'yield node)
(unless (and (zerop (length (%namespace-prefix node)))
(zerop (length (%namespace-uri node)))
(or (typep parent 'stp:document)
(zerop
(length
(stp:find-namespace "" parent)))))
(yield (%namespace-prefix node)
(%namespace-uri node)))
(dolist (a (%attributes node))
(when (plusp (length (namespace-prefix a)))
(yield (namespace-prefix a) (namespace-uri a))))
(setf node parent))
(iterate))
(stp:document
(yield "xml" "http://www.w3.org/XML/1998/namespace")
#+nil (yield "xmlns" "http://www.w3.org/2000/xmlns/")
(setf node nil)
(iterate)))))
(recurse))))
(define-default-method xpath-protocol:node-text ((node node))
(string-value node))
(define-default-method xpath-protocol:node-text ((node stp-namespace))
(stp-namespace-uri node))
(define-default-method xpath-protocol:node-p ((node node))
t)
(define-default-method xpath-protocol:node-type-p ((node node) type)
(declare (ignore type))
nil)
(define-default-method xpath-protocol:node-type-p ((node stp-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 document :document)
(deftypemapping comment :comment)
(deftypemapping processing-instruction :processing-instruction)
(deftypemapping text :text)
(deftypemapping attribute :attribute)
(deftypemapping element :element)
(deftypemapping stp-namespace :namespace))
(defun normalize-text-nodes! (node)
(when (typep node 'stp:parent-node)
(let ((children (%children node)))
(when (and children (loop
for child across children
for a = nil then b
for b = (typep child 'text)
thereis
(and b (or a
(zerop (length (stp:data child)))))))
(let ((previous nil)
(results '()))
(stp:do-children (child node)
(cond
((not (typep child 'stp:text))
(when previous
(push (stp:make-text
(apply #'concatenate 'string (nreverse previous)))
results)
(setf (%parent (car results)) node)
(setf previous nil))
(push child results))
(previous
(push (stp:data child) previous))
((zerop (length (stp:data child))))
(t
(setf previous (list (stp:data child))))))
(when previous
(push (stp:make-text
(apply #'concatenate 'string (nreverse previous)))
results)
(setf (%parent (car results)) node))
(setf (cxml-stp-impl::%children node)
(let ((n (length results)))
(make-array n
:fill-pointer n
:initial-contents (nreverse results)))))))))
(define-default-method xpath-protocol:get-element-by-id ((node stp:node) id)
(let* ((document (stp:document node))
(dtd (when (stp:document-type document)
(stp:dtd (stp:document-type document)))))
(when dtd
(block nil
(flet ((test (node)
(when (typep node 'stp:element)
(let ((elmdef
(cxml::find-element (stp:qualified-name node) dtd)))
(when elmdef
(dolist (attdef (cxml::elmdef-attributes elmdef))
(when (eq :ID (cxml::attdef-type attdef))
(let* ((name (cxml::attdef-name attdef))
(value (stp:attribute-value node name)))
(when (and value (equal value id))
(return node))))))))))
(find-recursively-if #'test document))))))
(define-default-method xpath-protocol:unparsed-entity-uri
((node stp:node) name)
(let ((doctype (stp:document-type (stp:document node))))
(when doctype
(let ((dtd (stp:dtd doctype)))
(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))))))))))
(define-default-method xpath-protocol:local-name ((node stp:text)) "")
(define-default-method xpath-protocol:namespace-prefix ((node stp:text)) "")
(define-default-method xpath-protocol:namespace-uri ((node stp:text)) "")
(define-default-method xpath-protocol:qualified-name ((node stp:text)) "")
(define-default-method xpath-protocol:local-name ((node stp:comment)) "")
(define-default-method xpath-protocol:namespace-prefix ((node stp:comment)) "")
(define-default-method xpath-protocol:namespace-uri ((node stp:comment)) "")
(define-default-method xpath-protocol:qualified-name
((node stp:comment))
"")
(define-default-method xpath-protocol:namespace-prefix
((node stp:processing-instruction))
"")
(define-default-method xpath-protocol:local-name
((node stp:processing-instruction))
(stp:target node))
(define-default-method xpath-protocol:qualified-name
((node stp:processing-instruction))
(stp:target node))
(define-default-method xpath-protocol:namespace-uri
((node stp:processing-instruction))
"")
(define-default-method xpath-protocol:namespace-uri
((node stp:document))
"")
(define-default-method xpath-protocol:namespace-prefix ((node stp:document))
"")
(define-default-method xpath-protocol:qualified-name ((node stp:document)) "")
(define-default-method xpath-protocol:local-name ((node stp:document)) "")
(define-default-method xpath-protocol:processing-instruction-target
((node stp:node))
node)
(define-default-method xpath-protocol:processing-instruction-target
((node stp:processing-instruction))
(stp:target node))