|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (sxml simple) |
|
#:use-module (sxml ssax input-parse) |
|
#:use-module (sxml ssax) |
|
#:use-module (sxml transform) |
|
#:use-module (ice-9 match) |
|
#:export (xml->sxml sxml->xml sxml->string)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (ssax:reverse-collect-str fragments) |
|
(cond |
|
((null? fragments) '()) |
|
((null? (cdr fragments)) fragments) |
|
(else |
|
(let loop ((fragments fragments) (result '()) (strs '())) |
|
(cond |
|
((null? fragments) |
|
(if (null? strs) result |
|
(cons (string-concatenate/shared strs) result))) |
|
((string? (car fragments)) |
|
(loop (cdr fragments) result (cons (car fragments) strs))) |
|
(else |
|
(loop (cdr fragments) |
|
(cons |
|
(car fragments) |
|
(if (null? strs) result |
|
(cons (string-concatenate/shared strs) result))) |
|
'()))))))) |
|
|
|
(define (read-internal-doctype-as-string port) |
|
(string-concatenate/shared |
|
(let loop () |
|
(let ((fragment |
|
(next-token '() '(#\]) "reading internal DOCTYPE" port))) |
|
(if (eqv? #\> (peek-next-char port)) |
|
(begin |
|
(read-char port) |
|
(cons fragment '())) |
|
(cons* fragment "]" (loop))))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define* (xml->sxml #:optional (string-or-port (current-input-port)) #:key |
|
(namespaces '()) |
|
(declare-namespaces? #t) |
|
(trim-whitespace? #f) |
|
(entities '()) |
|
(default-entity-handler #f) |
|
(doctype-handler #f)) |
|
"Use SSAX to parse an XML document into SXML. Takes one optional |
|
argument, @var{string-or-port}, which defaults to the current input |
|
port." |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (munge-namespaces namespaces) |
|
(map (lambda (el) |
|
(match el |
|
((prefix . uri-string) |
|
(cons* (and declare-namespaces? prefix) |
|
prefix |
|
(ssax:uri-string->symbol uri-string))))) |
|
namespaces)) |
|
|
|
(define (user-namespaces) |
|
(munge-namespaces namespaces)) |
|
|
|
(define (user-entities) |
|
(if (and default-entity-handler |
|
(not (assq '*DEFAULT* entities))) |
|
(acons '*DEFAULT* default-entity-handler entities) |
|
entities)) |
|
|
|
(define (name->sxml name) |
|
(match name |
|
((prefix . local-part) |
|
(symbol-append prefix (string->symbol ":") local-part)) |
|
(_ name))) |
|
|
|
(define (doctype-continuation seed) |
|
(lambda* (#:key (entities '()) (namespaces '())) |
|
(values #f |
|
(append entities (user-entities)) |
|
(append (munge-namespaces namespaces) (user-namespaces)) |
|
seed))) |
|
|
|
|
|
|
|
|
|
(define parser |
|
(ssax:make-parser |
|
NEW-LEVEL-SEED |
|
(lambda (elem-gi attributes namespaces expected-content seed) |
|
'()) |
|
|
|
FINISH-ELEMENT |
|
(lambda (elem-gi attributes namespaces parent-seed seed) |
|
(let ((seed (if trim-whitespace? |
|
(ssax:reverse-collect-str-drop-ws seed) |
|
(ssax:reverse-collect-str seed))) |
|
(attrs (attlist-fold |
|
(lambda (attr accum) |
|
(cons (list (name->sxml (car attr)) (cdr attr)) |
|
accum)) |
|
'() attributes))) |
|
(acons (name->sxml elem-gi) |
|
(if (null? attrs) |
|
seed |
|
(cons (cons '@ attrs) seed)) |
|
parent-seed))) |
|
|
|
CHAR-DATA-HANDLER |
|
(lambda (string1 string2 seed) |
|
(if (string-null? string2) |
|
(cons string1 seed) |
|
(cons* string2 string1 seed))) |
|
|
|
DOCTYPE |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(lambda (port docname systemid internal-subset? seed) |
|
(call-with-values |
|
(lambda () |
|
(cond |
|
(doctype-handler |
|
(doctype-handler docname systemid |
|
(and internal-subset? |
|
(read-internal-doctype-as-string port)))) |
|
(else |
|
(when internal-subset? |
|
(ssax:skip-internal-dtd port)) |
|
(values)))) |
|
(doctype-continuation seed))) |
|
|
|
UNDECL-ROOT |
|
|
|
|
|
(lambda (elem-gi seed) |
|
(call-with-values |
|
(lambda () |
|
(if doctype-handler |
|
(doctype-handler #f #f #f) |
|
(values))) |
|
(doctype-continuation seed))) |
|
|
|
PI |
|
((*DEFAULT* |
|
. (lambda (port pi-tag seed) |
|
(cons |
|
(list '*PI* pi-tag (ssax:read-pi-body-as-string port)) |
|
seed)))))) |
|
|
|
(let* ((port (if (string? string-or-port) |
|
(open-input-string string-or-port) |
|
string-or-port)) |
|
(elements (reverse (parser port '())))) |
|
`(*TOP* ,@elements))) |
|
|
|
(define check-name |
|
(let ((*good-cache* (make-hash-table))) |
|
(lambda (name) |
|
(if (not (hashq-ref *good-cache* name)) |
|
(let* ((str (symbol->string name)) |
|
(i (string-index str #\:)) |
|
(head (or (and i (substring str 0 i)) str)) |
|
(tail (and i (substring str (1+ i))))) |
|
(and i (string-index (substring str (1+ i)) #\:) |
|
(error "Invalid QName: more than one colon" name)) |
|
(for-each |
|
(lambda (s) |
|
(and s |
|
(or (char-alphabetic? (string-ref s 0)) |
|
(eq? (string-ref s 0) #\_) |
|
(error "Invalid name starting character" s name)) |
|
(string-for-each |
|
(lambda (c) |
|
(or (char-alphabetic? c) (string-index "0123456789.-_" c) |
|
(error "Invalid name character" c s name))) |
|
s))) |
|
(list head tail)) |
|
(hashq-set! *good-cache* name #t)))))) |
|
|
|
|
|
|
|
|
|
|
|
(define (attribute-value->xml value port) |
|
(cond |
|
((pair? value) |
|
(attribute-value->xml (car value) port) |
|
(attribute-value->xml (cdr value) port)) |
|
((null? value) |
|
*unspecified*) |
|
((string? value) |
|
(string->escaped-xml value port)) |
|
((procedure? value) |
|
(with-output-to-port port value)) |
|
(else |
|
(string->escaped-xml |
|
(call-with-output-string (lambda (port) (display value port))) |
|
port)))) |
|
|
|
(define (attribute->xml attr value port) |
|
(check-name attr) |
|
(display attr port) |
|
(display "=\"" port) |
|
(attribute-value->xml value port) |
|
(display #\" port)) |
|
|
|
(define (element->xml tag attrs body port) |
|
(check-name tag) |
|
(display #\< port) |
|
(display tag port) |
|
(if attrs |
|
(let lp ((attrs attrs)) |
|
(if (pair? attrs) |
|
(let ((attr (car attrs))) |
|
(display #\space port) |
|
(if (pair? attr) |
|
(attribute->xml (car attr) (cdr attr) port) |
|
(error "bad attribute" tag attr)) |
|
(lp (cdr attrs))) |
|
(if (not (null? attrs)) |
|
(error "bad attributes" tag attrs))))) |
|
(if (pair? body) |
|
(begin |
|
(display #\> port) |
|
(let lp ((body body)) |
|
(cond |
|
((pair? body) |
|
(sxml->xml (car body) port) |
|
(lp (cdr body))) |
|
((null? body) |
|
(display "</" port) |
|
(display tag port) |
|
(display ">" port)) |
|
(else |
|
(error "bad element body" tag body))))) |
|
(display " />" port))) |
|
|
|
|
|
(define (entity->xml name port) |
|
(display #\& port) |
|
(display name port) |
|
(display #\; port)) |
|
|
|
|
|
(define (pi->xml tag str port) |
|
(display "<?" port) |
|
(display tag port) |
|
(display #\space port) |
|
(display str port) |
|
(display "?>" port)) |
|
|
|
(define* (sxml->xml tree #:optional (port (current-output-port))) |
|
"Serialize the sxml tree @var{tree} as XML. The output will be written |
|
to the current output port, unless the optional argument @var{port} is |
|
present." |
|
(cond |
|
((pair? tree) |
|
(if (symbol? (car tree)) |
|
|
|
(let ((tag (car tree))) |
|
(case tag |
|
((*TOP*) |
|
(sxml->xml (cdr tree) port)) |
|
((*ENTITY*) |
|
(if (and (list? (cdr tree)) (= (length (cdr tree)) 1)) |
|
(entity->xml (cadr tree) port) |
|
(error "bad *ENTITY* args" (cdr tree)))) |
|
((*PI*) |
|
(if (and (list? (cdr tree)) (= (length (cdr tree)) 2)) |
|
(pi->xml (cadr tree) (caddr tree) port) |
|
(error "bad *PI* args" (cdr tree)))) |
|
(else |
|
(let* ((elems (cdr tree)) |
|
(attrs (and (pair? elems) (pair? (car elems)) |
|
(eq? '@ (caar elems)) |
|
(cdar elems)))) |
|
(element->xml tag attrs (if attrs (cdr elems) elems) port))))) |
|
|
|
(for-each (lambda (x) (sxml->xml x port)) tree))) |
|
((string? tree) |
|
(string->escaped-xml tree port)) |
|
((null? tree) *unspecified*) |
|
((not tree) *unspecified*) |
|
((eqv? tree #t) *unspecified*) |
|
((procedure? tree) |
|
(with-output-to-port port tree)) |
|
(else |
|
(string->escaped-xml |
|
(call-with-output-string (lambda (port) (display tree port))) |
|
port)))) |
|
|
|
(define (sxml->string sxml) |
|
"Detag an sxml tree @var{sxml} into a string. Does not perform any |
|
formatting." |
|
(string-concatenate-reverse |
|
(foldts |
|
(lambda (seed tree) |
|
'()) |
|
(lambda (seed kid-seed tree) |
|
(append! kid-seed seed)) |
|
(lambda (seed tree) |
|
(if (string? tree) (cons tree seed) seed)) |
|
'() |
|
sxml))) |
|
|
|
(define (make-char-quotator char-encoding) |
|
(let ((bad-chars (list->char-set (map car char-encoding)))) |
|
|
|
|
|
|
|
|
|
(define (index-cset str i charset) |
|
(string-index str charset i)) |
|
|
|
|
|
(lambda (str port) |
|
(let ((bad-pos (index-cset str 0 bad-chars))) |
|
(if (not bad-pos) |
|
(display str port) |
|
(let loop ((from 0) (to bad-pos)) |
|
(cond |
|
((>= from (string-length str)) *unspecified*) |
|
((not to) |
|
(display (substring str from (string-length str)) port)) |
|
(else |
|
(let ((quoted-char |
|
(cdr (assv (string-ref str to) char-encoding))) |
|
(new-to |
|
(index-cset str (+ 1 to) bad-chars))) |
|
(if (< from to) |
|
(display (substring str from to) port)) |
|
(display quoted-char port) |
|
(loop (1+ to) new-to)))))))))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
(define string->escaped-xml |
|
(make-char-quotator |
|
'((#\< . "<") (#\> . ">") (#\& . "&") (#\" . """)))) |
|
|
|
|
|
|
|
|
|
|