r2991: *** empty log message ***
[kmrcl.git] / ml-class.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          ml-class.lisp
6 ;;;; Purpose:       Markup Language Metaclass
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
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.
13 ;;;;
14 ;;;; $Id: ml-class.lisp,v 1.6 2002/10/13 17:39:50 kevin Exp $
15 ;;;;
16 ;;;; This file, part of KMRCL, is Copyright (c) 2002 by Kevin M. Rosenberg
17 ;;;;
18 ;;;; KMRCL users are granted the rights to distribute and use this software
19 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
20 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
21 ;;;; *************************************************************************
22  
23 (in-package :kmrcl)
24
25 (declaim (optimize (speed 3) (safety 1)))
26
27 (defun ml-class-of (obj)
28   #-(or cmu sbcl) (class-of obj)
29   #+sbcl (sb-pcl:class-of obj)
30   #+cmu (pcl:class-of obj))
31
32 (defclass ml-class (standard-class)
33   ((title :initarg :title :type string :reader ml-std-title
34           :documentation 
35 "Print Title for class")
36    (fields :initarg :fields :reader ml-std-fields
37            :documentation
38 "List of field lists for printing. Format is 
39    ((fieldname type optional-formatter) ... )")
40    (subobjects-lists 
41     :initarg :subobjects-lists :reader ml-std-subobjects-lists
42     :documentation 
43 "List of fields that contain a list of subobjects objects.")
44    (ref-fields 
45     :initarg :ref-fields :type list :reader ml-std-ref-field
46     :documentation 
47     "List of fields that can be referred to by browsers. 
48 Format is ((field-name field-lookup-func other-link-params) ...)")
49    
50    ;;; The remainder of these fields are calculated one time
51    ;;; in finalize-inheritence.
52    (value-func :initform nil :type function :reader ml-std-value-func)
53    (xmlvalue-func :initform nil :type function :reader ml-std-xmlvalue-func)
54    (fmtstr-text :initform nil :type string :reader ml-std-fmtstr-text)
55    (fmtstr-html :initform nil :type string :reader ml-std-fmtstr-html)
56    (fmtstr-xml :initform nil :type string :reader ml-std-fmtstr-xml)
57    (fmtstr-text-labels :initform nil :type string :reader ml-std-fmtstr-text-labels)
58    (fmtstr-html-labels :initform nil :type string :reader ml-std-fmtstr-html-labels)
59    (fmtstr-xml-labels :initform nil :type string :reader ml-std-fmtstr-xml-labels)
60    (fmtstr-html-ref :initform nil :type string :reader ml-std-fmtstr-html-ref)
61    (fmtstr-xml-ref :initform nil :type string :reader ml-std-fmtstr-xml-ref)
62    (fmtstr-html-ref-labels :initform nil :type string :reader ml-std-fmtstr-html-ref-labels)
63    (fmtstr-xml-ref-labels :initform nil :type string :reader ml-std-fmtstr-xml-ref-labels)
64    )
65   (:default-initargs :title nil :fields nil :subobjects-lists nil :ref-fields nil)
66   (:documentation "Metaclass for Markup Language classes."))
67
68 #+cmu
69 (defmethod pcl:validate-superclass ((class ml-class) (superclass pcl::standard-class))
70   t)
71
72 #+sbcl
73 (defmethod sb-pcl:validate-superclass ((class ml-class) (superclass sb-pcl::standard-class))
74   t)
75
76 #+cmu
77 (defmethod pcl:validate-superclass ((class pcl::standard-class) (superclass ml-class))
78   t)
79
80 #+sbcl
81 (defmethod sb-pcl:validate-superclass ((class sb-pcl::standard-class) (superclass ml-class))
82   t)
83
84 #+allegro
85 (defmethod mop:finalize-inheritance :after ((cl ml-class))
86   (init-ml-class cl))
87
88 #+lispworks
89 (defmethod clos:finalize-inheritance :after ((cl ml-class))
90   (init-ml-class cl))
91
92 #+cmu
93 (defmethod pcl:finalize-inheritance :after ((cl ml-class))
94   (init-ml-class cl))
95
96 #+sbcl
97 (defmethod sb-pcl:finalize-inheritance :after ((cl ml-class))
98   (init-ml-class cl))
99
100 #+lispworks
101 (defmethod clos:process-a-class-option ((class ml-class)
102                                         (name (eql :title))
103                                         value)
104   (unless value
105     (error "ml-class title must have a value"))
106   (if (null (cdr value))
107       (list name (car value))
108     (list name `',value)))
109
110 #+lispworks
111 (defmethod clos:process-a-class-option ((class ml-class)
112                                         (name (eql :fields))
113                                         value)
114   (unless value
115     (error "ml-class fields must have a value"))
116   (list name `',value))
117
118 #+lispworks
119 (defmethod clos:process-a-class-option ((class ml-class)
120                                         (name (eql :ref-fields))
121                                         value)
122   (unless value
123     (error "ml-class ref-fields must have a value"))
124   (list name `',value))
125
126 #+lispworks
127 (defmethod clos:process-a-class-option ((class ml-class)
128                                         (name (eql :subobjects-lists))
129                                         value)
130   (unless value
131     (error "ml-class subobjects-lists must have a value"))
132   (list name `',value))
133
134 ;;;; Class initialization function
135       
136 (defun init-ml-class (cl)
137     (let ((fmtstr-text "")
138           (fmtstr-html "")
139           (fmtstr-xml "")
140           (fmtstr-text-labels "")
141           (fmtstr-html-labels "")
142           (fmtstr-xml-labels "")
143           (fmtstr-html-ref "")
144           (fmtstr-xml-ref "")
145           (fmtstr-html-ref-labels "")
146           (fmtstr-xml-ref-labels "")
147           (first-field t)
148           (value-func '())
149           (xmlvalue-func '())
150           (classname (class-name cl))
151           (ref-fields (slot-value cl 'ref-fields)))
152       (declare (ignore classname))
153       (dolist (f (slot-value cl 'fields))
154         (let ((name (car f))
155               (namestr (symbol-name (car f)))
156               (namestr-lower (string-downcase (symbol-name (car f))))
157               (type (cadr f))
158               (formatter (caddr f))
159               (value-fmt "~a")
160               (plain-value-func nil)
161               html-str xml-str html-label-str xml-label-str)
162           
163           (when (or (eql type :integer) (eql type :fixnum))
164             (setq value-fmt "~d"))
165           
166           (when (eql type :commainteger)
167             (setq value-fmt "~:d"))
168           
169           (when (eql type :boolean)
170             (setq value-fmt "~a"))
171             
172           (if first-field
173               (setq first-field nil)
174             (progn
175               (string-append fmtstr-text " ")
176               (string-append fmtstr-html " ")
177               (string-append fmtstr-xml " ")
178               (string-append fmtstr-text-labels " ")
179               (string-append fmtstr-html-labels " ")
180               (string-append fmtstr-xml-labels " ")
181               (string-append fmtstr-html-ref " ")
182               (string-append fmtstr-xml-ref " ")
183               (string-append fmtstr-html-ref-labels " ")
184               (string-append fmtstr-xml-ref-labels " ")))
185           
186           (setq html-str value-fmt)
187           (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
188           (setq html-label-str (concatenate 'string "<i>" namestr-lower "</i> " value-fmt))
189           (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
190           
191           (string-append fmtstr-text value-fmt)
192           (string-append fmtstr-html html-str)
193           (string-append fmtstr-xml xml-str)
194           (string-append fmtstr-text-labels namestr-lower " " value-fmt)
195           (string-append fmtstr-html-labels html-label-str)
196           (string-append fmtstr-xml-labels xml-label-str)
197           
198           (if (find name ref-fields :key #'car)
199             (progn
200               (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
201               (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
202               (string-append fmtstr-html-ref-labels "<i>" namestr-lower "</i> <~~a>" value-fmt "</~~a>")
203               (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>"))
204             (progn
205               (string-append fmtstr-html-ref html-str)
206               (string-append fmtstr-xml-ref xml-str)
207               (string-append fmtstr-html-ref-labels html-label-str)
208               (string-append fmtstr-xml-ref-labels xml-label-str)))
209           
210           (if formatter
211               (setq plain-value-func 
212                 (list `(,formatter (,(concat-symbol-pkg 
213                                       :umlisp namestr) x))))
214             (setq plain-value-func 
215               (list `(,(concat-symbol-pkg 
216                         :umlisp namestr) x))))
217           (setq value-func (append value-func plain-value-func))
218           
219           (if (eql type :cdata)
220               (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
221             (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
222           ))
223
224       (setq value-func `(lambda (x) (values ,@value-func)))
225       (setq value-func (compile nil (eval value-func)))
226       (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
227       (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
228
229       (setf (slot-value cl 'fmtstr-text) fmtstr-text)
230       (setf (slot-value cl 'fmtstr-html) fmtstr-html)
231       (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
232       (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
233       (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
234       (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
235       (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
236       (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
237       (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
238       (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
239       (setf (slot-value cl 'value-func) value-func)
240       (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
241     (values))
242
243
244 (defun ml-class-fmtstr-text (obj)
245   (slot-value (ml-class-of obj) 'fmtstr-text))
246
247 (defun ml-class-fmtstr-html (obj)
248   (slot-value (ml-class-of obj) 'fmtstr-html))
249
250 (defun ml-class-fmtstr-xml (obj)
251   (slot-value (ml-class-of obj) 'fmtstr-xml))
252
253 (defun ml-class-fmtstr-text-labels (obj)
254   (slot-value (ml-class-of obj) 'fmtstr-text-labels))
255
256 (defun ml-class-fmtstr-html-labels (obj)
257   (slot-value (ml-class-of obj) 'fmtstr-html-labels))
258
259 (defun ml-class-fmtstr-xml-labels (obj)
260   (slot-value (ml-class-of obj) 'fmtstr-xml-labels))
261
262 (defun ml-class-value-func (obj)
263   (slot-value (ml-class-of obj) 'value-func))
264
265 (defun ml-class-xmlvalue-func (obj)
266   (slot-value (ml-class-of obj) 'xmlvalue-func))
267
268 (eval-when (:compile-toplevel :load-toplevel :execute)
269 (defun ml-class-title (obj)
270   (awhen (slot-value (ml-class-of obj) 'title)
271             (if (consp it)
272                 (car it)
273               it))))
274
275 (defun ml-class-subobjects-lists (obj)
276   (slot-value (ml-class-of obj) 'subobjects-lists))
277
278 (defun ml-class-ref-fields (obj)
279   (slot-value (ml-class-of obj) 'ref-fields))
280
281 (defun ml-class-fields (obj)
282   (slot-value (ml-class-of obj) 'fields))
283
284 (defun ml-class-fmtstr-html-ref (obj)
285   (slot-value (ml-class-of obj) 'fmtstr-html-ref))
286
287 (defun ml-class-fmtstr-xml-ref (obj)
288   (slot-value (ml-class-of obj) 'fmtstr-xml-ref))
289
290 (defun ml-class-fmtstr-html-ref-labels (obj)
291   (slot-value (ml-class-of obj) 'fmtstr-html-ref-labels))
292
293 (defun ml-class-fmtstr-xml-ref-labels (obj)
294   (slot-value (ml-class-of obj) 'fmtstr-xml-ref-labels))
295
296 ;;; Class name functions
297
298 (defmethod ml-class-stdname ((name string))
299   (string-downcase (subseq name :start 1)))
300   
301 (defmethod ml-class-stdname ((cl standard-object))
302   (string-downcase (subseq (class-name (ml-class-of cl)) :start 1)))
303   
304 ;;;; Generic Print functions
305
306 (defparameter *default-textformat* nil)
307 (defparameter *default-htmlformat* nil)
308 (defparameter *default-htmlrefformat* nil)
309 (defparameter *default-xmlformat* nil)
310 (defparameter *default-xmlrefformat* nil)
311 (defparameter *default-nullformat* nil)
312 (defparameter *default-init-format?* nil)
313
314 (defun make-format-instance (fmt)
315   (unless *default-init-format?*
316     (setq *default-textformat* (make-instance 'textformat))
317     (setq *default-htmlformat* (make-instance 'htmlformat))
318     (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
319     (setq *default-xmlformat* (make-instance 'xmlformat))
320     (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
321     (setq *default-nullformat* (make-instance 'nullformat))
322     (setq *default-init-format?* t))
323   
324   (case fmt
325       (:text *default-textformat*)
326       (:html *default-htmlformat*)
327       (:htmlref *default-htmlrefformat*)
328       (:xml  *default-xmlformat*)
329       (:xmlref *default-xmlrefformat*)
330       (:null *default-nullformat*)
331       (otherwise *default-textformat*)))
332     
333 ;;;; Output format classes for print ml-classes
334
335 (defclass dataformat ()
336   ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
337    (file-end-str :type string :initarg :file-end-str :reader file-end-str)
338    (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
339    (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
340    (list-start-indent :initarg :list-start-indent :reader list-start-indent)
341    (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
342    (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
343    (list-end-indent :initarg :list-end-indent :reader list-end-indent)
344    (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
345    (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
346    (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
347    (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
348    (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
349    (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
350    (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
351    (obj-data-fmtstr :initarg :obj-data-fmtstr :reader  obj-data-fmtstr)
352    (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader  obj-data-fmtstr-labels)
353    (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
354    (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
355    (link-ref :initarg :link-ref :reader link-ref))
356   (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
357                      :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
358                      :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
359                      :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
360                      :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
361                      :obj-data-value-func nil :link-ref nil)
362   (:documentation "Parent for all dataformat objects"))
363
364 (defclass binaryformat (dataformat)
365   ())
366
367 (defclass nullformat (dataformat)
368   ())
369
370 (defun text-list-start-value-func (obj nitems)
371   (values (ml-class-title obj) nitems))
372
373 (defclass textformat (dataformat) 
374   ()    
375   (:default-initargs :list-start-fmtstr "~a~P:~%"
376     :list-start-value-func #'text-list-start-value-func
377     :list-start-indent t
378     :obj-data-indent t
379     :obj-data-fmtstr #'ml-class-fmtstr-text
380     :obj-data-fmtstr-labels #'ml-class-fmtstr-text-labels
381     :obj-data-end-fmtstr "~%"
382     :obj-data-value-func #'ml-class-value-func))
383
384 (defclass htmlformat (textformat) 
385   ()
386   (:default-initargs :file-start-str "<html><body>~%"
387     :file-end-str "</body><html>~%"
388     :list-start-indent t
389     :list-start-fmtstr "<p><b>~a~P:</b></p><ul>~%"
390     :list-start-value-func #'text-list-start-value-func
391     :list-end-fmtstr "</ul>~%"
392     :list-end-indent t
393     :list-end-value-func #'identity
394     :obj-start-indent t
395     :obj-start-fmtstr "<li>"
396     :obj-start-value-func #'identity
397     :obj-end-indent  t
398     :obj-end-fmtstr  "</li>~%"
399     :obj-end-value-func #'identity
400     :obj-data-indent t
401     :obj-data-fmtstr #'ml-class-fmtstr-html-labels
402     :obj-data-fmtstr-labels #'ml-class-fmtstr-html-labels
403     :obj-data-value-func #'ml-class-value-func))
404
405
406 (defun class-name-of (obj)
407   (string-downcase (class-name (ml-class-of obj))))
408
409 (defun xmlformat-list-end-value-func (x)
410   (format nil "~alist" (string-downcase (class-name (ml-class-of x)))))
411
412 (defun xmlformat-list-start-value-func (x nitems) 
413   (values (format nil "~alist" (string-downcase (class-name (ml-class-of x)))) (ml-class-title x) nitems))
414
415 (defclass xmlformat (textformat) 
416   ()
417   (:default-initargs :file-start-str "" ; (std-xml-header)
418     :list-start-indent  t
419     :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
420     :list-start-value-func #'xmlformat-list-start-value-func
421     :list-end-indent  t
422     :list-end-fmtstr "</~a>~%"
423     :list-end-value-func #'xmlformat-list-end-value-func
424     :obj-start-fmtstr "<~a>"
425     :obj-start-value-func #'class-name-of
426     :obj-start-indent t
427     :obj-end-fmtstr "</~a>~%"
428     :obj-end-value-func #'class-name-of
429     :obj-end-indent nil
430     :obj-data-indent nil
431     :obj-data-fmtstr #'ml-class-fmtstr-xml
432     :obj-data-fmtstr-labels #'ml-class-fmtstr-xml-labels
433     :obj-data-value-func #'ml-class-xmlvalue-func))
434
435 (defclass link-ref ()
436   ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
437    (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
438    (page-name :type string :initarg :page-name :accessor page-name)
439    (href-head :type string :initarg :href-head :accessor href-head)
440    (href-end :type string :initarg :href-end :accessor href-end)
441    (ampersand :type string :initarg :ampersand :accessor ampersand))
442   (:default-initargs :fmtstr nil 
443     :fmtstr-labels nil 
444     :page-name "disp-func1" 
445     :href-head nil :href-end nil :ampersand nil)
446   (:documentation "Formatting for a linked reference"))
447
448 (defclass html-link-ref (link-ref)
449   ()
450   (:default-initargs :fmtstr #'ml-class-fmtstr-html-ref  
451     :fmtstr-labels #'ml-class-fmtstr-html-ref-labels
452     :href-head "a href=" 
453     :href-end "a" 
454     :ampersand "&"))
455
456 (defclass xml-link-ref (link-ref)
457   ()
458   (:default-initargs :fmtstr #'ml-class-fmtstr-xml-ref 
459     :fmtstr-labels #'ml-class-fmtstr-xml-ref-labels
460     :href-head "xmllink xlink:type=\"simple\" xlink:href=" 
461     :href-end "xmllink" 
462     :ampersand "&amp;"))
463
464
465 (defclass htmlrefformat (htmlformat)
466   ()
467   (:default-initargs :link-ref (make-instance 'html-link-ref)))
468
469 (defclass xmlrefformat (xmlformat)
470   ()
471   (:default-initargs :link-ref (make-instance 'xml-link-ref)))
472
473
474 ;;; File Start and Ends
475
476 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
477
478 (defmethod fmt-file-start ((fmt textformat) (s stream))
479   (aif (file-start-str fmt)
480       (format s it)))
481
482 (defmethod fmt-file-end ((fmt textformat) (s stream))
483   (aif (file-end-str fmt)
484           (format s it)))
485
486 ;;; List Start and Ends
487
488 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
489   (if (list-start-indent fmt)
490       (indent-spaces indent s))
491   (aif (list-start-fmtstr fmt)
492           (apply #'format s it
493                  (multiple-value-list
494                   (funcall (list-start-value-func fmt) x num-items)))))
495
496 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
497   (declare (ignore num-items))
498   (if (list-end-indent fmt)
499       (indent-spaces indent s))
500   (aif (list-end-fmtstr fmt)
501           (apply #'format s it
502                  (multiple-value-list
503                   (funcall (list-end-value-func fmt) x)))))
504
505 ;;; Object Start and Ends
506
507 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
508   (if (obj-start-indent fmt)
509       (indent-spaces indent s))
510   (aif (obj-start-fmtstr fmt)
511           (apply #'format s it
512                  (multiple-value-list
513                   (funcall (obj-start-value-func fmt) x)))))
514
515 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
516   (if (obj-end-indent fmt)
517       (indent-spaces indent s))
518   (aif (obj-end-fmtstr fmt)
519           (apply #'format s it
520                  (multiple-value-list
521                   (funcall (obj-end-value-func fmt) x)))))
522   
523 ;;; Object Data 
524
525 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
526   (declare (ignore obj fieldname))
527   (format nil "~a\"~a?func=~a~akey=~a~a\"" 
528           (href-head ref) (make-url (page-name ref)) fieldfunc 
529           (ampersand ref) fieldvalue
530           (if refvars
531               (let ((varstr ""))
532                 (dolist (var refvars)
533                   (string-append varstr (format nil "~a~a=~a" 
534                                                 (ampersand ref) (car var) (cadr var))))
535                 varstr)
536             "")))
537
538 (defmethod make-link-end (obj (ref link-ref) fieldname)
539   (declare (ignore obj fieldname))
540   (format nil "~a" (href-end ref))
541   )
542
543 (defmethod fmt-obj-data (x (fmt textformat) s
544                          &optional (indent 0) (label nil) (refvars nil))
545   (if (obj-data-indent fmt)
546       (indent-spaces indent s))
547   (if (link-ref fmt)
548       (fmt-obj-data-with-ref x fmt s label refvars)
549     (fmt-obj-data-plain x fmt s label))
550   (aif (obj-data-end-fmtstr fmt)
551        (format s it)))
552
553 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
554   (if label
555       (apply #'format s
556              (funcall (obj-data-fmtstr-labels fmt) x)
557              (multiple-value-list 
558               (funcall (funcall (obj-data-value-func fmt) x) x)))
559     (apply #'format s (funcall (obj-data-fmtstr fmt) x)
560            (multiple-value-list
561             (funcall (funcall (obj-data-value-func fmt) x) x)))))
562
563 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
564   (let ((refstr (make-ref-data-str x fmt label))
565         (refvalues nil)
566         (field-values 
567          (multiple-value-list
568           (funcall (funcall (obj-data-value-func fmt) x) x))))
569     
570     ;; make list of reference link fields for printing to refstr template
571     (dolist (field (ml-class-ref-fields x))
572       (let ((link-start 
573              (make-link-start x (link-ref fmt) (car field) (cadr field)
574                               (nth (position (car field) (ml-class-fields x) :key #'car) field-values)  
575                               (append (caddr field) refvars)))
576             (link-end (make-link-end x (link-ref fmt) (car field))))
577         (push link-start refvalues)
578         (push link-end refvalues)))
579     (setq refvalues (nreverse refvalues))
580     
581     (apply #'format s refstr refvalues)))
582   
583 (defmethod obj-data (x)
584   "Returns the objects data as a string. Used by common-graphics outline function"
585   (let ((fmt (make-format-instance :text)))
586     (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
587            (multiple-value-list 
588             (funcall (funcall (obj-data-value-func fmt) x) x)))))
589
590 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
591   "Return fmt string for that contains ~a slots for reference link start and end"
592   (unless (link-ref fmt)
593     (error "fmt does not contain a link-ref"))
594   (let ((refstr 
595          (if label
596              (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
597                     (multiple-value-list
598                       (funcall (funcall (obj-data-value-func fmt) x) x)))
599            (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
600                   (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
601     refstr))
602   
603 ;;; Display method for objects
604
605
606 (defmethod load-all-subobjects (objs)
607   "Load all subobjects if they have not already been loaded."
608   (when objs
609     (let ((objlist (mklist objs)))
610       (dolist (obj objlist)
611         (awhen (ml-class-subobjects-lists obj)  ;; access list of functions
612           (dolist (child-obj it)   ;; for each child function
613             (awhen (funcall (car child-obj) obj)
614               (load-all-subobjects it))))))
615     objs))
616
617 (defmethod output-ml-class (objs (fmt dataformat) (strm stream) 
618                             &optional (label nil) (english-only-function nil)
619                                       (indent 0) (subobjects nil) (refvars nil))
620   "Display a single or list of ml-class instances and their subobjects"
621   (when objs
622     (setq objs (mklist objs))
623     (let ((nobjs (length objs)))
624       (fmt-list-start (car objs) fmt strm indent nobjs)
625       (dolist (obj objs)
626         (unless (and english-only-function (not (funcall english-only-function obj)))
627           (fmt-obj-start obj fmt strm indent)
628           (fmt-obj-data obj fmt strm (1+ indent) label refvars)
629           (if subobjects
630               (awhen (ml-class-subobjects-lists obj)  ;; access list of functions
631                         (dolist (child-obj it)   ;; for each child function
632                           (awhen (funcall (car child-obj) obj) ;; access set of child objects
633                                     (output-ml-class it fmt strm label 
634                                                      english-only-function
635                                                      (1+ indent) subobjects refvars)))))
636           (fmt-obj-end obj fmt strm indent)))
637       (fmt-list-end (car objs) fmt strm indent nobjs))
638     t))
639
640 (defun display-ml-class (objs &key (os *standard-output*) (format :text)
641                                 (label nil) (english-only-function nil) (subobjects nil)
642                                 (file-wrapper t) (refvars nil))
643   "EXPORTED Function: displays a ml-class. Simplies call to output-ml-class"
644   (let ((fmt (make-format-instance format)))
645     (if file-wrapper
646         (fmt-file-start fmt os))
647     (when objs
648       (output-ml-class objs fmt os label english-only-function 0 subobjects refvars))
649     (if file-wrapper
650         (fmt-file-end fmt os)))
651   objs)