1 (in-package #:topic-maps.ctm)
3 (defun directive-iri (parser code message)
4 ;; Reuse the existing IRI/QName path without interning a datatype/identity Topic.
5 (unless (member (token-kind (look parser)) '(:iri :qname))
6 (problem 'ctm-rejection code (token-position (look parser)) message))
7 (literal-iri-reference parser))
9 (defun resource-octets-p (value)
10 (and (vectorp value) (not (stringp value))
11 (every (lambda (byte) (and (integerp byte) (<= 0 byte 255))) value)))
13 (defun read-mergemap-directive (parser map)
14 "CTM 3.16.3: immediately read a separate Map and import only its content."
15 (let* ((start (consume parser))
16 (resource (directive-iri parser :expected-mergemap-resource "Mergemap requires a resource IRI reference."))
17 (notation (directive-iri parser :expected-mergemap-notation "Mergemap requires a notation IRI reference."))
18 (position (token-position start))
19 (frame (list :purpose :mergemap :source *source-name* :document-iri (parser-document-iri parser)
20 :position position :position-unit :character :resource-iri resource :notation notation :resource-source nil))
21 (*resource-context* (cons frame *resource-context*)))
22 (cond ((string= notation "http://psi.topicmaps.org/iso13250/xtm")
23 (problem 'ctm-unsupported :xtm-mergemap-notation position "XTM is a mandatory standard notation, but its deserializer is not implemented."))
24 ((not (string= notation "http://psi.topicmaps.org/iso13250/ctm"))
25 (problem 'ctm-rejection :unknown-mergemap-notation position "The notation IRI is not supported by this processor.")))
26 (unless (parser-resource-resolver parser)
27 (problem 'ctm-unsupported :resource-resolver-required position "Mergemap needs an explicit resource resolver; no resource was loaded."))
28 ;; This stack tracks active deserializations only. A repeated completed
29 ;; resource is read again, with a fresh parser and its own wildcard state.
30 (when (member resource *active-resource-iris* :test #'string=)
31 (problem 'ctm-unsupported :cyclic-mergemap position "Active recursive resource dependency; cycle rejection is implementation policy."))
32 (when (>= (length *active-resource-iris*) (1+ (parser-max-resource-depth parser)))
33 (problem 'ctm-unsupported :resource-depth-limit position "Configured nested-resource limit reached; this is not a CTM syntax rule."))
34 (multiple-value-bind (bytes source-key)
36 (funcall (parser-resource-resolver parser) resource :purpose :mergemap
37 :from-document-iri (parser-document-iri parser) :notation notation)
39 (problem 'ctm-resource-error :resource-resolver-failure position (princ-to-string condition))))
40 (setf (getf frame :resource-source) (or source-key resource))
42 (problem 'ctm-resource-error :resource-unavailable position "Resolver declined or did not supply the requested resource."))
43 (unless (and (resource-octets-p bytes) (or (null source-key) (stringp source-key)))
44 (problem 'ctm-resource-error :invalid-resource-result position "Resolver must return an octet vector and a string source key or NIL."))
46 (let ((*source-name* (or source-key resource)) (*source-text* ""))
47 ;; Decoding uses the existing strict UTF-8 decoder and BOM policy.
48 ;; The key is diagnostic only; RESOURCE remains the Document IRI.
49 (deserialize-string (decode-utf8 bytes :strip-bom t) :document-iri resource
50 :source-name *source-name* :resource-resolver (parser-resource-resolver parser)
51 :max-resource-depth (parser-max-resource-depth parser)))))
52 (handler-case (topic-maps.tmdm:import-map-content map source)
53 (topic-maps.tmdm:item-identifier-error (c)
54 (problem 'ctm-rejection (topic-maps.tmdm:item-identifier-error-code c) position (princ-to-string c)))
55 (topic-maps.tmdm:core-subject-error (c)
56 (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c) position (princ-to-string c)))
57 (topic-maps.tmdm:variant-error (c)
58 (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c) position (princ-to-string c)))
59 (topic-maps.tmdm:reification-error (c)
60 (problem 'ctm-rejection (topic-maps.tmdm:reification-error-code c) position (princ-to-string c))))))))
62 (defun read-directive (parser map)
63 (cond ((string= "prefix" (token-value (look parser))) (read-prefix-directive parser))
64 ((string= "mergemap" (token-value (look parser))) (read-mergemap-directive parser map))
65 (t (problem 'ctm-unsupported :unsupported-directive (token-position (look parser)) "Directive is outside the supported subset."))))