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.7 2002/11/22 16:05:08 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 :reader ml-std-title
84 "Print Title for class")
88 "List of fields that contain a list of subobjects objects.")
90 :initarg :references :type list :reader ml-std-references
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 :reader ml-std-value-func)
98 (xmlvalue-func :initform nil :type function :reader ml-std-xmlvalue-func)
99 (fmtstr-text :initform nil :type string :reader ml-std-fmtstr-text)
100 (fmtstr-html :initform nil :type string :reader ml-std-fmtstr-html)
101 (fmtstr-xml :initform nil :type string :reader ml-std-fmtstr-xml)
102 (fmtstr-text-labels :initform nil :type string :reader ml-std-fmtstr-text-labels)
103 (fmtstr-html-labels :initform nil :type string :reader ml-std-fmtstr-html-labels)
104 (fmtstr-xml-labels :initform nil :type string :reader ml-std-fmtstr-xml-labels)
105 (fmtstr-html-ref :initform nil :type string :reader ml-std-fmtstr-html-ref)
106 (fmtstr-xml-ref :initform nil :type string :reader ml-std-fmtstr-xml-ref)
107 (fmtstr-html-ref-labels :initform nil :type string :reader ml-std-fmtstr-html-ref-labels)
108 (fmtstr-xml-ref-labels :initform nil :type string :reader ml-std-fmtstr-xml-ref-labels)
110 (:default-initargs :title nil :subobjects nil :references nil)
111 (:documentation "Metaclass for Markup Language classes."))
113 (defclass subobject ()
114 ((name :type symbol :initform nil :initarg :name :reader name)
115 (reader :type function :initform nil :initarg :reader :reader reader)))
117 (defmethod print-object ((obj subobject) (s stream))
118 (print-unreadable-object (obj s :type t :identity t)
119 (format s "~S" (name obj))))
121 (defclass reference ()
122 ((name :type symbol :initform nil :initarg :name :reader name)
123 (lookup :type function :initform nil :initarg :lookup :reader lookup)
124 (link-parameters :type list :initform nil :initarg :link-parameters
125 :reader link-parameters)))
127 (defmethod print-object ((obj reference) (s stream))
128 (print-unreadable-object (obj s :type t :identity t)
129 (format s "~S" (name obj))))
132 (defmethod validate-superclass ((class hyperobject-class) (superclass standard-class))
135 (defmethod finalize-inheritance :after ((cl hyperobject-class))
136 (init-hyperobject-class cl))
139 (defmethod direct-slot-definition-class ((cl hyperobject-class)
141 (find-class 'hyperobject-dsd))
146 (defmethod clos:process-a-class-option ((class hyperobject-class)
150 (error "hyperobject-class title must have a value"))
151 (if (null (cdr value))
152 (list name (car value))
153 (list name `',value)))
155 (defmethod (compute-effective-slot-definition) :around
156 ((cl hyperobject-class) #+(or allegro lispworks) slot dsds)
157 (declare (ignorable slot))
158 (let* ((dsd (car dsds))
159 (ho-type (slot-value dsd 'type)))
160 (setf (slot-value dsd 'ho-type) ho-type)
161 (setf (slot-value dsd 'type) (convert-ho-type ho-type))
162 (let ((ia (computer-effective-slot-definition-initargs
163 cl #+lispworks slot dsds)))
165 #'make-instance 'hyperobject-esd
167 :format-func (slot-value dsd 'format-func)
168 :subobject (slot-value dsd 'subobject)
169 :reference (slot-value dsd 'reference)
173 (defun convert-ho-type (ho-type)
174 (check-type ho-type symbol)
175 (case (intern (symbol-name ho-type) (symbol-name :keyword))
191 ;;;; Class initialization function
193 (defun init-hyperobject-class (cl)
194 (let ((fmtstr-text "")
197 (fmtstr-text-labels "")
198 (fmtstr-html-labels "")
199 (fmtstr-xml-labels "")
202 (fmtstr-html-ref-labels "")
203 (fmtstr-xml-ref-labels "")
207 (classname (class-name cl))
208 (package (symbol-package (class-name cl)))
211 (declare (ignore classname))
212 (dolist (f (class-slots cl))
213 (if (slot-value f 'subobject)
214 (push (make-instance 'subobject :name (slot-definition-name f)
215 :reader (if (eq t (esd-subobject f))
216 (slot-definition-name f)
219 (let ((name (slot-definition-name f))
220 (namestr (symbol-name (slot-definition-name f)))
221 (namestr-lower (string-downcase (symbol-name (slot-definition-name f))))
222 (type (slot-value f 'ho-type))
223 (formatter (slot-value f 'format-func))
225 (plain-value-func nil)
226 html-str xml-str html-label-str xml-label-str)
228 (when (or (eql type :integer) (eql type :fixnum))
229 (setq value-fmt "~d"))
231 (when (eql type :commainteger)
232 (setq value-fmt "~:d"))
234 (when (eql type :boolean)
235 (setq value-fmt "~a"))
238 (setq first-field nil)
240 (string-append fmtstr-text " ")
241 (string-append fmtstr-html " ")
242 (string-append fmtstr-xml " ")
243 (string-append fmtstr-text-labels " ")
244 (string-append fmtstr-html-labels " ")
245 (string-append fmtstr-xml-labels " ")
246 (string-append fmtstr-html-ref " ")
247 (string-append fmtstr-xml-ref " ")
248 (string-append fmtstr-html-ref-labels " ")
249 (string-append fmtstr-xml-ref-labels " ")))
251 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
252 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
253 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
254 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
256 (string-append fmtstr-text value-fmt)
257 (string-append fmtstr-html html-str)
258 (string-append fmtstr-xml xml-str)
259 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
260 (string-append fmtstr-html-labels html-label-str)
261 (string-append fmtstr-xml-labels xml-label-str)
263 (if (esd-reference f)
265 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
266 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
267 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
268 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>")
269 (push (make-instance 'reference :name name :lookup (esd-reference f))
272 (string-append fmtstr-html-ref html-str)
273 (string-append fmtstr-xml-ref xml-str)
274 (string-append fmtstr-html-ref-labels html-label-str)
275 (string-append fmtstr-xml-ref-labels xml-label-str)))
278 (setq plain-value-func
279 (list `(,formatter (,(intern namestr package) x))))
280 (setq plain-value-func
281 (list `(,(intern namestr package) x))))
282 (setq value-func (append value-func plain-value-func))
284 (if (eql type :cdata)
285 (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
286 (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
289 (setf (slot-value cl 'references) references)
290 (setf (slot-value cl 'subobjects) subobjects)
293 (setq value-func `(lambda (x) (values ,@value-func)))
294 (setq value-func `(lambda () (values))))
295 (setq value-func (compile nil (eval value-func)))
298 (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
299 (setq xmlvalue-func `(lambda () (values))))
300 (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
302 (setf (slot-value cl 'fmtstr-text) fmtstr-text)
303 (setf (slot-value cl 'fmtstr-html) fmtstr-html)
304 (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
305 (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
306 (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
307 (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
308 (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
309 (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
310 (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
311 (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
312 (setf (slot-value cl 'value-func) value-func)
313 (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
317 (defun hyperobject-class-fmtstr-text (obj)
318 (slot-value (class-of obj) 'fmtstr-text))
320 (defun hyperobject-class-fmtstr-html (obj)
321 (slot-value (class-of obj) 'fmtstr-html))
323 (defun hyperobject-class-fmtstr-xml (obj)
324 (slot-value (class-of obj) 'fmtstr-xml))
326 (defun hyperobject-class-fmtstr-text-labels (obj)
327 (slot-value (class-of obj) 'fmtstr-text-labels))
329 (defun hyperobject-class-fmtstr-html-labels (obj)
330 (slot-value (class-of obj) 'fmtstr-html-labels))
332 (defun hyperobject-class-fmtstr-xml-labels (obj)
333 (slot-value (class-of obj) 'fmtstr-xml-labels))
335 (defun hyperobject-class-value-func (obj)
336 (slot-value (class-of obj) 'value-func))
338 (defun hyperobject-class-xmlvalue-func (obj)
339 (slot-value (class-of obj) 'xmlvalue-func))
341 (eval-when (:compile-toplevel :load-toplevel :execute)
342 (defun hyperobject-class-title (obj)
343 (awhen (slot-value (class-of obj) 'title)
348 (defun hyperobject-class-subobjects (obj)
349 (slot-value (class-of obj) 'subobjects))
351 (defun hyperobject-class-references (obj)
352 (slot-value (class-of obj) 'references))
354 (defun hyperobject-class-fields (obj)
355 (class-slots (class-of obj)))
357 (defun hyperobject-class-fmtstr-html-ref (obj)
358 (slot-value (class-of obj) 'fmtstr-html-ref))
360 (defun hyperobject-class-fmtstr-xml-ref (obj)
361 (slot-value (class-of obj) 'fmtstr-xml-ref))
363 (defun hyperobject-class-fmtstr-html-ref-labels (obj)
364 (slot-value (class-of obj) 'fmtstr-html-ref-labels))
366 (defun hyperobject-class-fmtstr-xml-ref-labels (obj)
367 (slot-value (class-of obj) 'fmtstr-xml-ref-labels))
369 ;;;; Generic Print functions
371 (defparameter *default-textformat* nil)
372 (defparameter *default-htmlformat* nil)
373 (defparameter *default-htmlrefformat* nil)
374 (defparameter *default-xhtmlformat* nil)
375 (defparameter *default-xhtmlrefformat* nil)
376 (defparameter *default-xmlformat* nil)
377 (defparameter *default-xmlrefformat* nil)
378 (defparameter *default-ie-xmlrefformat* nil)
379 (defparameter *default-nullformat* nil)
380 (defparameter *default-init-format?* nil)
382 (defun make-format-instance (fmt)
383 (unless *default-init-format?*
384 (setq *default-textformat* (make-instance 'textformat))
385 (setq *default-htmlformat* (make-instance 'htmlformat))
386 (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
387 (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
388 (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
389 (setq *default-xmlformat* (make-instance 'xmlformat))
390 (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
391 (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
392 (setq *default-nullformat* (make-instance 'nullformat))
393 (setq *default-init-format?* t))
396 (:text *default-textformat*)
397 (:html *default-htmlformat*)
398 (:htmlref *default-htmlrefformat*)
399 (:xhtml *default-xhtmlformat*)
400 (:xhtmlref *default-xhtmlrefformat*)
401 (:xml *default-xmlformat*)
402 (:xmlref *default-xmlrefformat*)
403 (:ie-xmlref *default-ie-xmlrefformat*)
404 (:null *default-nullformat*)
405 (otherwise *default-textformat*)))
407 ;;;; Output format classes for print hyperobject-classes
409 (defclass dataformat ()
410 ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
411 (file-end-str :type string :initarg :file-end-str :reader file-end-str)
412 (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
413 (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
414 (list-start-indent :initarg :list-start-indent :reader list-start-indent)
415 (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
416 (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
417 (list-end-indent :initarg :list-end-indent :reader list-end-indent)
418 (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
419 (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
420 (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
421 (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
422 (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
423 (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
424 (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
425 (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
426 (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
427 (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
428 (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
429 (link-ref :initarg :link-ref :reader link-ref))
430 (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
431 :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
432 :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
433 :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
434 :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
435 :obj-data-value-func nil :link-ref nil)
436 (:documentation "Parent for all dataformat objects"))
438 (defclass binaryformat (dataformat)
441 (defclass nullformat (dataformat)
444 (defun text-list-start-value-func (obj nitems)
445 (values (hyperobject-class-title obj) nitems))
447 (defclass textformat (dataformat)
449 (:default-initargs :list-start-fmtstr "~a~P:~%"
450 :list-start-value-func #'text-list-start-value-func
453 :obj-data-fmtstr #'hyperobject-class-fmtstr-text
454 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-text-labels
455 :obj-data-end-fmtstr "~%"
456 :obj-data-value-func #'hyperobject-class-value-func))
459 (defun class-name-of (obj)
460 (string-downcase (class-name (class-of obj))))
462 (defun htmlformat-list-start-value-func (x nitems)
463 (values (hyperobject-class-title x) nitems (class-name-of x)))
465 (defclass htmlformat (textformat)
467 (:default-initargs :file-start-str "<html><body>~%"
468 :file-end-str "</body><html>~%"
470 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
471 :list-start-value-func #'htmlformat-list-start-value-func
472 :list-end-fmtstr "</ul></div>~%"
474 :list-end-value-func #'identity
476 :obj-start-fmtstr "<li>"
477 :obj-start-value-func #'identity
479 :obj-end-fmtstr "</li>~%"
480 :obj-end-value-func #'identity
482 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
483 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
484 :obj-data-value-func #'hyperobject-class-value-func))
486 (defclass xhtmlformat (textformat)
488 (:default-initargs :file-start-str "<html><body>~%"
489 :file-end-str "</body><html>~%"
491 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
492 :list-start-value-func #'htmlformat-list-start-value-func
493 :list-end-fmtstr "</ul></div>~%"
495 :list-end-value-func #'identity
497 :obj-start-fmtstr "<li>"
498 :obj-start-value-func #'identity
500 :obj-end-fmtstr "</li>~%"
501 :obj-end-value-func #'identity
503 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
504 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
505 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
508 (defun xmlformat-list-end-value-func (x)
509 (format nil "~alist" (class-name-of x)))
511 (defun xmlformat-list-start-value-func (x nitems)
512 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-title x) nitems))
514 (defclass xmlformat (textformat)
516 (:default-initargs :file-start-str "" ; (std-xml-header)
518 :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
519 :list-start-value-func #'xmlformat-list-start-value-func
521 :list-end-fmtstr "</~a>~%"
522 :list-end-value-func #'xmlformat-list-end-value-func
523 :obj-start-fmtstr "<~a>"
524 :obj-start-value-func #'class-name-of
526 :obj-end-fmtstr "</~a>~%"
527 :obj-end-value-func #'class-name-of
530 :obj-data-fmtstr #'hyperobject-class-fmtstr-xml
531 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-xml-labels
532 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
534 (defclass link-ref ()
535 ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
536 (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
537 (page-name :type string :initarg :page-name :accessor page-name)
538 (href-head :type string :initarg :href-head :accessor href-head)
539 (href-end :type string :initarg :href-end :accessor href-end)
540 (ampersand :type string :initarg :ampersand :accessor ampersand))
541 (:default-initargs :fmtstr nil
543 :page-name "disp-func1"
544 :href-head nil :href-end nil :ampersand nil)
545 (:documentation "Formatting for a linked reference"))
547 (defclass html-link-ref (link-ref)
549 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
550 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
555 (defclass xhtml-link-ref (link-ref)
557 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
558 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
563 (defclass xml-link-ref (link-ref)
565 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-xml-ref
566 :fmtstr-labels #'hyperobject-class-fmtstr-xml-ref-labels
567 :href-head "xmllink xlink:type=\"simple\" xlink:href="
570 (:documentation "Mozilla's and W3's idea of a link with XML"))
572 (defclass ie-xml-link-ref (xml-link-ref)
574 (:default-initargs :href-head "html:a href="
576 (:documentation "Internet Explorer's idea of a link with XML"))
579 (defclass htmlrefformat (htmlformat)
581 (:default-initargs :link-ref (make-instance 'html-link-ref)))
583 (defclass xhtmlrefformat (xhtmlformat)
585 (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
587 (defclass xmlrefformat (xmlformat)
589 (:default-initargs :link-ref (make-instance 'xml-link-ref)))
591 (defclass ie-xmlrefformat (xmlformat)
593 (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
596 ;;; File Start and Ends
598 (defgeneric fmt-file-start (fmt s))
599 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
601 (defmethod fmt-file-start ((fmt textformat) (s stream))
602 (aif (file-start-str fmt)
605 (defgeneric fmt-file-end (fmt s))
606 (defmethod fmt-file-end ((fmt textformat) (s stream))
607 (aif (file-end-str fmt)
610 ;;; List Start and Ends
612 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
613 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
614 (if (list-start-indent fmt)
615 (indent-spaces indent s))
616 (aif (list-start-fmtstr fmt)
619 (funcall (list-start-value-func fmt) x num-items)))))
621 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
622 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
623 (declare (ignore num-items))
624 (if (list-end-indent fmt)
625 (indent-spaces indent s))
626 (aif (list-end-fmtstr fmt)
629 (funcall (list-end-value-func fmt) x)))))
631 ;;; Object Start and Ends
633 (defgeneric fmt-obj-start (obj fmt s &optional indent))
634 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
635 (if (obj-start-indent fmt)
636 (indent-spaces indent s))
637 (aif (obj-start-fmtstr fmt)
640 (funcall (obj-start-value-func fmt) x)))))
642 (defgeneric fmt-obj-end (obj fmt s &optional indent))
643 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
644 (if (obj-end-indent fmt)
645 (indent-spaces indent s))
646 (aif (obj-end-fmtstr fmt)
649 (funcall (obj-end-value-func fmt) x)))))
653 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
654 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
655 (declare (ignore obj fieldname))
656 (format nil "~a\"~a?func=~a~akey=~a~a\""
657 (href-head ref) (make-url (page-name ref)) fieldfunc
658 (ampersand ref) fieldvalue
661 (dolist (var refvars)
662 (string-append varstr (format nil "~a~a=~a"
663 (ampersand ref) (car var) (cadr var))))
667 (defgeneric make-link-end (obj ref fieldname))
668 (defmethod make-link-end (obj (ref link-ref) fieldname)
669 (declare (ignore obj fieldname))
670 (format nil "~a" (href-end ref))
673 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
674 (defmethod fmt-obj-data (x (fmt textformat) s
675 &optional (indent 0) (label nil) (refvars nil))
676 (if (obj-data-indent fmt)
677 (indent-spaces indent s))
679 (fmt-obj-data-with-ref x fmt s label refvars)
680 (fmt-obj-data-plain x fmt s label))
681 (aif (obj-data-end-fmtstr fmt)
684 (defgeneric fmt-obj-data-plain (obj fmt s label))
685 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
688 (funcall (obj-data-fmtstr-labels fmt) x)
690 (funcall (funcall (obj-data-value-func fmt) x) x)))
691 (apply #'format s (funcall (obj-data-fmtstr fmt) x)
693 (funcall (funcall (obj-data-value-func fmt) x) x)))))
695 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
696 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
697 (let ((refstr (make-ref-data-str x fmt label))
701 (funcall (funcall (obj-data-value-func fmt) x) x))))
703 ;; make list of reference link fields for printing to refstr template
704 (dolist (ref (hyperobject-class-references x))
706 (make-link-start x (link-ref fmt) (name ref) (lookup ref)
707 (nth (position (name ref)
708 (hyperobject-class-fields x)
710 (slot-definition-name x)))
712 (append (link-parameters ref) refvars)))
713 (link-end (make-link-end x (link-ref fmt) (name ref))))
714 (push link-start refvalues)
715 (push link-end refvalues)))
716 (setq refvalues (nreverse refvalues))
718 (apply #'format s refstr refvalues)))
720 (defgeneric obj-data (obj))
721 (defmethod obj-data (x)
722 "Returns the objects data as a string. Used by common-graphics outline function"
723 (let ((fmt (make-format-instance :text)))
724 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
726 (funcall (funcall (obj-data-value-func fmt) x) x)))))
728 (defgeneric make-ref-data-str (obj fmt &optional label))
729 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
730 "Return fmt string for that contains ~a slots for reference link start and end"
731 (unless (link-ref fmt)
732 (error "fmt does not contain a link-ref"))
735 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
737 (funcall (funcall (obj-data-value-func fmt) x) x)))
738 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
739 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
742 ;;; Display method for objects
745 (defgeneric load-all-subobjects (objs))
746 (defmethod load-all-subobjects (objs)
747 "Load all subobjects if they have not already been loaded."
749 (let ((objlist (mklist objs)))
750 (dolist (obj objlist)
751 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
752 (dolist (child-obj it) ;; for each child function
753 (awhen (funcall (reader child-obj) obj)
754 (load-all-subobjects it))))))
757 (defgeneric print-hyperobject-class (objs fmt strm
758 &optional label english-only-function
759 indent subobjects refvars))
761 (defmethod print-hyperobject-class (objs (fmt dataformat) (strm stream)
762 &optional (label nil) (indent 0)
763 (english-only-function nil)
764 (subobjects nil) (refvars nil))
765 "Display a single or list of hyperobject-class instances and their subobjects"
767 (setq objs (mklist objs))
768 (let ((nobjs (length objs)))
769 (fmt-list-start (car objs) fmt strm indent nobjs)
771 (unless (and english-only-function
772 (multiple-value-bind (eng term) (funcall english-only-function obj)
773 (and term (not eng))))
774 (fmt-obj-start obj fmt strm indent)
775 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
777 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
778 (dolist (child-obj it) ;; for each child function
779 (awhen (funcall (reader child-obj) obj) ;; access set of child objects
780 (print-hyperobject-class it fmt strm label
781 (1+ indent) english-only-function
782 subobjects refvars)))))
783 (fmt-obj-end obj fmt strm indent)))
784 (fmt-list-end (car objs) fmt strm indent nobjs))
789 (defun print-hyperobject (objs &key (os *standard-output*) (format :text)
790 (label nil) (english-only-function nil)
791 (subobjects nil) (file-wrapper t) (refvars nil))
792 "EXPORTED Function: prints hyperobject-class objects. Simplies call to print-hyperobject-class"
793 (let ((fmt (make-format-instance format)))
795 (fmt-file-start fmt os))
797 (print-hyperobject-class objs fmt os label 0 english-only-function subobjects refvars))
799 (fmt-file-end fmt os)))
803 (defclass hyperobject ()
805 (:metaclass hyperobject-class))
808 (defmethod print-object ((obj hyperobject) (s stream))
809 (print-unreadable-object (obj s :type t :identity t)
810 (let ((fmt (make-instance 'hyperobject::textformat)))
812 s (funcall (hyperobject-mop::obj-data-fmtstr fmt) obj)
814 (funcall (funcall (hyperobject-mop::obj-data-value-func fmt) obj) obj))))))