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