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