dom.lisp
 1 ;;;; -*- mode: lisp -*-
2 ;;;;
3 ;;;; $Id: dom.lisp,v 1.2 2005/08/29 15:01:47 scaekenberghe Exp $
4 ;;;;
5 ;;;; This is the generic simple DOM parser and printer interface.
6 ;;;;
7 ;;;; Copyright (C) 2002, 2004 Sven Van Caekenberghe, Beta Nine BVBA.
8 ;;;;
9 ;;;; You are granted the rights to distribute and use this software
10 ;;;; as governed by the terms of the Lisp Lesser General Public License
11 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
12
13 (in-package :s-xml)
14
15 ;;; top level DOM parser interface
16
17 (defgeneric parse-xml-dom (stream output-type)
18 (:documentation "Parse a character stream as XML and generate a DOM of output-type"))
19
20 (defun parse-xml (stream &key (output-type :lxml))
21 "Parse a character stream as XML and generate a DOM of output-type, defaulting to :lxml"
22 (parse-xml-dom stream output-type))
23
24 (defun parse-xml-string (string &key (output-type :lxml))
25 "Parse a string as XML and generate a DOM of output-type, defaulting to :lxml"
26 (with-input-from-string (stream string)
27 (parse-xml-dom stream output-type)))
28
29 (defun parse-xml-file (filename &key (output-type :lxml))
30 "Parse a character file as XML and generate a DOM of output-type, defaulting to :lxml"
31 (with-open-file (in filename :direction :input)
32 (parse-xml-dom in output-type)))
33
34 ;;; top level DOM printer interface
35
36 (defgeneric print-xml-dom (dom input-type stream pretty level)
37 (:documentation "Generate XML output on a character stream from a DOM of input-type, optionally pretty printing using level"))
38
39 (defun print-xml (dom &key (stream t) (pretty nil) (input-type :lxml) (header))
40 "Generate XML output on a character stream (t by default) from a DOM of input-type (:lxml by default), optionally pretty printing (off by default), or adding a header (none by default)"
41 (when header (format stream header))
42 (when pretty (terpri stream))
43 (print-xml-dom dom input-type stream pretty 1))
44
45 (defun print-xml-string (dom &key (pretty nil) (input-type :lxml))
46 "Generate XML output to a string from a DOM of input-type (:lxml by default), optionally pretty printing (off by default)"
47 (with-output-to-string (stream)
48 (print-xml dom :stream stream :pretty pretty :input-type input-type)))
49
50 ;;; shared/common support functions
51
52 (defun print-spaces (n stream &optional (preceding-newline t))
53 (when preceding-newline
54 (terpri stream))
55 (loop :repeat n
56 :do (write-char #\Space stream)))
57
58 (defun print-solitary-tag (tag stream)
59 (write-char #\< stream)
60 (print-identifier tag stream)
61 (write-string "/>" stream))
62
63 (defun print-closing-tag (tag stream)
64 (write-string "</" stream)
65 (print-identifier tag stream)
66 (write-char #\> stream))
67
68 (defun print-attribute (name value stream)
69 (write-char #\space stream)
70 (print-identifier name stream t)
71 (write-string "=\"" stream)
72 (print-string-xml value stream)
73 (write-char #\" stream))
74
75 ;;;; eof