1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: hyperobject.lisp
6 ;;;; Purpose: Hyper Object Metaclass
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; This metaclass as functions to classes to allow display
11 ;;;; in Text, HTML, and XML formats. This includes hyperlinking
12 ;;;; capability and sub-objects.
14 ;;;; $Id: hyperobject.lisp,v 1.13 2002/11/25 02:10:38 kevin Exp $
16 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
18 ;;;; *************************************************************************
20 (in-package :hyperobject)
22 (eval-when (:compile-toplevel :execute)
23 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
25 (eval-when (:compile-toplevel :load-toplevel :execute)
28 `(mop::class-slots mop::slot-definition-name mop:finalize-inheritance
29 mop::standard-direct-slot-definition mop::standard-effective-slot-definition
30 mop:direct-slot-definition-class mop:compute-effective-slot-definition
31 excl::compute-effective-slot-definition-initargs)
33 `(clos:class-slots clos::slot-definition-name clos:finalize-inheritance
34 clos::standard-direct-slot-definition clos::standard-effective-slot-definition
35 clos:direct-slot-definition-class clos:compute-effective-slot-definition
36 clos::compute-effective-slot-definition-initargs)
38 `(sb-pcl:class-of sb-pcl:class-name sb-pcl:class-slots sb-pcl::standard-class
39 sb-pcl::slot-definition-name sb-pcl:finalize-inheritance
40 sb-pcl::standard-direct-slot-definition
41 sb-pcl::standard-effective-slot-definition sb-pcl::validate-superclass
42 sb-pcl:direct-slot-definition-class sb-pcl:compute-effective-slot-definition
43 sb-pcl::compute-effective-slot-definition-initargs)
45 `(pcl:class-of pcl:class-name pcl:class-slots pcl::standard-class
46 pcl::slot-definition-name pcl:finalize-inheritance
47 pcl::standard-direct-slot-definition pcl::standard-effective-slot-definition
48 pcl::validate-superclass pcl:direct-slot-definition-class
49 pcl:compute-effective-slot-definition
50 pcl::compute-effective-slot-definition-initargs)
58 (defclass hyperobject-dsd (standard-direct-slot-definition)
59 ((ho-type :initarg :ho-type :initform nil)
60 (print-formatter :initarg :print-formatter :initform nil)
61 (subobject :initarg :subobject :initform nil)
62 (reference :initarg :reference :initform nil)
63 (description :initarg :description :initform nil)
66 (defclass hyperobject-esd (standard-effective-slot-definition)
67 ((ho-type :initarg :ho-type :initform nil :accessor esd-ho-type)
68 (print-formatter :initarg :print-formatter :initform nil :accessor esd-print-formatter)
69 (subobject :initarg :subobject :initform nil :accessor esd-subobject)
70 (reference :initarg :reference :initform nil :accessor esd-reference)
71 (description :initarg :description :initform nil :accessor esd-description)
76 (defclass hyperobject-class (standard-class)
77 ( ;; slots initialized in defclass
78 (title :initarg :title :type string :initform nil
79 :documentation "Print Title for class")
80 (print-slots :initarg :print-slots :type list :initform nil
81 :documentation "List of slots to print")
82 (description :initarg :description :initform nil
83 :documentation "Class description")
85 ;;; The remainder of these fields are calculated one time
86 ;;; in finalize-inheritence.
88 (subobjects :initform nil :documentation
89 "List of fields that contain a list of subobjects objects.")
90 (references :type list :initform nil :documentation
91 "List of fields that have references")
93 (value-func :initform nil :type function)
94 (xmlvalue-func :initform nil :type function)
95 (fmtstr-text :initform nil :type string)
96 (fmtstr-html :initform nil :type string)
97 (fmtstr-xml :initform nil :type string)
98 (fmtstr-text-labels :initform nil :type string)
99 (fmtstr-html-labels :initform nil :type string)
100 (fmtstr-xml-labels :initform nil :type string)
101 (fmtstr-html-ref :initform nil :type string)
102 (fmtstr-xml-ref :initform nil :type string)
103 (fmtstr-html-ref-labels :initform nil :type string)
104 (fmtstr-xml-ref-labels :initform nil :type string)
106 (:documentation "Metaclass for Markup Language classes."))
108 (defclass subobject ()
109 ((name :type symbol :initform nil :initarg :name :reader name)
110 (reader :type function :initform nil :initarg :reader :reader reader)))
112 (defmethod print-object ((obj subobject) (s stream))
113 (print-unreadable-object (obj s :type t :identity t)
114 (format s "~S" (name obj))))
116 (defclass reference ()
117 ((name :type symbol :initform nil :initarg :name :reader name)
118 (lookup :type function :initform nil :initarg :lookup :reader lookup)
119 (link-parameters :type list :initform nil :initarg :link-parameters
120 :reader link-parameters)))
122 (defmethod print-object ((obj reference) (s stream))
123 (print-unreadable-object (obj s :type t :identity t)
124 (format s "~S" (name obj))))
127 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
130 (defmethod finalize-inheritance :after ((cl hyperobject-class))
131 (init-hyperobject-class cl))
134 (defmethod direct-slot-definition-class ((cl hyperobject-class)
137 (find-class 'hyperobject-dsd))
139 (defmacro define-class-slot (slot-name &optional required)
141 `(defmethod clos:process-a-class-option ((class hyperobject-class)
142 (name (eql ,slot-name))
144 (when (and ,required (null value))
145 (error "hyperobject class slot ~A must have a value" name))
146 (if (null (cdr value))
147 (list name (car value))
148 (list name `',value)))
149 #+(or allegro sbcl cmu scl)
150 (declare (ignore slot-name required))
153 (define-class-slot :title)
154 (define-class-slot :print-slots)
155 (define-class-slot :description)
158 (defmethod compute-effective-slot-definition :around
159 ((cl hyperobject-class) #+(or allegro lispworks) name dsds)
160 #+allergo (declare (ignore name))
161 (let* ((dsd (car dsds))
162 (ho-type (slot-value dsd 'type)))
163 (setf (slot-value dsd 'ho-type) ho-type)
164 (setf (slot-value dsd 'type) (convert-ho-type ho-type))
165 (let ((ia (compute-effective-slot-definition-initargs
166 cl #+lispworks name dsds)))
168 #'make-instance 'hyperobject-esd
170 :print-formatter (slot-value dsd 'print-formatter)
171 :subobject (slot-value dsd 'subobject)
172 :reference (slot-value dsd 'reference)
173 :description (slot-value dsd 'description)
177 (defun convert-ho-type (ho-type)
178 (check-type ho-type symbol)
179 (case (intern (symbol-name ho-type) (symbol-name :keyword))
195 ;;;; Class initialization function
197 (defun find-slot-by-name (cl name)
198 (find name (class-slots cl) :key #'slot-definition-name))
201 (defun process-subobjects (cl)
202 "Process class subobjects slot"
203 (setf (slot-value cl 'subobjects)
204 (let ((subobjects '()))
205 (dolist (slot (class-slots cl))
206 (when (slot-value slot 'subobject)
207 (push (make-instance 'subobject :name (slot-definition-name slot)
208 :reader (if (eq t (esd-subobject slot))
209 (slot-definition-name slot)
210 (esd-subobject slot)))
214 (defun process-documentation (cl)
215 "Calculate class documentation slot"
216 (awhen (slot-value cl 'title)
217 (setf (slot-value cl 'title) (car it)))
218 (awhen (slot-value cl 'description)
219 (setf (slot-value cl 'description) (car it)))
221 (let ((*print-circle* nil))
222 (setf (documentation (class-name cl) 'class)
223 (format nil "Hyperobject~A~A~A~A"
224 (aif (slot-value cl 'title)
225 (format nil ": ~A" it ""))
226 (aif (slot-value cl 'description)
227 (format nil "~%Class description: ~A" it) "")
228 (aif (slot-value cl 'subobjects)
229 (format nil "~%Subobjects:~{ ~A~}" (mapcar #'name it)) "")
230 (aif (slot-value cl 'print-slots)
231 (format nil "~%Print-slots:~{ ~A~}" it) "")
234 (defun process-views (cl)
235 "Calculate all view slots for a hyperobject class"
236 (let ((fmtstr-text "")
239 (fmtstr-text-labels "")
240 (fmtstr-html-labels "")
241 (fmtstr-xml-labels "")
244 (fmtstr-html-ref-labels "")
245 (fmtstr-xml-ref-labels "")
249 (classname (class-name cl))
250 (package (symbol-package (class-name cl)))
252 (declare (ignore classname))
253 (dolist (slot-name (slot-value cl 'print-slots))
254 (let ((slot (find-slot-by-name cl slot-name)))
256 (error "Slot ~A is not found in class ~S" slot-name cl))
257 (let ((name (slot-definition-name slot))
258 (namestr (symbol-name (slot-definition-name slot)))
259 (namestr-lower (string-downcase (symbol-name (slot-definition-name slot))))
260 (type (slot-value slot 'ho-type))
261 (print-formatter (slot-value slot 'print-formatter))
263 (plain-value-func nil)
264 html-str xml-str html-label-str xml-label-str)
266 (when (or (eql type :integer) (eql type :fixnum))
267 (setq value-fmt "~d"))
269 (when (eql type :boolean)
270 (setq value-fmt "~a"))
273 (setq first-field nil)
275 (string-append fmtstr-text " ")
276 (string-append fmtstr-html " ")
277 (string-append fmtstr-xml " ")
278 (string-append fmtstr-text-labels " ")
279 (string-append fmtstr-html-labels " ")
280 (string-append fmtstr-xml-labels " ")
281 (string-append fmtstr-html-ref " ")
282 (string-append fmtstr-xml-ref " ")
283 (string-append fmtstr-html-ref-labels " ")
284 (string-append fmtstr-xml-ref-labels " ")))
286 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
287 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
288 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
289 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
291 (string-append fmtstr-text value-fmt)
292 (string-append fmtstr-html html-str)
293 (string-append fmtstr-xml xml-str)
294 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
295 (string-append fmtstr-html-labels html-label-str)
296 (string-append fmtstr-xml-labels xml-label-str)
298 (if (esd-reference slot)
300 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
301 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
302 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
303 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>")
304 (push (make-instance 'reference :name name :lookup (esd-reference slot))
307 (string-append fmtstr-html-ref html-str)
308 (string-append fmtstr-xml-ref xml-str)
309 (string-append fmtstr-html-ref-labels html-label-str)
310 (string-append fmtstr-xml-ref-labels xml-label-str)))
313 (setq plain-value-func
314 (list `(,print-formatter (,(intern namestr package) x))))
315 (setq plain-value-func
316 (list `(,(intern namestr package) x))))
317 (setq value-func (append value-func plain-value-func))
319 (if (eql type :cdata)
320 (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
321 (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
324 (setf (slot-value cl 'references) references)
327 (setq value-func `(lambda (x) (values ,@value-func)))
328 (setq value-func `(lambda () (values))))
329 (setq value-func (compile nil (eval value-func)))
332 (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
333 (setq xmlvalue-func `(lambda () (values))))
334 (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
336 (setf (slot-value cl 'fmtstr-text) fmtstr-text)
337 (setf (slot-value cl 'fmtstr-html) fmtstr-html)
338 (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
339 (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
340 (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
341 (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
342 (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
343 (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
344 (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
345 (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
346 (setf (slot-value cl 'value-func) value-func)
347 (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
350 (defun init-hyperobject-class (cl)
351 "Initialize a hyperobject class. Calculates all class slots"
352 (process-subobjects cl)
354 (process-documentation cl))
356 (defun hyperobject-class-fmtstr-text (obj)
357 (slot-value (class-of obj) 'fmtstr-text))
359 (defun hyperobject-class-fmtstr-html (obj)
360 (slot-value (class-of obj) 'fmtstr-html))
362 (defun hyperobject-class-fmtstr-xml (obj)
363 (slot-value (class-of obj) 'fmtstr-xml))
365 (defun hyperobject-class-fmtstr-text-labels (obj)
366 (slot-value (class-of obj) 'fmtstr-text-labels))
368 (defun hyperobject-class-fmtstr-html-labels (obj)
369 (slot-value (class-of obj) 'fmtstr-html-labels))
371 (defun hyperobject-class-fmtstr-xml-labels (obj)
372 (slot-value (class-of obj) 'fmtstr-xml-labels))
374 (defun hyperobject-class-value-func (obj)
375 (slot-value (class-of obj) 'value-func))
377 (defun hyperobject-class-xmlvalue-func (obj)
378 (slot-value (class-of obj) 'xmlvalue-func))
380 (eval-when (:compile-toplevel :load-toplevel :execute)
381 (defun hyperobject-class-title (obj)
382 (awhen (slot-value (class-of obj) 'title)
387 (defun hyperobject-class-subobjects (obj)
388 (slot-value (class-of obj) 'subobjects))
390 (defun hyperobject-class-references (obj)
391 (slot-value (class-of obj) 'references))
393 (defun hyperobject-class-fields (obj)
394 (class-slots (class-of obj)))
396 (defun hyperobject-class-fmtstr-html-ref (obj)
397 (slot-value (class-of obj) 'fmtstr-html-ref))
399 (defun hyperobject-class-fmtstr-xml-ref (obj)
400 (slot-value (class-of obj) 'fmtstr-xml-ref))
402 (defun hyperobject-class-fmtstr-html-ref-labels (obj)
403 (slot-value (class-of obj) 'fmtstr-html-ref-labels))
405 (defun hyperobject-class-fmtstr-xml-ref-labels (obj)
406 (slot-value (class-of obj) 'fmtstr-xml-ref-labels))
408 ;;;; Generic Print functions
410 (defparameter *default-textformat* nil)
411 (defparameter *default-htmlformat* nil)
412 (defparameter *default-htmlrefformat* nil)
413 (defparameter *default-xhtmlformat* nil)
414 (defparameter *default-xhtmlrefformat* nil)
415 (defparameter *default-xmlformat* nil)
416 (defparameter *default-xmlrefformat* nil)
417 (defparameter *default-ie-xmlrefformat* nil)
418 (defparameter *default-nullformat* nil)
419 (defparameter *default-init-format?* nil)
421 (defun make-format-instance (fmt)
422 (unless *default-init-format?*
423 (setq *default-textformat* (make-instance 'textformat))
424 (setq *default-htmlformat* (make-instance 'htmlformat))
425 (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
426 (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
427 (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
428 (setq *default-xmlformat* (make-instance 'xmlformat))
429 (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
430 (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
431 (setq *default-nullformat* (make-instance 'nullformat))
432 (setq *default-init-format?* t))
435 (:text *default-textformat*)
436 (:html *default-htmlformat*)
437 (:htmlref *default-htmlrefformat*)
438 (:xhtml *default-xhtmlformat*)
439 (:xhtmlref *default-xhtmlrefformat*)
440 (:xml *default-xmlformat*)
441 (:xmlref *default-xmlrefformat*)
442 (:ie-xmlref *default-ie-xmlrefformat*)
443 (:null *default-nullformat*)
444 (otherwise *default-textformat*)))
446 ;;;; Output format classes for print hyperobject-classes
448 (defclass dataformat ()
449 ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
450 (file-end-str :type string :initarg :file-end-str :reader file-end-str)
451 (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
452 (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
453 (list-start-indent :initarg :list-start-indent :reader list-start-indent)
454 (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
455 (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
456 (list-end-indent :initarg :list-end-indent :reader list-end-indent)
457 (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
458 (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
459 (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
460 (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
461 (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
462 (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
463 (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
464 (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
465 (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
466 (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
467 (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
468 (link-ref :initarg :link-ref :reader link-ref))
469 (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
470 :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
471 :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
472 :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
473 :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
474 :obj-data-value-func nil :link-ref nil)
475 (:documentation "Parent for all dataformat objects"))
477 (defclass binaryformat (dataformat)
480 (defclass nullformat (dataformat)
483 (defun text-list-start-value-func (obj nitems)
484 (values (hyperobject-class-title obj) nitems))
486 (defclass textformat (dataformat)
488 (:default-initargs :list-start-fmtstr "~a~P:~%"
489 :list-start-value-func #'text-list-start-value-func
492 :obj-data-fmtstr #'hyperobject-class-fmtstr-text
493 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-text-labels
494 :obj-data-end-fmtstr "~%"
495 :obj-data-value-func #'hyperobject-class-value-func))
498 (defun class-name-of (obj)
499 (string-downcase (class-name (class-of obj))))
501 (defun htmlformat-list-start-value-func (x nitems)
502 (values (hyperobject-class-title x) nitems (class-name-of x)))
504 (defclass htmlformat (textformat)
506 (:default-initargs :file-start-str "<html><body>~%"
507 :file-end-str "</body><html>~%"
509 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
510 :list-start-value-func #'htmlformat-list-start-value-func
511 :list-end-fmtstr "</ul></div>~%"
513 :list-end-value-func #'identity
515 :obj-start-fmtstr "<li>"
516 :obj-start-value-func #'identity
518 :obj-end-fmtstr "</li>~%"
519 :obj-end-value-func #'identity
521 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
522 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
523 :obj-data-value-func #'hyperobject-class-value-func))
525 (defclass xhtmlformat (textformat)
527 (:default-initargs :file-start-str "<html><body>~%"
528 :file-end-str "</body><html>~%"
530 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
531 :list-start-value-func #'htmlformat-list-start-value-func
532 :list-end-fmtstr "</ul></div>~%"
534 :list-end-value-func #'identity
536 :obj-start-fmtstr "<li>"
537 :obj-start-value-func #'identity
539 :obj-end-fmtstr "</li>~%"
540 :obj-end-value-func #'identity
542 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
543 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
544 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
547 (defun xmlformat-list-end-value-func (x)
548 (format nil "~alist" (class-name-of x)))
550 (defun xmlformat-list-start-value-func (x nitems)
551 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-title x) nitems))
553 (defclass xmlformat (textformat)
555 (:default-initargs :file-start-str "" ; (std-xml-header)
557 :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
558 :list-start-value-func #'xmlformat-list-start-value-func
560 :list-end-fmtstr "</~a>~%"
561 :list-end-value-func #'xmlformat-list-end-value-func
562 :obj-start-fmtstr "<~a>"
563 :obj-start-value-func #'class-name-of
565 :obj-end-fmtstr "</~a>~%"
566 :obj-end-value-func #'class-name-of
569 :obj-data-fmtstr #'hyperobject-class-fmtstr-xml
570 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-xml-labels
571 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
573 (defclass link-ref ()
574 ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
575 (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
576 (page-name :type string :initarg :page-name :accessor page-name)
577 (href-head :type string :initarg :href-head :accessor href-head)
578 (href-end :type string :initarg :href-end :accessor href-end)
579 (ampersand :type string :initarg :ampersand :accessor ampersand))
580 (:default-initargs :fmtstr nil
582 :page-name "disp-func1"
583 :href-head nil :href-end nil :ampersand nil)
584 (:documentation "Formatting for a linked reference"))
586 (defclass html-link-ref (link-ref)
588 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
589 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
594 (defclass xhtml-link-ref (link-ref)
596 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
597 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
602 (defclass xml-link-ref (link-ref)
604 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-xml-ref
605 :fmtstr-labels #'hyperobject-class-fmtstr-xml-ref-labels
606 :href-head "xmllink xlink:type=\"simple\" xlink:href="
609 (:documentation "Mozilla's and W3's idea of a link with XML"))
611 (defclass ie-xml-link-ref (xml-link-ref)
613 (:default-initargs :href-head "html:a href="
615 (:documentation "Internet Explorer's idea of a link with XML"))
618 (defclass htmlrefformat (htmlformat)
620 (:default-initargs :link-ref (make-instance 'html-link-ref)))
622 (defclass xhtmlrefformat (xhtmlformat)
624 (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
626 (defclass xmlrefformat (xmlformat)
628 (:default-initargs :link-ref (make-instance 'xml-link-ref)))
630 (defclass ie-xmlrefformat (xmlformat)
632 (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
635 ;;; File Start and Ends
637 (defgeneric fmt-file-start (fmt s))
638 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
640 (defmethod fmt-file-start ((fmt textformat) (s stream))
641 (aif (file-start-str fmt)
644 (defgeneric fmt-file-end (fmt s))
645 (defmethod fmt-file-end ((fmt textformat) (s stream))
646 (aif (file-end-str fmt)
649 ;;; List Start and Ends
651 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
652 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
653 (if (list-start-indent fmt)
654 (indent-spaces indent s))
655 (aif (list-start-fmtstr fmt)
658 (funcall (list-start-value-func fmt) x num-items)))))
660 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
661 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
662 (declare (ignore num-items))
663 (if (list-end-indent fmt)
664 (indent-spaces indent s))
665 (aif (list-end-fmtstr fmt)
668 (funcall (list-end-value-func fmt) x)))))
670 ;;; Object Start and Ends
672 (defgeneric fmt-obj-start (obj fmt s &optional indent))
673 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
674 (if (obj-start-indent fmt)
675 (indent-spaces indent s))
676 (aif (obj-start-fmtstr fmt)
679 (funcall (obj-start-value-func fmt) x)))))
681 (defgeneric fmt-obj-end (obj fmt s &optional indent))
682 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
683 (if (obj-end-indent fmt)
684 (indent-spaces indent s))
685 (aif (obj-end-fmtstr fmt)
688 (funcall (obj-end-value-func fmt) x)))))
692 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
693 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
694 (declare (ignore obj fieldname))
695 (format nil "~a\"~a?func=~a~akey=~a~a\""
696 (href-head ref) (make-url (page-name ref)) fieldfunc
697 (ampersand ref) fieldvalue
700 (dolist (var refvars)
701 (string-append varstr (format nil "~a~a=~a"
702 (ampersand ref) (car var) (cadr var))))
706 (defgeneric make-link-end (obj ref fieldname))
707 (defmethod make-link-end (obj (ref link-ref) fieldname)
708 (declare (ignore obj fieldname))
709 (format nil "~a" (href-end ref))
712 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
713 (defmethod fmt-obj-data (x (fmt textformat) s
714 &optional (indent 0) (label nil) (refvars nil))
715 (if (obj-data-indent fmt)
716 (indent-spaces indent s))
718 (fmt-obj-data-with-ref x fmt s label refvars)
719 (fmt-obj-data-plain x fmt s label))
720 (aif (obj-data-end-fmtstr fmt)
723 (defgeneric fmt-obj-data-plain (obj fmt s label))
724 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
727 (funcall (obj-data-fmtstr-labels fmt) x)
729 (funcall (funcall (obj-data-value-func fmt) x) x)))
730 (apply #'format s (funcall (obj-data-fmtstr fmt) x)
732 (funcall (funcall (obj-data-value-func fmt) x) x)))))
734 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
735 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
736 (let ((refstr (make-ref-data-str x fmt label))
740 (funcall (funcall (obj-data-value-func fmt) x) x))))
742 ;; make list of reference link fields for printing to refstr template
743 (dolist (ref (hyperobject-class-references x))
745 (make-link-start x (link-ref fmt) (name ref) (lookup ref)
746 (nth (position (name ref)
747 (hyperobject-class-fields x)
749 (slot-definition-name x)))
751 (append (link-parameters ref) refvars)))
752 (link-end (make-link-end x (link-ref fmt) (name ref))))
753 (push link-start refvalues)
754 (push link-end refvalues)))
755 (setq refvalues (nreverse refvalues))
757 (apply #'format s refstr refvalues)))
759 (defgeneric obj-data (obj))
760 (defmethod obj-data (x)
761 "Returns the objects data as a string. Used by common-graphics outline function"
762 (let ((fmt (make-format-instance :text)))
763 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
765 (funcall (funcall (obj-data-value-func fmt) x) x)))))
767 (defgeneric make-ref-data-str (obj fmt &optional label))
768 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
769 "Return fmt string for that contains ~a slots for reference link start and end"
770 (unless (link-ref fmt)
771 (error "fmt does not contain a link-ref"))
774 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
776 (funcall (funcall (obj-data-value-func fmt) x) x)))
777 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
778 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
781 ;;; Display method for objects
784 (defgeneric load-all-subobjects (objs))
785 (defmethod load-all-subobjects (objs)
786 "Load all subobjects if they have not already been loaded."
788 (let ((objlist (mklist objs)))
789 (dolist (obj objlist)
790 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
791 (dolist (child-obj it) ;; for each child function
792 (awhen (funcall (reader child-obj) obj)
793 (load-all-subobjects it))))))
796 (defgeneric print-hyperobject-class (objs fmt strm
797 &optional label english-only-function
798 indent subobjects refvars))
800 (defmethod print-hyperobject-class (objs (fmt dataformat) (strm stream)
801 &optional (label nil) (indent 0)
802 (english-only-function nil)
803 (subobjects nil) (refvars nil))
804 "Display a single or list of hyperobject-class instances and their subobjects"
806 (setq objs (mklist objs))
807 (let ((nobjs (length objs)))
808 (fmt-list-start (car objs) fmt strm indent nobjs)
810 (unless (and english-only-function
811 (multiple-value-bind (eng term) (funcall english-only-function obj)
812 (and term (not eng))))
813 (fmt-obj-start obj fmt strm indent)
814 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
816 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
817 (dolist (child-obj it) ;; for each child function
818 (awhen (funcall (reader child-obj) obj) ;; access set of child objects
819 (print-hyperobject-class it fmt strm label
820 (1+ indent) english-only-function
821 subobjects refvars)))))
822 (fmt-obj-end obj fmt strm indent)))
823 (fmt-list-end (car objs) fmt strm indent nobjs))
828 (defun print-hyperobject (objs &key (os *standard-output*) (format :text)
829 (label nil) (english-only-function nil)
830 (subobjects nil) (file-wrapper t) (refvars nil))
831 "EXPORTED Function: prints hyperobject-class objects. Simplies call to print-hyperobject-class"
832 (let ((fmt (make-format-instance format)))
834 (fmt-file-start fmt os))
836 (print-hyperobject-class objs fmt os label 0 english-only-function subobjects refvars))
838 (fmt-file-end fmt os)))