]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/ctm/resources.lisp
test(ctm): register seven reviewed negative Slice 20B contracts
[topic-maps.git] / src / ctm / resources.lisp
1 (in-package #:topic-maps.ctm)
2
3 (defun directive-iri (parser code message)
4 ;; Reuse the existing IRI/QName path without interning an 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))
8
9 (defun resource-octets-p (value)
10 (and (vectorp value) (not (stringp value))
11 (every (lambda (byte) (and (integerp byte) (<= 0 byte 255))) value)))
12
13 (defgeneric mergemap-reader (notation)
14 (:documentation "Internal notation dispatch. The explicit CTM/XTM adapter adds :XTM."))
15 (defmethod mergemap-reader ((notation t)) nil)
16 (defmethod mergemap-reader ((notation (eql :ctm)))
17 (lambda (bytes resource source-key parser)
18 (deserialize-string (decode-ctm-resource bytes source-key)
19 :document-iri resource :source-name source-key
20 :resource-resolver (parser-resource-resolver parser)
21 :max-resource-depth (parser-max-resource-depth parser))))
22 (defun mergemap-notation-kind (notation position)
23 (cond ((string= notation "http://psi.topicmaps.org/iso13250/ctm") :ctm)
24 ((string= notation "http://psi.topicmaps.org/iso13250/xtm") :xtm)
25 (t (problem 'ctm-rejection :unknown-mergemap-notation position
26 "The notation IRI is not supported by this processor."))))
27 (defun decode-ctm-resource (bytes source-key)
28 ;; Only CTM resources use the CTM decoder, including the existing BOM policy.
29 (let ((*source-name* source-key) (*source-text* ""))
30 (decode-utf8 bytes :strip-bom t)))
31
32 (defun call-with-resource (parser resource purpose position notation continuation)
33 "Controlled retrieval of untouched octets. Decoding belongs to the notation reader."
34 (let* ((frame (list :purpose purpose :source *source-name* :document-iri (parser-document-iri parser)
35 :position position :position-unit :character :resource-iri resource :notation notation :resource-source nil))
36 (*resource-context* (cons frame *resource-context*)))
37 (when (eq purpose :mergemap) (mergemap-notation-kind notation position))
38 (unless (parser-resource-resolver parser)
39 (problem 'ctm-unsupported :resource-resolver-required position "An explicit resource resolver is required; no resource was loaded."))
40 (when (and (eq purpose :mergemap)
41 (not (mergemap-reader (mergemap-notation-kind notation position))))
42 (problem 'ctm-unsupported :xtm-mergemap-reader-required position
43 "Load topic-maps/ctm/xtm with the qualified XML source environment before XTM Mergemap."))
44 ;; Active recursion and depth limits are implementation resource policy,
45 ;; not claims that recursive dependencies are invalid CTM syntax.
46 (when (member resource *active-resource-iris* :test #'string=)
47 (problem 'ctm-unsupported (ecase purpose (:mergemap :cyclic-mergemap) (:include :cyclic-include)) position
48 "Active recursive resource dependency; cycle rejection is implementation policy."))
49 (when (>= (length *active-resource-iris*) (1+ (parser-max-resource-depth parser)))
50 (problem 'ctm-unsupported :resource-depth-limit position "Configured nested-resource limit reached; this is not a CTM syntax rule."))
51 (multiple-value-bind (bytes source-key)
52 (handler-case
53 (funcall (parser-resource-resolver parser) resource :purpose purpose
54 :from-document-iri (parser-document-iri parser) :notation notation)
55 (error (condition)
56 (problem 'ctm-resource-error :resource-resolver-failure position (princ-to-string condition) :character condition)))
57 (setf (getf frame :resource-source) (or source-key resource))
58 (unless bytes
59 (problem 'ctm-resource-error :resource-unavailable position "Resolver declined or did not supply the requested resource."))
60 (unless (and (resource-octets-p bytes) (or (null source-key) (stringp source-key)))
61 (problem 'ctm-resource-error :invalid-resource-result position "Resolver must return an octet vector and a string source key or NIL."))
62 (funcall continuation bytes (or source-key resource)))))
63
64 (defun call-with-import-diagnostics (position thunk)
65 ;; Keep source errors at the resource source; model transfer/alias errors are
66 ;; attributed to the caller's directive with its active resource context.
67 (handler-case (funcall thunk)
68 (topic-maps.tmdm:item-identifier-error (c)
69 (problem 'ctm-rejection (topic-maps.tmdm:item-identifier-error-code c) position (princ-to-string c) :character c))
70 (topic-maps.tmdm:core-subject-error (c)
71 (problem 'ctm-rejection (topic-maps.tmdm:core-subject-error-code c) position (princ-to-string c) :character c))
72 (topic-maps.tmdm:variant-error (c)
73 (problem 'ctm-rejection (topic-maps.tmdm:variant-error-code c) position (princ-to-string c) :character c))
74 (topic-maps.tmdm:reification-error (c)
75 (problem 'ctm-rejection (topic-maps.tmdm:reification-error-code c) position (princ-to-string c) :character c))))
76
77 (defun read-mergemap-directive (parser map)
78 "CTM 3.16.3: immediately read a separate Map and import only its content."
79 (let* ((start (consume parser))
80 (resource (directive-iri parser :expected-mergemap-resource "Mergemap requires a resource IRI reference."))
81 (notation (directive-iri parser :expected-mergemap-notation "Mergemap requires a notation IRI reference."))
82 (position (token-position start)))
83 (call-with-resource parser resource :mergemap position notation
84 (lambda (bytes source-key)
85 (let ((source (funcall (mergemap-reader (mergemap-notation-kind notation position))
86 bytes resource source-key parser)))
87 (call-with-import-diagnostics position
88 (lambda () (topic-maps.tmdm:import-map-content map source))))))))
89
90 (defun add-include-aliases (map child-document parent-document position)
91 "CTM 3.16.2: one snapshot/pass per Include level, original IIs retained.
92 Use literal string-prefix matching, not an assumed fragment/path boundary.
93 Already resolved locator suffixes are not CTM-percent-decoded again."
94 (let ((aliases
95 (loop for topic in (topic-maps.tmdm:topic-map-topics map) append
96 (loop for identifier in (topic-maps.tmdm:topic-item-identifiers topic)
97 when (starts-with child-document identifier)
98 collect (list topic (resolve-iri (subseq identifier (length child-document))
99 parent-document position :unescape nil))))))
100 (dolist (alias aliases)
101 ;; A preceding addition may replace/merge this Topic. The public operation
102 ;; resolves its retained handle and uses the existing identity machinery.
103 (topic-maps.tmdm:add-topic-identifier (first alias) :item-identifier (second alias))))
104 map)
105
106 (defun read-include-directive (parser map)
107 "CTM 3.16.2, with caller-context execution of imported Template bodies."
108 (let* ((start (consume parser)) (position (token-position start))
109 (resource (directive-iri parser :expected-include-resource "Include requires a resource IRI reference.")))
110 (call-with-resource parser resource :include position nil
111 (lambda (bytes source-key)
112 (multiple-value-bind (source counter child)
113 (deserialize-context (decode-ctm-resource bytes source-key) :document-iri resource :source-name source-key
114 :wildcard-iri (parser-document-iri parser) :wildcard-counter (parser-wildcard-counter parser)
115 :resource-resolver (parser-resource-resolver parser)
116 :max-resource-depth (parser-max-resource-depth parser))
117 ;; Reject register conflicts before modifying the caller's Map,
118 ;; registry or Counter. Prefix bindings and named-Wildcard caches are
119 ;; per processor and are deliberately not transferred.
120 (maphash (lambda (key definition)
121 (when (gethash key (parser-templates parser))
122 (problem 'duplicate-template :duplicate-template position
123 (format nil "Included Template ~A/~D is already defined (source ~A, character ~D)."
124 (first key) (second key) (template-definition-source-name definition)
125 (template-definition-position definition)))))
126 (parser-templates child))
127 (call-with-import-diagnostics position
128 (lambda ()
129 (add-include-aliases source resource (parser-document-iri parser) position)
130 (topic-maps.tmdm:import-map-content map source)))
131 (maphash (lambda (key definition) (setf (gethash key (parser-templates parser)) definition))
132 (parser-templates child))
133 (setf (parser-wildcard-counter parser) counter)
134 map)))))
135
136 (defun read-directive (parser map)
137 (cond ((string= "prefix" (token-value (look parser))) (read-prefix-directive parser))
138 ((string= "mergemap" (token-value (look parser))) (read-mergemap-directive parser map))
139 ((string= "include" (token-value (look parser))) (read-include-directive parser map))
140 (t (problem 'ctm-unsupported :unsupported-directive (token-position (look parser)) "Directive is outside the supported subset."))))