counter.lisp
 1 ;;;; -*- mode: lisp -*-
2 ;;;;
3 ;;;; $Id: counter.lisp,v 1.2 2004/06/11 11:14:43 scaekenberghe Exp $
4 ;;;;
5 ;;;; A simple SSAX counter example that can be used as a performance test
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 (defclass count-xml-seed ()
16 ((elements :initform 0)
17 (attributes :initform 0)
18 (characters :initform 0)))
19
20 (defun count-xml-new-element-hook (name attributes seed)
21 (declare (ignore name))
22 (incf (slot-value seed 'elements))
23 (incf (slot-value seed 'attributes) (length attributes))
24 seed)
25
26 (defun count-xml-text-hook (string seed)
27 (incf (slot-value seed 'characters) (length string))
28 seed)
29
30 (defun count-xml (in)
31 "Parse a toplevel XML element from stream in, counting elements, attributes and characters"
32 (start-parse-xml in
33 (make-instance 'xml-parser-state
34 :seed (make-instance 'count-xml-seed)
35 :new-element-hook #'count-xml-new-element-hook
36 :text-hook #'count-xml-text-hook)))
37
38 (defun count-xml-file (pathname)
39 "Parse XMl from the file at pathname, counting elements, attributes and characters"
40 (with-open-file (in pathname)
41 (let ((result (count-xml in)))
42 (with-slots (elements attributes characters) result
43 (format t
44 "~a contains ~d XML elements, ~d attributes and ~d characters.~%"
45 pathname elements attributes characters)))))
46
47 ;;;; eof