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