tracer.lisp
 1 ;;;; -*- mode: lisp -*-
2 ;;;;
3 ;;;; $Id: tracer.lisp,v 1.2 2004/06/11 11:14:43 scaekenberghe Exp $
4 ;;;;
5 ;;;; A simple SSAX tracer example that can be used to understand how the hooks are called
6 ;;;;
7 ;;;; Copyright (C) 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 (defun trace-xml-log (level msg &rest args)
16 (indent *standard-output* level)
17 (apply #'format *standard-output* msg args)
18 (terpri *standard-output*))
19
20 (defun trace-xml-new-element-hook (name attributes seed)
21 (let ((new-seed (cons (1+ (car seed)) (1+ (cdr seed)))))
22 (trace-xml-log (car seed)
23 "(new-element :name ~s :attributes ~:[()~;~:*~s~] :seed ~s) => ~s"
24 name attributes seed new-seed)
25 new-seed))
26
27 (defun trace-xml-finish-element-hook (name attributes parent-seed seed)
28 (let ((new-seed (cons (1- (car seed)) (1+ (cdr seed)))))
29 (trace-xml-log (car parent-seed)
30 "(finish-element :name ~s :attributes ~:[()~;~:*~s~] :parent-seed ~s :seed ~s) => ~s"
31 name attributes parent-seed seed new-seed)
32 new-seed))
33
34 (defun trace-xml-text-hook (string seed)
35 (let ((new-seed (cons (car seed) (1+ (cdr seed)))))
36 (trace-xml-log (car seed)
37 "(text :string ~s :seed ~s) => ~s"
38 string seed new-seed)
39 new-seed))
40
41 (defun trace-xml (in)
42 "Parse and trace a toplevel XML element from stream in"
43 (start-parse-xml in
44 (make-instance 'xml-parser-state
45 :seed (cons 0 0)
46 ;; seed car is xml element nesting level
47 ;; seed cdr is ever increasing from element to element
48 :new-element-hook #'trace-xml-new-element-hook
49 :finish-element-hook #'trace-xml-finish-element-hook
50 :text-hook #'trace-xml-text-hook)))
51
52 (defun trace-xml-file (pathname)
53 "Parse and trace XMl from the file at pathname"
54 (with-open-file (in pathname)
55 (trace-xml in)))
56
57 ;;;; eof