]> dreyeck.freedombox.rocks Git - topic-maps.git/blobdiff - src/cxtm/canonicalizer.lisp
examples: explore project work through native Topic Maps (slice 22U)
[topic-maps.git] / src / cxtm / canonicalizer.lisp
index 52b6d14ae8163e83e76ed6c24ad4430900fff3d0..52c19bc6f0ca91cae7074d0def305faf30f6eaf2 100644 (file)
           (require-exact-item r 'association-role)
           (unless (and (eq r (canonical-association-role r)) (eq (association-role-parent r) a))
             (fail :invalid-parent "Role must be current and owned by its Association."))
           (require-exact-item r 'association-role)
           (unless (and (eq r (canonical-association-role r)) (eq (association-role-parent r) a))
             (fail :invalid-parent "Role must be current and owned by its Association."))
-          (reference (association-role-type r)) (reference (association-role-player r)))))))
+          (reference (association-role-type r)) (reference (association-role-player r))))))
+  (handler-case (topic-maps.tmdm::validate-map-core-subjects map)
+    (topic-maps.tmdm:core-subject-error (c)
+      (fail (topic-maps.tmdm:core-subject-error-code c) (princ-to-string c))))
+  (handler-case (topic-maps.tmdm::validate-map-item-identifiers map)
+    (topic-maps.tmdm:item-identifier-error (c)
+      (fail (topic-maps.tmdm:item-identifier-error-code c) (princ-to-string c))))
+  (handler-case (topic-maps.tmdm::validate-map-reification map)
+    (topic-maps.tmdm:reification-error (c)
+      (fail (topic-maps.tmdm:reification-error-code c) (princ-to-string c)))))
 
 (defun xml-text (text out)
   (loop for c across (nfc text) for n = (char-code c) do
 
 (defun xml-text (text out)
   (loop for c across (nfc text) for n = (char-code c) do
 
 (defun canonicalize (map &key base-locator)
   "Return CXTM as a string. BASE-LOCATOR is an explicit absolute IRI (CXTM 3.1).
 
 (defun canonicalize (map &key base-locator)
   "Return CXTM as a string. BASE-LOCATOR is an explicit absolute IRI (CXTM 3.1).
-Only the existing TMDM schema is supported: Topic-set Scope, null Reifier, no
-Variants or non-Topic identifiers other than Occurrence identifiers. Does not mutate MAP or handles."
+Supports Scope, Variants, Item Identifiers and same-map Reification.
+Does not mutate MAP or retained handles."
   (unless (and (stringp base-locator) (position #\: base-locator)
                (let ((scheme (subseq base-locator 0 (position #\: base-locator))))
                  (and (plusp (length scheme))
   (unless (and (stringp base-locator) (position #\: base-locator)
                (let ((scheme (subseq base-locator 0 (position #\: base-locator))))
                  (and (plusp (length scheme))
@@ -252,32 +261,42 @@ Variants or non-Topic identifiers other than Occurrence identifiers. Does not mu
       (loop for r in (sorted (association-roles a) #'role-order) for index from 1 do
         (setf (gethash r role-numbers) index)))
     (with-output-to-string (out)
       (loop for r in (sorted (association-roles a) #'role-order) for index from 1 do
         (setf (gethash r role-numbers) index)))
     (with-output-to-string (out)
-      (labels ((type-ref (topic)
+      (labels ((start (tag item number)
+                 ;; CXTM 3.11, canonical XML attribute order: number, reifier.
+                 (format out "<~A" tag)
+                 (when number (format out " number=\"~D\"" number))
+                 (when (item-reifier item)
+                   (format out " reifier=\"~D\"" (gethash (item-reifier item) topic-numbers)))
+                 (format out ">~%"))
+               (type-ref (topic)
                  (format out "<type topicref=\"~D\"></type>~%" (gethash topic topic-numbers)))
                (role-ref (r)
                  (format out "<rolePlayed ref=\"association.~D.role.~D\"></rolePlayed>~%"
                          (gethash (association-role-parent r) association-numbers)
                          (gethash r role-numbers))))
                  (format out "<type topicref=\"~D\"></type>~%" (gethash topic topic-numbers)))
                (role-ref (r)
                  (format out "<rolePlayed ref=\"association.~D.role.~D\"></rolePlayed>~%"
                          (gethash (association-role-parent r) association-numbers)
                          (gethash r role-numbers))))
-        (format out "<topicMap>~%")
+        (start "topicMap" map nil)
+        (emit-locators "itemIdentifiers" (item-identifiers map) out)
         (loop for topic in topics for number from 1 do
           (format out "<topic number=\"~D\">~%" number)
           (emit-locators "subjectIdentifiers" (topic-subject-identifiers topic) out)
           (emit-locators "subjectLocators" (topic-subject-locators topic) out)
           (emit-locators "itemIdentifiers" (topic-item-identifiers topic) out)
           (loop for name in (sorted (topic-names topic) #'name-order) for index from 1 do
         (loop for topic in topics for number from 1 do
           (format out "<topic number=\"~D\">~%" number)
           (emit-locators "subjectIdentifiers" (topic-subject-identifiers topic) out)
           (emit-locators "subjectLocators" (topic-subject-locators topic) out)
           (emit-locators "itemIdentifiers" (topic-item-identifiers topic) out)
           (loop for name in (sorted (topic-names topic) #'name-order) for index from 1 do
-            (format out "<name number=\"~D\">~%<value>" index)
+            (start "name" name index) (write-string "<value>" out)
             (xml-text (topic-name-value name) out) (format out "</value>~%")
             (type-ref (topic-name-type name))
             (emit-scope (topic-name-scope name) topic-numbers out)
             (loop for item in (sorted (topic-name-variants name) #'variant-order) for number from 1 do
             (xml-text (topic-name-value name) out) (format out "</value>~%")
             (type-ref (topic-name-type name))
             (emit-scope (topic-name-scope name) topic-numbers out)
             (loop for item in (sorted (topic-name-variants name) #'variant-order) for number from 1 do
-              (format out "<variant number=\"~D\">~%<value>" number)
+              (start "variant" item number) (write-string "<value>" out)
               (xml-text (canonical-literal-value (variant-value item) (variant-datatype item)) out)
               (format out "</value>~%<datatype>") (xml-text (variant-datatype item) out)
               (format out "</datatype>~%") (emit-scope (variant-scope item) topic-numbers out)
               (xml-text (canonical-literal-value (variant-value item) (variant-datatype item)) out)
               (format out "</value>~%<datatype>") (xml-text (variant-datatype item) out)
               (format out "</datatype>~%") (emit-scope (variant-scope item) topic-numbers out)
+              (emit-locators "itemIdentifiers" (item-identifiers item) out)
               (format out "</variant>~%"))
               (format out "</variant>~%"))
+            (emit-locators "itemIdentifiers" (item-identifiers name) out)
             (format out "</name>~%"))
           (loop for item in (sorted (topic-occurrences topic) #'occurrence-order) for index from 1 do
             (format out "</name>~%"))
           (loop for item in (sorted (topic-occurrences topic) #'occurrence-order) for index from 1 do
-            (format out "<occurrence number=\"~D\">~%<value>" index)
+            (start "occurrence" item index) (write-string "<value>" out)
             (xml-text (canonical-literal-value (occurrence-value item) (occurrence-datatype item)) out)
             (format out "</value>~%<datatype>")
             ;; CXTM 3.14: datatype locator is NOT base-normalized.
             (xml-text (canonical-literal-value (occurrence-value item) (occurrence-datatype item)) out)
             (format out "</value>~%<datatype>")
             ;; CXTM 3.14: datatype locator is NOT base-normalized.
@@ -289,13 +308,16 @@ Variants or non-Topic identifiers other than Occurrence identifiers. Does not mu
           (dolist (r (sorted (topic-roles-played topic) #'role-order)) (role-ref r))
           (format out "</topic>~%"))
         (loop for a in associations for number from 1 do
           (dolist (r (sorted (topic-roles-played topic) #'role-order)) (role-ref r))
           (format out "</topic>~%"))
         (loop for a in associations for number from 1 do
-          (format out "<association number=\"~D\">~%" number)
+          (start "association" a number)
           (type-ref (association-type a))
           (loop for r in (sorted (association-roles a) #'role-order) for index from 1 do
           (type-ref (association-type a))
           (loop for r in (sorted (association-roles a) #'role-order) for index from 1 do
-            (format out "<role number=\"~D\">~%<player topicref=\"~D\"></player>~%"
-                    index (gethash (association-role-player r) topic-numbers))
-            (type-ref (association-role-type r)) (format out "</role>~%"))
+            (start "role" r index)
+            (format out "<player topicref=\"~D\"></player>~%" (gethash (association-role-player r) topic-numbers))
+            (type-ref (association-role-type r))
+            (emit-locators "itemIdentifiers" (item-identifiers r) out)
+            (format out "</role>~%"))
           (emit-scope (association-scope a) topic-numbers out)
           (emit-scope (association-scope a) topic-numbers out)
+          (emit-locators "itemIdentifiers" (item-identifiers a) out)
           (format out "</association>~%"))
         ;; CXTM 3.1 step 2 appends LF after EVERY element, including the root.
         (format out "</topicMap>~%")))))
           (format out "</association>~%"))
         ;; CXTM 3.1 step 2 appends LF after EVERY element, including the root.
         (format out "</topicMap>~%")))))