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.1 2002/11/03 19:59:10 kevin Exp $
16 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
18 ;;;; *************************************************************************
20 (in-package :hyperobject)
23 (eval-when (:compile-toplevel :execute)
24 (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
29 (defun my-class-of (obj)
30 #-(or cmu sbcl) (class-of obj)
31 #+sbcl (sb-pcl:class-of obj)
32 #+cmu (pcl:class-of obj))
34 (defun my-class-name (obj)
35 #-(or cmu sbcl) (class-name obj)
36 #+sbcl (sb-pcl:class-name obj)
37 #+cmu (pcl:class-name obj))
39 (defun xml-cdata (str)
40 (concatenate 'string "<![CDATA[" str "]]>"))
43 (defclass ho-class (#-(or cmu sbcl) standard-class
44 #+cmu pcl::standard-class
45 #+sbcl sb-pcl::standard-class)
46 ((title :initarg :title :type string :reader ml-std-title
48 "Print Title for class")
49 (fields :initarg :fields :reader ml-std-fields
51 "List of field lists for printing. Format is
52 ((fieldname type optional-formatter) ... )")
54 :initarg :subobjects-lists :reader ml-std-subobjects-lists
56 "List of fields that contain a list of subobjects objects.")
58 :initarg :ref-fields :type list :reader ml-std-ref-field
60 "List of fields that can be referred to by browsers.
61 Format is ((field-name field-lookup-func other-link-params) ...)")
63 ;;; The remainder of these fields are calculated one time
64 ;;; in finalize-inheritence.
65 (value-func :initform nil :type function :reader ml-std-value-func)
66 (xmlvalue-func :initform nil :type function :reader ml-std-xmlvalue-func)
67 (fmtstr-text :initform nil :type string :reader ml-std-fmtstr-text)
68 (fmtstr-html :initform nil :type string :reader ml-std-fmtstr-html)
69 (fmtstr-xml :initform nil :type string :reader ml-std-fmtstr-xml)
70 (fmtstr-text-labels :initform nil :type string :reader ml-std-fmtstr-text-labels)
71 (fmtstr-html-labels :initform nil :type string :reader ml-std-fmtstr-html-labels)
72 (fmtstr-xml-labels :initform nil :type string :reader ml-std-fmtstr-xml-labels)
73 (fmtstr-html-ref :initform nil :type string :reader ml-std-fmtstr-html-ref)
74 (fmtstr-xml-ref :initform nil :type string :reader ml-std-fmtstr-xml-ref)
75 (fmtstr-html-ref-labels :initform nil :type string :reader ml-std-fmtstr-html-ref-labels)
76 (fmtstr-xml-ref-labels :initform nil :type string :reader ml-std-fmtstr-xml-ref-labels)
78 (:default-initargs :title nil :fields nil :subobjects-lists nil :ref-fields nil)
79 (:documentation "Metaclass for Markup Language classes."))
82 (defmethod pcl:finalize-inheritance :after ((cl ho-class))
86 (defmethod clos:finalize-inheritance :after ((cl ho-class))
91 (defmethod sb-pcl:finalize-inheritance :after ((cl ho-class))
96 (defmethod pcl:validate-superclass ((class ho-class) (superclass pcl::standard-class))
100 (defmethod clos:validate-superclass ((class ho-class) (superclass standard-class))
104 (defmethod sb-pcl:validate-superclass ((class ho-class) (superclass sb-pcl::standard-class))
108 (defmethod mop:finalize-inheritance :after ((cl ho-class))
112 (defmethod clos:finalize-inheritance :after ((cl ho-class))
116 (defmethod clos:process-a-class-option ((class ho-class)
120 (error "ho-class title must have a value"))
121 (if (null (cdr value))
122 (list name (car value))
123 (list name `',value)))
126 (defmethod clos:process-a-class-option ((class ho-class)
130 (error "ho-class fields must have a value"))
131 (list name `',value))
134 (defmethod clos:process-a-class-option ((class ho-class)
135 (name (eql :ref-fields))
138 (error "ho-class ref-fields must have a value"))
139 (list name `',value))
142 (defmethod clos:process-a-class-option ((class ho-class)
143 (name (eql :subobjects-lists))
146 (error "ho-class subobjects-lists must have a value"))
147 (list name `',value))
149 ;;;; Class initialization function
151 (defun init-ho-class (cl)
152 (let ((fmtstr-text "")
155 (fmtstr-text-labels "")
156 (fmtstr-html-labels "")
157 (fmtstr-xml-labels "")
160 (fmtstr-html-ref-labels "")
161 (fmtstr-xml-ref-labels "")
165 (classname (class-name cl))
166 (package (symbol-package (my-class-name cl)))
167 (ref-fields (slot-value cl 'ref-fields)))
168 (declare (ignore classname))
169 (dolist (f (slot-value cl 'fields))
171 (namestr (symbol-name (car f)))
172 (namestr-lower (string-downcase (symbol-name (car f))))
174 (formatter (caddr f))
176 (plain-value-func nil)
177 html-str xml-str html-label-str xml-label-str)
179 (when (or (eql type :integer) (eql type :fixnum))
180 (setq value-fmt "~d"))
182 (when (eql type :commainteger)
183 (setq value-fmt "~:d"))
185 (when (eql type :boolean)
186 (setq value-fmt "~a"))
189 (setq first-field nil)
191 (string-append fmtstr-text " ")
192 (string-append fmtstr-html " ")
193 (string-append fmtstr-xml " ")
194 (string-append fmtstr-text-labels " ")
195 (string-append fmtstr-html-labels " ")
196 (string-append fmtstr-xml-labels " ")
197 (string-append fmtstr-html-ref " ")
198 (string-append fmtstr-xml-ref " ")
199 (string-append fmtstr-html-ref-labels " ")
200 (string-append fmtstr-xml-ref-labels " ")))
202 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
203 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
204 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
205 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
207 (string-append fmtstr-text value-fmt)
208 (string-append fmtstr-html html-str)
209 (string-append fmtstr-xml xml-str)
210 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
211 (string-append fmtstr-html-labels html-label-str)
212 (string-append fmtstr-xml-labels xml-label-str)
214 (if (find name ref-fields :key #'car)
216 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
217 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
218 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
219 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>"))
221 (string-append fmtstr-html-ref html-str)
222 (string-append fmtstr-xml-ref xml-str)
223 (string-append fmtstr-html-ref-labels html-label-str)
224 (string-append fmtstr-xml-ref-labels xml-label-str)))
227 (setq plain-value-func
228 (list `(,formatter (,(intern namestr package) x))))
229 (setq plain-value-func
230 (list `(,(intern namestr package) x))))
231 (setq value-func (append value-func plain-value-func))
233 (if (eql type :cdata)
234 (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
235 (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
239 (setq value-func `(lambda (x) (values ,@value-func)))
240 (setq value-func `(lambda () (values))))
241 (setq value-func (compile nil (eval value-func)))
244 (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
245 (setq xmlvalue-func `(lambda () (values))))
246 (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
248 (setf (slot-value cl 'fmtstr-text) fmtstr-text)
249 (setf (slot-value cl 'fmtstr-html) fmtstr-html)
250 (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
251 (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
252 (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
253 (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
254 (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
255 (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
256 (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
257 (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
258 (setf (slot-value cl 'value-func) value-func)
259 (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
263 (defun ho-class-fmtstr-text (obj)
264 (slot-value (my-class-of obj) 'fmtstr-text))
266 (defun ho-class-fmtstr-html (obj)
267 (slot-value (my-class-of obj) 'fmtstr-html))
269 (defun ho-class-fmtstr-xml (obj)
270 (slot-value (my-class-of obj) 'fmtstr-xml))
272 (defun ho-class-fmtstr-text-labels (obj)
273 (slot-value (my-class-of obj) 'fmtstr-text-labels))
275 (defun ho-class-fmtstr-html-labels (obj)
276 (slot-value (my-class-of obj) 'fmtstr-html-labels))
278 (defun ho-class-fmtstr-xml-labels (obj)
279 (slot-value (my-class-of obj) 'fmtstr-xml-labels))
281 (defun ho-class-value-func (obj)
282 (slot-value (my-class-of obj) 'value-func))
284 (defun ho-class-xmlvalue-func (obj)
285 (slot-value (my-class-of obj) 'xmlvalue-func))
287 (eval-when (:compile-toplevel :load-toplevel :execute)
288 (defun ho-class-title (obj)
289 (awhen (slot-value (my-class-of obj) 'title)
294 (defun ho-class-subobjects-lists (obj)
295 (slot-value (my-class-of obj) 'subobjects-lists))
297 (defun ho-class-ref-fields (obj)
298 (slot-value (my-class-of obj) 'ref-fields))
300 (defun ho-class-fields (obj)
301 (slot-value (my-class-of obj) 'fields))
303 (defun ho-class-fmtstr-html-ref (obj)
304 (slot-value (my-class-of obj) 'fmtstr-html-ref))
306 (defun ho-class-fmtstr-xml-ref (obj)
307 (slot-value (my-class-of obj) 'fmtstr-xml-ref))
309 (defun ho-class-fmtstr-html-ref-labels (obj)
310 (slot-value (my-class-of obj) 'fmtstr-html-ref-labels))
312 (defun ho-class-fmtstr-xml-ref-labels (obj)
313 (slot-value (my-class-of obj) 'fmtstr-xml-ref-labels))
315 ;;; Class name functions
317 (defgeneric ho-class-stdname (x))
318 (defmethod ho-class-stdname ((name string))
319 (string-downcase (subseq name 1)))
321 (defmethod ho-class-stdname ((cl standard-object))
322 (string-downcase (subseq (my-class-name (my-class-of cl)) 1)))
324 ;;;; Generic Print functions
326 (defparameter *default-textformat* nil)
327 (defparameter *default-htmlformat* nil)
328 (defparameter *default-htmlrefformat* nil)
329 (defparameter *default-xhtmlformat* nil)
330 (defparameter *default-xhtmlrefformat* nil)
331 (defparameter *default-xmlformat* nil)
332 (defparameter *default-xmlrefformat* nil)
333 (defparameter *default-ie-xmlrefformat* nil)
334 (defparameter *default-nullformat* nil)
335 (defparameter *default-init-format?* nil)
337 (defun make-format-instance (fmt)
338 (unless *default-init-format?*
339 (setq *default-textformat* (make-instance 'textformat))
340 (setq *default-htmlformat* (make-instance 'htmlformat))
341 (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
342 (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
343 (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
344 (setq *default-xmlformat* (make-instance 'xmlformat))
345 (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
346 (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
347 (setq *default-nullformat* (make-instance 'nullformat))
348 (setq *default-init-format?* t))
351 (:text *default-textformat*)
352 (:html *default-htmlformat*)
353 (:htmlref *default-htmlrefformat*)
354 (:xhtml *default-xhtmlformat*)
355 (:xhtmlref *default-xhtmlrefformat*)
356 (:xml *default-xmlformat*)
357 (:xmlref *default-xmlrefformat*)
358 (:ie-xmlref *default-ie-xmlrefformat*)
359 (:null *default-nullformat*)
360 (otherwise *default-textformat*)))
362 ;;;; Output format classes for print ho-classes
364 (defclass dataformat ()
365 ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
366 (file-end-str :type string :initarg :file-end-str :reader file-end-str)
367 (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
368 (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
369 (list-start-indent :initarg :list-start-indent :reader list-start-indent)
370 (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
371 (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
372 (list-end-indent :initarg :list-end-indent :reader list-end-indent)
373 (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
374 (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
375 (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
376 (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
377 (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
378 (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
379 (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
380 (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
381 (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
382 (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
383 (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
384 (link-ref :initarg :link-ref :reader link-ref))
385 (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
386 :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
387 :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
388 :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
389 :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
390 :obj-data-value-func nil :link-ref nil)
391 (:documentation "Parent for all dataformat objects"))
393 (defclass binaryformat (dataformat)
396 (defclass nullformat (dataformat)
399 (defun text-list-start-value-func (obj nitems)
400 (values (ho-class-title obj) nitems))
402 (defclass textformat (dataformat)
404 (:default-initargs :list-start-fmtstr "~a~P:~%"
405 :list-start-value-func #'text-list-start-value-func
408 :obj-data-fmtstr #'ho-class-fmtstr-text
409 :obj-data-fmtstr-labels #'ho-class-fmtstr-text-labels
410 :obj-data-end-fmtstr "~%"
411 :obj-data-value-func #'ho-class-value-func))
414 (defun class-name-of (obj)
415 (string-downcase (my-class-name (my-class-of obj))))
417 (defun htmlformat-list-start-value-func (x nitems)
418 (values (ho-class-title x) nitems (class-name-of x)))
420 (defclass htmlformat (textformat)
422 (:default-initargs :file-start-str "<html><body>~%"
423 :file-end-str "</body><html>~%"
425 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
426 :list-start-value-func #'htmlformat-list-start-value-func
427 :list-end-fmtstr "</ul></div>~%"
429 :list-end-value-func #'identity
431 :obj-start-fmtstr "<li>"
432 :obj-start-value-func #'identity
434 :obj-end-fmtstr "</li>~%"
435 :obj-end-value-func #'identity
437 :obj-data-fmtstr #'ho-class-fmtstr-html-labels
438 :obj-data-fmtstr-labels #'ho-class-fmtstr-html-labels
439 :obj-data-value-func #'ho-class-value-func))
441 (defclass xhtmlformat (textformat)
443 (:default-initargs :file-start-str "<html><body>~%"
444 :file-end-str "</body><html>~%"
446 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
447 :list-start-value-func #'htmlformat-list-start-value-func
448 :list-end-fmtstr "</ul></div>~%"
450 :list-end-value-func #'identity
452 :obj-start-fmtstr "<li>"
453 :obj-start-value-func #'identity
455 :obj-end-fmtstr "</li>~%"
456 :obj-end-value-func #'identity
458 :obj-data-fmtstr #'ho-class-fmtstr-html-labels
459 :obj-data-fmtstr-labels #'ho-class-fmtstr-html-labels
460 :obj-data-value-func #'ho-class-xmlvalue-func))
463 (defun xmlformat-list-end-value-func (x)
464 (format nil "~alist" (class-name-of x)))
466 (defun xmlformat-list-start-value-func (x nitems)
467 (values (format nil "~alist" (class-name-of x)) (ho-class-title x) nitems))
469 (defclass xmlformat (textformat)
471 (:default-initargs :file-start-str "" ; (std-xml-header)
473 :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
474 :list-start-value-func #'xmlformat-list-start-value-func
476 :list-end-fmtstr "</~a>~%"
477 :list-end-value-func #'xmlformat-list-end-value-func
478 :obj-start-fmtstr "<~a>"
479 :obj-start-value-func #'class-name-of
481 :obj-end-fmtstr "</~a>~%"
482 :obj-end-value-func #'class-name-of
485 :obj-data-fmtstr #'ho-class-fmtstr-xml
486 :obj-data-fmtstr-labels #'ho-class-fmtstr-xml-labels
487 :obj-data-value-func #'ho-class-xmlvalue-func))
489 (defclass link-ref ()
490 ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
491 (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
492 (page-name :type string :initarg :page-name :accessor page-name)
493 (href-head :type string :initarg :href-head :accessor href-head)
494 (href-end :type string :initarg :href-end :accessor href-end)
495 (ampersand :type string :initarg :ampersand :accessor ampersand))
496 (:default-initargs :fmtstr nil
498 :page-name "disp-func1"
499 :href-head nil :href-end nil :ampersand nil)
500 (:documentation "Formatting for a linked reference"))
502 (defclass html-link-ref (link-ref)
504 (:default-initargs :fmtstr #'ho-class-fmtstr-html-ref
505 :fmtstr-labels #'ho-class-fmtstr-html-ref-labels
510 (defclass xhtml-link-ref (link-ref)
512 (:default-initargs :fmtstr #'ho-class-fmtstr-html-ref
513 :fmtstr-labels #'ho-class-fmtstr-html-ref-labels
518 (defclass xml-link-ref (link-ref)
520 (:default-initargs :fmtstr #'ho-class-fmtstr-xml-ref
521 :fmtstr-labels #'ho-class-fmtstr-xml-ref-labels
522 :href-head "xmllink xlink:type=\"simple\" xlink:href="
525 (:documentation "Mozilla's and W3's idea of a link with XML"))
527 (defclass ie-xml-link-ref (xml-link-ref)
529 (:default-initargs :href-head "html:a href="
531 (:documentation "Internet Explorer's idea of a link with XML"))
534 (defclass htmlrefformat (htmlformat)
536 (:default-initargs :link-ref (make-instance 'html-link-ref)))
538 (defclass xhtmlrefformat (xhtmlformat)
540 (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
542 (defclass xmlrefformat (xmlformat)
544 (:default-initargs :link-ref (make-instance 'xml-link-ref)))
546 (defclass ie-xmlrefformat (xmlformat)
548 (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
551 ;;; File Start and Ends
553 (defgeneric fmt-file-start (fmt s))
554 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
556 (defmethod fmt-file-start ((fmt textformat) (s stream))
557 (aif (file-start-str fmt)
560 (defgeneric fmt-file-end (fmt s))
561 (defmethod fmt-file-end ((fmt textformat) (s stream))
562 (aif (file-end-str fmt)
565 ;;; List Start and Ends
567 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
568 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
569 (if (list-start-indent fmt)
570 (indent-spaces indent s))
571 (aif (list-start-fmtstr fmt)
574 (funcall (list-start-value-func fmt) x num-items)))))
576 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
577 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
578 (declare (ignore num-items))
579 (if (list-end-indent fmt)
580 (indent-spaces indent s))
581 (aif (list-end-fmtstr fmt)
584 (funcall (list-end-value-func fmt) x)))))
586 ;;; Object Start and Ends
588 (defgeneric fmt-obj-start (obj fmt s &optional indent))
589 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
590 (if (obj-start-indent fmt)
591 (indent-spaces indent s))
592 (aif (obj-start-fmtstr fmt)
595 (funcall (obj-start-value-func fmt) x)))))
597 (defgeneric fmt-obj-end (obj fmt s &optional indent))
598 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
599 (if (obj-end-indent fmt)
600 (indent-spaces indent s))
601 (aif (obj-end-fmtstr fmt)
604 (funcall (obj-end-value-func fmt) x)))))
608 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
609 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
610 (declare (ignore obj fieldname))
611 (format nil "~a\"~a?func=~a~akey=~a~a\""
612 (href-head ref) (make-url (page-name ref)) fieldfunc
613 (ampersand ref) fieldvalue
616 (dolist (var refvars)
617 (string-append varstr (format nil "~a~a=~a"
618 (ampersand ref) (car var) (cadr var))))
622 (defgeneric make-link-end (obj ref fieldname))
623 (defmethod make-link-end (obj (ref link-ref) fieldname)
624 (declare (ignore obj fieldname))
625 (format nil "~a" (href-end ref))
628 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
629 (defmethod fmt-obj-data (x (fmt textformat) s
630 &optional (indent 0) (label nil) (refvars nil))
631 (if (obj-data-indent fmt)
632 (indent-spaces indent s))
634 (fmt-obj-data-with-ref x fmt s label refvars)
635 (fmt-obj-data-plain x fmt s label))
636 (aif (obj-data-end-fmtstr fmt)
639 (defgeneric fmt-obj-data-plain (obj fmt s label))
640 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
643 (funcall (obj-data-fmtstr-labels fmt) x)
645 (funcall (funcall (obj-data-value-func fmt) x) x)))
646 (apply #'format s (funcall (obj-data-fmtstr fmt) x)
648 (funcall (funcall (obj-data-value-func fmt) x) x)))))
650 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
651 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
652 (let ((refstr (make-ref-data-str x fmt label))
656 (funcall (funcall (obj-data-value-func fmt) x) x))))
658 ;; make list of reference link fields for printing to refstr template
659 (dolist (field (ho-class-ref-fields x))
661 (make-link-start x (link-ref fmt) (car field) (cadr field)
662 (nth (position (car field) (ho-class-fields x) :key #'car) field-values)
663 (append (caddr field) refvars)))
664 (link-end (make-link-end x (link-ref fmt) (car field))))
665 (push link-start refvalues)
666 (push link-end refvalues)))
667 (setq refvalues (nreverse refvalues))
669 (apply #'format s refstr refvalues)))
671 (defgeneric obj-data (obj))
672 (defmethod obj-data (x)
673 "Returns the objects data as a string. Used by common-graphics outline function"
674 (let ((fmt (make-format-instance :text)))
675 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
677 (funcall (funcall (obj-data-value-func fmt) x) x)))))
679 (defgeneric make-ref-data-str (obj fmt &optional label))
680 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
681 "Return fmt string for that contains ~a slots for reference link start and end"
682 (unless (link-ref fmt)
683 (error "fmt does not contain a link-ref"))
686 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
688 (funcall (funcall (obj-data-value-func fmt) x) x)))
689 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
690 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
693 ;;; Display method for objects
696 (defgeneric load-all-subobjects (objs))
697 (defmethod load-all-subobjects (objs)
698 "Load all subobjects if they have not already been loaded."
700 (let ((objlist (mklist objs)))
701 (dolist (obj objlist)
702 (awhen (ho-class-subobjects-lists obj) ;; access list of functions
703 (dolist (child-obj it) ;; for each child function
704 (awhen (funcall (car child-obj) obj)
705 (load-all-subobjects it))))))
708 (defgeneric output-ho-class (objs fmt strm
709 &optional label english-only-function
710 indent subobjects refvars))
712 (defmethod output-ho-class (objs (fmt dataformat) (strm stream)
713 &optional (label nil) (indent 0)
714 (english-only-function nil)
715 (subobjects nil) (refvars nil))
716 "Display a single or list of ho-class instances and their subobjects"
718 (setq objs (mklist objs))
719 (let ((nobjs (length objs)))
720 (fmt-list-start (car objs) fmt strm indent nobjs)
722 (unless (and english-only-function
723 (multiple-value-bind (eng term) (funcall english-only-function obj)
724 (and term (not eng))))
725 (fmt-obj-start obj fmt strm indent)
726 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
728 (awhen (ho-class-subobjects-lists obj) ;; access list of functions
729 (dolist (child-obj it) ;; for each child function
730 (awhen (funcall (car child-obj) obj) ;; access set of child objects
731 (output-ho-class it fmt strm label
732 english-only-function
733 (1+ indent) subobjects refvars)))))
734 (fmt-obj-end obj fmt strm indent)))
735 (fmt-list-end (car objs) fmt strm indent nobjs))
739 (defun print-ho (objs &key (os *standard-output*) (format :text)
740 (label nil) (english-only-function nil)
741 (subobjects nil) (file-wrapper t) (refvars nil))
742 "EXPORTED Function: prints ho-class objects. Simplies call to output-ho-class"
743 (let ((fmt (make-format-instance format)))
745 (fmt-file-start fmt os))
747 (output-ho-class objs fmt os label english-only-function 0 subobjects refvars))
749 (fmt-file-end fmt os)))