PageRenderTime 30ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/names-reader.lisp

https://bitbucket.org/raghu/libxml-clisp
Lisp | 118 lines | 70 code | 14 blank | 34 comment | 2 complexity | 34b47bc9b50682e0365d3a48e5acf2d7 MD5 | raw file
  1. ;;;; names-reader.lisp --- example for libxml-clisp tutorial
  2. ;;; Copyright (C) 2009 N. Raghavendra. All rights reserved.
  3. ;;;
  4. ;;; Redistribution and use in source and binary forms, with or without
  5. ;;; modification, are permitted provided that the following conditions
  6. ;;; are met:
  7. ;;; 1. Redistributions of source code must retain the above copyright
  8. ;;; notice, this list of conditions and the following disclaimer.
  9. ;;; 2. Redistributions in binary form must reproduce the above
  10. ;;; copyright notice, this list of conditions and the following
  11. ;;; disclaimer in the documentation and/or other materials provided
  12. ;;; with the distribution.
  13. ;;;
  14. ;;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
  15. ;;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. ;;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  17. ;;; ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  18. ;;; DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  19. ;;; DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  20. ;;; GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  21. ;;; INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. ;;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  23. ;;; NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. ;;; SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. ;;; N. Raghavendra <raghu@retrotexts.net>
  26. ;;;
  27. ;;; Created: 2009-09-13
  28. ;;;
  29. ;;; $Hg$
  30. (in-package "NET.RETROTEXTS.LIBXML-CLISP.EXAMPLES")
  31. (defun element-names-reader (filename stream)
  32. "Print to STREAM the names of all elements of the document from FILENAME.
  33. The printing is done in depth-first order. FILENAME must be a
  34. pathname designator."
  35. (with-text-reader (reader filename)
  36. (loop for reader-status = (text-reader-read reader)
  37. while reader-status
  38. do (process-node reader stream))))
  39. (defun process-node (text-reader stream)
  40. (when (eql (text-reader-node-type text-reader) 'xml-reader-type-element)
  41. (format stream
  42. (format nil "~~~D@T~~A~%" (* 4 (text-reader-depth text-reader)))
  43. (xml-string-to-string (text-reader-name text-reader)))))
  44. (defun test-element-names-reader (string)
  45. (with-temp-file (test-file)
  46. (with-open-file (out test-file :direction :output)
  47. (write-string string out))
  48. (with-output-to-string (stream)
  49. (element-names-reader test-file stream))))
  50. (defvar *element-names-reader-test-data*
  51. "<?xml version=\"1.0\"?>
  52. <article>
  53. <articleinfo>
  54. <author>
  55. <firstname>Foo</firstname>
  56. <surname>Bar</surname>
  57. </author>
  58. <title>An Example Article</title>
  59. <titleabbrev>Example art</titleabbrev>
  60. <keywordset>
  61. <keyword>an articleinfo keyword</keyword>
  62. <keyword>another articleinfo keyword</keyword>
  63. </keywordset>
  64. </articleinfo>
  65. <section>
  66. <sectioninfo>
  67. <keywordset>
  68. <keyword>a sectioninfo keyword</keyword>
  69. </keywordset>
  70. </sectioninfo>
  71. <title>A Section</title>
  72. <para>This is a paragraph in a section.</para>
  73. </section>
  74. </article>")
  75. (defvar *element-names-reader-test-value*
  76. "article
  77. articleinfo
  78. author
  79. firstname
  80. surname
  81. title
  82. titleabbrev
  83. keywordset
  84. keyword
  85. keyword
  86. section
  87. sectioninfo
  88. keywordset
  89. keyword
  90. title
  91. para
  92. ")
  93. (defun test-names-reader ()
  94. (test-libxml-clisp #'test-element-names-reader
  95. *element-names-reader-test-data*
  96. *element-names-reader-test-value*))
  97. (provide-example 'names-reader)
  98. ;;; Local Variables:
  99. ;;; mode: lisp
  100. ;;; comment-column: 32
  101. ;;; End:
  102. ;;;; names-reader.lisp ends here