]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/xtm/reader.lisp
Support XTM item identity on occurrences associations and roles
[topic-maps.git] / src / xtm / reader.lisp
1 (in-package #:topic-maps.xtm)
2
3 (defparameter +namespace+ "http://www.topicmaps.org/xtm/")
4 ;; This is a source tree of XML events, not a second Topic Maps object model.
5 ;; Positions are SAX approximations at the end of a start tag, never byte spans.
6 (defstruct node uri name attributes children line column)
7 (defclass source-handler (sax:default-handler)
8 ((stack :initform nil :accessor handler-stack)
9 (root :initform nil :accessor handler-root)))
10 (defmethod sax:start-element ((h source-handler) uri local qname attributes)
11 (declare (ignore qname))
12 (let ((node (make-node :uri uri :name local :line (sax:line-number h)
13 :column (sax:column-number h)
14 :attributes (mapcar (lambda (a)
15 (list (sax:attribute-namespace-uri a)
16 (sax:attribute-local-name a)
17 (sax:attribute-value a))) attributes))))
18 (if (handler-stack h) (push node (node-children (first (handler-stack h))))
19 (setf (handler-root h) node))
20 (push node (handler-stack h))))
21 (defmethod sax:characters ((h source-handler) data)
22 (when (handler-stack h) (push (copy-seq data) (node-children (first (handler-stack h))))))
23 (defmethod sax:end-element ((h source-handler) uri local qname)
24 (declare (ignore uri local qname))
25 (let ((node (pop (handler-stack h)))) (setf (node-children node) (nreverse (node-children node)))))
26 (defmethod sax:end-document ((h source-handler)) (handler-root h))
27
28 (defun xml-space-p (c) (find c '(#\Space #\Tab #\Newline #\Return)))
29 (defun collapse-space (text)
30 ;; xsd:ID has whiteSpace=collapse, even with a non-validating XML processor.
31 (with-output-to-string (out)
32 (let ((written nil) (pending nil))
33 (loop for c across text do
34 (if (xml-space-p c) (when written (setf pending t))
35 (progn (when pending (write-char #\Space out))
36 (write-char c out) (setf written t pending nil)))))))
37 (defun ncname-p (text)
38 ;; Use the pinned XML processor's XML 1.0 name character tables; exclude ':'
39 ;; for XML Schema ID/NCName and XPointer shorthand pointers (XTM 4.5/4.20).
40 (and (plusp (length text)) (not (find #\: text))
41 (cxml::valid-name-p (runes:string-rod text))))
42 (defun attributes (node allowed required)
43 (dolist (attr (node-attributes node))
44 (unless (and (or (null (first attr)) (equal (first attr) ""))
45 (member (second attr) allowed :test #'string=))
46 (reject :unexpected-attribute (format nil "Attribute ~S on ~A." (second attr) (node-name node)) node)))
47 (dolist (name required)
48 (unless (find name (node-attributes node) :key #'second :test #'string=)
49 (reject (if (string= name "id") :missing-id :missing-attribute)
50 (format nil "Required ~A attribute is absent." name) node))))
51 (defun attr (node name)
52 (third (find name (node-attributes node) :key #'second :test #'string=)))
53 (defun elements (node)
54 (loop for child in (node-children node)
55 if (stringp child) do
56 (unless (every #'xml-space-p child)
57 (reject :unexpected-text "Only XML whitespace is allowed here." node))
58 else collect child))
59 (defun check-namespace (node)
60 (unless (equal (node-uri node) +namespace+)
61 (reject :invalid-namespace "Expected the XTM 2.0 namespace." node)))
62 (defun unsupported (node)
63 (reject :unsupported-construct (format nil "XTM ~A is not implemented in this subset." (node-name node))
64 node 'xtm-unsupported))
65 (defun leaf-href (node)
66 (attributes node '("href") '("href"))
67 (when (elements node) (reject :unexpected-child "A locator element must be empty." node))
68 (resolve-iri (attr node "href") *document-iri* node))
69 (defun reference-iri (node)
70 (let* ((iri (leaf-href node)) (fragment (iri-parts-fragment (split-iri iri))))
71 (unless (and fragment (ncname-p fragment))
72 (reject :invalid-topic-ref "topicRef requires an XPointer shorthand fragment." node))
73 iri))
74
75 (defun validate-source (root)
76 "Check the supported XTM schema before model construction; keep event order.
77 Unsupported legal extension points are explicit; no claim to validate excluded
78 constructs' complete subgrammars is made. XML errors have already been checked."
79 (let ((ids (make-hash-table :test #'equal))) ; XML ID uniqueness only, no Topics.
80 (labels ((validate (node context)
81 (check-namespace node)
82 (let ((name (node-name node)))
83 (unless (member name context :test #'string=)
84 (reject :unexpected-element (format nil "Unexpected ~A element." name) node))
85 (cond
86 ((string= name "topicMap")
87 (attributes node '("version" "reifier") '("version"))
88 (unless (equal (attr node "version") "2.0")
89 (reject :invalid-version "This reader accepts XTM version 2.0 only." node))
90 (when (attr node "reifier")
91 (resolve-iri (attr node "reifier") *document-iri* node))
92 (let ((phase 0))
93 (dolist (child (elements node))
94 (let* ((name (node-name child))
95 (next (cond ((string= name "itemIdentity") 0)
96 ((string= name "mergeMap") 1) (t 2))))
97 (when (< next phase) (reject :element-order "topicMap child order is invalid." child))
98 (setf phase next)
99 (validate child '("itemIdentity" "mergeMap" "topic" "association"))))))
100 ((string= name "mergeMap") (mergemap-iri node))
101 ((string= name "topic")
102 (attributes node '("id") '("id"))
103 (let ((id (collapse-space (attr node "id"))))
104 (unless (ncname-p id) (reject :invalid-id "topic/@id must be an XML Schema ID." node))
105 (when (gethash id ids) (reject :duplicate-id "Duplicate XML ID in this document." node))
106 (setf (gethash id ids) t))
107 (let ((phase 0) (instance-seen nil))
108 (dolist (child (elements node))
109 (let* ((name (node-name child))
110 (next (cond ((member name '("itemIdentity" "subjectLocator" "subjectIdentifier")
111 :test #'string=) 0)
112 ((string= name "instanceOf") 1) (t 2))))
113 (when (< next phase) (reject :element-order "topic child order is invalid." child))
114 (when (= next 1)
115 (when instance-seen (reject :duplicate-instance-of "Only one instanceOf element is allowed." child))
116 (setf instance-seen t))
117 (setf phase next)
118 (validate child '("itemIdentity" "subjectLocator" "subjectIdentifier" "instanceOf" "name" "occurrence"))))))
119 ((string= name "instanceOf")
120 (attributes node nil nil)
121 (unless (elements node) (reject :empty-instance-of "instanceOf needs at least one topicRef." node))
122 (dolist (child (elements node)) (validate child '("topicRef"))))
123 ((string= name "name")
124 (attributes node '("reifier") nil)
125 (when (attr node "reifier")
126 (resolve-iri (attr node "reifier") *document-iri* node))
127 (let ((phase 0) (type-seen nil) (scope-seen nil) (value-seen nil))
128 (dolist (child (elements node))
129 (check-namespace child)
130 (let* ((kind (node-name child))
131 (next (position kind '("itemIdentity" "type" "scope" "value" "variant") :test #'string=)))
132 (unless next (reject :unexpected-element (format nil "Unexpected ~A element." kind) child))
133 (when (or (< next phase) (and (string= kind "variant") (not value-seen)))
134 (reject :element-order "name child order is invalid." child))
135 (setf phase next)
136 (cond
137 ((member kind '("itemIdentity" "variant") :test #'string=) nil)
138 ((string= kind "type")
139 (when type-seen (reject :duplicate-name-type "Only one name type is allowed." child))
140 (setf type-seen t))
141 ((string= kind "scope")
142 (when scope-seen (reject :duplicate-name-scope "Only one name scope is allowed." child))
143 (setf scope-seen t))
144 ((string= kind "value")
145 (when value-seen (reject :duplicate-name-value "Only one name value is allowed." child))
146 (setf value-seen t))
147 ;; Valid extension points remain explicitly unsupported.
148 ;; Their complete subgrammars are outside this subset.
149 (t (unsupported child)))
150 (validate child '("itemIdentity" "type" "scope" "value" "variant"))))
151 (unless value-seen (reject :missing-name-value "name requires a value element." node))))
152 ((string= name "variant")
153 (attributes node '("reifier") nil)
154 (when (attr node "reifier") (resolve-iri (attr node "reifier") *document-iri* node))
155 (let ((phase 0) (scope-seen nil) (resource-seen nil))
156 (dolist (child (elements node))
157 (check-namespace child)
158 (let* ((kind (node-name child))
159 (next (cond ((string= kind "itemIdentity") 0)
160 ((string= kind "scope") 1)
161 ((member kind '("resourceData" "resourceRef") :test #'string=) 2))))
162 (unless next (reject :unexpected-element (format nil "Unexpected ~A element." kind) child))
163 (when (< next phase) (reject :element-order "variant child order is invalid." child))
164 (setf phase next)
165 (cond
166 ((string= kind "itemIdentity") nil)
167 ((string= kind "scope")
168 (when scope-seen (reject :duplicate-variant-scope "Only one variant scope is allowed." child))
169 (setf scope-seen t))
170 (t
171 (unless scope-seen (reject :missing-variant-scope "variant requires scope before its resource." child))
172 (when resource-seen (reject :variant-resource-cardinality "variant requires exactly one resource." child))
173 (setf resource-seen t)))
174 (validate child '("itemIdentity" "scope" "resourceData" "resourceRef"))))
175 (unless scope-seen (reject :missing-variant-scope "variant requires a scope element." node))
176 (unless resource-seen (reject :missing-variant-resource "variant requires a resource element." node))))
177 ((string= name "occurrence")
178 (attributes node '("reifier") nil)
179 (when (attr node "reifier")
180 (resolve-iri (attr node "reifier") *document-iri* node)
181 (reject :unsupported-construct "XTM occurrence reification is not implemented in this subset." node 'xtm-unsupported))
182 (let ((phase 0) (type-seen nil) (scope-seen nil) (resource-seen nil))
183 (dolist (child (elements node))
184 (check-namespace child)
185 (let* ((kind (node-name child))
186 (next (cond ((string= kind "itemIdentity") 0)
187 ((string= kind "type") 1)
188 ((string= kind "scope") 2)
189 ((member kind '("resourceData" "resourceRef") :test #'string=) 3))))
190 (unless next (reject :unexpected-element (format nil "Unexpected ~A element." kind) child))
191 (when (< next phase) (reject :element-order "occurrence child order is invalid." child))
192 (setf phase next)
193 (cond
194 ((string= kind "itemIdentity") nil)
195 ((string= kind "type")
196 (when type-seen (reject :duplicate-occurrence-type "Only one occurrence type is allowed." child))
197 (setf type-seen t))
198 ((string= kind "scope")
199 (unless type-seen (reject :missing-occurrence-type "occurrence requires type before scope." child))
200 (when scope-seen (reject :duplicate-occurrence-scope "Only one occurrence scope is allowed." child))
201 (setf scope-seen t))
202 (t
203 (unless type-seen (reject :missing-occurrence-type "occurrence requires type before its resource." child))
204 (when resource-seen (reject :occurrence-resource-cardinality "occurrence requires exactly one resource." child))
205 (setf resource-seen t)))
206 (validate child '("itemIdentity" "type" "scope" "resourceData" "resourceRef"))))
207 (unless type-seen (reject :missing-occurrence-type "occurrence requires a type element." node))
208 (unless resource-seen (reject :missing-occurrence-resource "occurrence requires a resource element." node))))
209 ((member name '("association" "role") :test #'string=)
210 (attributes node '("reifier") nil)
211 (when (attr node "reifier")
212 (resolve-iri (attr node "reifier") *document-iri* node)
213 (reject :unsupported-construct
214 (format nil "XTM ~A reification is not implemented in this subset." name) node 'xtm-unsupported))
215 (let ((association-p (string= name "association"))
216 (phase 0) (type-seen nil) (scope-seen nil) (participants 0))
217 (dolist (child (elements node))
218 (check-namespace child)
219 (let* ((kind (node-name child))
220 (next (cond ((string= kind "itemIdentity") 0)
221 ((string= kind "type") 1)
222 ((and association-p (string= kind "scope")) 2)
223 ((string= kind (if association-p "role" "topicRef")) 3))))
224 (unless next (reject :unexpected-element (format nil "Unexpected ~A element." kind) child))
225 (when (< next phase) (reject :element-order (format nil "~A child order is invalid." name) child))
226 (setf phase next)
227 (cond
228 ((string= kind "itemIdentity") nil)
229 ((string= kind "type")
230 (when type-seen (reject :duplicate-type (format nil "Only one ~A type is allowed." name) child))
231 (setf type-seen t))
232 (t
233 (unless type-seen
234 (reject :missing-type (format nil "~A requires type before ~A." name kind) child))
235 (if (string= kind "scope")
236 (progn
237 (when scope-seen (reject :duplicate-scope "Only one association scope is allowed." child))
238 (setf scope-seen t))
239 (progn
240 (incf participants)
241 (when (and (not association-p) (> participants 1))
242 (reject :role-player-cardinality "role requires exactly one topicRef player." child))))))
243 (validate child (if association-p '("itemIdentity" "type" "scope" "role") '("itemIdentity" "type" "topicRef")))))
244 (unless type-seen (reject :missing-type (format nil "~A requires a type element." name) node))
245 (when (zerop participants)
246 (reject (if association-p :missing-roles :missing-role-player)
247 (if association-p "association requires at least one role." "role requires a topicRef player.") node))))
248 ((string= name "resourceRef") (leaf-href node))
249 ((string= name "resourceData")
250 (attributes node '("datatype") nil)
251 (let ((datatype (attr node "datatype")))
252 ;; DATATYPE is already an absolute IRI, not content to
253 ;; resolve against the document or percent-normalize.
254 (when datatype
255 (unless (iri-parts-scheme (split-iri datatype))
256 (reject :invalid-resource-data-datatype "resourceData datatype must be an absolute IRI." node))
257 (validate-decoded-reference (unescape-reference datatype node) node)
258 (when (equal datatype "http://www.w3.org/2001/XMLSchema#anyType")
259 (reject :resource-data-datatype "xsd:anyType XML canonicalization is outside this subset." node 'xtm-unsupported)))
260 (dolist (child (node-children node))
261 (unless (stringp child)
262 (reject :unexpected-child "Textual resourceData cannot contain child elements." child)))))
263 ((string= name "type")
264 (attributes node nil nil)
265 (let ((refs (elements node)))
266 (unless (= 1 (length refs)) (reject :type-cardinality "type requires exactly one topicRef." node))
267 (validate (first refs) '("topicRef"))))
268 ((string= name "scope")
269 (attributes node nil nil)
270 (unless (elements node) (reject :empty-scope "scope requires at least one topicRef." node))
271 (dolist (child (elements node)) (validate child '("topicRef"))))
272 ((string= name "value")
273 (attributes node nil nil)
274 ;; VALUE accepts all character data, including empty text and
275 ;; whitespace. ELEMENTS would apply the wrong text policy.
276 (dolist (child (node-children node))
277 (unless (stringp child) (reject :unexpected-child "value accepts character data only." child))))
278 ((string= name "topicRef") (reference-iri node))
279 ((member name '("itemIdentity" "subjectIdentifier" "subjectLocator") :test #'string=)
280 (leaf-href node))
281 (t (unsupported node))))))
282 (validate root '("topicMap"))))
283 root)
284
285 (defun find-reference-topic (map iri)
286 ;; XTM 4.20 searches the actual model, not a speculative pre-scan registry.
287 ;; Unlike INTERN-TOPIC, a hit MUST NOT add another identifier category.
288 (let ((topic (find-if (lambda (topic)
289 (or (member iri (topic-maps.tmdm:topic-subject-identifiers topic) :test #'string=)
290 (member iri (topic-maps.tmdm:topic-item-identifiers topic) :test #'string=)))
291 (topic-maps.tmdm:topic-map-topics map))))
292 (if topic (topic-maps.tmdm:canonical-topic topic)
293 (topic-maps.tmdm:intern-topic map :item-identifier iri))))
294 (defun read-item-reifier (node item map)
295 ;; XTM 4.3.2 looks up Item Identifiers only, unlike topicRef (4.20).
296 ;; INTERN-TOPIC adds the II on a miss; any SI/II identity merging then
297 ;; follows the existing TMDM rules, independently of this lookup.
298 (when (attr node "reifier")
299 (let* ((iri (resolve-iri (attr node "reifier") *document-iri* node))
300 (topic (or (find iri (topic-maps.tmdm:topic-map-topics map)
301 :test (lambda (iri topic)
302 (member iri (topic-maps.tmdm:topic-item-identifiers topic) :test #'string=)))
303 (topic-maps.tmdm:intern-topic map :item-identifier iri))))
304 (return-from read-item-reifier (topic-maps.tmdm:set-item-reifier item topic))))
305 item)
306 (defun read-map-reifier (node map)
307 (read-item-reifier node map map))
308 (defun core-topic (map local)
309 (topic-maps.tmdm:intern-topic map :subject-identifier
310 (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local)))
311 (defun read-instance-of (node owner map)
312 (dolist (ref (elements node))
313 (let ((type (find-reference-topic map (reference-iri ref))))
314 (topic-maps.tmdm:make-association map (core-topic map "type-instance")
315 (list (list (core-topic map "type") type)
316 (list (core-topic map "instance") (topic-maps.tmdm:canonical-topic owner)))))))
317 (defun read-item-identities (node item)
318 ;; XTM 4.6: assign locators to the actual canonical item, never intern Topics.
319 ;; ADD-ITEM-IDENTIFIER owns set semantics and cross-item conflict checks.
320 (dolist (child (elements node) item)
321 (when (string= (node-name child) "itemIdentity")
322 (setf item (topic-maps.tmdm:add-item-identifier item (leaf-href child))))))
323 (defun read-association (node map)
324 ;; Resolve references in source order, then construct the complete native
325 ;; Association. The constructor may already merge Associations AND Roles.
326 (let ((type nil) (scope nil) (roles nil) (role-nodes nil))
327 (labels ((reference (ref) (find-reference-topic map (reference-iri ref)))
328 (type-reference (node) (reference (first (elements node)))))
329 (dolist (child (elements node))
330 (cond ((string= (node-name child) "type") (setf type (type-reference child)))
331 ((string= (node-name child) "scope") (setf scope (mapcar #'reference (elements child))))
332 ((string= (node-name child) "role")
333 (let ((children (elements child)))
334 (push (list (type-reference (find "type" children :key #'node-name :test #'string=))
335 (reference (find "topicRef" children :key #'node-name :test #'string=))) roles)
336 (push child role-nodes)))))
337 (let ((association (read-item-identities node
338 (topic-maps.tmdm:make-association map type (reverse roles) :scope scope))))
339 ;; Match source metadata to the native Role by its complete signature
340 ;; within THIS canonical Parent. Role order/II are not equality keys.
341 (loop for pair in (reverse roles) for source in (reverse role-nodes) do
342 (let ((role (find-if (lambda (role)
343 (and (eq (topic-maps.tmdm:association-role-type role)
344 (topic-maps.tmdm:canonical-topic (first pair)))
345 (eq (topic-maps.tmdm:association-role-player role)
346 (topic-maps.tmdm:canonical-topic (second pair)))))
347 (topic-maps.tmdm:association-roles association))))
348 (read-item-identities source role)))
349 (topic-maps.tmdm:canonical-association association)))))
350 (defun read-resource-value (resource)
351 "Return value and datatype for a validated resource node (XTM 4.16/4.17).
352 This operation neither looks up Topics nor fetches resources. ANYTYPE is rejected
353 by source validation before model construction. Preserve other datatype IRIs."
354 (if (string= (node-name resource) "resourceRef")
355 (values (leaf-href resource) (topic-maps.tmdm:iri-datatype))
356 (let ((datatype (or (attr resource "datatype") (topic-maps.tmdm:string-datatype)))
357 (text (with-output-to-string (out)
358 (dolist (part (node-children resource)) (write-string part out)))))
359 (values (if (equal datatype (topic-maps.tmdm:iri-datatype))
360 (resolve-iri text *document-iri* resource)
361 text)
362 datatype))))
363 (defun read-variant (node name map)
364 (let* ((children (elements node))
365 (scope-node (find "scope" children :key #'node-name :test #'string=))
366 (resource (find-if (lambda (child) (member (node-name child) '("resourceData" "resourceRef") :test #'string=)) children))
367 (explicit (mapcar (lambda (ref) (find-reference-topic map (reference-iri ref))) (elements scope-node))))
368 (multiple-value-bind (value datatype) (read-resource-value resource)
369 ;; ADD-VARIANT owns canonicalization and the proper-superset constraint.
370 (let ((variant (topic-maps.tmdm:add-variant name value :datatype datatype
371 :scope (append (topic-maps.tmdm:topic-name-scope name) explicit))))
372 (setf variant (read-item-identities node variant))
373 ;; Reification may normalize again; retain the native canonical handle.
374 (topic-maps.tmdm:canonical-variant (read-item-reifier node variant map))))))
375 (defun read-name (node owner map)
376 (let* ((children (elements node))
377 (type-node (find "type" children :key #'node-name :test #'string=))
378 (scope-node (find "scope" children :key #'node-name :test #'string=))
379 (type (when type-node (find-reference-topic map (reference-iri (first (elements type-node))))))
380 (scope (when scope-node (mapcar (lambda (ref) (find-reference-topic map (reference-iri ref)))
381 (elements scope-node))))
382 (value-node (find "value" children :key #'node-name :test #'string=))
383 (value (with-output-to-string (out)
384 (dolist (part (node-children value-node)) (write-string part out)))))
385 ;; SAX has already resolved XML character/entity references. Preserve the
386 ;; resulting Unicode sequence without trimming or decoding it again.
387 ;; ADD-NAME owns canonical set semantics and normalization after merging.
388 (let ((name (if type-node
389 (topic-maps.tmdm:add-name owner value :type type :scope scope)
390 (topic-maps.tmdm:add-default-name owner value :scope scope))))
391 ;; Item identifiers belong to the canonical Name, never its owner/type.
392 ;; Use the shared model API for set semantics and global conflicts.
393 (setf name (read-item-identities node name))
394 ;; ADD-NAME may have returned an existing canonical Name. Reifier
395 ;; resolution/merging can normalize it again: retain the native result
396 ;; before processing Variants and return the current Name handle.
397 (setf name (read-item-reifier node name map))
398 (dolist (child children)
399 (when (string= (node-name child) "variant") (read-variant child name map)))
400 (topic-maps.tmdm:canonical-topic-name name))))
401 (defun read-occurrence (node owner map)
402 ;; XTM 4.13-4.17/4.20: reuse native Topics and the qualified resource path.
403 ;; ADD-OCCURRENCE owns live Parent/Type/Scope handles and TMDM 5.6/6.5 merging.
404 (let* ((children (elements node))
405 (type-node (find "type" children :key #'node-name :test #'string=))
406 (type (find-reference-topic map (reference-iri (first (elements type-node)))))
407 (scope-node (find "scope" children :key #'node-name :test #'string=))
408 (scope (when scope-node (mapcar (lambda (ref) (find-reference-topic map (reference-iri ref)))
409 (elements scope-node))))
410 (resource (find-if (lambda (child) (member (node-name child) '("resourceData" "resourceRef") :test #'string=)) children)))
411 (multiple-value-bind (value datatype) (read-resource-value resource)
412 (topic-maps.tmdm:canonical-occurrence
413 (read-item-identities node
414 (topic-maps.tmdm:add-occurrence owner type value :datatype datatype :scope scope))))))
415 (defun read-topic (node map)
416 (let ((topic (topic-maps.tmdm:intern-topic map :item-identifier
417 (concatenate 'string *document-iri* "#" (collapse-space (attr node "id"))))))
418 (dolist (child (elements node))
419 (let ((name (node-name child)))
420 (cond
421 ((string= name "instanceOf") (read-instance-of child topic map))
422 ((string= name "name") (read-name child topic map))
423 ((string= name "occurrence") (read-occurrence child topic map))
424 (t (setf topic (topic-maps.tmdm:add-topic-identifier topic
425 (cond ((string= name "itemIdentity") :item-identifier)
426 ((string= name "subjectIdentifier") :subject-identifier)
427 (t :subject-locator))
428 (leaf-href child)))))))
429 (topic-maps.tmdm:canonical-topic topic)))
430 (defun mergemap-iri (node)
431 (let ((iri (leaf-href node)))
432 (when (iri-parts-fragment (split-iri iri))
433 (reject :mergemap-fragment "mergeMap href must not have a fragment identifier." node))
434 iri))
435 (defun resource-failure (code detail node &optional cause)
436 (error 'xtm-resource-error :code code :detail detail :cause cause
437 :document-iri *document-iri* :source-key *source-key*
438 :line (node-line node) :column (node-column node)))
439 (defun resource-octets-p (value)
440 ;; Same resolver value convention as CTM, without a dependency on its Reader.
441 (and (vectorp value) (not (stringp value))
442 (every (lambda (n) (and (integerp n) (<= 0 n 255))) value)))
443 (defun read-mergemap (node map resolver max-depth active-iris)
444 (let ((resource (mergemap-iri node)))
445 (unless resolver
446 (reject :resource-resolver-required "mergeMap requires an explicit resource resolver; no resource was loaded." node 'xtm-unsupported))
447 ;; These are finite resource-policy guards, not normative cycle semantics.
448 ;; Each independent public call starts a new active path; siblings may repeat.
449 (when (member resource active-iris :test #'string=)
450 (reject :cyclic-mergemap "Active recursive XTM resource dependency; cycle processing is not implemented." node 'xtm-unsupported))
451 (when (>= (length active-iris) (1+ max-depth))
452 (reject :resource-depth-limit "Configured nested XTM resource limit reached; this is implementation policy." node 'xtm-unsupported))
453 (multiple-value-bind (bytes source-key)
454 (handler-case
455 (funcall resolver resource :purpose :mergemap :from-document-iri *document-iri*
456 :notation "http://psi.topicmaps.org/iso13250/xtm")
457 (error (c) (resource-failure :resource-resolver-failure (princ-to-string c) node c)))
458 (unless bytes
459 (resource-failure :resource-unavailable "Resolver declined or did not supply the requested XTM resource." node))
460 (unless (and (resource-octets-p bytes) (or (null source-key) (stringp source-key)))
461 (resource-failure :invalid-resource-result "Resolver must return an octet vector and a string source key or NIL." node))
462 (let ((source (deserialize-document
463 (if (typep bytes '(vector (unsigned-byte 8))) bytes
464 (map '(vector (unsigned-byte 8)) #'identity bytes))
465 resource (or source-key resource) resolver max-depth active-iris)))
466 ;; Import only Topics/Associations. The existing operation preflights
467 ;; conflicts, preserves SOURCE, and excludes its Map identifiers/reifier.
468 (handler-case (topic-maps.tmdm:import-map-content map source)
469 (error (c) (resource-failure :mergemap-import-failure (princ-to-string c) node c)))))))
470 (defun deserialize-octets (bytes &key document-iri source-key resource-resolver (max-resource-depth 32))
471 "Deserialize raw XTM 2.0 octets into a fresh TMDM Map.
472 DOCUMENT-IRI is required, absolute and fragment-free. SOURCE-KEY is diagnostic.
473 RESOURCE-RESOLVER is optional: (resolver absolute-iri :purpose :mergemap
474 :from-document-iri parent-iri :notation XTM-notation-IRI)
475 returns raw octets and an optional diagnostic source key. No implicit I/O.
476 Children use their resolved IRI as Document IRI and the same explicit resolver.
477 MAX-RESOURCE-DEPTH bounds nested imports (default 32); active cycles are
478 Unsupported implementation policy, not a normative XTM invalidity decision.
479 Child XML/XTM/model errors retain their original contexts; resolver/import
480 failures use XTM-RESOURCE-ERROR with the original ERROR-CAUSE. No supplied Map
481 is mutated. Each public call has its own resource recursion context."
482 (check-type max-resource-depth (integer 0 *))
483 (deserialize-document bytes document-iri source-key resource-resolver max-resource-depth nil))
484 (defun deserialize-document (bytes document-iri source-key resource-resolver max-resource-depth active-iris)
485 (check-type bytes (vector (unsigned-byte 8)))
486 (let ((*document-iri* document-iri) (*source-key* source-key)
487 (handler (make-instance 'source-handler)))
488 (require-document-iri document-iri)
489 (let* ((root
490 (handler-case (topic-maps.xml:parse-octets bytes handler)
491 (cxml:well-formedness-violation (c)
492 (error 'xtm-xml-error :code :xml-not-well-formed :cause c
493 :detail (princ-to-string c) :document-iri document-iri :source-key source-key
494 :line (sax:line-number handler) :column (sax:column-number handler)))
495 (topic-maps.xml:doctype-forbidden (c)
496 (error 'xtm-xml-error :code :doctype-forbidden :cause c
497 :detail "DOCTYPE forbidden by the mandatory XML byte boundary."
498 :document-iri document-iri :source-key source-key
499 :line (sax:line-number handler) :column (sax:column-number handler)))))
500 (map (topic-maps.tmdm:make-topic-map)))
501 (validate-source root)
502 ;; The attribute is processed before itemIdentity and later Topic nodes.
503 (read-map-reifier root map)
504 (dolist (child (elements root))
505 (cond ((string= (node-name child) "itemIdentity")
506 (topic-maps.tmdm:add-item-identifier map (leaf-href child)))
507 ((string= (node-name child) "mergeMap")
508 (read-mergemap child map resource-resolver max-resource-depth
509 (cons document-iri active-iris)))
510 ((string= (node-name child) "association") (read-association child map))
511 (t (read-topic child map))))
512 map)))