Commit a3b99d08 authored by Sven Van Caekenberghe's avatar Sven Van Caekenberghe
Browse files

*** empty log message ***

parents
# CVS default ignores begin
tags
TAGS
.make.state
.nse_depinfo
*~
#*
.#*
,*
_$*
*$
*.old
*.bak
*.BAK
*.orig
*.rej
.del-*
*.a
*.olb
*.o
*.obj
*.so
*.exe
*.Z
*.elc
*.ln
core
# CVS default ignores end
default:
@echo Possible targets:
@echo clean-openmcl --- remove all '*.dfsl' recursively
@echo clean-lw --- remove all '*.nfasl' recursively
@echo clean-emacs --- remove all '*~' recursively
@echo clean --- all of the above
clean-openmcl:
find . -name "*.dfsl" | xargs rm
clean-lw:
find . -name "*.nfasl" | xargs rm
clean-emacs:
find . -name "*~" | xargs rm
clean: clean-openmcl clean-lw clean-emacs
;;;; -*- Mode: LISP -*-
;;;;
;;;; $Id: xml.asd,v 1.2 2003/12/13 22:27:02 sven Exp $
;;;;
;;;; The S-XML ASDF system definition
;;;;
;;;; Copyright (C) 2002, 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 :asdf)
(defsystem :s-xml
:name "S-XML"
:author "Sven Van Caekenberghe <svc@mac.com>"
:version "3"
:maintainer "Sven Van Caekenberghe <svc@mac.com>, Brian Mastenbrook <>, Rudi Schlatte <>"
:licence "Lisp Lesser General Public License (LLGPL)"
:description "Simple Common Lisp XML Parser"
:long-description "S-XML is a Common Lisp implementation of a simple XML parser, with a SAX-like and DOM interface"
:components
((:module
:src
:components ((:file "package")
(:file "xml" :depends-on ("package"))
(:file "dom" :depends-on ("package"))
(:file "lxml-dom" :depends-on ("dom"))
(:file "sxml-dom" :depends-on ("dom"))
(:file "xml-struct-dom" :depends-on ("dom"))))))
;;;; eof
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: dom.lisp,v 1.5 2003/05/23 12:07:44 sven Exp $
;;;;
;;;; This is the generic simple DOM parser and printer interface.
;;;;
;;;; Copyright (C) 2002, 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)
;;; top level DOM parser interface
(defgeneric parse-xml-dom (stream output-type)
(:documentation "Parse a character stream as XML and generate a DOM of output-type"))
(defun parse-xml (stream &key (output-type :lxml))
"Parse a character stream as XML and generate a DOM of output-type, defaulting to :lxml"
(parse-xml-dom stream output-type))
(defun parse-xml-string (string &key (output-type :lxml))
"Parse a string as XML and generate a DOM of output-type, defaulting to :lxml"
(with-input-from-string (stream string)
(parse-xml-dom stream output-type)))
(defun parse-xml-file (filename &key (output-type :lxml))
"Parse a character file as XML and generate a DOM of output-type, defaulting to :lxml"
(with-open-file (in filename :direction :input)
(parse-xml-dom in output-type)))
;;; top level DOM printer interface
(defgeneric print-xml-dom (dom input-type stream pretty level)
(:documentation "Generate XML output on a character stream from a DOM of input-type, optionally pretty printing using level"))
(defun print-xml (dom &key (stream t) (pretty nil) (input-type :lxml) (header))
"Generate XML output on a character stream (t by default) from a DOM of input-type (:lxml by default), optionally pretty printing (off by default), or adding a header (none by default)"
(when header (format stream header))
(when pretty (terpri stream))
(print-xml-dom dom input-type stream pretty 1))
(defun print-xml-string (dom &key (pretty nil) (input-type :lxml))
"Generate XML output to a string from a DOM of input-type (:lxml by default), optionally pretty printing (off by default)"
(with-output-to-string (stream)
(print-xml dom :stream stream :pretty pretty :input-type input-type)))
;;;; eof
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: lxml-dom.lisp,v 1.4 2003/06/05 12:50:18 sven Exp $
;;;;
;;;; LXML implementation of the generic DOM parser and printer.
;;;;
;;;; Copyright (C) 2002, 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 lxml hooks to generate lxml
(defun lxml-new-element-hook (name attributes seed)
(declare (ignore name attributes seed))
'())
(defun lxml-finish-element-hook (name attributes parent-seed seed)
(let ((xml-element
(cond ((and (null seed) (null attributes))
name)
(attributes
`((,name ,@(let (list)
(dolist (attribute attributes list)
(push (cdr attribute) list)
(push (car attribute) list))))
,@(nreverse seed)))
(t
`(,name ,@(nreverse seed))))))
(cons xml-element parent-seed)))
(defun lxml-text-hook (string seed)
(cons string seed))
;;; standard DOM interfaces
(defmethod parse-xml-dom (stream (output-type (eql :lxml)))
(car (start-parse-xml stream
(make-instance 'xml-parser-state
:new-element-hook #'lxml-new-element-hook
:finish-element-hook #'lxml-finish-element-hook
:text-hook #'lxml-text-hook))))
(defmethod print-xml-dom (dom (input-type (eql :lxml)) stream pretty level)
(cond ((symbolp dom) (format stream "<~a/>" dom))
((stringp dom) (print-string-xml dom stream))
((consp dom)
(let (tag attributes)
(cond ((symbolp (car dom)) (setf tag (car dom)))
((consp (car dom)) (setf tag (caar dom) attributes (cdar dom)))
(t (error "Input not recognized as LXML ~s" dom)))
(format stream "<~a" tag)
(labels ((print-attributes (attributes)
(unless (null attributes)
(format stream " ~a=\"" (car attributes))
(print-string-xml (cadr attributes) stream)
(format stream "\"")
(print-attributes (cddr attributes)))))
(when attributes (print-attributes attributes)))
(if (cdr dom)
(let ((children (cdr dom)))
(format stream ">")
(if (and (= (length children) 1) (stringp (first children)))
(print-string-xml (first children) stream)
(progn
(dolist (child children)
(when pretty
(terpri stream)
(dotimes (i (* 2 level)) (write-char #\space stream)))
(if (stringp child)
(print-string-xml child stream)
(print-xml-dom child input-type stream pretty (1+ level))))
(when pretty
(terpri stream)
(dotimes (i (* 2 (1- level))) (write-char #\space stream)))))
(format stream "</~a>" tag))
(format stream "/>" tag))))
(t (error "Input not recognized as LXML ~s" dom))))
;;;; eof
\ No newline at end of file
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: xml.lisp,v 1.19 2004/06/04 08:10:09 sven Exp $
;;;;
;;;; This is a Common Lisp implementation of a very basic XML parser.
;;;; The parser is non-validating and not at all complete (no CDATA).
;;;; The API into the parser is pure functional parser hook model that comes from SSAX,
;;;; see also http://pobox.com/~oleg/ftp/Scheme/xml.html or http://ssax.sourceforge.net
;;;; Different DOM models are provided, an XSML, an LXML and a xml-element struct based one.
;;;;
;;;; Copyright (C) 2002, 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.
(defpackage s-xml
(:use common-lisp)
(:export
;; main parser interface
#:start-parse-xml
#:print-string-xml
#:echo-xml
#:xml-parser-error #:xml-parser-error-message #:xml-parser-error-args #:xml-parser-error-stream
#:xml-parser-state #:get-entities #:get-seed
#:get-new-element-hook #:get-finish-element-hook #:get-text-hook
;; dom parser and printer
#:parse-xml-dom #:parse-xml #:parse-xml-string #:parse-xml-file
#:print-xml-dom #:print-xml #:print-xml-string
;; xml-element structure
#:make-xml-element #:xml-element-children #:xml-element-name
#:xml-element-p #:new-xml-element #:first-xml-element-child)
(:documentation
"A simple XML parser with an efficient, purely functional, event-based interface as well as a DOM interface"))
;;;; eof
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: sxml-dom.lisp,v 1.4 2003/06/05 12:50:18 sven 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)
(cond ((stringp dom) (print-string-xml dom stream))
((consp dom)
(let ((tag (car dom))
attributes
children)
(if (and (consp (cadr dom)) (eq (caadr dom) :@))
(setf attributes (cdadr dom)
children (cddr dom))
(setf children (cdr dom)))
(format stream "<~a" tag)
(dolist (pair attributes)
(format stream " ~a=\"" (car pair))
(print-string-xml (cadr pair) stream)
(format stream "\""))
(if children
(progn
(format stream ">")
(if (and (= (length children) 1) (stringp (first children)))
(print-string-xml (first children) stream)
(progn
(dolist (child children)
(when pretty
(terpri stream)
(dotimes (i (* 2 level)) (write-char #\space stream)))
(if (stringp child)
(print-string-xml child stream)
(print-xml-dom child input-type stream pretty (1+ level))))
(when pretty
(terpri stream)
(dotimes (i (* 2 (1- level))) (write-char #\space stream)))))
(format stream "</~a>" tag))
(format stream "/>" tag))))
(t (error "Input not recognized as SXML ~s" dom))))
;;;; eof
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: xml-struct-dom.lisp,v 1.5 2003/06/05 12:50:18 sven Exp $
;;;;
;;;; XML-STRUCT implementation of the generic DOM parser and printer.
;;;;
;;;; Copyright (C) 2002, 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)
;;; xml-element struct datastructure and API
(defstruct xml-element
name ; :tag-name
attributes ; a assoc list of (:attribute-name . "attribute-value")
children ; a list of children/content either text strings or xml-elements
)
(setf (documentation 'xml-element-p 'function)
"Return T when the argument is an xml-element struct"
(documentation 'xml-element-attributes 'function)
"Return the alist of attribute names and values dotted pairs from an xml-element struct"
(documentation 'xml-element-children 'function)
"Return the list of children from an xml-element struct"
(documentation 'xml-element-name 'function)
"Return the name from an xml-element struct"
(documentation 'make-xml-element 'function)
"Make and return a new xml-element struct")
(defun xml-element-attribute (xml-element key)
"Return the string value of the attribute with name the keyword :key
of xml-element if any, return null if not found"
(let ((pair (assoc key (xml-element-attributes xml-element) :test #'eq)))
(when pair (cdr pair))))
(defun (setf xml-element-attribute) (value xml-element key)
"Set the string value of the attribute with name the keyword :key of
xml-element, creating a new attribute if necessary or overwriting an
existing one, returning the value"
(let ((attributes (xml-element-attributes xml-element)))
(if (null attributes)
(push (cons key value) (xml-element-attributes xml-element))
(let ((pair (assoc key attributes :test #'eq)))
(if pair
(setf (cdr pair) value)
(push (cons key value) (xml-element-attributes xml-element)))))
value))
(defun new-xml-element (name &rest children)
"Make a new xml-element with name and children"
(make-xml-element :name name :children children))
(defun first-xml-element-child (xml-element)
"Get the first child of an xml-element"
(first (xml-element-children xml-element)))
(defun xml-equal (xml-1 xml-2)
(and (xml-element-p xml-1)
(xml-element-p xml-2)
(eq (xml-element-name xml-1)
(xml-element-name xml-2))
(equal (xml-element-attributes xml-1)
(xml-element-attributes xml-2))
(reduce #'(lambda (&optional (x t) (y t)) (and x y))
(mapcar #'(lambda (x y)
(or (and (stringp x) (stringp y) (string= x y))
(xml-equal x y)))
(xml-element-children xml-1)
(xml-element-children xml-2)))))
;;; printing xml structures
(defmethod print-xml-dom (xml-element (input-type (eql :xml-struct)) stream pretty level)
(format stream "<~a" (xml-element-name xml-element))
(dolist (attribute (xml-element-attributes xml-element))
(format stream " ~a=\"" (car attribute))
(print-string-xml (cdr attribute) stream)
(format stream "\""))
(let ((children (xml-element-children xml-element)))
(if children
(progn
(format stream ">")
(if (and (= (length children) 1) (stringp (first children)))
(print-string-xml (first children) stream)
(progn
(dolist (child children)
(when pretty
(terpri stream)
(dotimes (i (* 2 level)) (write-char #\space stream)))
(if (stringp child)
(print-string-xml child stream)
(print-xml-dom child input-type stream pretty (1+ level))))
(when pretty
(terpri stream)
(dotimes (i (* 2 (1- level))) (write-char #\space stream)))))
(format stream "</~a>" (xml-element-name xml-element)))
(format stream "/>"))))
;;; the standard hooks to generate xml-element structs
(defun standard-new-element-hook (name attributes seed)
(declare (ignore name attributes seed))
'())
(defun standard-finish-element-hook (name attributes parent-seed seed)
(let ((xml-element (make-xml-element :name name
:attributes attributes
:children (nreverse seed))))
(cons xml-element parent-seed)))
(defun standard-text-hook (string seed)
(cons string seed))
;;; top level standard parser interfaces
(defmethod parse-xml-dom (stream (output-type (eql :xml-struct)))
(car (start-parse-xml stream
(make-instance 'xml-parser-state
:new-element-hook #'standard-new-element-hook
:finish-element-hook #'standard-finish-element-hook
:text-hook #'standard-text-hook))))
;;;; eof
;;;; -*- mode: lisp -*-
;;;;
;;;; $Id: xml.lisp,v 1.19 2004/06/04 08:10:09 sven Exp $
;;;;
;;;; This is a Common Lisp implementation of a very basic XML parser.
;;;; The parser is non-validating and not at all complete (no CDATA).
;;;; The API into the parser is a pure functional parser hook model that comes from SSAX,
;;;; see also http://pobox.com/~oleg/ftp/Scheme/xml.html or http://ssax.sourceforge.net
;;;; Different DOM models are provided, an XSML, an LXML and a xml-element struct based one.
;;;;
;;;; Copyright (C) 2002, 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)
;;; error reporting
(define-condition xml-parser-error (error)
((message :initarg :message :reader xml-parser-error-message)
(args :initarg :args :reader xml-parser-error-args)
(stream :initarg :stream :reader xml-parser-error-stream :initform nil))
(:report (lambda (condition stream)
(format stream
"XML parser ~?~@[ near stream position ~d~]."
(xml-parser-error-message condition)
(xml-parser-error-args condition)
(and (xml-parser-error-stream condition)
(file-position (xml-parser-error-stream condition))))))
(:documentation "Thrown by the XML parser to indicate errorneous input"))
(setf (documentation 'xml-parser-error-message 'function)
"Get the message from an XML parser error"
(documentation 'xml-parser-error-args 'function)
"Get the error arguments from an XML parser error"
(documentation 'xml-parser-error-stream 'function)
"Get the stream from an XML parser error")
(defun parser-error (message &optional args stream)
(make-condition 'xml-parser-error
:message message
:args args
:stream stream))
;;; utilities
(defun whitespace-char-p (char)
"Is char an XML whitespace character ?"
(or (char= char #\space)
(char= char #\tab)
(char= char #\return)
(char= char #\linefeed)))
(defun identifier-char-p (char)
"Is char an XML identifier character ?"
(or (and (char<= #\A char) (char<= char #\Z))
(and (char<= #\a char) (char<= char #\z))
(and (char<= #\0 char) (char<= char #\9))
(char= char #\-)
(char= char #\_)
(char= char #\.)
(char= char #\:)))
(defun skip-whitespace (stream)
"Skip over XML whitespace in stream, return first non-whitespace
character which was peeked but not read, return nil on eof"
(loop
(let ((char (peek-char nil stream nil nil)))
(if (and char (whitespace-char-p char))
(read-char stream)
(return char)))))
(defun make-extendable-string (&optional (size 10))
"Make an extendable string which is a one-dimensional character
array which is adjustable and has a fill pointer"
(make-array size
:element-type 'character
:adjustable t
:fill-pointer 0))
(defun print-string-xml (string stream)
"Write the characters of string to stream using basic XML conventions"
(loop for char across string
do (case char
(#\& (write-string "&amp;" stream))
(#\< (write-string "&lt;" stream))
(#\> (write-string "&gt;" stream))
(#\" (write-string "&quot;" stream))
(#\' (write-string "&apos;" stream))
(t (if (and (<= 32 (char-code char))
(<= (char-code char) 126))
(write-char char stream)
(progn
(write-string "&#x" stream)
(write (char-code char) :stream stream :base 16)
(write-char #\; stream)))))))
(defun make-standard-entities ()
"A hashtable mapping XML entity names to their replacement strings,
filled with the standard set"
(let ((entities (make-hash-table :test #'equal)))
(setf (gethash "amp" entities) (string #\&)
(gethash "quot" entities) (string #\")
(gethash "apos" entities) (string #\')
(gethash "lt" entities) (string #\<)
(gethash "gt" entities) (string #\>)
(gethash "nbsp" entities) (string #\space))
entities))
(defun resolve-entity (stream extendable-string entities &optional (entity (make-extendable-string)))
"Read and resolve an XML entity from stream, positioned on the '&'
entity marker, accepting &name; , &DEC; and &#HEX; formats,
destructively modifying string, which is also returned,
destructively modifying entity, incorrect entity formats result in
errors"
(loop
(let ((char (read-char stream nil nil)))
(cond ((null char) (error (parser-error "encountered eof before end of entity")))
((char= #\; char) (return))
(t (vector-push-extend char entity)))))
(if (char= (char entity 0) #\#)
(let ((code (if (char= (char entity 1) #\x)
(parse-integer entity :start 2 :radix 16)
(parse-integer entity :start 1 :radix 10))))
(if (null code) (error (parser-error "encountered incorrect entity &~s;" (list entity) stream)))
(vector-push-extend (code-char code) extendable-string))
(let ((value (gethash entity entities)))
(if value
(dotimes (i (length value))
(vector-push-extend (char value i) extendable-string))
(error (parser-error "encountered unknown entity &~s;" (list entity) stream)))))
extendable-string)
;;; the parser state
(defclass xml-parser-state ()
((entities :documentation "A hashtable mapping XML entity names to their replacement stings"
:accessor get-entities
:initarg :entities
:initform (make-standard-entities))
(seed :documentation "The user seed object"
:accessor get-seed
:initarg :seed
:initform nil)
(buffer :documentation "The main reusable character buffer"
:accessor get-buffer
:initform (make-extendable-string))
(mini-buffer :documentation "The secondary, smaller reusable character buffer"
:accessor get-mini-buffer
:initform (make-extendable-string))
(new-element-hook :documentation "Called when new element starts"
;; Handle a new xml element with name and attributes,
;; receiving seed from parent if any or top level