(in-package #:topic-maps.xtm) (defparameter +namespace+ "http://www.topicmaps.org/xtm/") ;; This is a source tree of XML events, not a second Topic Maps object model. ;; Positions are SAX approximations at the end of a start tag, never byte spans. (defstruct node uri name attributes children line column) (defclass source-handler (sax:default-handler) ((stack :initform nil :accessor handler-stack) (root :initform nil :accessor handler-root))) (defmethod sax:start-element ((h source-handler) uri local qname attributes) (declare (ignore qname)) (let ((node (make-node :uri uri :name local :line (sax:line-number h) :column (sax:column-number h) :attributes (mapcar (lambda (a) (list (sax:attribute-namespace-uri a) (sax:attribute-local-name a) (sax:attribute-value a))) attributes)))) (if (handler-stack h) (push node (node-children (first (handler-stack h)))) (setf (handler-root h) node)) (push node (handler-stack h)))) (defmethod sax:characters ((h source-handler) data) (when (handler-stack h) (push (copy-seq data) (node-children (first (handler-stack h)))))) (defmethod sax:end-element ((h source-handler) uri local qname) (declare (ignore uri local qname)) (let ((node (pop (handler-stack h)))) (setf (node-children node) (nreverse (node-children node))))) (defmethod sax:end-document ((h source-handler)) (handler-root h)) (defun xml-space-p (c) (find c '(#\Space #\Tab #\Newline #\Return))) (defun collapse-space (text) ;; xsd:ID has whiteSpace=collapse, even with a non-validating XML processor. (with-output-to-string (out) (let ((written nil) (pending nil)) (loop for c across text do (if (xml-space-p c) (when written (setf pending t)) (progn (when pending (write-char #\Space out)) (write-char c out) (setf written t pending nil))))))) (defun ncname-p (text) ;; Use the pinned XML processor's XML 1.0 name character tables; exclude ':' ;; for XML Schema ID/NCName and XPointer shorthand pointers (XTM 4.5/4.20). (and (plusp (length text)) (not (find #\: text)) (cxml::valid-name-p (runes:string-rod text)))) (defun attributes (node allowed required) (dolist (attr (node-attributes node)) (unless (and (or (null (first attr)) (equal (first attr) "")) (member (second attr) allowed :test #'string=)) (reject :unexpected-attribute (format nil "Attribute ~S on ~A." (second attr) (node-name node)) node))) (dolist (name required) (unless (find name (node-attributes node) :key #'second :test #'string=) (reject (if (string= name "id") :missing-id :missing-attribute) (format nil "Required ~A attribute is absent." name) node)))) (defun attr (node name) (third (find name (node-attributes node) :key #'second :test #'string=))) (defun elements (node) (loop for child in (node-children node) if (stringp child) do (unless (every #'xml-space-p child) (reject :unexpected-text "Only XML whitespace is allowed here." node)) else collect child)) (defun check-namespace (node) (unless (equal (node-uri node) +namespace+) (reject :invalid-namespace "Expected the XTM 2.0 namespace." node))) (defun unsupported (node) (reject :unsupported-construct (format nil "XTM ~A is not implemented in this subset." (node-name node)) node 'xtm-unsupported)) (defun check-reifier (node) (when (attr node "reifier") (reject :reification "XTM Reification is outside this subset." node 'xtm-unsupported))) (defun leaf-href (node) (attributes node '("href") '("href")) (when (elements node) (reject :unexpected-child "A locator element must be empty." node)) (resolve-iri (attr node "href") *document-iri* node)) (defun reference-iri (node) (let* ((iri (leaf-href node)) (fragment (iri-parts-fragment (split-iri iri)))) (unless (and fragment (ncname-p fragment)) (reject :invalid-topic-ref "topicRef requires an XPointer shorthand fragment." node)) iri)) (defun validate-source (root) "Check the supported XTM schema before model construction; keep event order. Unsupported legal extension points are explicit; no claim to validate excluded constructs' complete subgrammars is made. XML errors have already been checked." (let ((ids (make-hash-table :test #'equal))) ; XML ID uniqueness only, no Topics. (labels ((validate (node context) (check-namespace node) (let ((name (node-name node))) (unless (member name context :test #'string=) (reject :unexpected-element (format nil "Unexpected ~A element." name) node)) (cond ((string= name "topicMap") (attributes node '("version" "reifier") '("version")) (unless (equal (attr node "version") "2.0") (reject :invalid-version "This reader accepts XTM version 2.0 only." node)) (check-reifier node) (let ((phase 0)) (dolist (child (elements node)) (let* ((name (node-name child)) (next (cond ((string= name "itemIdentity") 0) ((string= name "mergeMap") 1) (t 2)))) (when (< next phase) (reject :element-order "topicMap child order is invalid." child)) (setf phase next) (validate child '("itemIdentity" "mergeMap" "topic" "association")))))) ((string= name "topic") (attributes node '("id") '("id")) (let ((id (collapse-space (attr node "id")))) (unless (ncname-p id) (reject :invalid-id "topic/@id must be an XML Schema ID." node)) (when (gethash id ids) (reject :duplicate-id "Duplicate XML ID in this document." node)) (setf (gethash id ids) t)) (let ((phase 0) (instance-seen nil)) (dolist (child (elements node)) (let* ((name (node-name child)) (next (cond ((member name '("itemIdentity" "subjectLocator" "subjectIdentifier") :test #'string=) 0) ((string= name "instanceOf") 1) (t 2)))) (when (< next phase) (reject :element-order "topic child order is invalid." child)) (when (= next 1) (when instance-seen (reject :duplicate-instance-of "Only one instanceOf element is allowed." child)) (setf instance-seen t)) (setf phase next) (validate child '("itemIdentity" "subjectLocator" "subjectIdentifier" "instanceOf" "name" "occurrence")))))) ((string= name "instanceOf") (attributes node nil nil) (unless (elements node) (reject :empty-instance-of "instanceOf needs at least one topicRef." node)) (dolist (child (elements node)) (validate child '("topicRef")))) ((string= name "name") (attributes node '("reifier") nil) (check-reifier node) (let ((phase 0) (type-seen nil) (scope-seen nil) (value-seen nil)) (dolist (child (elements node)) (check-namespace child) (let* ((kind (node-name child)) (next (position kind '("itemIdentity" "type" "scope" "value" "variant") :test #'string=))) (unless next (reject :unexpected-element (format nil "Unexpected ~A element." kind) child)) (when (or (< next phase) (and (string= kind "variant") (not value-seen))) (reject :element-order "name child order is invalid." child)) (setf phase next) (cond ((member kind '("itemIdentity" "variant") :test #'string=) nil) ((string= kind "type") (when type-seen (reject :duplicate-name-type "Only one name type is allowed." child)) (setf type-seen t)) ((string= kind "scope") (when scope-seen (reject :duplicate-name-scope "Only one name scope is allowed." child)) (setf scope-seen t)) ((string= kind "value") (when value-seen (reject :duplicate-name-value "Only one name value is allowed." child)) (setf value-seen t)) ;; Valid extension points remain explicitly unsupported. ;; Their complete subgrammars are outside this subset. (t (unsupported child))) (validate child '("itemIdentity" "type" "scope" "value" "variant")))) (unless value-seen (reject :missing-name-value "name requires a value element." node)))) ((string= name "variant") (attributes node '("reifier") nil) (check-reifier node) (let ((phase 0) (scope-seen nil) (resource-seen nil)) (dolist (child (elements node)) (check-namespace child) (let* ((kind (node-name child)) (next (cond ((string= kind "itemIdentity") 0) ((string= kind "scope") 1) ((member kind '("resourceData" "resourceRef") :test #'string=) 2)))) (unless next (reject :unexpected-element (format nil "Unexpected ~A element." kind) child)) (when (< next phase) (reject :element-order "variant child order is invalid." child)) (setf phase next) (cond ((string= kind "itemIdentity") nil) ((string= kind "scope") (when scope-seen (reject :duplicate-variant-scope "Only one variant scope is allowed." child)) (setf scope-seen t)) (t (unless scope-seen (reject :missing-variant-scope "variant requires scope before its resource." child)) (when resource-seen (reject :variant-resource-cardinality "variant requires exactly one resource." child)) (setf resource-seen t))) (validate child '("itemIdentity" "scope" "resourceData" "resourceRef")))) (unless scope-seen (reject :missing-variant-scope "variant requires a scope element." node)) (unless resource-seen (reject :missing-variant-resource "variant requires a resource element." node)))) ((string= name "resourceRef") (leaf-href node)) ((string= name "resourceData") (attributes node '("datatype") nil) (let ((datatype (attr node "datatype"))) ;; DATATYPE is already an absolute IRI, not content to ;; resolve against the document or percent-normalize. (when datatype (unless (iri-parts-scheme (split-iri datatype)) (reject :invalid-resource-data-datatype "resourceData datatype must be an absolute IRI." node)) (validate-decoded-reference (unescape-reference datatype node) node) (unless (member datatype (list (topic-maps.tmdm:string-datatype) (topic-maps.tmdm:iri-datatype)) :test #'equal) (reject :resource-data-datatype "Only textual xsd:string and xsd:anyURI resourceData are implemented." node 'xtm-unsupported))) (dolist (child (node-children node)) (unless (stringp child) (if (or (equal datatype (topic-maps.tmdm:iri-datatype)) (equal (node-uri child) +namespace+)) (reject :unexpected-child (if (equal datatype (topic-maps.tmdm:iri-datatype)) "anyURI resourceData cannot contain child elements." "resourceData cannot contain XTM markup.") child) (reject :resource-data-markup "Embedded XML resourceData is outside this subset." child 'xtm-unsupported)))))) ((string= name "type") (attributes node nil nil) (let ((refs (elements node))) (unless (= 1 (length refs)) (reject :type-cardinality "type requires exactly one topicRef." node)) (validate (first refs) '("topicRef")))) ((string= name "scope") (attributes node nil nil) (unless (elements node) (reject :empty-scope "scope requires at least one topicRef." node)) (dolist (child (elements node)) (validate child '("topicRef")))) ((string= name "value") (attributes node nil nil) ;; VALUE accepts all character data, including empty text and ;; whitespace. ELEMENTS would apply the wrong text policy. (dolist (child (node-children node)) (unless (stringp child) (reject :unexpected-child "value accepts character data only." child)))) ((string= name "topicRef") (reference-iri node)) ((member name '("itemIdentity" "subjectIdentifier" "subjectLocator") :test #'string=) (leaf-href node)) (t (unsupported node)))))) (validate root '("topicMap")))) root) (defun find-reference-topic (map iri) ;; XTM 4.20 searches the actual model, not a speculative pre-scan registry. ;; Unlike INTERN-TOPIC, a hit MUST NOT add another identifier category. (let ((topic (find-if (lambda (topic) (or (member iri (topic-maps.tmdm:topic-subject-identifiers topic) :test #'string=) (member iri (topic-maps.tmdm:topic-item-identifiers topic) :test #'string=))) (topic-maps.tmdm:topic-map-topics map)))) (if topic (topic-maps.tmdm:canonical-topic topic) (topic-maps.tmdm:intern-topic map :item-identifier iri)))) (defun core-topic (map local) (topic-maps.tmdm:intern-topic map :subject-identifier (concatenate 'string "http://psi.topicmaps.org/iso13250/model/" local))) (defun read-instance-of (node owner map) (dolist (ref (elements node)) (let ((type (find-reference-topic map (reference-iri ref)))) (topic-maps.tmdm:make-association map (core-topic map "type-instance") (list (list (core-topic map "type") type) (list (core-topic map "instance") (topic-maps.tmdm:canonical-topic owner))))))) (defun read-variant (node name map) (let* ((children (elements node)) (scope-node (find "scope" children :key #'node-name :test #'string=)) (resource (find-if (lambda (child) (member (node-name child) '("resourceData" "resourceRef") :test #'string=)) children)) (reference-p (string= (node-name resource) "resourceRef")) (uri-data-p (and (not reference-p) (equal (attr resource "datatype") (topic-maps.tmdm:iri-datatype)))) (explicit (mapcar (lambda (ref) (find-reference-topic map (reference-iri ref))) (elements scope-node))) ;; A resource locator is a value, not a topic reference. LEAF-HREF uses ;; the shared XTM IRI resolver without topic lookup or resource access. (text (unless reference-p (with-output-to-string (out) (dolist (part (node-children resource)) (write-string part out))))) (value (cond (reference-p (leaf-href resource)) (uri-data-p (resolve-iri text *document-iri* resource)) (t text)))) ;; ADD-VARIANT canonicalizes the union and enforces the proper superset. ;; A nonempty explicit scope may still add no theme to the Name scope. (let ((variant (topic-maps.tmdm:add-variant name value :datatype (if (or reference-p uri-data-p) (topic-maps.tmdm:iri-datatype) (topic-maps.tmdm:string-datatype)) :scope (append (topic-maps.tmdm:topic-name-scope name) explicit)))) ;; Normalize first: an equal Variant may already own this identifier. ;; Keep the native canonical result and its global conflict conditions. (dolist (child children variant) (when (string= (node-name child) "itemIdentity") (setf variant (topic-maps.tmdm:add-item-identifier variant (leaf-href child)))))))) (defun read-name (node owner map) (let* ((children (elements node)) (type-node (find "type" children :key #'node-name :test #'string=)) (scope-node (find "scope" children :key #'node-name :test #'string=)) (type (when type-node (find-reference-topic map (reference-iri (first (elements type-node)))))) (scope (when scope-node (mapcar (lambda (ref) (find-reference-topic map (reference-iri ref))) (elements scope-node)))) (value-node (find "value" children :key #'node-name :test #'string=)) (value (with-output-to-string (out) (dolist (part (node-children value-node)) (write-string part out))))) ;; SAX has already resolved XML character/entity references. Preserve the ;; resulting Unicode sequence without trimming or decoding it again. ;; ADD-NAME owns canonical set semantics and normalization after merging. (let ((name (if type-node (topic-maps.tmdm:add-name owner value :type type :scope scope) (topic-maps.tmdm:add-default-name owner value :scope scope)))) ;; Item identifiers belong to the canonical Name, never its owner/type. ;; Use the shared model API for set semantics and global conflicts. (dolist (child children name) (cond ((string= (node-name child) "itemIdentity") (setf name (topic-maps.tmdm:add-item-identifier name (leaf-href child)))) ((string= (node-name child) "variant") (read-variant child name map))))))) (defun read-topic (node map) (let ((topic (topic-maps.tmdm:intern-topic map :item-identifier (concatenate 'string *document-iri* "#" (collapse-space (attr node "id")))))) (dolist (child (elements node)) (let ((name (node-name child))) (cond ((string= name "instanceOf") (read-instance-of child topic map)) ((string= name "name") (read-name child topic map)) (t (setf topic (topic-maps.tmdm:add-topic-identifier topic (cond ((string= name "itemIdentity") :item-identifier) ((string= name "subjectIdentifier") :subject-identifier) (t :subject-locator)) (leaf-href child))))))) (topic-maps.tmdm:canonical-topic topic))) (defun deserialize-octets (bytes &key document-iri source-key) "Deserialize raw XML octets into a new TMDM Topic Map (XTM 2.0 identity/Name subset). DOCUMENT-IRI is required, absolute and fragment-free. SOURCE-KEY is a diagnostic label only. XML positions are approximate SAX line/column values, not spans. XML-policy errors, XTM rejection/unsupported and original TMDM conflicts remain distinct. No resources are fetched. No externally supplied Map is mutated." (check-type bytes (vector (unsigned-byte 8))) (let ((*document-iri* document-iri) (*source-key* source-key) (handler (make-instance 'source-handler))) (require-document-iri document-iri) (let* ((root (handler-case (topic-maps.xml:parse-octets bytes handler) (cxml:well-formedness-violation (c) (error 'xtm-xml-error :code :xml-not-well-formed :cause c :detail (princ-to-string c) :document-iri document-iri :source-key source-key :line (sax:line-number handler) :column (sax:column-number handler))) (topic-maps.xml:doctype-forbidden (c) (error 'xtm-xml-error :code :doctype-forbidden :cause c :detail "DOCTYPE forbidden by the mandatory XML byte boundary." :document-iri document-iri :source-key source-key :line (sax:line-number handler) :column (sax:column-number handler))))) (map (topic-maps.tmdm:make-topic-map))) (validate-source root) (dolist (child (elements root)) (if (string= (node-name child) "itemIdentity") (topic-maps.tmdm:add-item-identifier map (leaf-href child)) (read-topic child map))) map)))