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.8 2002/11/22 19:14:17 kevin Exp $
16 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
18 ;;;; *************************************************************************
20 (in-package :hyperobject-mop)
22 (eval-when (:compile-toplevel :execute)
23 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
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
36 clos:class-slots clos::slot-definition-name clos:finalize-inheritance
37 clos::standard-direct-slot-definition clos::standard-effective-slot-definition
38 clos:direct-slot-definition-class clos:compute-effective-slot-definition
39 clos::compute-effective-slot-definition-initargs
44 pcl:class-of pcl:class-name pcl:class-slots pcl::standard-class
45 pcl::slot-definition-name pcl:finalize-inheritance
46 pcl::standard-direct-slot-definition pcl::standard-effective-slot-definition
47 pcl::validate-superclass pcl:direct-slot-definition-class
48 pcl:compute-effective-slot-definition
49 pcl::compute-effective-slot-definition-initargs
54 sb-pcl:class-of sb-pcl:class-name sb-pcl:class-slots sb-pcl::standard-class
55 sb-pcl::slot-definition-name sb-pcl:finalize-inheritance
56 sb-pcl::standard-direct-slot-definition
57 sb-pcl::standard-effective-slot-definition sb-pcl::validate-superclass
58 sb-pcl:direct-slot-definition-class sb-pcl:compute-effective-slot-definition
59 sb-pcl::compute-effective-slot-definition-initargs
65 (defclass hyperobject-dsd (standard-direct-slot-definition)
66 ((ho-type :initarg :ho-type :initform nil :accessor dsd-ho-type)
67 (format-func :initarg :format-func :initform nil :accessor dsd-format-func)
68 (subobject :initarg :subobject :initform nil :accessor dsd-subobject)
69 (reference :initarg :reference :initform nil :accessor dsd-reference)
72 (defclass hyperobject-esd (standard-effective-slot-definition)
73 ((ho-type :initarg :ho-type :initform nil :accessor esd-ho-type)
74 (format-func :initarg :format-func :initform nil :accessor esd-format-func)
75 (subobject :initarg :subobject :initform nil :accessor esd-subobject)
76 (reference :initarg :reference :initform nil :accessor esd-reference)
81 (defclass hyperobject-class (standard-class)
82 ((title :initarg :title :type string :initform nil
83 :documentation "Print Title for class")
84 (print-slots :initarg :print-slots :type list :initform nil
85 :documentation "List of slots to print")
87 :initarg :subobjects :initform nil
88 :documentation "List of fields that contain a list of subobjects objects.")
90 :initarg :references :type list :initform nil
92 "List of fields that can be referred to by browsers.
93 Format is ((field-name field-lookup-func other-link-params) ...)")
95 ;;; The remainder of these fields are calculated one time
96 ;;; in finalize-inheritence.
97 (value-func :initform nil :type function)
98 (xmlvalue-func :initform nil :type function)
99 (fmtstr-text :initform nil :type string)
100 (fmtstr-html :initform nil :type string)
101 (fmtstr-xml :initform nil :type string)
102 (fmtstr-text-labels :initform nil :type string)
103 (fmtstr-html-labels :initform nil :type string)
104 (fmtstr-xml-labels :initform nil :type string)
105 (fmtstr-html-ref :initform nil :type string)
106 (fmtstr-xml-ref :initform nil :type string)
107 (fmtstr-html-ref-labels :initform nil :type string)
108 (fmtstr-xml-ref-labels :initform nil :type string)
110 (:documentation "Metaclass for Markup Language classes."))
112 (defclass subobject ()
113 ((name :type symbol :initform nil :initarg :name :reader name)
114 (reader :type function :initform nil :initarg :reader :reader reader)))
116 (defmethod print-object ((obj subobject) (s stream))
117 (print-unreadable-object (obj s :type t :identity t)
118 (format s "~S" (name obj))))
120 (defclass reference ()
121 ((name :type symbol :initform nil :initarg :name :reader name)
122 (lookup :type function :initform nil :initarg :lookup :reader lookup)
123 (link-parameters :type list :initform nil :initarg :link-parameters
124 :reader link-parameters)))
126 (defmethod print-object ((obj reference) (s stream))
127 (print-unreadable-object (obj s :type t :identity t)
128 (format s "~S" (name obj))))
131 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
134 (defmethod finalize-inheritance :after ((cl hyperobject-class))
135 (init-hyperobject-class cl))
138 (defmethod direct-slot-definition-class ((cl hyperobject-class)
140 (find-class 'hyperobject-dsd))
145 (defmethod clos:process-a-class-option ((class hyperobject-class)
149 (error "hyperobject-class title must have a value"))
150 (if (null (cdr value))
151 (list name (car value))
152 (list name `',value)))
155 (defmethod clos:process-a-class-option ((class hyperobject-class)
156 (name (eql :print-slots))
158 (if (null (cdr value))
159 (list name (car value))
160 (list name `',value)))
162 (defmethod (compute-effective-slot-definition) :around
163 ((cl hyperobject-class) #+(or allegro lispworks) slot dsds)
164 (declare (ignorable slot))
165 (let* ((dsd (car dsds))
166 (ho-type (slot-value dsd 'type)))
167 (setf (slot-value dsd 'ho-type) ho-type)
168 (setf (slot-value dsd 'type) (convert-ho-type ho-type))
169 (let ((ia (computer-effective-slot-definition-initargs
170 cl #+lispworks slot dsds)))
172 #'make-instance 'hyperobject-esd
174 :format-func (slot-value dsd 'format-func)
175 :subobject (slot-value dsd 'subobject)
176 :reference (slot-value dsd 'reference)
180 (defun convert-ho-type (ho-type)
181 (check-type ho-type symbol)
182 (case (intern (symbol-name ho-type) (symbol-name :keyword))
198 ;;;; Class initialization function
200 (defun find-slot-by-name (cl name)
201 (find name (class-slots cl) :key #'slot-definition-name))
203 (defun init-hyperobject-class (cl)
204 (let ((fmtstr-text "")
207 (fmtstr-text-labels "")
208 (fmtstr-html-labels "")
209 (fmtstr-xml-labels "")
212 (fmtstr-html-ref-labels "")
213 (fmtstr-xml-ref-labels "")
217 (classname (class-name cl))
218 (package (symbol-package (class-name cl)))
221 (declare (ignore classname))
223 (dolist (slot-name (slot-value cl 'print-slots))
224 (let ((slot (find-slot-by-name cl slot-name)))
226 (error "Slot ~A is not found in class ~S" slot-name cl))
227 (if (slot-value slot 'subobject)
228 (push (make-instance 'subobject :name (slot-definition-name slot)
229 :reader (if (eq t (esd-subobject slot))
230 (slot-definition-name slot)
231 (esd-subobject slot)))
233 (let ((name (slot-definition-name slot))
234 (namestr (symbol-name (slot-definition-name slot)))
235 (namestr-lower (string-downcase (symbol-name (slot-definition-name slot))))
236 (type (slot-value slot 'ho-type))
237 (formatter (slot-value slot 'format-func))
239 (plain-value-func nil)
240 html-str xml-str html-label-str xml-label-str)
242 (when (or (eql type :integer) (eql type :fixnum))
243 (setq value-fmt "~d"))
245 (when (eql type :commainteger)
246 (setq value-fmt "~:d"))
248 (when (eql type :boolean)
249 (setq value-fmt "~a"))
252 (setq first-field nil)
254 (string-append fmtstr-text " ")
255 (string-append fmtstr-html " ")
256 (string-append fmtstr-xml " ")
257 (string-append fmtstr-text-labels " ")
258 (string-append fmtstr-html-labels " ")
259 (string-append fmtstr-xml-labels " ")
260 (string-append fmtstr-html-ref " ")
261 (string-append fmtstr-xml-ref " ")
262 (string-append fmtstr-html-ref-labels " ")
263 (string-append fmtstr-xml-ref-labels " ")))
265 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
266 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
267 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
268 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
270 (string-append fmtstr-text value-fmt)
271 (string-append fmtstr-html html-str)
272 (string-append fmtstr-xml xml-str)
273 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
274 (string-append fmtstr-html-labels html-label-str)
275 (string-append fmtstr-xml-labels xml-label-str)
277 (if (esd-reference slot)
279 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
280 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
281 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
282 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>")
283 (push (make-instance 'reference :name name :lookup (esd-reference slot))
286 (string-append fmtstr-html-ref html-str)
287 (string-append fmtstr-xml-ref xml-str)
288 (string-append fmtstr-html-ref-labels html-label-str)
289 (string-append fmtstr-xml-ref-labels xml-label-str)))
292 (setq plain-value-func
293 (list `(,formatter (,(intern namestr package) x))))
294 (setq plain-value-func
295 (list `(,(intern namestr package) x))))
296 (setq value-func (append value-func plain-value-func))
298 (if (eql type :cdata)
299 (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
300 (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
303 (setf (slot-value cl 'references) references)
304 (setf (slot-value cl 'subobjects) subobjects)
307 (setq value-func `(lambda (x) (values ,@value-func)))
308 (setq value-func `(lambda () (values))))
309 (setq value-func (compile nil (eval value-func)))
312 (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
313 (setq xmlvalue-func `(lambda () (values))))
314 (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
316 (setf (slot-value cl 'fmtstr-text) fmtstr-text)
317 (setf (slot-value cl 'fmtstr-html) fmtstr-html)
318 (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
319 (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
320 (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
321 (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
322 (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
323 (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
324 (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
325 (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
326 (setf (slot-value cl 'value-func) value-func)
327 (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
331 (defun hyperobject-class-fmtstr-text (obj)
332 (slot-value (class-of obj) 'fmtstr-text))
334 (defun hyperobject-class-fmtstr-html (obj)
335 (slot-value (class-of obj) 'fmtstr-html))
337 (defun hyperobject-class-fmtstr-xml (obj)
338 (slot-value (class-of obj) 'fmtstr-xml))
340 (defun hyperobject-class-fmtstr-text-labels (obj)
341 (slot-value (class-of obj) 'fmtstr-text-labels))
343 (defun hyperobject-class-fmtstr-html-labels (obj)
344 (slot-value (class-of obj) 'fmtstr-html-labels))
346 (defun hyperobject-class-fmtstr-xml-labels (obj)
347 (slot-value (class-of obj) 'fmtstr-xml-labels))
349 (defun hyperobject-class-value-func (obj)
350 (slot-value (class-of obj) 'value-func))
352 (defun hyperobject-class-xmlvalue-func (obj)
353 (slot-value (class-of obj) 'xmlvalue-func))
355 (eval-when (:compile-toplevel :load-toplevel :execute)
356 (defun hyperobject-class-title (obj)
357 (awhen (slot-value (class-of obj) 'title)
362 (defun hyperobject-class-subobjects (obj)
363 (slot-value (class-of obj) 'subobjects))
365 (defun hyperobject-class-references (obj)
366 (slot-value (class-of obj) 'references))
368 (defun hyperobject-class-fields (obj)
369 (class-slots (class-of obj)))
371 (defun hyperobject-class-fmtstr-html-ref (obj)
372 (slot-value (class-of obj) 'fmtstr-html-ref))
374 (defun hyperobject-class-fmtstr-xml-ref (obj)
375 (slot-value (class-of obj) 'fmtstr-xml-ref))
377 (defun hyperobject-class-fmtstr-html-ref-labels (obj)
378 (slot-value (class-of obj) 'fmtstr-html-ref-labels))
380 (defun hyperobject-class-fmtstr-xml-ref-labels (obj)
381 (slot-value (class-of obj) 'fmtstr-xml-ref-labels))
383 ;;;; Generic Print functions
385 (defparameter *default-textformat* nil)
386 (defparameter *default-htmlformat* nil)
387 (defparameter *default-htmlrefformat* nil)
388 (defparameter *default-xhtmlformat* nil)
389 (defparameter *default-xhtmlrefformat* nil)
390 (defparameter *default-xmlformat* nil)
391 (defparameter *default-xmlrefformat* nil)
392 (defparameter *default-ie-xmlrefformat* nil)
393 (defparameter *default-nullformat* nil)
394 (defparameter *default-init-format?* nil)
396 (defun make-format-instance (fmt)
397 (unless *default-init-format?*
398 (setq *default-textformat* (make-instance 'textformat))
399 (setq *default-htmlformat* (make-instance 'htmlformat))
400 (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
401 (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
402 (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
403 (setq *default-xmlformat* (make-instance 'xmlformat))
404 (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
405 (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
406 (setq *default-nullformat* (make-instance 'nullformat))
407 (setq *default-init-format?* t))
410 (:text *default-textformat*)
411 (:html *default-htmlformat*)
412 (:htmlref *default-htmlrefformat*)
413 (:xhtml *default-xhtmlformat*)
414 (:xhtmlref *default-xhtmlrefformat*)
415 (:xml *default-xmlformat*)
416 (:xmlref *default-xmlrefformat*)
417 (:ie-xmlref *default-ie-xmlrefformat*)
418 (:null *default-nullformat*)
419 (otherwise *default-textformat*)))
421 ;;;; Output format classes for print hyperobject-classes
423 (defclass dataformat ()
424 ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
425 (file-end-str :type string :initarg :file-end-str :reader file-end-str)
426 (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
427 (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
428 (list-start-indent :initarg :list-start-indent :reader list-start-indent)
429 (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
430 (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
431 (list-end-indent :initarg :list-end-indent :reader list-end-indent)
432 (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
433 (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
434 (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
435 (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
436 (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
437 (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
438 (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
439 (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
440 (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
441 (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
442 (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
443 (link-ref :initarg :link-ref :reader link-ref))
444 (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
445 :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
446 :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
447 :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
448 :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
449 :obj-data-value-func nil :link-ref nil)
450 (:documentation "Parent for all dataformat objects"))
452 (defclass binaryformat (dataformat)
455 (defclass nullformat (dataformat)
458 (defun text-list-start-value-func (obj nitems)
459 (values (hyperobject-class-title obj) nitems))
461 (defclass textformat (dataformat)
463 (:default-initargs :list-start-fmtstr "~a~P:~%"
464 :list-start-value-func #'text-list-start-value-func
467 :obj-data-fmtstr #'hyperobject-class-fmtstr-text
468 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-text-labels
469 :obj-data-end-fmtstr "~%"
470 :obj-data-value-func #'hyperobject-class-value-func))
473 (defun class-name-of (obj)
474 (string-downcase (class-name (class-of obj))))
476 (defun htmlformat-list-start-value-func (x nitems)
477 (values (hyperobject-class-title x) nitems (class-name-of x)))
479 (defclass htmlformat (textformat)
481 (:default-initargs :file-start-str "<html><body>~%"
482 :file-end-str "</body><html>~%"
484 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
485 :list-start-value-func #'htmlformat-list-start-value-func
486 :list-end-fmtstr "</ul></div>~%"
488 :list-end-value-func #'identity
490 :obj-start-fmtstr "<li>"
491 :obj-start-value-func #'identity
493 :obj-end-fmtstr "</li>~%"
494 :obj-end-value-func #'identity
496 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
497 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
498 :obj-data-value-func #'hyperobject-class-value-func))
500 (defclass xhtmlformat (textformat)
502 (:default-initargs :file-start-str "<html><body>~%"
503 :file-end-str "</body><html>~%"
505 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
506 :list-start-value-func #'htmlformat-list-start-value-func
507 :list-end-fmtstr "</ul></div>~%"
509 :list-end-value-func #'identity
511 :obj-start-fmtstr "<li>"
512 :obj-start-value-func #'identity
514 :obj-end-fmtstr "</li>~%"
515 :obj-end-value-func #'identity
517 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
518 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
519 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
522 (defun xmlformat-list-end-value-func (x)
523 (format nil "~alist" (class-name-of x)))
525 (defun xmlformat-list-start-value-func (x nitems)
526 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-title x) nitems))
528 (defclass xmlformat (textformat)
530 (:default-initargs :file-start-str "" ; (std-xml-header)
532 :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
533 :list-start-value-func #'xmlformat-list-start-value-func
535 :list-end-fmtstr "</~a>~%"
536 :list-end-value-func #'xmlformat-list-end-value-func
537 :obj-start-fmtstr "<~a>"
538 :obj-start-value-func #'class-name-of
540 :obj-end-fmtstr "</~a>~%"
541 :obj-end-value-func #'class-name-of
544 :obj-data-fmtstr #'hyperobject-class-fmtstr-xml
545 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-xml-labels
546 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
548 (defclass link-ref ()
549 ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
550 (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
551 (page-name :type string :initarg :page-name :accessor page-name)
552 (href-head :type string :initarg :href-head :accessor href-head)
553 (href-end :type string :initarg :href-end :accessor href-end)
554 (ampersand :type string :initarg :ampersand :accessor ampersand))
555 (:default-initargs :fmtstr nil
557 :page-name "disp-func1"
558 :href-head nil :href-end nil :ampersand nil)
559 (:documentation "Formatting for a linked reference"))
561 (defclass html-link-ref (link-ref)
563 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
564 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
569 (defclass xhtml-link-ref (link-ref)
571 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
572 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
577 (defclass xml-link-ref (link-ref)
579 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-xml-ref
580 :fmtstr-labels #'hyperobject-class-fmtstr-xml-ref-labels
581 :href-head "xmllink xlink:type=\"simple\" xlink:href="
584 (:documentation "Mozilla's and W3's idea of a link with XML"))
586 (defclass ie-xml-link-ref (xml-link-ref)
588 (:default-initargs :href-head "html:a href="
590 (:documentation "Internet Explorer's idea of a link with XML"))
593 (defclass htmlrefformat (htmlformat)
595 (:default-initargs :link-ref (make-instance 'html-link-ref)))
597 (defclass xhtmlrefformat (xhtmlformat)
599 (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
601 (defclass xmlrefformat (xmlformat)
603 (:default-initargs :link-ref (make-instance 'xml-link-ref)))
605 (defclass ie-xmlrefformat (xmlformat)
607 (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
610 ;;; File Start and Ends
612 (defgeneric fmt-file-start (fmt s))
613 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
615 (defmethod fmt-file-start ((fmt textformat) (s stream))
616 (aif (file-start-str fmt)
619 (defgeneric fmt-file-end (fmt s))
620 (defmethod fmt-file-end ((fmt textformat) (s stream))
621 (aif (file-end-str fmt)
624 ;;; List Start and Ends
626 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
627 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
628 (if (list-start-indent fmt)
629 (indent-spaces indent s))
630 (aif (list-start-fmtstr fmt)
633 (funcall (list-start-value-func fmt) x num-items)))))
635 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
636 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
637 (declare (ignore num-items))
638 (if (list-end-indent fmt)
639 (indent-spaces indent s))
640 (aif (list-end-fmtstr fmt)
643 (funcall (list-end-value-func fmt) x)))))
645 ;;; Object Start and Ends
647 (defgeneric fmt-obj-start (obj fmt s &optional indent))
648 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
649 (if (obj-start-indent fmt)
650 (indent-spaces indent s))
651 (aif (obj-start-fmtstr fmt)
654 (funcall (obj-start-value-func fmt) x)))))
656 (defgeneric fmt-obj-end (obj fmt s &optional indent))
657 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
658 (if (obj-end-indent fmt)
659 (indent-spaces indent s))
660 (aif (obj-end-fmtstr fmt)
663 (funcall (obj-end-value-func fmt) x)))))
667 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
668 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
669 (declare (ignore obj fieldname))
670 (format nil "~a\"~a?func=~a~akey=~a~a\""
671 (href-head ref) (make-url (page-name ref)) fieldfunc
672 (ampersand ref) fieldvalue
675 (dolist (var refvars)
676 (string-append varstr (format nil "~a~a=~a"
677 (ampersand ref) (car var) (cadr var))))
681 (defgeneric make-link-end (obj ref fieldname))
682 (defmethod make-link-end (obj (ref link-ref) fieldname)
683 (declare (ignore obj fieldname))
684 (format nil "~a" (href-end ref))
687 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
688 (defmethod fmt-obj-data (x (fmt textformat) s
689 &optional (indent 0) (label nil) (refvars nil))
690 (if (obj-data-indent fmt)
691 (indent-spaces indent s))
693 (fmt-obj-data-with-ref x fmt s label refvars)
694 (fmt-obj-data-plain x fmt s label))
695 (aif (obj-data-end-fmtstr fmt)
698 (defgeneric fmt-obj-data-plain (obj fmt s label))
699 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
702 (funcall (obj-data-fmtstr-labels fmt) x)
704 (funcall (funcall (obj-data-value-func fmt) x) x)))
705 (apply #'format s (funcall (obj-data-fmtstr fmt) x)
707 (funcall (funcall (obj-data-value-func fmt) x) x)))))
709 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
710 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
711 (let ((refstr (make-ref-data-str x fmt label))
715 (funcall (funcall (obj-data-value-func fmt) x) x))))
717 ;; make list of reference link fields for printing to refstr template
718 (dolist (ref (hyperobject-class-references x))
720 (make-link-start x (link-ref fmt) (name ref) (lookup ref)
721 (nth (position (name ref)
722 (hyperobject-class-fields x)
724 (slot-definition-name x)))
726 (append (link-parameters ref) refvars)))
727 (link-end (make-link-end x (link-ref fmt) (name ref))))
728 (push link-start refvalues)
729 (push link-end refvalues)))
730 (setq refvalues (nreverse refvalues))
732 (apply #'format s refstr refvalues)))
734 (defgeneric obj-data (obj))
735 (defmethod obj-data (x)
736 "Returns the objects data as a string. Used by common-graphics outline function"
737 (let ((fmt (make-format-instance :text)))
738 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
740 (funcall (funcall (obj-data-value-func fmt) x) x)))))
742 (defgeneric make-ref-data-str (obj fmt &optional label))
743 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
744 "Return fmt string for that contains ~a slots for reference link start and end"
745 (unless (link-ref fmt)
746 (error "fmt does not contain a link-ref"))
749 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
751 (funcall (funcall (obj-data-value-func fmt) x) x)))
752 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
753 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
756 ;;; Display method for objects
759 (defgeneric load-all-subobjects (objs))
760 (defmethod load-all-subobjects (objs)
761 "Load all subobjects if they have not already been loaded."
763 (let ((objlist (mklist objs)))
764 (dolist (obj objlist)
765 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
766 (dolist (child-obj it) ;; for each child function
767 (awhen (funcall (reader child-obj) obj)
768 (load-all-subobjects it))))))
771 (defgeneric print-hyperobject-class (objs fmt strm
772 &optional label english-only-function
773 indent subobjects refvars))
775 (defmethod print-hyperobject-class (objs (fmt dataformat) (strm stream)
776 &optional (label nil) (indent 0)
777 (english-only-function nil)
778 (subobjects nil) (refvars nil))
779 "Display a single or list of hyperobject-class instances and their subobjects"
781 (setq objs (mklist objs))
782 (let ((nobjs (length objs)))
783 (fmt-list-start (car objs) fmt strm indent nobjs)
785 (unless (and english-only-function
786 (multiple-value-bind (eng term) (funcall english-only-function obj)
787 (and term (not eng))))
788 (fmt-obj-start obj fmt strm indent)
789 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
791 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
792 (dolist (child-obj it) ;; for each child function
793 (awhen (funcall (reader child-obj) obj) ;; access set of child objects
794 (print-hyperobject-class it fmt strm label
795 (1+ indent) english-only-function
796 subobjects refvars)))))
797 (fmt-obj-end obj fmt strm indent)))
798 (fmt-list-end (car objs) fmt strm indent nobjs))
803 (defun print-hyperobject (objs &key (os *standard-output*) (format :text)
804 (label nil) (english-only-function nil)
805 (subobjects nil) (file-wrapper t) (refvars nil))
806 "EXPORTED Function: prints hyperobject-class objects. Simplies call to print-hyperobject-class"
807 (let ((fmt (make-format-instance format)))
809 (fmt-file-start fmt os))
811 (print-hyperobject-class objs fmt os label 0 english-only-function subobjects refvars))
813 (fmt-file-end fmt os)))
817 (defclass hyperobject ()
819 (:metaclass hyperobject-class))
822 (defmethod print-object ((obj hyperobject) (s stream))
823 (print-unreadable-object (obj s :type t :identity t)
824 (let ((fmt (make-instance 'hyperobject::textformat)))
826 s (funcall (hyperobject-mop::obj-data-fmtstr fmt) obj)
828 (funcall (funcall (hyperobject-mop::obj-data-value-func fmt) obj) obj))))))