/examples/names-reader.lisp
Lisp | 118 lines | 70 code | 14 blank | 34 comment | 2 complexity | 34b47bc9b50682e0365d3a48e5acf2d7 MD5 | raw file
- ;;;; names-reader.lisp --- example for libxml-clisp tutorial
- ;;; Copyright (C) 2009 N. Raghavendra. All rights reserved.
- ;;;
- ;;; Redistribution and use in source and binary forms, with or without
- ;;; modification, are permitted provided that the following conditions
- ;;; are met:
- ;;; 1. Redistributions of source code must retain the above copyright
- ;;; notice, this list of conditions and the following disclaimer.
- ;;; 2. Redistributions in binary form must reproduce the above
- ;;; copyright notice, this list of conditions and the following
- ;;; disclaimer in the documentation and/or other materials provided
- ;;; with the distribution.
- ;;;
- ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
- ;;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
- ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
- ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
- ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- ;;; N. Raghavendra <raghu@retrotexts.net>
- ;;;
- ;;; Created: 2009-09-13
- ;;;
- ;;; $Hg$
- (in-package "NET.RETROTEXTS.LIBXML-CLISP.EXAMPLES")
- (defun element-names-reader (filename stream)
- "Print to STREAM the names of all elements of the document from FILENAME.
- The printing is done in depth-first order. FILENAME must be a
- pathname designator."
- (with-text-reader (reader filename)
- (loop for reader-status = (text-reader-read reader)
- while reader-status
- do (process-node reader stream))))
- (defun process-node (text-reader stream)
- (when (eql (text-reader-node-type text-reader) 'xml-reader-type-element)
- (format stream
- (format nil "~~~D@T~~A~%" (* 4 (text-reader-depth text-reader)))
- (xml-string-to-string (text-reader-name text-reader)))))
- (defun test-element-names-reader (string)
- (with-temp-file (test-file)
- (with-open-file (out test-file :direction :output)
- (write-string string out))
- (with-output-to-string (stream)
- (element-names-reader test-file stream))))
- (defvar *element-names-reader-test-data*
- "<?xml version=\"1.0\"?>
- <article>
- <articleinfo>
- <author>
- <firstname>Foo</firstname>
- <surname>Bar</surname>
- </author>
- <title>An Example Article</title>
- <titleabbrev>Example art</titleabbrev>
- <keywordset>
- <keyword>an articleinfo keyword</keyword>
- <keyword>another articleinfo keyword</keyword>
- </keywordset>
- </articleinfo>
- <section>
- <sectioninfo>
- <keywordset>
- <keyword>a sectioninfo keyword</keyword>
- </keywordset>
- </sectioninfo>
- <title>A Section</title>
- <para>This is a paragraph in a section.</para>
- </section>
- </article>")
- (defvar *element-names-reader-test-value*
- "article
- articleinfo
- author
- firstname
- surname
- title
- titleabbrev
- keywordset
- keyword
- keyword
- section
- sectioninfo
- keywordset
- keyword
- title
- para
- ")
- (defun test-names-reader ()
- (test-libxml-clisp #'test-element-names-reader
- *element-names-reader-test-data*
- *element-names-reader-test-value*))
- (provide-example 'names-reader)
- ;;; Local Variables:
- ;;; mode: lisp
- ;;; comment-column: 32
- ;;; End:
- ;;;; names-reader.lisp ends here