dom-impl.lisp 18.4 KB
Newer Older
david's avatar
david committed
1
(defpackage :dom-impl
david's avatar
david committed
2 3
  (:use :glisp)
  (:alias (:cdom :dom)))
david's avatar
david committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136

(in-package :dom-impl)

;; Classes

(defclass node ()
  ((parent      :initarg :parent        :initform nil)
   (children    :initarg :children      :initform nil)
   (owner       :initarg :owner         :initform nil)))

(defclass document (node)
  ((doc-type    :initarg :doc-type     :reader dom:doctype)))

(defclass document-fragment (node)
  ())

(defclass character-data (node)
  ((data        :initarg :data          :reader dom:data)))

(defclass attribute (node)
  ((name        :initarg :name          :reader dom:name)
   (value       :initarg :value         :reader dom:value)
   (specified-p :initarg :specified-p   :reader dom:specified)))

(defclass element (node)
  ((tag-name    :initarg :tag-name      :reader dom:tag-name)
   (attributes  :initarg :attributes    :reader dom:attributes
                :initform (make-instance 'named-node-map))))

(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) ))


;;; Implementation

;; document-fragment protocol
;; document protocol

(defmethod dom:implementation ((document document))
  'implementation)

(defmethod dom:document-element ((document document))
  (dolist (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))
  (make-instance 'element 
    :tag-name tag-name
    :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))
  (make-instance 'processing-instruction
    :owner document
    :target target
    :data data))

(defmethod dom:create-attribute ((document document) name)
  (setf name (rod name))
  (make-instance 'attribute
    :name name
    :specified-p nil                    ;???
    :owner document))

(defmethod dom:create-entity-reference ((document document) name)
  (setf name (rod name))
  (make-instance 'entity-reference
    :name name
    :owner document))

(defmethod dom:get-elements-by-tag-name ((document document) tag-name)
  (setf tag-name (rod tag-name))
  (let ((result nil))
    (setf tag-name (rod tag-name))
    (let ((wild-p (rod= tag-name '#.(string-rod "*"))))
david's avatar
david committed
137 138 139 140
      (labels ((tag-name-eq (a b)
                 ;; XXX ist das richtig?  TAG-NAME-EQ war undefiniert
                 (rod= a b))
               (walk (n)
david's avatar
david committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
                 (when (and (dom:element-p n)
                            (or wild-p (tag-name-eq tag-name (dom:node-name n))))
                   (push n result))
                 (mapc #'walk (dom:child-nodes n))))
        (walk document)
        (reverse result)))))

;;; 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))
  (car (slot-value node 'children)))

(defmethod dom:last-child ((node node))
  (car (last (slot-value node 'children))))

(defmethod dom:previous-sibling ((node node))
  (with-slots (parent) node
    (when parent
      (with-slots (children) parent
        (do ((q children (cdr q)))
david's avatar
david committed
167
            ((null (cdr q)) nil)
david's avatar
david committed
168 169 170 171 172 173 174 175
          (cond ((eq (cadr q) node)
                 (return (car q)))))))))

(defmethod dom:next-sibling ((node node))
  (with-slots (parent) node
    (when parent
      (with-slots (children) parent
        (do ((q children (cdr q)))
david's avatar
david committed
176
            ((null (cdr q)) nil)
david's avatar
david committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
          (cond ((eq (car q) node)
                 (return (cadr q)))))))))

(defmethod dom:owner-document ((node node))
  (slot-value node 'owner))

(defun ensure-valid-insertion-request (node new-child)
  (unless (can-adopt-p node new-child)
    ;; HIERARCHY_REQUEST_ERR
    (error "~S cannot adopt ~S." node new-child))
  (unless (eq (dom:owner-document node) 
              (dom:owner-document new-child))
    ;; WRONG_DOCUMENT_ERR
    (error "~S cannot adopt ~S, since it was created by a different document."
           node new-child))
  (with-slots (children) node
    (unless (null (slot-value new-child 'parent))
      (cond ((eq (slot-value new-child 'parent)
                 node)
             ;; remove it first
             (setf children (delete new-child children)))
            (t
             ;; otherwise it is an error.
             ;; GB_INTEGRITY_ERR
             (error "~S is already adopted." new-child)))) ))

(defmethod dom:insert-before ((node node) (new-child node) (ref-child t))
  (ensure-valid-insertion-request node new-child)
  (with-slots (children) node
    (cond ((eq (car children) ref-child)
           (setf (slot-value new-child 'parent) node)
           (setf children (cons new-child children)))
          (t
           (do ((q children (cdr q)))
               ((null (cdr q))
                (cond ((null ref-child)
                       (setf (slot-value new-child 'parent) node)
                       (setf (cdr q) (cons new-child nil)))
                      (t
                       ;; NOT_FOUND_ERR
                       (error "~S is no child of ~S." ref-child node))))
             (cond ((eq (cadr q) ref-child)
                    (setf (slot-value new-child 'parent) node)
                    (setf (cdr q) (cons new-child (cdr q)))
                    (return))))))
    new-child))

(defmethod dom:insert-before ((node node) (fragment document-fragment) ref-child)
  (dolist (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
    (do ((q children (cdr q)))
        ((null q)
         ;; NOT_FOUND_ERR
         (error "~S is no child of ~S." old-child node))
      (cond ((eq (car q) old-child)
             (setf (car q) new-child)
             (setf (slot-value new-child 'parent) node)
             (setf (slot-value old-child 'parent) nil)
             (return))))
    old-child))

david's avatar
david committed
243
(defmethod dom:remove-child ((node node) (old-child node))
david's avatar
david committed
244
  (with-slots (children) node
david's avatar
david committed
245 246 247 248
    (setf children (remove old-child children))
    (setf (slot-value old-child 'parent) nil)
    old-child))

david's avatar
david committed
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
(defmethod dom:append-child ((node node) (new-child node))
  (ensure-valid-insertion-request node new-child)
  (with-slots (children) node
    (setf children (nconc children (list new-child)))
    (setf (slot-value new-child 'parent) node)
    new-child))

(defmethod dom:has-child-nodes ((node node))
  (not (null (slot-value node 'children))))

(defmethod dom:append-child ((node node) (new-child document-fragment))
  (dolist (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:name 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))

;; attributes

;; (gibt es nur auf element)

(defmethod dom:attributes ((self node))
  nil)

;; dann fehlt noch can-adopt und attribute conventions fuer adoption

;;; 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)
  (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)
  (setf name (rod name))
  (with-slots (items) self
    (dolist (k items nil)
      (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
    (elt items index)))

;;; CHARACTER-DATA

(defmethod dom:length ((node character-data))
  (length (slot-value node 'value)))

(defmethod dom:substring-data ((node character-data) offset count)
  (subseq (slot-value node 'value) offset (+ offset count)))

(defmethod dom:append-data ((node character-data) arg)
  (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)
  (with-slots (value) node
    (let ((new (make-array (- (length value) count) :element-type (type-of 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)
  (setf arg (rod arg))
  (with-slots (value) node
    (replace value arg
             :start1 offset :end1 (+ offset count)
             :start2 0 :end2 count))
  (values))

;;; ATTR

;; hmm... value muss noch entities lesen und text-nodes in die hierarchie hngen.

(defmethod (setf dom:value) (new-value (node attribute))
  (setf (slot-value node 'value) (rod new-value)))

;;; 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))
  (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)
      nil)))

(defmethod dom:set-attribute ((element element) name value)
  (with-slots (owner) element
    (dom:set-attribute-node 
     element (make-instance 'attribute 
               :owner owner
               :name name
               :value value
               :specified-p t))
    (values)))

(defmethod dom:remove-attribute-node ((element element) (old-attr attribute))
  (let ((res (dom:remove-named-item element (dom:name old-attr))))
    (if res
        res
      ;; NOT_FOUND_ERR
      (error "Attribute not found."))))

(defmethod dom:get-elements-by-tag-name ((element element) name)
  name
  (error "Not implemented."))

(defmethod dom:normalize ((element element))
  (error "Not implemented.") )

;;; TEXT

(defmethod dom:split-text ((text text) offset)
  offset
  (error "Not implemented."))

;;; COMMENT -- nix
;;; CDATA-SECTION -- nix

;;; DOCUMENT-TYPE -- missing
;;; NOTATION -- nix
;;; ENTITY -- nix
;;; ENTITY-REFERENCE -- nix
;;; PROCESSING-INSTRUCTION -- nix

;; Notbehelf!
(defun can-adopt-p (x y) x y t)


;;; 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)
david's avatar
david committed
523 524 525 526 527 528 529 530


;; importNode

(defmethod import-node-internal (class document node deep &rest initargs)
  (let ((result (apply #'make-instance class :owner document initargs)))
    (when deep
      (dolist (child (dom:child-nodes node))
david's avatar
david committed
531 532
        (dom:append-child result (dom:import-node document child t))))
    result))
david's avatar
david committed
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583

(defmethod dom:import-node ((document document) (node attribute) deep)
  (declare (ignore deep))
  (import-node-internal 'attribute document node t
                        :name (dom:name node)
                        :value (dom:value 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 ((result (import-node-internal 'element document node deep
                                      :tag-name (dom:tag-name node))))
    (dolist (attribute (dom:items (dom:attributes node)))
      (when (dom:specified attribute)
        (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.
  (error "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 'notation document node deep
                        :name (dom:name node)
                        :public-id (dom:public-id node)
                        :system-id (dom:system-id 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 (dom:data node)))