Initial revision

This commit is contained in:
david
2005-03-13 18:02:10 +00:00
commit d6ca7664f4
81 changed files with 19663 additions and 0 deletions

459
dom/COPYING Normal file
View File

@ -0,0 +1,459 @@
GNU LESSER GENERAL PUBLIC LICENSE
Version 2.1, February 1999
Copyright (C) 1991, 1999 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
[This is the first released version of the Lesser GPL. It also counts
as the successor of the GNU Library Public License, version 2, hence
the version number 2.1.]
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
Licenses are intended to guarantee your freedom to share and change
free software--to make sure the software is free for all its users.
This license, the Lesser General Public License, applies to some
specially designated software packages--typically libraries--of the
Free Software Foundation and other authors who decide to use it. You
can use it too, but we suggest you first think carefully about whether
this license or the ordinary General Public License is the better
strategy to use in any particular case, based on the explanations below.
When we speak of free software, we are referring to freedom of use,
not price. Our General Public Licenses are designed to make sure that
you have the freedom to distribute copies of free software (and charge
for this service if you wish); that you receive source code or can get
it if you want it; that you can change the software and use pieces of
it in new free programs; and that you are informed that you can do
these things.
To protect your rights, we need to make restrictions that forbid
distributors to deny you these rights or to ask you to surrender these
rights. These restrictions translate to certain responsibilities for
you if you distribute copies of the library or if you modify it.
For example, if you distribute copies of the library, whether gratis
or for a fee, you must give the recipients all the rights that we gave
you. You must make sure that they, too, receive or can get the source
code. If you link other code with the library, you must provide
complete object files to the recipients, so that they can relink them
with the library after making changes to the library and recompiling
it. And you must show them these terms so they know their rights.
We protect your rights with a two-step method: (1) we copyright the
library, and (2) we offer you this license, which gives you legal
permission to copy, distribute and/or modify the library.
To protect each distributor, we want to make it very clear that
there is no warranty for the free library. Also, if the library is
modified by someone else and passed on, the recipients should know
that what they have is not the original version, so that the original
author's reputation will not be affected by problems that might be
introduced by others.
Finally, software patents pose a constant threat to the existence of
any free program. We wish to make sure that a company cannot
effectively restrict the users of a free program by obtaining a
restrictive license from a patent holder. Therefore, we insist that
any patent license obtained for a version of the library must be
consistent with the full freedom of use specified in this license.
Most GNU software, including some libraries, is covered by the
ordinary GNU General Public License. This license, the GNU Lesser
General Public License, applies to certain designated libraries, and
is quite different from the ordinary General Public License. We use
this license for certain libraries in order to permit linking those
libraries into non-free programs.
When a program is linked with a library, whether statically or using
a shared library, the combination of the two is legally speaking a
combined work, a derivative of the original library. The ordinary
General Public License therefore permits such linking only if the
entire combination fits its criteria of freedom. The Lesser General
Public License permits more lax criteria for linking other code with
the library.
We call this license the "Lesser" General Public License because it
does Less to protect the user's freedom than the ordinary General
Public License. It also provides other free software developers Less
of an advantage over competing non-free programs. These disadvantages
are the reason we use the ordinary General Public License for many
libraries. However, the Lesser license provides advantages in certain
special circumstances.
For example, on rare occasions, there may be a special need to
encourage the widest possible use of a certain library, so that it becomes
a de-facto standard. To achieve this, non-free programs must be
allowed to use the library. A more frequent case is that a free
library does the same job as widely used non-free libraries. In this
case, there is little to gain by limiting the free library to free
software only, so we use the Lesser General Public License.
In other cases, permission to use a particular library in non-free
programs enables a greater number of people to use a large body of
free software. For example, permission to use the GNU C Library in
non-free programs enables many more people to use the whole GNU
operating system, as well as its variant, the GNU/Linux operating
system.
Although the Lesser General Public License is Less protective of the
users' freedom, it does ensure that the user of a program that is
linked with the Library has the freedom and the wherewithal to run
that program using a modified version of the Library.
The precise terms and conditions for copying, distribution and
modification follow. Pay close attention to the difference between a
"work based on the library" and a "work that uses the library". The
former contains code derived from the library, whereas the latter must
be combined with the library in order to run.
GNU LESSER GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License Agreement applies to any software library or other
program which contains a notice placed by the copyright holder or
other authorized party saying it may be distributed under the terms of
this Lesser General Public License (also called "this License").
Each licensee is addressed as "you".
A "library" means a collection of software functions and/or data
prepared so as to be conveniently linked with application programs
(which use some of those functions and data) to form executables.
The "Library", below, refers to any such software library or work
which has been distributed under these terms. A "work based on the
Library" means either the Library or any derivative work under
copyright law: that is to say, a work containing the Library or a
portion of it, either verbatim or with modifications and/or translated
straightforwardly into another language. (Hereinafter, translation is
included without limitation in the term "modification".)
"Source code" for a work means the preferred form of the work for
making modifications to it. For a library, complete source code means
all the source code for all modules it contains, plus any associated
interface definition files, plus the scripts used to control compilation
and installation of the library.
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running a program using the Library is not restricted, and output from
such a program is covered only if its contents constitute a work based
on the Library (independent of the use of the Library in a tool for
writing it). Whether that is true depends on what the Library does
and what the program that uses the Library does.
1. You may copy and distribute verbatim copies of the Library's
complete source code as you receive it, in any medium, provided that
you conspicuously and appropriately publish on each copy an
appropriate copyright notice and disclaimer of warranty; keep intact
all the notices that refer to this License and to the absence of any
warranty; and distribute a copy of this License along with the
Library.
You may charge a fee for the physical act of transferring a copy,
and you may at your option offer warranty protection in exchange for a
fee.
2. You may modify your copy or copies of the Library or any portion
of it, thus forming a work based on the Library, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) The modified work must itself be a software library.
b) You must cause the files modified to carry prominent notices
stating that you changed the files and the date of any change.
c) You must cause the whole of the work to be licensed at no
charge to all third parties under the terms of this License.
d) If a facility in the modified Library refers to a function or a
table of data to be supplied by an application program that uses
the facility, other than as an argument passed when the facility
is invoked, then you must make a good faith effort to ensure that,
in the event an application does not supply such function or
table, the facility still operates, and performs whatever part of
its purpose remains meaningful.
(For example, a function in a library to compute square roots has
a purpose that is entirely well-defined independent of the
application. Therefore, Subsection 2d requires that any
application-supplied function or table used by this function must
be optional: if the application does not supply it, the square
root function must still compute square roots.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Library,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Library, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote
it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Library.
In addition, mere aggregation of another work not based on the Library
with the Library (or with a work based on the Library) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may opt to apply the terms of the ordinary GNU General Public
License instead of this License to a given copy of the Library. To do
this, you must alter all the notices that refer to this License, so
that they refer to the ordinary GNU General Public License, version 2,
instead of to this License. (If a newer version than version 2 of the
ordinary GNU General Public License has appeared, then you can specify
that version instead if you wish.) Do not make any other change in
these notices.
Once this change is made in a given copy, it is irreversible for
that copy, so the ordinary GNU General Public License applies to all
subsequent copies and derivative works made from that copy.
This option is useful when you wish to copy part of the code of
the Library into a program that is not a library.
4. You may copy and distribute the Library (or a portion or
derivative of it, under Section 2) in object code or executable form
under the terms of Sections 1 and 2 above provided that you accompany
it with the complete corresponding machine-readable source code, which
must be distributed under the terms of Sections 1 and 2 above on a
medium customarily used for software interchange.
If distribution of object code is made by offering access to copy
from a designated place, then offering equivalent access to copy the
source code from the same place satisfies the requirement to
distribute the source code, even though third parties are not
compelled to copy the source along with the object code.
5. A program that contains no derivative of any portion of the
Library, but is designed to work with the Library by being compiled or
linked with it, is called a "work that uses the Library". Such a
work, in isolation, is not a derivative work of the Library, and
therefore falls outside the scope of this License.
However, linking a "work that uses the Library" with the Library
creates an executable that is a derivative of the Library (because it
contains portions of the Library), rather than a "work that uses the
library". The executable is therefore covered by this License.
Section 6 states terms for distribution of such executables.
When a "work that uses the Library" uses material from a header file
that is part of the Library, the object code for the work may be a
derivative work of the Library even though the source code is not.
Whether this is true is especially significant if the work can be
linked without the Library, or if the work is itself a library. The
threshold for this to be true is not precisely defined by law.
If such an object file uses only numerical parameters, data
structure layouts and accessors, and small macros and small inline
functions (ten lines or less in length), then the use of the object
file is unrestricted, regardless of whether it is legally a derivative
work. (Executables containing this object code plus portions of the
Library will still fall under Section 6.)
Otherwise, if the work is a derivative of the Library, you may
distribute the object code for the work under the terms of Section 6.
Any executables containing that work also fall under Section 6,
whether or not they are linked directly with the Library itself.
6. As an exception to the Sections above, you may also combine or
link a "work that uses the Library" with the Library to produce a
work containing portions of the Library, and distribute that work
under terms of your choice, provided that the terms permit
modification of the work for the customer's own use and reverse
engineering for debugging such modifications.
You must give prominent notice with each copy of the work that the
Library is used in it and that the Library and its use are covered by
this License. You must supply a copy of this License. If the work
during execution displays copyright notices, you must include the
copyright notice for the Library among them, as well as a reference
directing the user to the copy of this License. Also, you must do one
of these things:
a) Accompany the work with the complete corresponding
machine-readable source code for the Library including whatever
changes were used in the work (which must be distributed under
Sections 1 and 2 above); and, if the work is an executable linked
with the Library, with the complete machine-readable "work that
uses the Library", as object code and/or source code, so that the
user can modify the Library and then relink to produce a modified
executable containing the modified Library. (It is understood
that the user who changes the contents of definitions files in the
Library will not necessarily be able to recompile the application
to use the modified definitions.)
b) Use a suitable shared library mechanism for linking with the
Library. A suitable mechanism is one that (1) uses at run time a
copy of the library already present on the user's computer system,
rather than copying library functions into the executable, and (2)
will operate properly with a modified version of the library, if
the user installs one, as long as the modified version is
interface-compatible with the version that the work was made with.
c) Accompany the work with a written offer, valid for at
least three years, to give the same user the materials
specified in Subsection 6a, above, for a charge no more
than the cost of performing this distribution.
d) If distribution of the work is made by offering access to copy
from a designated place, offer equivalent access to copy the above
specified materials from the same place.
e) Verify that the user has already received a copy of these
materials or that you have already sent this user a copy.
For an executable, the required form of the "work that uses the
Library" must include any data and utility programs needed for
reproducing the executable from it. However, as a special exception,
the materials to be distributed need not include anything that is
normally distributed (in either source or binary form) with the major
components (compiler, kernel, and so on) of the operating system on
which the executable runs, unless that component itself accompanies
the executable.
It may happen that this requirement contradicts the license
restrictions of other proprietary libraries that do not normally
accompany the operating system. Such a contradiction means you cannot
use both them and the Library together in an executable that you
distribute.
7. You may place library facilities that are a work based on the
Library side-by-side in a single library together with other library
facilities not covered by this License, and distribute such a combined
library, provided that the separate distribution of the work based on
the Library and of the other library facilities is otherwise
permitted, and provided that you do these two things:
a) Accompany the combined library with a copy of the same work
based on the Library, uncombined with any other library
facilities. This must be distributed under the terms of the
Sections above.
b) Give prominent notice with the combined library of the fact
that part of it is a work based on the Library, and explaining
where to find the accompanying uncombined form of the same work.
8. You may not copy, modify, sublicense, link with, or distribute
the Library except as expressly provided under this License. Any
attempt otherwise to copy, modify, sublicense, link with, or
distribute the Library is void, and will automatically terminate your
rights under this License. However, parties who have received copies,
or rights, from you under this License will not have their licenses
terminated so long as such parties remain in full compliance.
9. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Library or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Library (or any work based on the
Library), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Library or works based on it.
10. Each time you redistribute the Library (or any work based on the
Library), the recipient automatically receives a license from the
original licensor to copy, distribute, link with or modify the Library
subject to these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties with
this License.
11. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Library at all. For example, if a patent
license would not permit royalty-free redistribution of the Library by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Library.
If any portion of this section is held invalid or unenforceable under any
particular circumstance, the balance of the section is intended to apply,
and the section as a whole is intended to apply in other circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
12. If the distribution and/or use of the Library is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Library under this License may add
an explicit geographical distribution limitation excluding those countries,
so that distribution is permitted only in or among countries not thus
excluded. In such case, this License incorporates the limitation as if
written in the body of this License.
13. The Free Software Foundation may publish revised and/or new
versions of the Lesser General Public License from time to time.
Such new versions will be similar in spirit to the present version,
but may differ in detail to address new problems or concerns.
Each version is given a distinguishing version number. If the Library
specifies a version number of this License which applies to it and
"any later version", you have the option of following the terms and
conditions either of that version or of any later version published by
the Free Software Foundation. If the Library does not specify a
license version number, you may choose any version ever published by
the Free Software Foundation.
14. If you wish to incorporate parts of the Library into other free
programs whose distribution conditions are incompatible with these,
write to the author to ask for permission. For software which is
copyrighted by the Free Software Foundation, write to the Free
Software Foundation; we sometimes make exceptions for this. Our
decision will be guided by the two goals of preserving the free status
of all derivatives of our free software and of promoting the sharing
and reuse of software generally.
NO WARRANTY
15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY
KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE
LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN
WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY
AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU
FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR
CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE
LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
END OF TERMS AND CONDITIONS

