Commit d81401ac authored by Sven Van Caekenberghe's avatar Sven Van Caekenberghe
Browse files

further cleanup of examples and hook documentation

parent d192706b
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: xml.lisp,v 1.1.1.1 2004/06/07 18:49:58 scaekenberghe Exp $
;;;; $Id: xml.lisp,v 1.2 2004/06/11 08:22:47 scaekenberghe Exp $
;;;;
;;;; This is a Common Lisp implementation of a very basic XML parser.
;;;; The parser is non-validating and not at all complete (no CDATA).
......@@ -151,9 +151,10 @@
:accessor get-mini-buffer
:initform (make-extendable-string))
(new-element-hook :documentation "Called when new element starts"
;; Handle a new xml element with name and attributes,
;; receiving seed from parent if any or top level
;; return seed to be used for children/contents"
;; Handle the start of a new xml element with name and attributes,
;; receiving seed from previous element (sibling or parent)
;; return seed to be used for first child (content)
;; or directly to finish-element-hook
:accessor get-new-element-hook
:initarg :new-element-hook
:initform #'(lambda (name attributes seed)
......@@ -161,17 +162,20 @@
seed))
(finish-element-hook :documentation "Called when element ends"
;; Handle the end of an xml element with name and attributes,
;; receiving the seed that was passed by our parent,
;; receiving seed from last child/contents
;; return final seed for this element
;; receiving parent-seed, the seed passed to us when this element started,
;; i.e. passed to our corresponding new-element-hook
;; and receiving seed from last child (content)
;; or directly from new-element-hook
;; return final seed for this element to next element (sibling or parent)
:accessor get-finish-element-hook
:initarg :finish-element-hook
:initform #'(lambda (name attributes parent-seed seed)
(declare (ignore name attributes parent-seed))
seed))
(text-hook :documentation "Called when text is found"
;; Handle text in string, found as child/contents,
;; receiving seed from parent element, return final seed for this element
;; Handle text in string, found as contents,
;; receiving seed from previous element (sibling or parent),
;; return final seed for this element to next element (sibling or parent)
:accessor get-text-hook
:initarg :text-hook
:initform #'(lambda (string seed)
......
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: counter.lisp,v 1.1 2004/05/27 13:51:49 sven Exp $
;;;; $Id: counter.lisp,v 1.1.1.1 2004/06/07 18:49:59 scaekenberghe Exp $
;;;;
;;;; A simple SAX counter example that can be used as a performance test
;;;; A simple SSAX counter example that can be used as a performance test
;;;;
;;;; Copyright (C) 2004 Sven Van Caekenberghe, Beta Nine BVBA.
;;;;
......@@ -41,7 +41,7 @@
(let ((result (count-xml in)))
(with-slots (elements attributes characters) result
(format t
"~a countains ~d XML elements, ~d attributes and ~d characters.~%"
"~a contains ~d XML elements, ~d attributes and ~d characters.~%"
pathname elements attributes characters)))))
;;;; eof
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: lxml-dom.lisp,v 1.1.1.1 2004/06/07 18:49:56 scaekenberghe Exp $
;;;;
;;;; Remove markup from an XML document using the SSAX interface
;;;;
;;;; Copyright (C) 2004 Sven Van Caekenberghe, Beta Nine BVBA.
;;;;
;;;; You are granted the rights to distribute and use this software
;;;; as governed by the terms of the Lisp Lesser General Public License
;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
(in-package :s-xml)
(defun remove-xml-markup (in)
(let* ((state (make-instance 'xml-parser-state
:text-hook #'(lambda (string seed) (cons string seed))))
(result (start-parse-xml in state)))
(apply #'concatenate 'string (nreverse result))))
;;;; eof
\ No newline at end of file
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: counter.lisp,v 1.1.1.1 2004/06/07 18:49:59 scaekenberghe Exp $
;;;; $Id: tracer.lisp,v 1.1 2004/06/11 08:20:58 scaekenberghe Exp $
;;;;
;;;; A simple SAX tracer example that can be used to understand how the hooks are called
;;;; A simple SSAX tracer example that can be used to understand how the hooks are called
;;;;
;;;; Copyright (C) 2004 Sven Van Caekenberghe, Beta Nine BVBA.
;;;;
......@@ -18,28 +18,33 @@
(terpri *standard-output*))
(defun trace-xml-new-element-hook (name attributes seed)
(trace-xml-log (car seed)
"(new-element :name ~s :attributes ~:[()~;~:*s~] :seed ~s)"
name attributes seed)
(cons (1+ (car seed)) (1+ (cdr seed))))
(let ((new-seed (cons (1+ (car seed)) (1+ (cdr seed)))))
(trace-xml-log (car seed)
"(new-element :name ~s :attributes ~:[()~;~:*~s~] :seed ~s) => ~s"
name attributes seed new-seed)
new-seed))
(defun trace-xml-finish-element-hook (name attributes parent-seed seed)
(trace-xml-log (car parent-seed)
"(finish-element :name ~s :attributes ~:[()~;~:*s~] :parent-seed ~s :seed ~s)"
name attributes parent-seed seed)
(cons (1- (car seed)) (1+ (cdr seed))))
(let ((new-seed (cons (1- (car seed)) (1+ (cdr seed)))))
(trace-xml-log (car parent-seed)
"(finish-element :name ~s :attributes ~:[()~;~:*~s~] :parent-seed ~s :seed ~s) => ~s"
name attributes parent-seed seed new-seed)
new-seed))
(defun trace-xml-text-hook (string seed)
(trace-xml-log (car seed)
"(text :string ~s :seed ~s)"
string seed)
seed)
(let ((new-seed (cons (car seed) (1+ (cdr seed)))))
(trace-xml-log (car seed)
"(text :string ~s :seed ~s) => ~s"
string seed new-seed)
new-seed))
(defun trace-xml (in)
"Parse and trace a toplevel XML element from stream in"
(start-parse-xml in
(make-instance 'xml-parser-state
:seed (cons 0 0)
:seed (cons 0 0)
;; seed car is xml element nesting level
;; seed cdr is ever increasing from element to element
:new-element-hook #'trace-xml-new-element-hook
:finish-element-hook #'trace-xml-finish-element-hook
:text-hook #'trace-xml-text-hook)))
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment