summaryrefslogtreecommitdiff
path: root/third-party/s-xml/src/sxml-dom.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/src/sxml-dom.lisp
parent15937a1a4f1cf40bc55aa34eb71c67b88466ff57 (diff)
Add third-party XML processing libraries.
Ignore-this: 5ca28497555bf944858ca2f58bc8a62b darcs-hash:a0b0f9baa7c9b1259e755435db1fb17123630a6c
Diffstat (limited to 'third-party/s-xml/src/sxml-dom.lisp')
-rw-r--r--third-party/s-xml/src/sxml-dom.lisp76
1 files changed, 76 insertions, 0 deletions
diff --git a/third-party/s-xml/src/sxml-dom.lisp b/third-party/s-xml/src/sxml-dom.lisp
new file mode 100644
index 0000000..dee3de8
--- /dev/null
+++ b/third-party/s-xml/src/sxml-dom.lisp
@@ -0,0 +1,76 @@
+;;;; -*- mode: lisp -*-
+;;;;
+;;;; $Id: sxml-dom.lisp,v 1.5 2005-11-20 14:34:15 scaekenberghe Exp $
+;;;;
+;;;; LXML implementation of the generic DOM parser and printer.
+;;;;
+;;;; Copyright (C) 2003, 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)
+
+;;; the sxml hooks to generate sxml
+
+(defun sxml-new-element-hook (name attributes seed)
+ (declare (ignore name attributes seed))
+ '())
+
+(defun sxml-finish-element-hook (name attributes parent-seed seed)
+ (let ((xml-element (append (list name)
+ (when attributes
+ (list (let (list)
+ (dolist (attribute attributes (cons :@ list))
+ (push (list (car attribute) (cdr attribute)) list)))))
+ (nreverse seed))))
+ (cons xml-element parent-seed)))
+
+(defun sxml-text-hook (string seed)
+ (cons string seed))
+
+;;; the standard DOM interfaces
+
+(defmethod parse-xml-dom (stream (output-type (eql :sxml)))
+ (car (start-parse-xml stream
+ (make-instance 'xml-parser-state
+ :new-element-hook #'sxml-new-element-hook
+ :finish-element-hook #'sxml-finish-element-hook
+ :text-hook #'sxml-text-hook))))
+
+(defmethod print-xml-dom (dom (input-type (eql :sxml)) stream pretty level)
+ (declare (special *namespaces*))
+ (cond ((stringp dom) (print-string-xml dom stream))
+ ((consp dom)
+ (let ((tag (first dom))
+ attributes
+ children)
+ (if (and (consp (second dom)) (eq (first (second dom)) :@))
+ (setf attributes (rest (second dom))
+ children (rest (rest dom)))
+ (setf children (rest dom)))
+ (let ((*namespaces* (extend-namespaces (loop :for (name value) :in attributes
+ :collect (cons name value))
+ *namespaces*)))
+ (write-char #\< stream)
+ (print-identifier tag stream)
+ (loop :for (name value) :in attributes
+ :do (print-attribute name value stream))
+ (if children
+ (progn
+ (write-char #\> stream)
+ (if (and (= (length children) 1) (stringp (first children)))
+ (print-string-xml (first children) stream)
+ (progn
+ (dolist (child children)
+ (when pretty (print-spaces (* 2 level) stream))
+ (if (stringp child)
+ (print-string-xml child stream)
+ (print-xml-dom child input-type stream pretty (1+ level))))
+ (when pretty (print-spaces (* 2 (1- level)) stream))))
+ (print-closing-tag tag stream))
+ (write-string "/>" stream)))))
+ (t (error "Input not recognized as SXML ~s" dom))))
+
+;;;; eof