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