]> dreyeck.freedombox.rocks Git - topic-maps.git/blob - src/xml/parser.lisp
Verify CTM/XTM semantic equivalence through TMDM and CXTM (Slice 22F)
[topic-maps.git] / src / xml / parser.lisp
1 (in-package #:topic-maps.xml)
2
3 (define-condition doctype-forbidden (error) ())
4 (define-condition external-resource-forbidden (error)
5 ((system-id :initarg :system-id :reader denied-system-id)))
6 (define-condition unqualified-parser (error) ())
7
8 (defclass closed-handler (cxml:sax-proxy) ())
9
10 (defmethod sax:start-dtd ((handler closed-handler) name public-id system-id)
11 (declare (ignore handler name public-id system-id))
12 (error 'doctype-forbidden))
13
14 (defun deny-external-resource (public-id system-id)
15 (declare (ignore public-id))
16 ;; Returning NIL enables CXML's OPEN fallback. Always signal instead.
17 (error 'external-resource-forbidden :system-id system-id))
18
19 (defun parse-octets (bytes handler)
20 "Parse XML bytes, returning HANDLER's SAX result. Required XTM boundary.
21 No parser options can disable namespace processing or external-resource denial.
22 Reject all DOCTYPEs. Do not accept a filename or an already decoded string."
23 (check-type bytes (vector (unsigned-byte 8)))
24 (unless (and (boundp 'cxml::*topic-maps-encoding-patch*)
25 (equal (symbol-value 'cxml::*topic-maps-encoding-patch*) "19d0b-1")
26 (boundp 'runes-encoding::*topic-maps-encoding-patch*)
27 (equal (symbol-value 'runes-encoding::*topic-maps-encoding-patch*) "19d0b-1"))
28 (error 'unqualified-parser))
29 (let ((sax:*namespace-processing* t)
30 (sax:*include-xmlns-attributes* nil)
31 (cxml:*catalog* nil)
32 (cxml:*cache-all-dtds* nil)
33 (cxml:*dtd-cache* (cxml:make-dtd-cache)))
34 (cxml:parse-octets bytes (make-instance 'closed-handler :chained-handler handler)
35 :validate nil :disallow-internal-subset t
36 :entity-resolver #'deny-external-resource)))