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