-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathxmls-compat.lisp
352 lines (297 loc) · 11.9 KB
/
xmls-compat.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
;;; Copyright (c) 2008 Ivan Shvedunov. All rights reserved.
;;; Copyright (c) 2008 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-xmls)
(defstruct xmls-attribute
local-name
uri
value)
(defstruct xmls-namespace
parent
uri)
(defmethod xpath-protocol:node-p-using-navigator
((navi xpath-navigator) node)
nil)
(defmethod xpath-protocol:node-p-using-navigator
((navi xpath-navigator) (node cons))
t)
(defmethod xpath-protocol:node-p-using-navigator
((navi xpath-navigator) (node string))
;; check that we've seen it before
(with-slots (parents) navi
(nth-value 1 (gethash node parents))))
(defmethod xpath-protocol:node-p-using-navigator
((navi xpath-navigator) (node xmls-attribute))
t)
(defmethod xpath-protocol:node-p-using-navigator
((navi xpath-navigator) (node xmls-namespace))
t)
(defmethod xpath-protocol:node-equal-using-navigator
((navi xpath-navigator) (a t) (b t))
nil)
(defmethod xpath-protocol:node-equal-using-navigator
((navi xpath-navigator) (a cons) (b cons))
(eq a b))
(defmethod xpath-protocol:node-equal-using-navigator
((navi xpath-navigator) (a string) (b string))
(equal a b))
(defmethod xpath-protocol:hash-key-using-navigator
((navi xpath-navigator) node)
node)
(defun strip-prefix (car)
(let ((pos (position #\: car)))
(if pos
(subseq car (1+ pos))
car)))
(defmethod xpath-protocol:local-name-using-navigator
((navi xpath-navigator) (node cons))
(let ((car (car node)))
(if (consp car)
(car car)
(strip-prefix car))))
(defmethod xpath-protocol:namespace-prefix-using-navigator
((navi xpath-navigator) (node cons))
(register-prefix navi (node-ns node)))
;; the xmls tree doesn't record prefixes, only URIs, so just invent a prefix
(defun register-prefix (navi uri)
(with-slots (prefixes) navi
(if (zerop (length uri))
""
(or (gethash uri prefixes)
(setf (gethash uri prefixes)
(format nil "ns-~D" (hash-table-count prefixes)))))))
(defmethod xpath-protocol:namespace-uri-using-navigator
((navi xpath-navigator) (node cons))
(or (node-ns node) ""))
(defmethod xpath-protocol:qualified-name-using-navigator
((navi xpath-navigator) node)
(let ((uri (xpath-protocol:namespace-uri-using-navigator navi node))
(lname (xpath-protocol:local-name-using-navigator navi node)))
(if (zerop (length uri))
lname
(format nil "~A:~A"
(xpath-protocol:namespace-prefix-using-navigator navi node)
lname))))
(defmethod xpath-protocol:base-uri-using-navigator
((navi xpath-navigator) (node cons))
"#unsupported-by-xmls")
(defmethod xpath-protocol:local-name-using-navigator
((navi xpath-navigator) (node cons))
(node-name node))
(defun copy-node (x)
(etypecase x
(string
(replace (make-string (length x)) x))
(list
(apply #'list x))))
(defmethod xpath-protocol:parent-node-using-navigator
((navi xpath-navigator) node)
(with-slots (children parents) navi
(gethash node parents)))
(defmethod xpath-protocol:child-pipe-using-navigator
((navi xpath-navigator) (node cons))
(with-slots (children parents) navi
(multiple-value-bind (c cp)
(gethash node children)
(if cp
c
(setf (gethash node children)
(loop
for child in (node-children node)
;; protect against structure sharing, in particular strings
;; used multiple times:
for oops = (gethash child parents)
for replacement = (if oops (copy-node child) child)
collect replacement
;; register the parent
do (setf (gethash replacement parents) node)))))))
(defmethod xpath-protocol:attribute-pipe-using-navigator
((navi xpath-navigator) (node cons))
(with-slots (attributes parents) navi
(multiple-value-bind (a ap)
(gethash node attributes)
(if ap
a
(setf (gethash node attributes)
(loop
for (name value) in (second node)
for struct = (if (consp name)
(make-xmls-attribute
:local-name (car name)
:uri (cdr name)
:value value)
(make-xmls-attribute
:local-name (strip-prefix name)
:uri ""
:value value))
collect struct
;; register the parent
do (setf (gethash struct parents) struct)))))))
(defmethod xpath-protocol:namespace-pipe-using-navigator
((navi xpath-navigator) (node cons))
(with-slots (namespaces) navi
(multiple-value-bind (ns nsp)
(gethash node namespaces)
(if nsp
ns
(setf (gethash node namespaces)
(build-xpath-namespaces navi node))))))
(defun build-xpath-namespaces (navi node)
(with-slots (parents) navi
(let ((table (make-hash-table :test 'equal))
(result '()))
(labels ((record* (parent uri)
(unless (gethash uri table)
(let ((struct
(make-xmls-namespace :parent parent :uri uri)))
(setf (gethash struct parents) parent)
(setf (gethash uri table) struct))))
(record (parent node)
(let ((uri
(xpath-protocol:namespace-uri-using-navigator
navi node)))
(record* parent uri)))
(recurse (node)
(let ((parent (gethash node parents)))
(when parent
(recurse parent)))
(record node node)
(dolist (attribute
(xpath-protocol:attribute-pipe-using-navigator
navi node))
(record node attribute))))
(record* nil "http://www.w3.org/XML/1998/namespace")
(recurse node))
(maphash #'(lambda (prefix nsnode)
(declare (ignore prefix))
(push nsnode result))
table)
result)))
(defmethod xpath-protocol:node-p-using-navigator
((navi xpath-navigator) (node xmls-namespace))
t)
(defmethod xpath-protocol:child-pipe-using-navigator
((navi xpath-navigator) (node xmls-namespace))
nil)
(defmethod xpath-protocol:attribute-pipe-using-navigator
((navi xpath-navigator) (node xmls-namespace))
nil)
(defmethod xpath-protocol:namespace-pipe-using-navigator
((navi xpath-navigator) (node xmls-namespace))
nil)
(defmethod xpath-protocol:local-name-using-navigator
((navi xpath-navigator) (node xmls-namespace))
(xpath-protocol:namespace-prefix-using-navigator navi node))
(defmethod xpath-protocol:qualified-name-using-navigator
((navi xpath-navigator) (node xmls-namespace))
(xpath-protocol:namespace-prefix-using-navigator navi node))
(defmethod xpath-protocol:namespace-uri-using-navigator
((navi xpath-navigator) (node xmls-namespace))
(xmls-namespace-uri node))
(defmethod xpath-protocol:node-p-using-navigator
((navi xpath-navigator) (node xmls-attribute))
t)
(defmethod xpath-protocol:child-pipe-using-navigator
((navi xpath-navigator) (node xmls-attribute))
nil)
(defmethod xpath-protocol:attribute-pipe-using-navigator
((navi xpath-navigator) (node xmls-attribute))
nil)
(defmethod xpath-protocol:namespace-pipe-using-navigator
((navi xpath-navigator) (node xmls-attribute))
nil)
(defmethod xpath-protocol:local-name-using-navigator
((navi xpath-navigator) (node xmls-attribute))
(xmls-attribute-local-name node))
(defmethod xpath-protocol:namespace-uri-using-navigator
((navi xpath-navigator) (node xmls-attribute))
(xmls-attribute-uri node))
(defmethod xpath-protocol:node-text-using-navigator
((navi xpath-navigator) node)
(with-output-to-string (*standard-output*)
(labels ((write-text (node)
(etypecase node
(xmls-attribute
(write-string (xmls-attribute-value node)))
(string
(write-string node))
(cons
(mapc #'write-text (node-children node))))))
(write-text node))))
(defmethod xpath-protocol:node-type-p-using-navigator
((navi xpath-navigator) (node cons) (type t))
nil)
(defmethod xpath-protocol:node-type-p-using-navigator
((navi xpath-navigator) (node string) (type t))
nil)
(defmethod xpath-protocol:node-type-p-using-navigator
((navi xpath-navigator) (node xmls-attribute) (type t))
nil)
(defmethod xpath-protocol:node-type-p-using-navigator
((navi xpath-navigator) (node xmls-namespace) (type t))
nil)
(macrolet ((deftypemapping (class keyword)
`(defmethod xpath-protocol:node-type-p-using-navigator
((navi xpath-navigator) (node ,class) (type (eql ,keyword)))
t)))
(deftypemapping string :text)
(deftypemapping cons :element)
(deftypemapping xmls-attribute :attribute)
(deftypemapping xmls-namespace :namespace))
(defmethod xpath-protocol:local-name-using-navigator
((navi xpath-navigator) (node string))
"")
(defmethod xpath-protocol:namespace-prefix-using-navigator
((navi xpath-navigator) (node string))
"")
(defmethod xpath-protocol:namespace-uri-using-navigator
((navi xpath-navigator) (node string))
"")
(defmethod xpath-protocol:qualified-name-using-navigator
((navi xpath-navigator) (node string))
"")
(defmethod xpath-protocol:child-pipe-using-navigator
((navi xpath-navigator) (node string))
nil)
(defmethod xpath-protocol:attribute-pipe-using-navigator
((navi xpath-navigator) (node string))
nil)
(defmethod xpath-protocol:namespace-pipe-using-navigator
((navi xpath-navigator) (node string))
nil)
(defmethod xpath-protocol:get-element-by-id-using-navigator
((navi xpath-navigator) (node t) (id t))
nil)
(defmethod xpath-protocol:unparsed-entity-uri-using-navigator
((navi xpath-navigator) (node t) (name t))
nil)
(defmethod xpath-protocol:node-equal-using-navigator
((navi xpath-navigator) (a xmls-namespace) (b xmls-namespace))
(and (eq (xmls-namespace-parent a) (xmls-namespace-parent b))
(equal (xmls-namespace-uri a) (xmls-namespace-uri b))))
(defmethod xpath-protocol:hash-key-using-navigator
((navi xpath-navigator) (node xmls-namespace))
(cons (xmls-namespace-parent node) (xmls-namespace-uri node)))