From: Chris Hanson Date: Mon, 16 Jul 2001 20:41:29 +0000 (+0000) Subject: Add code to write XML structures. X-Git-Tag: 20090517-FFI~2646 X-Git-Url: https://birchwood-abbey.net/git?a=commitdiff_plain;h=b2254eed2f1e685cbb8a1fafff1d3bc1bbe63a41;p=mit-scheme.git Add code to write XML structures. --- diff --git a/v7/src/xml/xml-output.scm b/v7/src/xml/xml-output.scm new file mode 100644 index 000000000..8316edf7a --- /dev/null +++ b/v7/src/xml/xml-output.scm @@ -0,0 +1,321 @@ +;;; -*-Scheme-*- +;;; +;;; $Id: xml-output.scm,v 1.1 2001/07/16 20:40:30 cph Exp $ +;;; +;;; Copyright (c) 2001 Massachusetts Institute of Technology +;;; +;;; This program is free software; you can redistribute it and/or +;;; modify it under the terms of the GNU General Public License as +;;; published by the Free Software Foundation; either version 2 of the +;;; License, or (at your option) any later version. +;;; +;;; This program is distributed in the hope that it will be useful, +;;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +;;; General Public License for more details. +;;; +;;; You should have received a copy of the GNU General Public License +;;; along with this program; if not, write to the Free Software +;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +;;; 02111-1307, USA. + +;;;; XML output + +(declare (usual-integrations)) + +(define-generic write-xml (object port)) + +(define-method write-xml ((document xml-document-rtd) port) + (if (xml-document-declaration document) + (write-xml (xml-document-declaration document) port)) + (for-each (lambda (object) (write-xml object port)) + (xml-document-misc-1 document)) + (if (xml-document-dtd document) + (write-xml (xml-document-dtd document) port)) + (for-each (lambda (object) (write-xml object port)) + (xml-document-misc-2 document)) + (write-xml (xml-document-root document) port) + (for-each (lambda (object) (write-xml object port)) + (xml-document-misc-3 document))) + +(define-method write-xml ((declaration xml-declaration-rtd) port) + (write-string "" port)) + +(define-method write-xml ((element xml-element-rtd) port) + (write-string "<" port) + (write-xml-name (xml-element-name element) port) + (write-xml-attributes (xml-element-attributes element) port) + (let ((contents (xml-element-contents element))) + (if (pair? contents) + (begin + (write-string ">" port) + (for-each (lambda (content) (write-xml content port)) + contents) + (write-string "" port)) + (write-string "/>" port)))) + +(define-method write-xml ((pi xml-processing-instructions-rtd) port) + (write-string "" port)) + +(define-method write-xml ((element xml-uninterpreted-rtd) port) + ;; **** There's a quoting problem here -- char data that gets + ;; bundled into this must be quoted prior to combination with other + ;; elements. + (write-string (xml-uninterpreted-text element) port)) + +(define-method write-xml ((dtd xml-dtd-rtd) port) + ;;root external internal + (write-string "" port)) + +(define-method write-xml ((decl xml-!element-rtd) port) + (write-string "" port)) + +(define-method write-xml ((decl xml-!attlist-rtd) port) + (write-string "" port)) + +(define-method write-xml ((decl xml-!entity-rtd) port) + (write-string "" port)) + +(define-method write-xml ((decl xml-unparsed-!entity-rtd) port) + (write-string "" port)) + +(define-method write-xml ((decl xml-parameter-!entity-rtd) port) + (write-string "" port)) + +(define-method write-xml ((decl xml-!notation-rtd) port) + (write-string "" port)) + +(define-method write-xml ((string ) port) + (let ((end (string-length string))) + (do ((i 0 (fix:+ i 1))) + ((fix:= i end)) + (let ((char (string-ref string i))) + (cond ((char=? char #\<) + (write-string "<" port)) + ((char=? char #\&) + (write-string "&" port)) + (else + (write-char char port))))))) + +(define (write-xml-name name port) + (write-string (symbol-name name) port)) + +(define (write-xml-attributes attributes port) + (for-each (lambda (attribute) + (write-string " " port) + (write-xml-attribute attribute port)) + attributes)) + +(define (write-xml-attribute attribute port) + (write-xml-name (car attribute) port) + (write-string "=" port) + (write-xml-string (cdr attribute) port)) + +(define (write-xml-string string port) + (let ((quote-char (if (string-find-next-char string #\") #\' #\")) + (end (string-length string))) + (write-char quote-char port) + (do ((i 0 (fix:+ i 1))) + ((fix:= i end)) + (let ((char (string-ref string i))) + (cond ((char=? char quote-char) + (write-string (if (char=? char #\") """ "'") port)) + ((char=? char #\<) + (write-string "<" port)) + ((char=? char #\&) + (write-string "&" port)) + (else + (write-char char port))))) + (write-char quote-char port))) + +(define (write-entity-value string port) + (let ((quote-char (if (string-find-next-char string #\") #\' #\")) + (end (string-length string))) + (write-char quote-char port) + (do ((i 0 (fix:+ i 1))) + ((fix:= i end)) + (let ((char (string-ref string i))) + (cond ((char=? char quote-char) + (write-string (if (char=? char #\") """ "'") port)) + ((char=? char #\%) + (write-string "%" port)) + ((char=? char #\&) + (write-string "&" port)) + (else + (write-char char port))))) + (write-char quote-char port))) + +(define (write-xml-external-id id port) + (if (xml-external-id-id id) + (begin + (write-string "PUBLIC " port) + (write-xml-string (xml-external-id-id id) port)) + (write-string "SYSTEM" port)) + (if (xml-external-id-uri id) + (begin + (write-string " " port) + (write-xml-string (xml-external-id-uri id) port)))) \ No newline at end of file diff --git a/v7/src/xml/xml.pkg b/v7/src/xml/xml.pkg index c25674e21..2818aa50b 100644 --- a/v7/src/xml/xml.pkg +++ b/v7/src/xml/xml.pkg @@ -1,6 +1,6 @@ ;;; -*-Scheme-*- ;;; -;;; $Id: xml.pkg,v 1.6 2001/07/16 20:40:28 cph Exp $ +;;; $Id: xml.pkg,v 1.7 2001/07/16 20:41:29 cph Exp $ ;;; ;;; Copyright (c) 2001 Massachusetts Institute of Technology ;;; @@ -22,6 +22,7 @@ ;;;; XML: packaging (global-definitions "$brun/runtime") +(global-definitions "$bscm/sos/sos") (global-definitions "../parser/parser") (define-package (runtime xml)