[cxml-cvs] CVS cxml/xml
dlichteblau
dlichteblau at common-lisp.net
Sun Oct 14 17:50:13 UTC 2007
Update of /project/cxml/cvsroot/cxml/xml
In directory clnet:/tmp/cvs-serv8803/xml
Modified Files:
package.lisp sax-handler.lisp sax-proxy.lisp unparse.lisp
Log Message:
SAX overhaul for HAX integration.
* dom/dom-builder.lisp (DOM-BUILDER): Inherit from
sax:content-handler.
* klacks/klacks-impl.lisp (KLACKS-DTD-HANDLER): Inherit from
sax:default-handler.
* klacks/klacks.lisp (KLACKS:SERIALIZE-EVENT): Bugfix -- call
start-cdata and end-cdata on the handler, not the source.
* xml/package.lisp: New export WITH-OUTPUT-SINK.
* xml/sax-handler.lisp (SAX): New exports abstract-handler,
content-handler, default-handler. (STANDARD-ATTRIBUTE): Renamed
from attribute. (ATTRIBUTE-NAMESPACE-URI, ATTRIBUTE-LOCAL-NAME,
ATTRIBUTE-QNAME, ATTRIBUTE-VALUE, ATTRIBUTE-SPECIFIED-P): Wrapper
methods for standard-attribute. Wrapper methods for
hax:standard-attribute. (all events): pulled into a common
define-event form. New dummy method on null. Added a warning to
the default method. New error method on abstract-handler. New
dummy method on the respective default handler classes.
* xml/sax-proxy.lisp (BROADCAST-HANDLER): Inherit from
abstract-handler, not sax-parser-mixin.
* xml/unparse.lisp (sink): Inherit from sax:content-handler.
(WITH-OUTPUT-SINK): New macro. (INVOKE-WITH-OUTPUT-SINK): New
function.
--- /project/cxml/cvsroot/cxml/xml/package.lisp 2007/07/07 20:47:39 1.19
+++ /project/cxml/cvsroot/cxml/xml/package.lisp 2007/10/14 17:50:11 1.20
@@ -51,6 +51,7 @@
#:make-character-stream-sink/utf8
#:with-xml-output
+ #:with-output-sink
#:with-namespace
#:with-element
#:with-element*
--- /project/cxml/cvsroot/cxml/xml/sax-handler.lisp 2007/07/01 17:25:45 1.8
+++ /project/cxml/cvsroot/cxml/xml/sax-handler.lisp 2007/10/14 17:50:11 1.9
@@ -3,11 +3,12 @@
;;; Title: A SAX2-like API for the xml parser
;;; Created: 2003-06-30
;;; Author: Henrik Motakef <hmot at henrik-motakef.de>
-;;; Author: David Lichteblau (DTD-related changes)
+;;; Author: David Lichteblau
;;; License: BSD
;;; ---------------------------------------------------------------------------
;;; (c) copyright 2003 by Henrik Motakef
;;; (c) copyright 2004 knowledgeTools Int. GmbH
+;;; (c) copyright 2005-2007 David Lichteblau
;;; Redistribution and use in source and binary forms, with or without
;;; modification, are permitted provided that the following conditions are
@@ -34,15 +35,11 @@
;;; TODO/ Open Questions:
-;; o Should there be a predefined "handler" class, or even several
-;; (like Java SAX' ContentHandler, DTDHandler, LexicalHandler etc? I
-;; don't really see why.
;; o Missing stuff from Java SAX2:
;; * ignorable-whitespace
;; * skipped-entity
;; * The whole ErrorHandler class, this is better handled using
;; conditions (but isn't yet)
-;; * The LexicalHandler (start-cdata etc) would be nice [-- partly done]
(defpackage :sax
(:use :common-lisp)
@@ -50,6 +47,10 @@
#:*include-xmlns-attributes*
#:*use-xmlns-namespace*
+ #:abstract-handler
+ #:content-handler
+ #:default-handler
+
#:make-attribute
#:find-attribute
#:find-attribute-ns
@@ -98,7 +99,7 @@
(defclass sax-parser () ())
-(defclass sax-parser-mixin ()
+(defclass sax-parser-mixin () ;deprecated
((sax-parser :initform nil :reader sax-parser)))
(defgeneric line-number (sax-parser)
@@ -185,13 +186,75 @@
Setting this variable has no effect unless both
`*namespace-processing*' and `*include-xmlns-attributes*' are non-nil.")
-(defstruct attribute
+
+;;;; ATTRIBUTE
+
+(defstruct (standard-attribute (:constructor make-attribute))
namespace-uri
local-name
qname
value
specified-p)
+(defmethod (setf attribute-namespace-uri)
+ (newval (attribute standard-attribute))
+ (setf (standard-attribute-namespace-uri attribute) newval))
+
+(defmethod (setf attribute-local-name)
+ (newval (attribute standard-attribute))
+ (setf (standard-attribute-local-name attribute) newval))
+
+(defmethod (setf attribute-qname)
+ (newval (attribute standard-attribute))
+ (setf (standard-attribute-qname attribute) newval))
+
+(defmethod (setf attribute-value)
+ (newval (attribute standard-attribute))
+ (setf (standard-attribute-value attribute) newval))
+
+(defmethod (setf attribute-specified-p)
+ (newval (attribute standard-attribute))
+ (setf (standard-attribute-specified-p attribute) newval))
+
+(defgeneric attribute-namespace-uri (attribute)
+ (:method ((attribute standard-attribute))
+ (standard-attribute-namespace-uri attribute))
+ (:method ((attribute hax:standard-attribute))
+ ""))
+
+(defgeneric attribute-local-name (attribute)
+ (:method ((attribute standard-attribute))
+ (standard-attribute-local-name attribute))
+ (:method ((attribute hax:standard-attribute))
+ (runes:rod-downcase (hax:attribute-name attribute))))
+
+(defgeneric attribute-qname (attribute)
+ (:method ((attribute standard-attribute))
+ (standard-attribute-qname attribute))
+ (:method ((attribute hax:standard-attribute))
+ (runes:rod-downcase (hax:attribute-name attribute))))
+
+(defgeneric attribute-value (attribute)
+ (:method ((attribute standard-attribute))
+ (standard-attribute-value attribute))
+ (:method ((attribute hax:standard-attribute))
+ (hax:attribute-value attribute)))
+
+(defgeneric attribute-specified-p (attribute)
+ (:method ((attribute standard-attribute))
+ (standard-attribute-specified-p attribute))
+ (:method ((attribute hax:standard-attribute))
+ (hax:attribute-specified-p attribute)))
+
+(defmethod hax:attribute-name ((attribute standard-attribute))
+ (attribute-local-name attribute))
+
+(defmethod hax:attribute-value ((attribute standard-attribute))
+ (attribute-value attribute))
+
+(defmethod hax:attribute-specified-p ((attribute standard-attribute))
+ (attribute-specified-p attribute))
+
(defun %rod= (x y)
;; allow rods *and* strings *and* null
(cond
@@ -209,16 +272,197 @@
(%rod= lname (sax:attribute-local-name attr))))
attrs))
-(defgeneric start-document (handler)
- (:documentation "Called at the beginning of the parsing process,
+
+;;;; ABSTRACT-HANDLER and DEFAULT-HANDLER
+
+(defclass abstract-handler (sax-parser-mixin) ())
+(defclass content-handler (abstract-handler) ())
+(defclass default-handler (content-handler) ())
+
+
+;;;; EVENTS
+
+(macrolet ((define-event ((name default-handler-class)
+ (&rest args)
+ &body hax-body)
+ `(defgeneric ,name (handler , at args)
+ (:method ((handler null) , at args)
+ (declare (ignore , at args))
+ nil)
+ (:method ((handler t) , at args)
+ (declare (ignore , at args))
+ (error "deprecated SAX default method used by a handler ~
+ that is not a subclass of SAX:ABSTRACT-HANDLER ~
+ or HAX:ABSTRACT-HANDLER")
+ nil)
+ (:method ((handler abstract-handler) , at args)
+ (declare (ignore , at args))
+ (error "SAX event ~A not implemented by this handler"
+ ',name))
+ (:method ((handler ,default-handler-class) , at args)
+ (declare (ignore , at args))
+ nil)
+ (:method ((handler hax:abstract-handler) , at args)
+ (declare (ignorable , at args))
+ , at hax-body))))
+ (define-event (start-document default-handler)
+ ()
+ nil)
+
+ (define-event (start-element default-handler)
+ (namespace-uri local-name qname attributes)
+ (hax:start-element handler local-name attributes))
+
+ (define-event (start-prefix-mapping content-handler)
+ (prefix uri)
+ nil)
+
+ (define-event (characters default-handler)
+ (data)
+ (hax:characters handler data))
+
+ (define-event (processing-instruction default-handler)
+ (target data)
+ nil)
+
+ (define-event (end-prefix-mapping content-handler)
+ (prefix)
+ nil)
+
+ (define-event (end-element default-handler)
+ (namespace-uri local-name qname)
+ (hax:end-element handler local-name))
+
+ (define-event (end-document default-handler)
+ ()
+ (hax:end-document handler))
+
+ (define-event (comment content-handler)
+ (data)
+ (hax:comment handler data))
+
+ (define-event (start-cdata content-handler)
+ ()
+ nil)
+
+ (define-event (end-cdata content-handler)
+ ()
+ nil)
+
+ (define-event (start-dtd content-handler)
+ (name public-id system-id)
+ (hax:start-document handler name public-id system-id))
+
+ (define-event (end-dtd content-handler)
+ ()
+ nil)
+
+ (define-event (start-internal-subset content-handler)
+ ()
+ nil)
+
+ (define-event (end-internal-subset content-handler)
+ ()
+ nil)
+
+ (define-event (unparsed-internal-subset content-handler)
+ (str)
+ nil)
+
+ (define-event (unparsed-entity-declaration content-handler)
+ (name public-id system-id notation-name)
+ nil)
+
+ (define-event (external-entity-declaration content-handler)
+ (kind name public-id system-id)
+ nil)
+
+ (define-event (internal-entity-declaration content-handler)
+ (kind name value)
+ nil)
+
+ (define-event (notation-declaration content-handler)
+ (name public-id system-id)
+ nil)
+
+ (define-event (element-declaration content-handler)
+ (name model)
+ nil)
+
+ (define-event (attribute-declaration content-handler)
+ (element-name attribute-name type default)
+ nil)
+
+ (define-event (entity-resolver content-handler)
+ (resolver)
+ nil)
+
+ (define-event (dtd content-handler)
+ (dtd)
+ nil))
+
+;;; special case: this method is defined on abstract-handler through
+;;; sax-parser-mixin
+(defgeneric register-sax-parser (handler sax-parser)
+ (:method ((handler null) sax-parser)
+ (declare (ignore sax-parser))
+ nil)
+ (:method ((handler sax-parser-mixin) sax-parser)
+ (setf (slot-value handler 'sax-parser) sax-parser))
+ (:method ((handler t) sax-parser)
+ (declare (ignore sax-parser))
+ (error "deprecated sax default method used by a handler ~
+ that is not a subclass of sax:abstract-handler ~
+ or hax:abstract-handler")
+ nil)
+ (:method ((handler hax:abstract-handler) sax-parser)
+ (declare (ignorable sax-parser)) nil))
+
+
+;;;; HAX to SAX
+
+(defmethod hax:start-document ((handler abstract-handler) name pubid sysid)
+ (sax:start-document handler)
+ (sax:start-dtd handler name pubid sysid)
+ (sax:end-dtd handler name pubid sysid))
+
+(defmethod hax:start-element ((handler abstract-handler) name attributes)
+ (setf name (runes:rod-downcase name))
+ (sax:start-element handler
+ "http://www.w3.org/1999/xhtml"
+ name
+ name
+ attributes))
+
+(defmethod hax:end-element ((handler abstract-handler) name)
+ (setf name (runes:rod-downcase name))
+ (sax:end-element handler
+ "http://www.w3.org/1999/xhtml"
+ name
+ name))
+
+(defmethod hax:characters ((handler abstract-handler) data)
+ (sax:characters handler data))
+
+(defmethod hax:comment ((handler abstract-handler) str)
+ (sax:comment handler str))
+
+(defmethod hax:end-document ((handler abstract-handler))
+ (sax:end-document handler))
+
+
+
+;;;; Documentation
+
+(setf (documentation 'start-document 'function)
+ "Called at the beginning of the parsing process,
before any element, processing instruction or comment is reported.
Handlers that need to maintain internal state may use this to perform
any neccessary initializations.")
- (:method ((handler t)) nil))
-(defgeneric start-element (handler namespace-uri local-name qname attributes)
- (:documentation "Called to report the beginning of an element.
+(setf (documentation 'start-element 'function)
+ "Called to report the beginning of an element.
There will always be a corresponding call to end-element, even in the
case of an empty element (i.e. <foo/>).
@@ -235,12 +479,9 @@
apply. Additionally, namespace-declaring attributes (those whose name
is \"xmlns\" or starts with \"xmlns:\") are only included if
*include-xmlns-attributes* is non-nil.")
- (:method ((handler t) namespace-uri local-name qname attributes)
- (declare (ignore namespace-uri local-name qname attributes))
- nil))
-(defgeneric start-prefix-mapping (handler prefix uri)
- (:documentation "Called when the scope of a new prefix -> namespace-uri mapping begins.
+(setf (documentation 'start-prefix-mapping 'function)
+ "Called when the scope of a new prefix -> namespace-uri mapping begins.
This will always be called immediatly before the `start-element' event
for the element on which the namespaces are declared.
@@ -249,24 +490,21 @@
circumstances, for example when they have to deal with qualified names
in textual content. The parser will handle namespaces of elements and
attributes on its own.")
- (:method ((handler t) prefix uri) (declare (ignore prefix uri)) nil))
-(defgeneric characters (handler data)
- (:documentation "Called for textual element content.
+(setf (documentation 'characters 'function)
+ "Called for textual element content.
The data is passed as a rod, with all entity references resolved.
It is possible that the character content of an element is reported
via multiple subsequent calls to this generic function.")
- (:method ((handler t) data) (declare (ignore data)) nil))
-(defgeneric processing-instruction (handler target data)
- (:documentation "Called when a processing instruction is read.
+(setf (documentation 'processing-instruction 'function)
+ "Called when a processing instruction is read.
Both target and data are rods.")
- (:method ((handler t) target data) (declare (ignore target data)) nil))
-(defgeneric end-prefix-mapping (handler prefix)
- (:documentation "Called when a prefix -> namespace-uri mapping goes out of scope.
+(setf (documentation 'end-prefix-mapping 'function)
+ "Called when a prefix -> namespace-uri mapping goes out of scope.
This will always be called immediatly after the `end-element' event
for the element on which the namespace is declared. The order of the
@@ -276,147 +514,83 @@
circumstances, for example when they have to deal with qualified names
in textual content. The parser will handle namespaces of elements and
attributes on its own.")
- (:method ((handler t) prefix) prefix nil))
-(defgeneric end-element (handler namespace-uri local-name qname)
- (:documentation "Called to report the end of an element.
+(setf (documentation 'end-element 'function)
+ "Called to report the end of an element.
See the documentation for `start-element' for a description of the
parameters.")
- (:method ((handler t) namespace-uri local-name qname)
- (declare (ignore namespace-uri local-name qname))
- nil))
-(defgeneric end-document (handler)
- (:documentation "Called at the end of parsing a document.
+(setf (documentation 'end-document 'function)
+ "Called at the end of parsing a document.
This is always the last function called in the parsing process.
[160 lines skipped]
--- /project/cxml/cvsroot/cxml/xml/sax-proxy.lisp 2007/05/20 09:38:35 1.6
+++ /project/cxml/cvsroot/cxml/xml/sax-proxy.lisp 2007/10/14 17:50:11 1.7
@@ -8,7 +8,7 @@
(in-package :cxml)
-(defclass broadcast-handler (sax:sax-parser-mixin)
+(defclass broadcast-handler (sax:abstract-handler)
((handlers :initform nil
:initarg :handlers
:accessor broadcast-handler-handlers)))
--- /project/cxml/cvsroot/cxml/xml/unparse.lisp 2007/08/05 11:16:15 1.20
+++ /project/cxml/cvsroot/cxml/xml/unparse.lisp 2007/10/14 17:50:12 1.21
@@ -69,7 +69,7 @@
;;;; SINK: an xml output sink
-(defclass sink ()
+(defclass sink (sax:content-handler)
((ystream :initarg :ystream :accessor sink-ystream)
(width :initform 79 :initarg :width :accessor width)
(canonical :initform nil :initarg :canonical :accessor canonical)
@@ -561,6 +561,9 @@
(defmacro with-xml-output (sink &body body)
`(invoke-with-xml-output (lambda () , at body) ,sink))
+(defmacro with-output-sink ((var) &body body)
+ `(invoke-with-output-sink (lambda (,var) , at body)))
+
(defun invoke-with-xml-output (fn sink)
(let ((*sink* sink)
(*current-element* nil)
@@ -570,6 +573,10 @@
(funcall fn)
(sax:end-document *sink*)))
+(defun invoke-with-output-sink (fn)
+ (maybe-emit-start-tag)
+ (funcall fn *sink*))
+
(defmacro with-element (qname &body body)
`(invoke-with-element (lambda () , at body) ,qname))
More information about the Cxml-cvs
mailing list