1 ;;; -*- show-trailing-whitespace: t; indent-tabs-mode: nil -*-
3 ;;; Copyright (c) 2007,2008 David Lichteblau, Ivan Shvedunov.
4 ;;; All rights reserved.
6 ;;; Redistribution and use in source and binary forms, with or without
7 ;;; modification, are permitted provided that the following conditions
10 ;;; * Redistributions of source code must retain the above copyright
11 ;;; notice, this list of conditions and the following disclaimer.
13 ;;; * Redistributions in binary form must reproduce the above
14 ;;; copyright notice, this list of conditions and the following
15 ;;; disclaimer in the documentation and/or other materials
16 ;;; provided with the distribution.
18 ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR 'AS IS' AND ANY EXPRESSED
19 ;;; OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
24 ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 (in-package :xuriella
)
33 ;;; Handler for the HTML output method.
35 ;;; Dispatches requests to either an HTML sink or an XML sink, depending
36 ;;; on the namespace of the event.
38 ;;; Inserts the http-equiv meta tag.
40 (defclass combi-sink
(sax:content-handler
)
41 ((hax-target :initarg
:hax-target
:accessor sink-hax-target
)
42 (sax-target :initarg
:sax-target
:accessor sink-sax-target
)
43 (encoding :initarg
:encoding
:accessor sink-encoding
)))
45 (defmethod initialize-instance :after
((handler combi-sink
) &key
)
46 (setf (sink-encoding handler
)
47 (or (sink-encoding handler
) "utf-8")))
49 (defmethod sax:start-document
((handler combi-sink
))
52 (defmethod sax:start-dtd
((handler combi-sink
) name pubid systemid
)
53 (hax:start-document
(sink-hax-target handler
) name pubid systemid
))
55 (defun maybe-close-tag (combi-sink)
56 (cxml::maybe-close-tag
(sink-sax-target combi-sink
)))
58 (defmethod sax:start-element
((handler combi-sink
) uri lname qname attrs
)
59 (with-slots (hax-target sax-target encoding
) handler
60 (maybe-close-tag handler
)
63 (sax:start-element hax-target
*html
* lname qname attrs
)
64 (when (and encoding
(equalp lname
"head"))
65 (let* ((content (format nil
"text/html; charset=~A" encoding
))
67 (list (hax:make-attribute
"http-equiv" "Content-Type")
68 (hax:make-attribute
"content" content
))))
69 (sax:start-element hax-target
*html
* "meta" "meta" attrs
)
70 (sax:end-element hax-target
*html
* "meta" "meta"))))
72 (sax:start-element sax-target uri lname qname attrs
)))))
74 (defmethod sax:end-element
((handler combi-sink
) uri lname qname
)
75 (with-slots (hax-target sax-target
) handler
76 (maybe-close-tag handler
)
78 (sax:end-element hax-target
*html
* lname qname
)
79 (sax:end-element sax-target uri lname qname
))))
81 (defmethod sax:end-document
((handler combi-sink
))
82 (hax:end-document
(sink-hax-target handler
)))
84 (defmethod sax:processing-instruction
((handler combi-sink
) target data
)
85 (maybe-close-tag handler
)
86 (sax:processing-instruction
(sink-hax-target handler
) target data
))
88 (defmethod sax:characters
((handler combi-sink
) data
)
89 (maybe-close-tag handler
)
90 (sax:characters
(sink-hax-target handler
) data
))
92 (defmethod sax:unescaped
((handler combi-sink
) data
)
93 (maybe-close-tag handler
)
94 (sax:unescaped
(sink-hax-target handler
) data
))
96 (defmethod sax:comment
((handler combi-sink
) data
)
97 (maybe-close-tag handler
)
98 (sax:comment
(sink-hax-target handler
) data
))
103 ;;; Handler for the default output method.
105 ;;; Waits for the document element, then decides between combi-sink and
108 (defclass auto-detect-sink
(cxml:broadcast-handler
)
109 ((switchedp :initform nil
:accessor sink-switched-p
)
110 (buffered-events :initform
'() :accessor sink-buffered-events
)))
112 (defun make-auto-detect-sink (combi-sink)
113 (make-instance 'auto-detect-sink
:handlers
(list combi-sink
)))
115 (defmethod sax:start-document
((handler auto-detect-sink
))
118 (defmethod sax:start-dtd
((handler auto-detect-sink
) name pubid systemid
)
121 (defmethod sax:start-element
123 ((handler auto-detect-sink
) uri lname qname attrs
)
124 (unless (sink-switched-p handler
)
125 (if (and (equal uri
"") (string-equal lname
"html"))
126 (switch-to-html-output handler
)
127 (switch-to-xml-output handler
))))
129 (defmethod sax:end-document
:before
((handler auto-detect-sink
))
130 (unless (sink-switched-p handler
)
131 (switch-to-xml-output handler
)))
133 (defmethod sax:characters
((handler auto-detect-sink
) data
)
135 ((sink-switched-p handler
)
137 ((not (whitespacep data
))
138 (switch-to-xml-output handler
)
141 (push (list 'sax
:characters data
) (sink-buffered-events handler
)))))
143 (defmethod sax:processing-instruction
144 ((handler auto-detect-sink
) target data
)
146 ((sink-switched-p handler
)
149 (push (list 'sax
:processing-instruction target data
)
150 (sink-buffered-events handler
)))))
152 (defmethod sax:unescaped
((handler auto-detect-sink
) data
)
154 ((sink-switched-p handler
)
157 (push (list 'sax
:unescaped data
) (sink-buffered-events handler
)))))
159 (defmethod sax:comment
((handler auto-detect-sink
) data
)
161 ((sink-switched-p handler
)
164 (push (list 'sax
:comment data
) (sink-buffered-events handler
)))))
166 (define-condition |hey test suite
, this is an HTML document|
()
169 (defun switch-to-html-output (handler)
170 (signal '|hey test suite
, this is an HTML document|
)
171 (setf (sink-switched-p handler
) t
)
172 (replay-buffered-events handler
))
174 (defun switch-to-xml-output (handler)
175 (setf (sink-switched-p handler
) t
)
176 (setf (cxml:broadcast-handler-handlers handler
)
177 (list (sink-sax-target
178 (car (cxml:broadcast-handler-handlers handler
)))))
179 (replay-buffered-events handler
))
181 (defun replay-buffered-events (handler)
182 (sax:start-document
(car (cxml:broadcast-handler-handlers handler
)))
184 for
(event . args
) in
(nreverse (sink-buffered-events handler
))
185 do
(apply event handler args
)))