summaryrefslogtreecommitdiff
path: root/third-party/s-xml/examples/tracer.lisp
diff options
context:
space:
mode:
authorMatthias Benkard <code@mail.matthias.benkard.de>2009-10-30 20:52:07 +0100
committerMatthias Benkard <code@mail.matthias.benkard.de>2009-10-30 20:52:07 +0100
commitddb83b1fb2d305e0c06fc067d82d6bab5458b0fd (patch)
tree8f9003926f0b5295c7a04b2ca257c0a2155ce841 /third-party/s-xml/examples/tracer.lisp
parent15937a1a4f1cf40bc55aa34eb71c67b88466ff57 (diff)
Add third-party XML processing libraries.
Ignore-this: 5ca28497555bf944858ca2f58bc8a62b darcs-hash:a0b0f9baa7c9b1259e755435db1fb17123630a6c
Diffstat (limited to 'third-party/s-xml/examples/tracer.lisp')
-rw-r--r--third-party/s-xml/examples/tracer.lisp57
1 files changed, 57 insertions, 0 deletions
diff --git a/third-party/s-xml/examples/tracer.lisp b/third-party/s-xml/examples/tracer.lisp
new file mode 100644
index 0000000..44782f5
--- /dev/null
+++ b/third-party/s-xml/examples/tracer.lisp
@@ -0,0 +1,57 @@
+;;;; -*- mode: lisp -*-
+;;;;
+;;;; $Id: tracer.lisp,v 1.1 2008-02-15 13:54:57 scaekenberghe Exp $
+;;;;
+;;;; 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.
+;;;;
+;;;; 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 trace-xml-log (level msg &rest args)
+ (indent *standard-output* level)
+ (apply #'format *standard-output* msg args)
+ (terpri *standard-output*))
+
+(defun trace-xml-new-element-hook (name attributes 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)
+ (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)
+ (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 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)))
+
+(defun trace-xml-file (pathname)
+ "Parse and trace XMl from the file at pathname"
+ (with-open-file (in pathname)
+ (trace-xml in)))
+
+;;;; eof