139
dom/dom-builder.lisp Normal file
View File

@ -0,0 +1,139 @@
;;; XXX this DOM builder knows too much about the specifics of the DOM
;;; implementation for my taste. While document creation is not specified
;;; by the DOM Level 1 spec, we shouldn't really be manually setting slots
;;; in other nodes IMHO.
;;;
;;; As a follow-up to that, the children list is created in the wrong order
;;; and then reversed. Is it really worth the improved speed to do this?
;;; Calling APPEND-NODE would be portable.
;;;
;;; In particular, that design choice has lead to other bugs, for example the
;;; PARENT slot has to be set manually, too. A DOM test finally showed
;;; that this had been forgotten for Text nodes and PIs.
;;;
;;; Opinions?
;;;
;;; -- David
;;; Now at least the children list isn't reversed anymore, because I changed
;;; the representation to be an extensible vector. Still its not clear to
;;; me whether the DOM Builder should be affected by such changes at all.
;;;
;;; -- David
(in-package :dom-impl)
(defclass dom-builder ()
((document :initform nil :accessor document)
(element-stack :initform '() :accessor element-stack)))
(defun dom:make-dom-builder ()
(make-instance 'dom-builder))
(defun fast-push (new-element vector)
(vector-push-extend new-element vector (max 1 (array-dimension vector 0))))
(defmethod sax:start-document ((handler dom-builder))
(let ((document (make-instance 'dom-impl::document)))
(setf (slot-value document 'dom-impl::owner) nil
(slot-value document 'dom-impl::doc-type) nil)
(setf (document handler) document)
(push document (element-stack handler))))
(defmethod sax:end-document ((handler dom-builder))
(setf (slot-value (document handler) 'entities) xml::*entities*)
(let ((doctype (dom:doctype (document handler))))
(when doctype
(setf (slot-value (dom:entities doctype) 'read-only-p) t)
(setf (slot-value (dom:notations doctype) 'read-only-p) t)))
(document handler))
(defmethod sax:start-dtd ((handler dom-builder) name publicid systemid)
(declare (ignore publicid systemid))
(let* ((document (document handler))
(doctype (make-instance 'dom-impl::document-type
:name name
:notations (make-instance 'dom-impl::named-node-map
:element-type :notation
:owner document)
:entities (make-instance 'dom-impl::named-node-map
:element-type :entity
:owner document))))
(setf (slot-value doctype 'dom-impl::owner) document
(slot-value document 'dom-impl::doc-type) doctype)))
(defmethod sax:start-element ((handler dom-builder) namespace-uri local-name qname attributes)
(with-slots (document element-stack) handler
(let ((element (dom:create-element document qname))
(parent (car element-stack)))
(dolist (attr attributes)
(dom:set-attribute element (xml::attribute-qname attr) (xml::attribute-value attr)))
(setf (slot-value element 'dom-impl::parent) parent)
(fast-push element (slot-value parent 'dom-impl::children))
(push element element-stack))))
(defmethod sax:end-element ((handler dom-builder) namespace-uri local-name qname)
(pop (element-stack handler)))
(defmethod sax:characters ((handler dom-builder) data)
(with-slots (document element-stack) handler
(let* ((parent (car element-stack))
(last-child (dom:last-child parent)))
(cond
((eq (dom:node-type parent) :cdata-section)
(setf (dom:data parent) data))
((and last-child (eq (dom:node-type last-child) :text))
;; um entities herum wird SAX:CHARACTERS mehrfach aufgerufen fuer
;; den gleichen Textknoten. Hier muessen wir den bestehenden Knoten
;; erweitern, sonst ist das Dokument nicht normalisiert.
;; (XXX Oder sollte man besser den Parser entsprechend aendern?)
(dom:append-data last-child data))
(t
(let ((node (dom:create-text-node document data)))
(setf (slot-value node 'dom-impl::parent) parent)
(fast-push node (slot-value (car element-stack) 'dom-impl::children))))))))
(defmethod sax:start-cdata ((handler dom-builder))
(with-slots (document element-stack) handler
(let ((node (dom:create-cdata-section document #""))
(parent (car element-stack)))
(setf (slot-value node 'dom-impl::parent) parent)
(fast-push node (slot-value parent 'dom-impl::children))
(push node element-stack))))
(defmethod sax:end-cdata ((handler dom-builder))
(let ((node (pop (slot-value handler 'element-stack))))
(assert (eq (dom:node-type node) :cdata-section))))
(defmethod sax:processing-instruction ((handler dom-builder) target data)
(with-slots (document element-stack) handler
(let ((node (dom:create-processing-instruction document target data))
(parent (car element-stack)))
(setf (slot-value node 'dom-impl::parent) parent)
(fast-push node (slot-value (car element-stack) 'dom-impl::children)))))
(defmethod sax:comment ((handler dom-builder) data)
(with-slots (document element-stack) handler
(let ((node (dom:create-comment document data))
(parent (car element-stack)))
(setf (slot-value node 'dom-impl::parent) parent)
(fast-push node (slot-value (car element-stack) 'dom-impl::children)))))
(defmethod sax:unparsed-entity-declaration
((handler dom-builder) name public-id system-id notation-name)
(dom:set-named-item (dom:entities (dom:doctype (document handler)))
(make-instance 'dom-impl::entity
:owner (document handler)
:name name
:public-id public-id
:system-id system-id
:notation-name notation-name)))
(defmethod sax:notation-declaration
((handler dom-builder) name public-id system-id)
(dom:set-named-item (dom:notations (dom:doctype (document handler)))
(make-instance 'dom-impl::notation
:owner (document handler)
:name name
:public-id public-id
:system-id system-id)))

983
dom/dom-impl.lisp Normal file
View File

@ -0,0 +1,983 @@
(defpackage :dom-impl
(:use :cl :runes))
(in-package :dom-impl)
;; Classes
(define-condition dom-exception (error)
((key :initarg :key :reader dom-exception-key)
(string :initarg :string :reader dom-exception-string)
(arguments :initarg :arguments :reader dom-exception-arguments))
(:report
(lambda (c s)
(format s "~A (~D):~%~?"
(dom-exception-key c)
(dom:code c)
(dom-exception-string c)
(dom-exception-arguments c)))))
(defclass node ()
((parent :initarg :parent :initform nil)
(children :initarg :children :initform (make-node-list))
(owner :initarg :owner :initform nil)
(read-only-p :initform nil :reader read-only-p)
(map :initform nil)))
(defclass document (node)
((doc-type :initarg :doc-type :reader dom:doctype)
(entities :initform nil :reader entities)))
(defclass document-fragment (node)
())
(defclass character-data (node)
((value :initarg :data :reader dom:data)))
(defclass attribute (node)
((name :initarg :name :reader dom:name)
(specified-p :initarg :specified-p :reader dom:specified)))
(defmethod print-object ((object attribute) stream)
(print-unreadable-object (object stream :type t :identity t)
(format stream "~A=~S"
(rod-string (dom:name object))
(rod-string (dom:value object)))))
(defclass element (node)
((tag-name :initarg :tag-name :reader dom:tag-name)
(attributes :initarg :attributes :reader dom:attributes)))
(defmethod print-object ((object element) stream)
(print-unreadable-object (object stream :type t :identity t)
(princ (rod-string (dom:tag-name object)) stream)))
(defclass text (character-data)
())
(defclass comment (character-data)
())
(defclass cdata-section (text)
())
(defclass document-type (node)
((name :initarg :name :reader dom:name)
(entities :initarg :entities :reader dom:entities)
(notations :initarg :notations :reader dom:notations)))
(defclass notation (node)
((name :initarg :name :reader dom:name)
(public-id :initarg :public-id :reader dom:public-id)
(system-id :initarg :system-id :reader dom:system-id)))
(defclass entity (node)
((name :initarg :name :reader dom:name)
(public-id :initarg :public-id :reader dom:public-id)
(system-id :initarg :system-id :reader dom:system-id)
(notation-name :initarg :notation-name :reader dom:notation-name)))
(defclass entity-reference (node)
((name :initarg :name :reader dom:name)))
(defclass processing-instruction (node)
((target :initarg :target :reader dom:target)
(data :initarg :data :reader dom:data)))
(defclass named-node-map ()
((items :initarg :items :reader dom:items
:initform nil)
(owner :initarg :owner :reader dom:owner-document)
(read-only-p :initform nil :reader read-only-p)
(element-type :initarg :element-type)))
;;; Implementation
(defun assert-writeable (node)
(when (read-only-p node)
(dom-error :NO_MODIFICATION_ALLOWED_ERR "~S is marked read-only." node)))
(defun dom:map-node-list (fn nodelist)
(dotimes (i (dom:length nodelist))
(funcall fn (dom:item nodelist i))))
(defmacro dom:do-node-list ((var nodelist &optional resultform) &body body)
`(block nil
(dom:map-node-list (lambda (,var) ,@body) ,nodelist)
,resultform))
(defmacro dovector ((var vector &optional resultform) &body body)
`(loop
for ,var across ,vector do (progn ,@body)
,@(when resultform `(finally (return ,resultform)))))
(defun move (from to from-start to-start length)
;; like (setf (subseq to to-start (+ to-start length))
;; (subseq from from-start (+ from-start length)))
;; but without creating the garbage
(if (< to-start from-start)
(loop
repeat length
for i from from-start
for j from to-start
do (setf (elt to j) (elt from i)))
(loop
repeat length
for i from (+ from-start length -1) by -1
for j from (+ to-start length -1) by -1
do (setf (elt to j) (elt from i)))))
(defun adjust-vector-exponentially (vector new-dimension set-fill-pointer-p)
(let ((d (array-dimension vector 0)))
(when (< d new-dimension)
(loop
do (setf d (* 2 d))
while (< d new-dimension))
(adjust-array vector d))
(when set-fill-pointer-p
(setf (fill-pointer vector) new-dimension))))
(defun make-space (vector &optional (n 1))
(adjust-vector-exponentially vector (+ (length vector) n) nil))
(defun extension (vector)
(max (array-dimension vector 0) 1))
;; dom-exception
(defun dom-error (key fmt &rest args)
(error 'dom-exception :key key :string fmt :arguments args))
(defmethod dom:code ((self dom-exception))
(ecase (dom-exception-key self)
(:INDEX_SIZE_ERR 1)
(:DOMSTRING_SIZE_ERR 2)
(:HIERARCHY_REQUEST_ERR 3)
(:WRONG_DOCUMENT_ERR 4)
(:INVALID_CHARACTER_ERR 5)
(:NO_DATA_ALLOWED_ERR 6)
(:NO_MODIFICATION_ALLOWED_ERR 7)
(:NOT_FOUND_ERR 8)
(:NOT_SUPPORTED_ERR 9)
(:INUSE_ATTRIBUTE_ERR 10)))
;; document-fragment protocol
;; document protocol
(defmethod dom:implementation ((document document))
'implementation)
(defmethod dom:document-element ((document document))
(dovector (k (dom:child-nodes document))
(cond ((typep k 'element)
(return k)))))
(defmethod dom:create-element ((document document) tag-name)
(setf tag-name (rod tag-name))
(unless (xml::valid-name-p tag-name)
(dom-error :INVALID_CHARACTER_ERR "not a name: ~A" (rod-string tag-name)))
(make-instance 'element
:tag-name tag-name
:owner document
:attributes (make-instance 'named-node-map
:element-type :attribute
:owner document)))
(defmethod dom:create-document-fragment ((document document))
(make-instance 'document-fragment
:owner document))
(defmethod dom:create-text-node ((document document) data)
(setf data (rod data))
(make-instance 'text
:data data
:owner document))
(defmethod dom:create-comment ((document document) data)
(setf data (rod data))
(make-instance 'comment
:data data
:owner document))
(defmethod dom:create-cdata-section ((document document) data)
(setf data (rod data))
(make-instance 'cdata-section
:data data
:owner document))
(defmethod dom:create-processing-instruction ((document document) target data)
(setf target (rod target))
(setf data (rod data))
(unless (xml::valid-name-p target)
(dom-error :INVALID_CHARACTER_ERR "not a name: ~A" (rod-string target)))
(make-instance 'processing-instruction
:owner document
:target target
:data data))
(defmethod dom:create-attribute ((document document) name)
(setf name (rod name))
(unless (xml::valid-name-p name)
(dom-error :INVALID_CHARACTER_ERR "not a name: ~A" (rod-string name)))
(make-instance 'attribute
:name name
:specified-p t
:owner document))
(defmethod dom:create-entity-reference ((document document) name)
(setf name (rod name))
(unless (xml::valid-name-p name)
(dom-error :INVALID_CHARACTER_ERR "not a name: ~A" (rod-string name)))
(make-instance 'entity-reference
:name name
:owner document))
(defmethod get-elements-by-tag-name-internal (node tag-name)
(setf tag-name (rod tag-name))
(let ((result (make-node-list)))
(setf tag-name (rod tag-name))
(let ((wild-p (rod= tag-name '#.(string-rod "*"))))
(labels ((walk (n)
(dovector (c (dom:child-nodes n))
(when (dom:element-p c)
(when (or wild-p (rod= tag-name (dom:node-name c)))
(vector-push-extend c result (extension result)))
(walk c)))))
(walk node)))
result))
(defmethod dom:get-elements-by-tag-name ((document document) tag-name)
(get-elements-by-tag-name-internal document tag-name))
;;; Node
(defmethod dom:parent-node ((node node))
(slot-value node 'parent))
(defmethod dom:child-nodes ((node node))
(slot-value node 'children))
(defmethod dom:first-child ((node node))
(dom:item (slot-value node 'children) 0))
(defmethod dom:last-child ((node node))
(with-slots (children) node
(if (plusp (length children))
(elt children (1- (length children)))
nil)))
(defmethod dom:previous-sibling ((node node))
(with-slots (parent) node
(when parent
(with-slots (children) parent
(let ((index (1- (position node children))))
(if (eql index -1)
nil
(elt children index)))))))
(defmethod dom:next-sibling ((node node))
(with-slots (parent) node
(when parent
(with-slots (children) parent
(let ((index (1+ (position node children))))
(if (eql index (length children))
nil
(elt children index)))))))
(defmethod dom:owner-document ((node node))
(slot-value node 'owner))
(defun ensure-valid-insertion-request (node new-child)
(assert-writeable node)
(unless (can-adopt-p node new-child)
(dom-error :HIERARCHY_REQUEST_ERR "~S cannot adopt ~S." node new-child))
#+(or) ;XXX needs to be moved elsewhere
(when (eq (dom:node-type node) :document)
(let ((child-type (dom:node-type new-child)))
(when (and (member child-type '(:element :document-type))
(find child-type (dom:child-nodes node) :key #'dom:node-type))
(dom-error :HIERARCHY_REQUEST_ERR
"~S cannot adopt a second child of type ~S."
node child-type))))
(unless (eq (if (eq (dom:node-type node) :document)
node
(dom:owner-document node))
(dom:owner-document new-child))
(dom-error :WRONG_DOCUMENT_ERR
"~S cannot adopt ~S, since it was created by a different document."
node new-child))
(do ((n node (dom:parent-node n)))
((null n))
(when (eq n new-child)
(dom-error :HIERARCHY_REQUEST_ERR
"~S cannot adopt ~S, since that would create a cycle"
node new-child)))
(unless (null (slot-value new-child 'parent))
(dom:remove-child (slot-value new-child 'parent) new-child)))
(defmethod dom:insert-before ((node node) (new-child node) ref-child)
(ensure-valid-insertion-request node new-child)
(with-slots (children) node
(if ref-child
(let ((i (position ref-child children)))
(unless i
(dom-error :NOT_FOUND_ERR "~S is no child of ~S." ref-child node))
(make-space children 1)
(move children children i (1+ i) (- (length children) i))
(incf (fill-pointer children))
(setf (elt children i) new-child))
(vector-push-extend new-child children (extension children)))
(setf (slot-value new-child 'parent) node)
new-child))
(defmethod dom:insert-before ((node node) (fragment document-fragment) ref-child)
(dovector (child (dom:child-nodes fragment))
(dom:insert-before node child ref-child))
fragment)
(defmethod dom:replace-child ((node node) (new-child node) (old-child node))
(ensure-valid-insertion-request node new-child)
(with-slots (children) node
(let ((i (position old-child children)))
(unless i
(dom-error :NOT_FOUND_ERR "~S is no child of ~S." old-child node))
(setf (elt children i) new-child))
(setf (slot-value new-child 'parent) node)
(setf (slot-value old-child 'parent) nil)
old-child))
(defmethod dom:replace-child
((node node) (new-child document-fragment) (old-child node))
(dom:insert-before node new-child old-child)
(dom:remove-child node old-child))
(defmethod dom:remove-child ((node node) (old-child node))
(assert-writeable node)
(with-slots (children) node
(let ((i (position old-child children)))
(unless i
(dom-error :NOT_FOUND_ERR "~A not found in ~A" old-child node))
(move children children (1+ i) i (- (length children) i 1))
(decf (fill-pointer children)))
(setf (slot-value old-child 'parent) nil)
old-child))
(defmethod dom:append-child ((node node) (new-child node))
(ensure-valid-insertion-request node new-child)
(with-slots (children) node
(vector-push-extend new-child children (extension children))
(setf (slot-value new-child 'parent) node)
new-child))
(defmethod dom:has-child-nodes ((node node))
(plusp (length (slot-value node 'children))))
(defmethod dom:append-child ((node node) (new-child document-fragment))
(assert-writeable node)
(dovector (child (dom:child-nodes new-child))
(dom:append-child node child))
new-child)
;; was auf node noch implemetiert werden muss:
;; - node-type
;; - can-adopt-p
;; - ggf attributes
;; - node-name
;; - node-value
;; node-name
(defmethod dom:node-name ((self document))
'#.(string-rod "#document"))
(defmethod dom:node-name ((self document-fragment))
'#.(string-rod "#document-fragment"))
(defmethod dom:node-name ((self text))
'#.(string-rod "#text"))
(defmethod dom:node-name ((self cdata-section))
'#.(string-rod "#cdata-section"))
(defmethod dom:node-name ((self comment))
'#.(string-rod "#comment"))
(defmethod dom:node-name ((self attribute))
(dom:name self))
(defmethod dom:node-name ((self element))
(dom:tag-name self))
(defmethod dom:node-name ((self document-type))
(dom:name self))
(defmethod dom:node-name ((self notation))
(dom:name self))
(defmethod dom:node-name ((self entity))
(dom:name self))
(defmethod dom:node-name ((self entity-reference))
(dom:name self))
(defmethod dom:node-name ((self processing-instruction))
(dom:target self))
;; node-type
(defmethod dom:node-type ((self document)) :document)
(defmethod dom:node-type ((self document-fragment)) :document-fragment)
(defmethod dom:node-type ((self text)) :text)
(defmethod dom:node-type ((self comment)) :comment)
(defmethod dom:node-type ((self cdata-section)) :cdata-section)
(defmethod dom:node-type ((self attribute)) :attribute)
(defmethod dom:node-type ((self element)) :element)
(defmethod dom:node-type ((self document-type)) :document-type)
(defmethod dom:node-type ((self notation)) :notation)
(defmethod dom:node-type ((self entity)) :entity)
(defmethod dom:node-type ((self entity-reference)) :entity-reference)
(defmethod dom:node-type ((self processing-instruction)) :processing-instruction)
;; node-value
(defmethod dom:node-value ((self document)) nil)
(defmethod dom:node-value ((self document-fragment)) nil)
(defmethod dom:node-value ((self character-data)) (dom:data self))
(defmethod dom:node-value ((self attribute)) (dom:value self))
(defmethod dom:node-value ((self element)) nil)
(defmethod dom:node-value ((self document-type)) nil)
(defmethod dom:node-value ((self notation)) nil)
(defmethod dom:node-value ((self entity)) nil)
(defmethod dom:node-value ((self entity-reference)) nil)
(defmethod dom:node-value ((self processing-instruction)) (dom:data self))
;; (setf node-value), first the meaningful cases...
(defmethod (setf dom:node-value) (newval (self character-data))
(assert-writeable self)
(setf (dom:data self) newval))
(defmethod (setf dom:node-value) (newval (self attribute))
(assert-writeable self)
(setf (dom:value self) newval))
(defmethod (setf dom:node-value) (newval (self processing-instruction))
(assert-writeable self)
(setf (dom:data self) newval))
;; ... and (setf node-value), part II. The DOM Level 1 spec fails to explain
;; this case, but it is covered by the (Level 1) test suite and clarified
;; in Level 2:
;; nodeValue of type DOMString
;; The value of this node, depending on its type; see the
;; table above. When it is defined to be null, setting
;; it has no effect.
(defmethod (setf dom:node-value) (newval (self element))
(declare (ignore newval)))
(defmethod (setf dom:node-value) (newval (self entity-reference))
(declare (ignore newval)))
(defmethod (setf dom:node-value) (newval (self entity))
(declare (ignore newval)))
(defmethod (setf dom:node-value) (newval (self document))
(declare (ignore newval)))
(defmethod (setf dom:node-value) (newval (self document-type))
(declare (ignore newval)))
(defmethod (setf dom:node-value) (newval (self document-fragment))
(declare (ignore newval)))
(defmethod (setf dom:node-value) (newval (self notation))
(declare (ignore newval)))
;; attributes
;; (gibt es nur auf element)
(defmethod dom:attributes ((self node))
nil)
;; dann fehlt noch can-adopt und attribute conventions fuer adoption
;;; NodeList
(defun make-node-list (&optional initial-contents)
(make-array (length initial-contents)
:adjustable t
:fill-pointer (length initial-contents)
:initial-contents initial-contents))
(defmethod dom:item ((self vector) index)
(if (< index (length self))
(elt self index)
nil))
(defmethod dom:length ((self vector))
(length self))
;;; NAMED-NODE-MAP
(defmethod dom:get-named-item ((self named-node-map) name)
(setf name (rod name))
(with-slots (items) self
(dolist (k items nil)
(cond ((rod= name (dom:node-name k))
(return k))))))
(defmethod dom:set-named-item ((self named-node-map) arg)
(assert-writeable self)
(unless (eq (dom:node-type arg) (slot-value self 'element-type))
(dom-error :HIERARCHY_REQUEST_ERR
"~S cannot adopt ~S, since it is not of type ~S."
self arg (slot-value self 'element-type)))
(unless (eq (dom:owner-document self) (dom:owner-document arg))
(dom-error :WRONG_DOCUMENT_ERR
"~S cannot adopt ~S, since it was created by a different document."
self arg))
(let ((old-map (slot-value arg 'map)))
(when (and old-map (not (eq old-map self)))
(dom-error :INUSE_ATTRIBUTE_ERR "Attribute node already mapped" arg)))
(setf (slot-value arg 'map) self)
(let ((name (dom:node-name arg)))
(with-slots (items) self
(dolist (k items (progn (setf items (cons arg items))nil))
(cond ((rod= name (dom:node-name k))
(setf items (cons arg (delete k items)))
(return k)))))))
(defmethod dom:remove-named-item ((self named-node-map) name)
(assert-writeable self)
(setf name (rod name))
(with-slots (items) self
(dolist (k items (dom-error :NOT_FOUND_ERR "~A not found in ~A" name self))
(cond ((rod= name (dom:node-name k))
(setf items (delete k items))
(return k))))))
(defmethod dom:length ((self named-node-map))
(with-slots (items) self
(length items)))
(defmethod dom:item ((self named-node-map) index)
(with-slots (items) self
(do ((nthcdr items (cdr nthcdr))
(i index (1- i)))
((zerop i) (car nthcdr)))))
;;; CHARACTER-DATA
(defmethod (setf dom:data) (newval (self character-data))
(assert-writeable self)
(setf newval (rod newval))
(setf (slot-value self 'value) newval))
(defmethod dom:length ((node character-data))
(length (slot-value node 'value)))
(defmethod dom:substring-data ((node character-data) offset count)
(with-slots (value) node
(unless (<= 0 offset (length value))
(dom-error :INDEX_SIZE_ERR "offset is invalid"))
(let ((end (min (length value) (+ offset count))))
(subseq value offset end))))
(defmethod dom:append-data ((node character-data) arg)
(assert-writeable node)
(setq arg (rod arg))
(with-slots (value) node
(setf value (concatenate (type-of value) value arg)))
(values))
(defmethod dom:delete-data ((node character-data) offset count)
(assert-writeable node)
(with-slots (value) node
(unless (<= 0 offset (length value))
(dom-error :INDEX_SIZE_ERR "offset is invalid"))
(when (minusp count)
(dom-error :INDEX_SIZE_ERR "count is negative"))
(setf count (min count (- (length value) offset)))
(let ((new (make-array (- (length value) count)
:element-type (array-element-type value))))
(replace new value
:start1 0 :end1 offset
:start2 0 :end2 offset)
(replace new value
:start1 offset :end1 (length new)
:start2 (+ offset count) :end2 (length value))
(setf value new)))
(values))
(defmethod dom:replace-data ((node character-data) offset count arg)
;; Although we could implement this by calling DELETE-DATA, then INSERT-DATA,
;; we implement this function directly to avoid creating temporary garbage.
(assert-writeable node)
(setf arg (rod arg))
(with-slots (value) node
(unless (<= 0 offset (length value))
(dom-error :INDEX_SIZE_ERR "offset is invalid"))
(when (minusp count)
(dom-error :INDEX_SIZE_ERR "count is negative"))
(setf count (min count (- (length value) offset)))
(if (= count (length arg))
(replace value arg
:start1 offset :end1 (+ offset count)
:start2 0 :end2 count)
(let ((new (make-array (+ (length value) (length arg) (- count))
:element-type (array-element-type value))))
(replace new value :end1 offset)
(replace new arg :start1 offset)
(replace new value
:start1 (+ offset (length arg))
:start2 (+ offset count))
(setf value new))))
(values))
(defmethod dom:insert-data ((node character-data) offset arg)
(assert-writeable node)
(setf arg (rod arg))
(with-slots (value) node
(unless (<= 0 offset (length value))
(dom-error :INDEX_SIZE_ERR "offset is invalid"))
(let ((new (make-array (+ (length value) (length arg))
:element-type (array-element-type value)))
(arglen (length arg)))
(replace new value :end1 offset)
(replace new arg :start1 offset)
(replace new value :start1 (+ offset arglen) :start2 offset)
(setf value new)))
(values))
;;; ATTR
;;;
;;; An attribute value can be read and set as a string using DOM:VALUE
;;; or frobbed by changing the attribute's children!
;;;
;;; We store the value in a TEXT node and read this node's DATA slot
;;; when asked for our VALUE -- until the user changes the child nodes,
;;; in which case we have to compute VALUE by traversing the children.
(defmethod dom:value ((node attribute))
(with-slots (children) node
(cond
((zerop (length children))
#.(rod-string ""))
((and (eql (length children) 1)
(eq (dom:node-type (elt children 0)) :text))
;; we have as single TEXT-NODE child, just return its DATA
(dom:data (elt children 0)))
(t
;; traverse children to compute value
(attribute-to-string node)))))
(defmethod (setf dom:value) (new-value (node attribute))
(assert-writeable node)
(let ((rod (rod new-value)))
(with-slots (children owner) node
;; remove children, add new TEXT-NODE child
;; (alas, we must not reuse an old TEXT-NODE)
(while (plusp (length children))
(dom:remove-child node (dom:last-child node)))
(dom:append-child node (dom:create-text-node owner rod))))
new-value)
(defun attribute-to-string (attribute)
(let ((stream (make-rod-stream)))
(flet ((doit ()
(dovector (child (dom:child-nodes attribute))
(write-attribute-child child stream))))
(doit)
(initialize-rod-stream stream)
(doit))
(rod-stream-buf stream)))
(defmethod write-attribute-child ((node node) stream)
(write-rod (dom:node-value node) stream))
(defmethod write-attribute-child ((node entity-reference) stream)
(dovector (child (dom:child-nodes node))
(write-attribute-child child stream)))
;;; ROD-STREAM als Ersatz fuer MAKE-STRING-OUTPUT-STREAM zu verwenden,
;;; nur dass der Buffer statische Groesse hat. Solange er NIL ist,
;;; zaehlt der Stream nur die Runen. Dann ruft man INITIALIZE-ROD-STREAM
;;; auf, um den Buffer zu erzeugen und die Position zurueckzusetzen, und
;;; schreibt alles abermals. Dann ist der Buffer gefuellt.
(defstruct rod-stream
(buf nil)
(position 0))
(defun write-rod (rod rod-stream)
(let ((buf (rod-stream-buf rod-stream)))
(when buf
(move rod buf 0 (rod-stream-position rod-stream) (length rod)))
(incf (rod-stream-position rod-stream) (length rod)))
rod)
(defun initialize-rod-stream (stream)
(setf (rod-stream-buf stream) (make-rod (rod-stream-position stream)))
(setf (rod-stream-position stream) 0)
stream)
;;; ELEMENT
(defmethod dom:get-attribute-node ((element element) name)
(dom:get-named-item (dom:attributes element) name))
(defmethod dom:set-attribute-node ((element element) (new-attr attribute))
(assert-writeable element)
(dom:set-named-item (dom:attributes element) new-attr))
(defmethod dom:get-attribute ((element element) name)
(let ((a (dom:get-attribute-node element name)))
(if a
(dom:value a)
#.(string-rod ""))))
(defmethod dom:set-attribute ((element element) name value)
(assert-writeable element)
(with-slots (owner) element
(let ((attr (dom:create-attribute owner name)))
(setf (dom:value attr) value)
(dom:set-attribute-node element attr))
(values)))
(defmethod dom:remove-attribute ((element element) name)
(assert-writeable element)
(dom:remove-attribute-node element (dom:get-attribute-node element name)))
(defmethod dom:remove-attribute-node ((element element) (old-attr attribute))
(assert-writeable element)
(with-slots (items) (dom:attributes element)
(unless (find old-attr items)
(dom-error :NOT_FOUND_ERR "Attribute not found."))
(setf items (remove old-attr items))
old-attr))
(defmethod dom:get-elements-by-tag-name ((element element) name)
(assert-writeable element)
(get-elements-by-tag-name-internal element name))
(defmethod dom:normalize ((element element))
(assert-writeable element)
(labels ((walk (n)
(when (eq (dom:node-type n) :element)
(map nil #'walk (dom:items (dom:attributes n))))
(let ((children (dom:child-nodes n))
(i 0)
(previous nil))
;; careful here, we're modifying the array we are iterating over
(while (< i (length children))
(let ((child (elt children i)))
(cond
((not (eq (dom:node-type child) :text))
(setf previous nil)
(incf i))
((and previous (eq (dom:node-type previous) :text))
(setf (slot-value previous 'value)
(concatenate 'vector
(dom:data previous)
(dom:data child)))
(dom:remove-child n child)
;; not (incf i)
)
(t
(setf previous child)
(incf i))))))
(map nil #'walk (dom:child-nodes n))))
(walk element))
(values))
;;; TEXT
(defmethod dom:split-text ((text text) offset)
(assert-writeable text)
(with-slots (owner parent value) text
(unless (<= 0 offset (length value))
(dom-error :INDEX_SIZE_ERR "offset is invalid"))
(prog1
(dom:insert-before parent
(dom:create-text-node owner (subseq value offset))
(dom:next-sibling text))
(setf value (subseq value 0 offset)))))
;;; COMMENT -- nix
;;; CDATA-SECTION -- nix
;;; DOCUMENT-TYPE -- missing
;;; NOTATION -- nix
;;; ENTITY -- nix
;;; ENTITY-REFERENCE
(defmethod initialize-instance :after ((instance entity-reference) &key)
(let* ((owner (dom:owner-document instance))
(entities (or (entities owner) xml::*entities*))
(children (xml::resolve-entity (dom:name instance) entities)))
(setf (slot-value instance 'children)
(make-node-list
(map 'vector
(lambda (node) (dom:import-node owner node t))
children))))
(labels ((walk (n)
(setf (slot-value n 'read-only-p) t)
(when (dom:element-p n)
(map nil #'walk (dom:items (dom:attributes n))))
(map nil #'walk (dom:child-nodes n))))
(walk instance)))
;;; PROCESSING-INSTRUCTION
(defmethod (setf dom:data) (newval (self processing-instruction))
(assert-writeable self)
(setf newval (rod newval))
(setf (slot-value self 'data) newval))
;; das koennte man auch mit einer GF machen
(defun can-adopt-p (parent child)
(member (dom:node-type child)
(let ((default '(:element :processing-instruction :comment :text
:cdata-section :entity-reference)))
(etypecase parent
(document
'(:element :processing-instruction :comment :document-type))
(document-fragment default)
(document-type nil)
(entity-reference default)
(element default)
(attribute '(:text :entity-reference))
(processing-instruction nil)
(comment nil)
(text nil)
(cdata-section nil)
(entity default)
(notation nil)))))
;;; predicates
(defmethod dom:node-p ((object node)) t)
(defmethod dom:node-p ((object t)) nil)
(defmethod dom:document-p ((object document)) t)
(defmethod dom:document-p ((object t)) nil)
(defmethod dom:document-fragment-p ((object document-fragment)) t)
(defmethod dom:document-fragment-p ((object t)) nil)
(defmethod dom:character-data-p ((object character-data)) t)
(defmethod dom:character-data-p ((object t)) nil)
(defmethod dom:attribute-p ((object attribute)) t)
(defmethod dom:attribute-p ((object t)) nil)
(defmethod dom:element-p ((object element)) t)
(defmethod dom:element-p ((object t)) nil)
(defmethod dom:text-node-p ((object text)) t)
(defmethod dom:text-node-p ((object t)) nil)
(defmethod dom:comment-p ((object comment)) t)
(defmethod dom:comment-p ((object t)) nil)
(defmethod dom:cdata-section-p ((object cdata-section)) t)
(defmethod dom:cdata-section-p ((object t)) nil)
(defmethod dom:document-type-p ((object document-type)) t)
(defmethod dom:document-type-p ((object t)) nil)
(defmethod dom:notation-p ((object notation)) t)
(defmethod dom:notation-p ((object t)) nil)
(defmethod dom:entity-p ((object entity)) t)
(defmethod dom:entity-p ((object t)) nil)
(defmethod dom:entity-reference-p ((object entity-reference)) t)
(defmethod dom:entity-reference-p ((object t)) nil)
(defmethod dom:processing-instruction-p ((object processing-instruction)) t)
(defmethod dom:processing-instruction-p ((object t)) nil)
(defmethod dom:named-node-map-p ((object named-node-map)) t)
(defmethod dom:named-node-map-p ((object t)) nil)
;;; IMPORT-NODE
(defvar *clone-not-import* nil) ;not beautiful, I know. See below.
(defmethod import-node-internal (class document node deep &rest initargs)
(let ((result (apply #'make-instance class :owner document initargs)))
(when deep
(dovector (child (dom:child-nodes node))
(dom:append-child result (dom:import-node document child t))))
result))
(defmethod dom:import-node ((document document) (node attribute) deep)
(declare (ignore deep))
(import-node-internal 'attribute document node t :name (dom:name node)))
(defmethod dom:import-node ((document document) (node document-fragment) deep)
(import-node-internal 'document-fragment document node deep))
(defmethod dom:import-node ((document document) (node element) deep)
(let* ((attributes (make-instance 'named-node-map
:element-type :attribute
:owner document))
(result (import-node-internal 'element document node deep
:attributes attributes
:tag-name (dom:tag-name node))))
(dolist (attribute (dom:items (dom:attributes node)))
(when (or (dom:specified attribute) *clone-not-import*)
(dom:set-attribute result (dom:name attribute) (dom:value attribute))))
result))
(defmethod dom:import-node ((document document) (node entity) deep)
(import-node-internal 'entity document node deep
:public-id (dom:public-id node)
:system-id (dom:system-id node)
:notation-name (dom:notation-name node)))
(defmethod dom:import-node ((document document) (node entity-reference) deep)
(declare (ignore deep))
#+(or)
(import-node-internal 'entity-reference document node nil
:name (dom:name node))
;; XXX If the document being imported into provides a definition for
;; this entity name, its value is assigned.
(dom-error :NOT_SUPPORTED_ERR "not implemented"))
(defmethod dom:import-node ((document document) (node notation) deep)
(import-node-internal 'notation document node deep
:name (dom:name node)
:public-id (dom:public-id node)
:system-id (dom:system-id node)))
(defmethod dom:import-node
((document document) (node processing-instruction) deep)
(import-node-internal 'processing-instruction document node deep
:target (dom:target node)
:data (dom:data node)))
;; TEXT_NODE, CDATA_SECTION_NODE, COMMENT_NODE
(defmethod dom:import-node
((document document) (node character-data) deep)
(import-node-internal (class-of node) document node deep
:data (copy-seq (dom:data node))))
;;; CLONE-NODE
;;;
;;; As far as I can tell, cloneNode is the same as importNode, except
;;; for one difference involving element attributes: importNode imports
;;; only specified attributes, cloneNode copies even default values.
;;;
;;; Since I don't want to reimplement all of importNode here, we run
;;; importNode with a special flag...
(defmethod dom:clone-node ((node node) deep)
(let ((*clone-not-import* t))
(dom:import-node (dom:owner-document node) node deep)))

60
dom/dom-sax.lisp Normal file
View File

@ -0,0 +1,60 @@
(in-package :dom-impl)
(defun dom:map-document
(handler document
&key (include-xmlns-attributes sax:*include-xmlns-attributes*)
include-default-values)
(sax:start-document handler)
(let ((doctype (dom:doctype document)))
(when doctype
(sax:start-dtd handler (dom:name doctype) nil nil)
(let ((ns (dom:notations doctype)))
(dotimes (k (dom:length ns))
(let ((n (dom:item ns k)))
(sax:notation-declaration handler
(dom:name n)
(dom:public-id n)
(dom:system-id n)))
;; fixme: entities!
)
(sax:end-dtd handler))))
(labels ((walk (node)
(dom:do-node-list (child (dom:child-nodes node))
(ecase (dom:node-type child)
(:element
;; fixme: namespaces
(let ((attlist
(compute-attributes child
include-xmlns-attributes
include-default-values))
(lname (dom:tag-name child))
(qname (dom:tag-name child)))
(sax:start-element handler nil lname qname attlist)
(walk child)
(sax:end-element handler nil lname qname)))
(:cdata-section
(sax:start-cdata handler)
(sax:characters handler (dom:data child))
(sax:end-cdata handler))
(:text
(sax:characters handler (dom:data child)))
(:comment
(sax:comment handler (dom:data child)))
(:processing-instruction
(sax:processing-instruction handler
(dom:target child)
(dom:data child)))))))
(walk document))
(sax:end-document handler))
(defun compute-attributes (element xmlnsp defaultp)
(let ((results '()))
(dom:do-node-list (a (dom:attributes element))
(when (and (or defaultp (dom:specified a))
(or xmlnsp (not (cxml::xmlns-attr-p (dom:name a)))))
(push
(cxml::make-attribute :qname (dom:name a)
:value (dom:value a)
:specified-p (dom:specified a))
results)))
(reverse results)))

111
dom/package.lisp Normal file
View File

@ -0,0 +1,111 @@
(in-package :cl-user)
(defpackage :dom
(:use)
(:export
;; lisp-specific extensions
#:make-dom-builder
;; methods
#:has-feature
#:doctype
#:implementation
#:document-element
#:create-element
#:create-document-fragment
#:create-text-node
#:create-comment
#:create-cdata-section
#:create-processing-instruction
#:create-attribute
#:create-entity-reference
#:get-elements-by-tag-name
#:node-name
#:node-value
#:node-type
#:parent-node
#:child-nodes
#:first-child
#:last-child
#:previous-sibling
#:next-sibling
#:attributes
#:owner-document
#:insert-before
#:replace-child
#:remove-child
#:append-child
#:has-child-nodes
#:clone-node
#:item
#:length
#:get-named-item
#:set-named-item
#:remove-named-item
#:data
#:substring-data
#:append-data
#:insert-data
#:delete-data
#:replace-data
#:name
#:specified
#:value
#:tag-name
#:get-attribute
#:set-attribute
#:remove-attribute
#:get-attribute-node
#:set-attribute-node
#:remove-attribute-node
#:normalize
#:split-text
#:entities
#:notations
#:public-id
#:system-id
#:notation-name
#:target
#:import-node
#:code
;; protocol classes
#:dom-implementation
#:document-fragment
#:document
#:node
#:node-list
#:named-node-map
#:character-data
#:attr
#:element
#:text
#:comment
#:cdata-section
#:document-type
#:notation
#:entity
#:entity-reference
#:processing-instruction
;;
#:items
;;
#:node-p
#:document-p
#:document-fragment-p
#:character-data-p
#:attribute-p
#:element-p
#:text-node-p
#:comment-p
#:cdata-section-p
#:document-type-p
#:notation-p
#:entity-p
#:entity-reference-p
#:processing-instruction-p
#:named-node-map-p
#:map-node-list
#:do-node-list))

46
dom/simple-dom.lisp Normal file
View File

@ -0,0 +1,46 @@
(in-package :xml)
;;; Implementation of a simple but faster DOM.
(defclass simple-document ()
((children :initform nil :accessor simple-document-children)))
(defstruct node
parent)
(defstruct (processing-instruction (:include node))
target
data)
(defstruct (text (:include node)
(:constructor make-text-boa (parent data)))
data)
(defstruct (element (:include node))
gi
attributes
children)
(defmethod dom:create-processing-instruction ((document simple-document) target data)
(make-processing-instruction :target target :data data))
(defmethod dom:append-child ((node element) child)
(setf (node-parent child) node)
(push child (element-children node)))
(defmethod dom:append-child ((node simple-document) child)
(push child (simple-document-children node))
nil)
(defmethod dom:create-element ((document simple-document) name)
(make-element :gi name))
(defmethod dom:set-attribute ((node element) name value)
(push (cons name value)
(element-attributes node)))
(defmethod dom:create-text-node ((document simple-document) data)
(make-text-boa nil data))
(defmethod dom:create-cdata-section ((document simple-document) data)
(make-text-boa nil data))

66
dom/string-dom.lisp Normal file
View File

@ -0,0 +1,66 @@
;;; A wrapper package STRING-DOM around the ordinary DOM presents
;;; DOMString as Lisp STRING. This was a workaround until
;;; RUNE-IS-CHARACTER was implemented, but might still be useful on
;;; Lisps without Unicode support.
(defpackage :string-dom
(:use))
(eval-when (:compile-toplevel :load-toplevel :execute)
(do-external-symbols (var :dom)
(let* ((home-package
(if (member var '(dom:data dom:name dom:value dom:tag-name
dom:node-name dom:node-value
dom:substring-data dom:get-attribute
dom:set-attribute dom:public-id dom:system-id
dom:notation-name dom:target))
:string-dom
:dom))
(symbol (intern (symbol-name var) home-package)))
(import symbol :string-dom)
(export (list symbol) :string-dom))))
(defpackage :string-dom-impl (:use :cl))
(in-package :string-dom-impl)
(defun rod-to-string (frob)
(if (null frob)
nil
(map 'string #'code-char frob)))
(defun string-dom:data (node) (rod-to-string (dom:data node)))
(defun string-dom:name (node) (rod-to-string (dom:name node)))
(defun string-dom:value (node) (rod-to-string (dom:value node)))
(defun string-dom:tag-name (node) (rod-to-string (dom:tag-name node)))
(defun string-dom:node-name (node) (rod-to-string (dom:node-name node)))
(defun string-dom:node-value (node) (rod-to-string (dom:node-value node)))
(defun (setf string-dom:data) (newval node)
(setf (dom:data node) newval))
(defun (setf string-dom:value) (newval node)
(setf (dom:value node) newval))
(defun (setf string-dom:node-value) (newval node)
(setf (dom:node-value node) newval))
(defun string-dom:substring-data (node offset count)
(rod-to-string (dom:substring-data node offset count)))
(defun string-dom:get-attribute (elt name)
(rod-to-string (dom:get-attribute elt name)))
(defun string-dom:set-attribute (elt name value)
(dom:set-attribute elt (runes:rod name) (runes:rod value)))
(defun string-dom:public-id (node)
(rod-to-string (dom:public-id node)))
(defun string-dom:system-id (node)
(rod-to-string (dom:system-id node)))
(defun string-dom:notation-name (node)
(rod-to-string (dom:notation-name node)))
(defun string-dom:target (node)
(rod-to-string (dom:target node)))

9
dom/unparse.lisp Normal file
View File

@ -0,0 +1,9 @@
(in-package :cxml)
(defun unparse-document-to-octets (doc &rest initargs)
(let ((sink (apply #'make-octet-vector-sink initargs)))
(dom:map-document sink doc :include-default-values t)))
(defun unparse-document (doc character-stream &rest initargs)
(let ((sink (apply #'make-character-stream-sink character-stream initargs)))
(dom:map-document sink doc :include-default-values t)))

161
dom/xml-canonic.lisp Normal file
View File

@ -0,0 +1,161 @@
;;; -*- Mode: Lisp; Syntax: Common-Lisp; Package: XML; readtable: runes; Encoding: utf-8; -*-
;;; ---------------------------------------------------------------------------
;;; Title: Dump canonic XML according to J.Clark
;;; Created: 1999-09-09
;;; Author: Gilbert Baumann <unk6@rz.uni-karlsruhe.de>
;;; License: LGPL (See file COPYING for details).
;;; ---------------------------------------------------------------------------
;;; © copyright 1999 by Gilbert Baumann
;;; This library is free software; you can redistribute it and/or
;;; modify it under the terms of the GNU Library General Public
;;; License as published by the Free Software Foundation; either
;;; version 2 of the License, or (at your option) any later version.
;;;
;;; This library is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;; Library General Public License for more details.
;;;
;;; You should have received a copy of the GNU Library General Public
;;; License along with this library; if not, write to the
;;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;;; Boston, MA 02111-1307 USA.
(in-package :xml)
;;
;; | Canonical XML
;; | =============
;; |
;; | This document defines a subset of XML called canonical XML. The
;; | intended use of canonical XML is in testing XML processors, as a
;; | representation of the result of parsing an XML document.
;; |
;; | Every well-formed XML document has a unique structurally equivalent
;; | canonical XML document. Two structurally equivalent XML documents have
;; | a byte-for-byte identical canonical XML document. Canonicalizing an
;; | XML document requires only information that an XML processor is
;; | required to make available to an application.
;; |
;; | A canonical XML document conforms to the following grammar:
;; |
;; | CanonXML ::= Pi* element Pi*
;; | element ::= Stag (Datachar | Pi | element)* Etag
;; | Stag ::= '<' Name Atts '>'
;; | Etag ::= '</' Name '>'
;; | Pi ::= '<?' Name ' ' (((Char - S) Char*)? - (Char* '?>' Char*)) '?>'
;; | Atts ::= (' ' Name '=' '"' Datachar* '"')*
;; | Datachar ::= '&amp;' | '&lt;' | '&gt;' | '&quot;'
;; | | '&#9;'| '&#10;'| '&#13;'
;; | | (Char - ('&' | '<' | '>' | '"' | #x9 | #xA | #xD))
;; | Name ::= (see XML spec)
;; | Char ::= (see XML spec)
;; | S ::= (see XML spec)
;; |
;; | Attributes are in lexicographical order (in Unicode bit order).
;; |
;; | A canonical XML document is encoded in UTF-8.
;; |
;; | Ignorable white space is considered significant and is treated
;; | equivalently to data.
;;
;; -- James Clark (jjc@jclark.com)
(defvar *quux*) ;!!!BIG HACK!!!
(defun unparse-document (doc sink)
(map nil (rcurry #'unparse-node sink) (dom:child-nodes doc)))
(defun unparse-node (node sink)
(cond ((dom:element-p node)
(write-rune #/< sink)
(write-rod (dom:tag-name node) sink)
;; atts
(let ((atts (sort (copy-list (dom:items (dom:attributes node)))
#'rod< :key #'dom:name)))
(dolist (a atts)
(write-rune #/space sink)
(write-rod (dom:name a) sink)
(write-rune #/= sink)
(write-rune #/\" sink)
(let ((*quux* nil))
(map nil (lambda (c) (unparse-datachar c sink)) (dom:value a)))
(write-rune #/\" sink)))
(write-rod '#.(string-rod ">") sink)
(dom:do-node-list (k (dom:child-nodes node))
(unparse-node k sink))
(write-rod '#.(string-rod "</") sink)
(write-rod (dom:tag-name node) sink)
(write-rod '#.(string-rod ">") sink))
((dom:processing-instruction-p node)
(unless (rod-equal (dom:target node) '#.(string-rod "xml"))
(write-rod '#.(string-rod "<?") sink)
(write-rod (dom:target node) sink)
(write-rune #/space sink)
(write-rod (dom:data node) sink)
(write-rod '#.(string-rod "?>") sink) ))
((dom:text-node-p node)
(let ((*quux* nil))
(map nil (lambda (c) (unparse-datachar c sink))
(dom:data node))))
((dom:comment-p node))
(t
(error "Oops in unparse: ~S." node))))
(defun unparse-datachar (c sink)
(cond ((rune= c #/&) (write-rod '#.(string-rod "&amp;") sink))
((rune= c #/<) (write-rod '#.(string-rod "&lt;") sink))
((rune= c #/>) (write-rod '#.(string-rod "&gt;") sink))
((rune= c #/\") (write-rod '#.(string-rod "&quot;") sink))
((rune= c #/U+0009) (write-rod '#.(string-rod "&#9;") sink))
((rune= c #/U+000A) (write-rod '#.(string-rod "&#10;") sink))
((rune= c #/U+000D) (write-rod '#.(string-rod "&#13;") sink))
(t
(write-rune c sink))))
(defun write-rod (rod sink)
(let ((*quux* nil))
(map nil (lambda (c) (write-rune c sink)) rod)))
(defun write-rune (rune sink)
(let ((code (rune-code rune)))
(cond ((<= #xD800 code #xDBFF)
(setf *quux* code))
((<= #xDC00 code #xDFFF)
(let ((q (logior (ash (- *quux* #xD7C0) 10) (- code #xDC00))))
(write-rune-0 q sink))
(setf *quux* nil))
(t
(write-rune-0 code sink)))))
(defun write-rune-0 (code sink)
(labels ((wr (x)
(write-char (code-char x) sink)))
(cond ((<= #x00000000 code #x0000007F)
(wr code))
((<= #x00000080 code #x000007FF)
(wr (logior #b11000000 (ldb (byte 5 6) code)))
(wr (logior #b10000000 (ldb (byte 6 0) code))))
((<= #x00000800 code #x0000FFFF)
(wr (logior #b11100000 (ldb (byte 4 12) code)))
(wr (logior #b10000000 (ldb (byte 6 6) code)))
(wr (logior #b10000000 (ldb (byte 6 0) code))))
((<= #x00010000 code #x001FFFFF)
(wr (logior #b11110000 (ldb (byte 3 18) code)))
(wr (logior #b10000000 (ldb (byte 6 12) code)))
(wr (logior #b10000000 (ldb (byte 6 6) code)))
(wr (logior #b10000000 (ldb (byte 6 0) code))))
((<= #x00200000 code #x03FFFFFF)
(wr (logior #b11111000 (ldb (byte 2 24) code)))
(wr (logior #b10000000 (ldb (byte 6 18) code)))
(wr (logior #b10000000 (ldb (byte 6 12) code)))
(wr (logior #b10000000 (ldb (byte 6 6) code)))
(wr (logior #b10000000 (ldb (byte 6 0) code))))
((<= #x04000000 code #x7FFFFFFF)
(wr (logior #b11111100 (ldb (byte 1 30) code)))
(wr (logior #b10000000 (ldb (byte 6 24) code)))
(wr (logior #b10000000 (ldb (byte 6 18) code)))
(wr (logior #b10000000 (ldb (byte 6 12) code)))
(wr (logior #b10000000 (ldb (byte 6 6) code)))
(wr (logior #b10000000 (ldb (byte 6 0) code)))))))