e315706a4ff0fbb26f379776f652ac9a2c4c1807
[hyperobject.git] / hyperobject-no-mop.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10; Package: HYPEROBJECT -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          hyperobject.lisp
6 ;;;; Purpose:       Hyper Object (Plain - no Metaclass)
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Nov 2002
9 ;;;;
10 ;;;; This is a rewrite of hyperobjec't to avoid using metaclasses.
11 ;;;;
12 ;;;; $Id: hyperobject-no-mop.lisp,v 1.4 2002/11/23 22:19:17 kevin Exp $
13 ;;;;
14 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
15 ;;;;
16 ;;;; *************************************************************************
17  
18 (in-package :hyperobject-no-mop)
19
20
21 (eval-when (:compile-toplevel :execute)
22   (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
23
24
25 ;; Main class
26
27 (defclass hyperobject ()
28   ())
29
30 (defclass hyperobject-meta ()
31   ((fields :initform nil :type list)
32    (subobjects :initform nil :type list)
33    (references :initform nil :type list)
34    (value-func :initform nil :type function)
35    (xml-value-func :initform nil :type function)
36    (fmtstr-text :initform nil :type string)
37    (fmtstr-html :initform nil :type string)
38    (fmtstr-xml :initform nil :type string)
39    (fmtstr-text-labels :initform nil :type string)
40    (fmtstr-html-labels :initform nil :type string)
41    (fmtstr-xml-labels :initform nil :type string)
42    (fmtstr-html-ref :initform nil :type string)
43    (fmtstr-xml-ref :initform nil :type string)
44    (fmtstr-html-ref-labels :initform nil :type string)
45    (fmtstr-xml-ref-labels :initform nil :type string)
46    )
47   (:documentation "Class holding meta information for hyperobjects"))
48
49 (defclass field ()
50   ((name :type symbol :initform nil :initarg :name :reader name)
51    (print-formatter :initform nil :initarg :print-formatter :reader print-formatter)
52    (cl-type :initform nil :reader cl-type)
53    (ho-type :initform nil :reader ho-type)
54    (subobject :initform nil :reader subobject)
55    (reference :initform nil :reader reference)
56    ))
57
58
59 (defmethod print-object ((obj field) (s stream))
60   (print-unreadable-object (obj s :type t :identity t)
61     (format s "~S" (name obj))))
62
63 (defclass subobject ()
64   ((name :type symbol :initform nil :initarg :name :reader name)
65    (reader :type function :initform nil :initarg :reader :reader reader)))
66
67 (defmethod print-object ((obj subobject) (s stream))
68   (print-unreadable-object (obj s :type t :identity t)
69     (format s "~S" (name obj))))
70
71 (defclass reference ()
72   ((name :type symbol :initform nil :initarg :name :reader name)
73    (lookup :type function :initform nil :initarg :lookup :reader lookup)
74    (link-parameters :type list :initform nil :initarg :link-parameters
75                     :reader link-parameters)))
76
77 (defmethod print-object ((obj reference) (s stream))
78   (print-unreadable-object (obj s :type t :identity t)
79     (format s "~S" (name obj))))
80    
81 (defun remove-keys (key-names args)
82   (loop for ( name val ) on args by #'cddr
83         unless (member (symbol-name name) key-names 
84                        :key #'symbol-name :test 'equal)
85         append (list name val)))
86
87 (defun convert-ho-type (ho-type)
88   (check-type ho-type symbol)
89   (case (intern (symbol-name ho-type) (symbol-name :keyword))
90     (:string
91      'string)
92     (:fixnum
93      'fixnum)
94     (:boolean
95      'boolean)
96     (:integer
97      'integer)
98     (:cdata
99      'string)
100     (:float
101      'float)
102     (otherwise
103      ho-type)))
104
105 (defun process-hyper-fields (raw-fields meta)
106   (let* ((fields '())
107          (references '())
108          (subobjects '())
109          (processed-fields
110           (loop for field in raw-fields
111              collecting
112                (destructuring-bind
113                      (name &rest rest &key
114                            ;; the following list of keywords is reproduced below in the
115                            ;; remove-keys form.  important to keep them in sync
116                            type reader writer accessor initform print-formatter initarg
117                            reference subobject
118                            ;; list ends
119                            &allow-other-keys) field
120                  (let ((other-args (remove-keys
121                                     '(type reader writer accessor initform print-formatter
122                                       initarg reference subobject)
123                                rest))
124                        (field-obj (make-instance 'field :name name)) 
125                        (kv nil))
126                    (declare (ignore other-args))
127                    (push field-obj fields)
128                    (loop for (k v) in `((:type ,type) (:reader ,reader) (:writer ,writer)
129                                         (:accessor ,accessor) (:initform ,initform)
130                                         (:print-formatter ,print-formatter) (:initarg ,initarg)
131                                         (:subobject ,subobject) (:reference ,reference))
132                       do
133                         (case k
134                           (:initarg
135                            (push (list :initarg
136                                        (if v
137                                      v
138                                      (intern (symbol-name name)
139                                              (symbol-name :keyword))))
140                                  kv))
141                           (:accessor
142                            (when v
143                              (push (list :accessor v) kv)))
144                           (:print-formatter
145                            (when v
146                              (setf (slot-value field-obj 'print-formatter) v)))
147                           (:writer
148                            (when v
149                              (push (list :writer v) kv)))
150                           (:reader
151                            (if v
152                                (push (list :reader v) kv)
153                        (push (list :reader name) kv)))
154                           (:type
155                            (when v
156                              (setf (slot-value field-obj 'ho-type) v)
157                              (setf (slot-value field-obj 'cl-type) (convert-ho-type v))
158                              (push (list :type (cl-type field-obj)) kv)))
159                           (:subobject
160                            (when v
161                              (let ((subobj (make-instance
162                                             'subobject :name name
163                                             :reader
164                                             (if (eq v t)
165                                                 name
166                                                 v))))
167                                (setf (slot-value field-obj 'subobject) subobj)
168                                (push subobj subobjects))))
169                           (:reference
170                            (when v
171                              (let ((ref (make-instance 'reference :name name :lookup v)))
172                                (setf (slot-value field-obj 'reference) ref)
173                                (push ref references))))
174                           ))
175                    (append
176                     (list name)
177                     (loop for (k v) in kv
178                        collecting k
179                        collecting v)))))))
180     (setf (slot-value meta 'fields) (nreverse fields))
181     (setf (slot-value meta 'references) (nreverse references))
182     (setf (slot-value meta 'subobjects) (nreverse subobjects))
183     processed-fields))
184     
185   
186 (defun process-title (name meta)
187   (let ((title (cadr (assoc :title meta))))
188     (if title
189         (if (symbolp title)
190             (symbol-name title)
191             title)
192         (symbol-name name))))
193
194 (defun process-documentation (meta)
195   (let ((doc (cadr (assoc :title meta))))
196     (if (and doc (symbolp doc))
197         (symbol-name doc)
198         doc)))
199
200 ;;;; Class initialization function
201 (defun init-hyperobject-class (name meta)
202     (let ((fmtstr-text "")
203           (fmtstr-html "")
204           (fmtstr-xml "")
205           (fmtstr-text-labels "")
206           (fmtstr-html-labels "")
207           (fmtstr-xml-labels "")
208           (fmtstr-html-ref "")
209           (fmtstr-xml-ref "")
210           (fmtstr-html-ref-labels "")
211           (fmtstr-xml-ref-labels "")
212           (first-field t)
213           (value-func '())
214           (xml-value-func '())
215           (package (symbol-package name)))
216       (dolist (field (slot-value meta 'fields))
217         (declare (ignore rest))
218         (let* ((name (name field))
219                (print-formatter (print-formatter field))
220                (type (ho-type field))
221                (reference (reference field))
222                (namestr (symbol-name name))
223                (namestr-lower (string-downcase (symbol-name name)))
224                (value-fmt "~a")
225                (plain-value-func nil)
226                html-str xml-str html-label-str xml-label-str)
227           (unless (subobject field)
228             (when (or (eql type :integer) (eql type :fixnum))
229               (setq value-fmt "~d"))
230             
231             (when (eql type :boolean)
232               (setq value-fmt "~a"))
233             
234             (if first-field
235                 (setq first-field nil)
236                 (progn
237                   (string-append fmtstr-text " ")
238                   (string-append fmtstr-html " ")
239                   (string-append fmtstr-xml " ")
240                   (string-append fmtstr-text-labels " ")
241                   (string-append fmtstr-html-labels " ")
242                   (string-append fmtstr-xml-labels " ")
243                   (string-append fmtstr-html-ref " ")
244                   (string-append fmtstr-xml-ref " ")
245                   (string-append fmtstr-html-ref-labels " ")
246                   (string-append fmtstr-xml-ref-labels " ")))
247             
248             (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
249             (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
250             (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
251             (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
252             
253             (string-append fmtstr-text value-fmt)
254             (string-append fmtstr-html html-str)
255             (string-append fmtstr-xml xml-str)
256             (string-append fmtstr-text-labels namestr-lower " " value-fmt)
257             (string-append fmtstr-html-labels html-label-str)
258             (string-append fmtstr-xml-labels xml-label-str)
259
260             (if reference
261                 (progn
262                   (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
263                   (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
264                   (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
265                   (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>"))
266                 (progn
267                   (string-append fmtstr-html-ref html-str)
268                   (string-append fmtstr-xml-ref xml-str)
269                   (string-append fmtstr-html-ref-labels html-label-str)
270                   (string-append fmtstr-xml-ref-labels xml-label-str)))
271
272             (if print-formatter
273                 (setq plain-value-func 
274                       (list `(,print-formatter (,(intern namestr package) x))))
275                 (setq plain-value-func 
276                       (list `(,(intern namestr package) x))))
277             (setq value-func (append value-func plain-value-func))
278             
279             (if (eql type :cdata)
280                 (setq xml-value-func (append xml-value-func (list `(xml-cdata ,@plain-value-func))))
281                 (setq xml-value-func (append xml-value-func plain-value-func)))
282             )))
283         
284       (if value-func
285           (setq value-func `(lambda (x) (values ,@value-func)))
286           (setq value-func `(lambda () (values))))
287       
288       (if xml-value-func
289           (setq xml-value-func `(lambda (x) (values ,@xml-value-func)))
290           (setq xml-value-func `(lambda () (values))))
291         
292       (setf (slot-value meta 'fmtstr-text) fmtstr-text)
293       (setf (slot-value meta 'fmtstr-html) fmtstr-html)
294       (setf (slot-value meta 'fmtstr-xml) fmtstr-xml)
295       (setf (slot-value meta 'fmtstr-text-labels) fmtstr-text-labels)
296       (setf (slot-value meta 'fmtstr-html-labels) fmtstr-html-labels)
297       (setf (slot-value meta 'fmtstr-xml-labels) fmtstr-xml-labels)
298       (setf (slot-value meta 'fmtstr-html-ref) fmtstr-html-ref)
299       (setf (slot-value meta 'fmtstr-xml-ref) fmtstr-xml-ref)
300       (setf (slot-value meta 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
301       (setf (slot-value meta 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
302       (setf (slot-value meta 'value-func) value-func)
303       (setf (slot-value meta 'xml-value-func) xml-value-func))
304     (values))
305
306 (defgeneric ho-title (obj) )
307 (defgeneric ho-name (obj) )
308 (defgeneric ho-value-func (obj) )
309 (defgeneric ho-xml-value-func (obj) )
310 (defgeneric ho-fmtstr-text (obj) )
311 (defgeneric ho-fmtstr-html (obj) )
312 (defgeneric ho-fmtstr-xml (obj) )
313 (defgeneric ho-fmtstr-text-labels (obj) )
314 (defgeneric ho-fmtstr-html-labels (obj) )
315 (defgeneric ho-fmtstr-xml-labels (obj) )
316 (defgeneric ho-fmtstr-html-ref (obj) )
317 (defgeneric ho-fmtstr-xml-ref (obj) )
318 (defgeneric ho-fmtstr-html-ref-labels (obj) )
319 (defgeneric ho-fmtstr-xml-ref-labels (obj) )
320 (defgeneric ho-fields (obj) )
321 (defgeneric ho-references (obj) )
322 (defgeneric ho-subobjects (obj) )
323   
324 (defmacro define-hyperobject (name parents fields &rest meta-fields)
325   (let* ((meta (make-instance 'hyperobject-meta))
326          (cl-fields (process-hyper-fields fields meta))
327          (title (process-title name meta-fields))
328          (documentation (process-documentation meta-fields))
329          (value-func (gensym))
330          (xml-value-func (gensym)))
331     (init-hyperobject-class name meta)
332     `(progn
333        (eval-when (:compile-toplevel :load-toplevel :execute)
334          (defclass ,name ,(append parents (list 'hyperobject)) ,cl-fields
335            ,@(and documentation (list (list :documentation documentation)))))
336        (let ((,value-func (compile nil (eval (slot-value ,meta 'value-func))))
337              (,xml-value-func (compile nil (eval (slot-value ,meta 'xml-value-func)))))
338          (defmethod ho-title ((obj ,name))
339            ,title)
340          (defmethod ho-name ((obj ,name))
341            ,(string-downcase (symbol-name name)))
342          (defmethod ho-fields ((obj ,name))
343            ',(slot-value meta 'fields))
344          (defmethod ho-references ((obj ,name))
345            ',(slot-value meta 'references))
346          (defmethod ho-subobjects ((obj ,name))
347            ',(slot-value meta 'subobjects))
348          (defmethod ho-value-func ((obj ,name))
349            ,value-func)
350          (defmethod ho-xml-value-func ((obj ,name))
351            ,xml-value-func)
352          (defmethod ho-fmtstr-text ((obj ,name))
353            ,(slot-value meta 'fmtstr-text))
354          (defmethod ho-fmtstr-html ((obj ,name))
355            ,(slot-value meta 'fmtstr-html))
356          (defmethod ho-fmtstr-xml ((obj ,name))
357            ,(slot-value meta 'fmtstr-xml))
358          (defmethod ho-fmtstr-text-labels ((obj ,name))
359            ,(slot-value meta 'fmtstr-text-labels))
360          (defmethod ho-fmtstr-html-labels ((obj ,name))
361            ,(slot-value meta 'fmtstr-html-labels))
362          (defmethod ho-fmtstr-xml-labels ((obj ,name))
363            ,(slot-value meta 'fmtstr-xml-labels))
364          (defmethod ho-fmtstr-html-ref ((obj ,name))
365            ,(slot-value meta 'fmtstr-html-ref))
366          (defmethod ho-fmtstr-xml-ref ((obj ,name))
367            ,(slot-value meta 'fmtstr-xml-ref))
368          (defmethod ho-fmtstr-html-ref-labels ((obj ,name))
369            ,(slot-value meta 'fmtstr-html-ref-labels))
370          (defmethod ho-fmtstr-xml-ref-labels ((obj ,name))
371            ,(slot-value meta 'fmtstr-xml-ref-labels))
372          ))))
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 hoes
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 (ho-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 #'ho-fmtstr-text
459     :obj-data-fmtstr-labels #'ho-fmtstr-text-labels
460     :obj-data-end-fmtstr "~%"
461     :obj-data-value-func #'ho-value-func))
462
463
464 (defun htmlformat-list-start-value-func (x nitems) 
465   (values (ho-title x) nitems (ho-name x)))
466
467 (defclass htmlformat (textformat) 
468   ()
469   (:default-initargs :file-start-str "<html><body>~%"
470     :file-end-str "</body><html>~%"
471     :list-start-indent t
472     :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
473     :list-start-value-func #'htmlformat-list-start-value-func
474     :list-end-fmtstr "</ul></div>~%"
475     :list-end-indent t
476     :list-end-value-func #'identity
477     :obj-start-indent t
478     :obj-start-fmtstr "<li>"
479     :obj-start-value-func #'identity
480     :obj-end-indent  t
481     :obj-end-fmtstr  "</li>~%"
482     :obj-end-value-func #'identity
483     :obj-data-indent t
484     :obj-data-fmtstr #'ho-fmtstr-html-labels
485     :obj-data-fmtstr-labels #'ho-fmtstr-html-labels
486     :obj-data-value-func #'ho-value-func))
487
488 (defclass xhtmlformat (textformat) 
489   ()
490   (:default-initargs :file-start-str "<html><body>~%"
491     :file-end-str "</body><html>~%"
492     :list-start-indent t
493     :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
494     :list-start-value-func #'htmlformat-list-start-value-func
495     :list-end-fmtstr "</ul></div>~%"
496     :list-end-indent t
497     :list-end-value-func #'identity
498     :obj-start-indent t
499     :obj-start-fmtstr "<li>"
500     :obj-start-value-func #'identity
501     :obj-end-indent  t
502     :obj-end-fmtstr  "</li>~%"
503     :obj-end-value-func #'identity
504     :obj-data-indent t
505     :obj-data-fmtstr #'ho-fmtstr-html-labels
506     :obj-data-fmtstr-labels #'ho-fmtstr-html-labels
507     :obj-data-value-func #'ho-xml-value-func))
508
509
510 (defun xmlformat-list-end-value-func (x)
511   (format nil "~alist" (ho-name x)))
512
513 (defun xmlformat-list-start-value-func (x nitems) 
514   (values (format nil "~alist" (ho-name x)) (ho-title x) nitems))
515
516 (defclass xmlformat (textformat) 
517   ()
518   (:default-initargs :file-start-str "" ; (std-xml-header)
519     :list-start-indent  t
520     :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
521     :list-start-value-func #'xmlformat-list-start-value-func
522     :list-end-indent  t
523     :list-end-fmtstr "</~a>~%"
524     :list-end-value-func #'xmlformat-list-end-value-func
525     :obj-start-fmtstr "<~a>"
526     :obj-start-value-func #'ho-name
527     :obj-start-indent t
528     :obj-end-fmtstr "</~a>~%"
529     :obj-end-value-func #'ho-name
530     :obj-end-indent nil
531     :obj-data-indent nil
532     :obj-data-fmtstr #'ho-fmtstr-xml
533     :obj-data-fmtstr-labels #'ho-fmtstr-xml-labels
534     :obj-data-value-func #'ho-xml-value-func))
535
536 (defclass link-ref ()
537   ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
538    (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
539    (page-name :type string :initarg :page-name :accessor page-name)
540    (href-head :type string :initarg :href-head :accessor href-head)
541    (href-end :type string :initarg :href-end :accessor href-end)
542    (ampersand :type string :initarg :ampersand :accessor ampersand))
543   (:default-initargs :fmtstr nil 
544     :fmtstr-labels nil 
545     :page-name "disp-func1" 
546     :href-head nil :href-end nil :ampersand nil)
547   (:documentation "Formatting for a linked reference"))
548
549 (defclass html-link-ref (link-ref)
550   ()
551   (:default-initargs :fmtstr #'ho-fmtstr-html-ref  
552     :fmtstr-labels #'ho-fmtstr-html-ref-labels
553     :href-head "a href=" 
554     :href-end "a" 
555     :ampersand "&"))
556
557 (defclass xhtml-link-ref (link-ref)
558   ()
559   (:default-initargs :fmtstr #'ho-fmtstr-html-ref  
560     :fmtstr-labels #'ho-fmtstr-html-ref-labels
561     :href-head "a href=" 
562     :href-end "a" 
563     :ampersand "&amp;"))
564
565 (defclass xml-link-ref (link-ref)
566   ()
567   (:default-initargs :fmtstr #'ho-fmtstr-xml-ref 
568                      :fmtstr-labels #'ho-fmtstr-xml-ref-labels
569                      :href-head "xmllink xlink:type=\"simple\" xlink:href=" 
570                      :href-end "xmllink" 
571                      :ampersand "&amp;")
572   (:documentation "Mozilla's and W3's idea of a link with XML"))
573
574 (defclass ie-xml-link-ref (xml-link-ref)
575   ()
576   (:default-initargs :href-head "html:a href=" 
577                      :href-end "html:a" )
578   (:documentation "Internet Explorer's idea of a link with XML"))
579
580
581 (defclass htmlrefformat (htmlformat)
582   ()
583   (:default-initargs :link-ref (make-instance 'html-link-ref)))
584
585 (defclass xhtmlrefformat (xhtmlformat)
586   ()
587   (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
588
589 (defclass xmlrefformat (xmlformat)
590   ()
591   (:default-initargs :link-ref (make-instance 'xml-link-ref)))
592
593 (defclass ie-xmlrefformat (xmlformat)
594   ()
595   (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
596
597
598 ;;; File Start and Ends
599
600 (defgeneric fmt-file-start (fmt s))
601 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
602
603 (defmethod fmt-file-start ((fmt textformat) (s stream))
604   (aif (file-start-str fmt)
605       (format s it)))
606
607 (defgeneric fmt-file-end (fmt s))
608 (defmethod fmt-file-end ((fmt textformat) (s stream))
609   (aif (file-end-str fmt)
610           (format s it)))
611
612 ;;; List Start and Ends
613
614 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
615 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
616   (if (list-start-indent fmt)
617       (indent-spaces indent s))
618   (aif (list-start-fmtstr fmt)
619           (apply #'format s it
620                  (multiple-value-list
621                   (funcall (list-start-value-func fmt) x num-items)))))
622
623 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
624 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
625   (declare (ignore num-items))
626   (if (list-end-indent fmt)
627       (indent-spaces indent s))
628   (aif (list-end-fmtstr fmt)
629           (apply #'format s it
630                  (multiple-value-list
631                   (funcall (list-end-value-func fmt) x)))))
632
633 ;;; Object Start and Ends
634
635 (defgeneric fmt-obj-start (obj fmt s &optional indent))
636 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
637   (if (obj-start-indent fmt)
638       (indent-spaces indent s))
639   (aif (obj-start-fmtstr fmt)
640           (apply #'format s it
641                  (multiple-value-list
642                   (funcall (obj-start-value-func fmt) x)))))
643
644 (defgeneric fmt-obj-end (obj fmt s &optional indent))
645 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
646   (if (obj-end-indent fmt)
647       (indent-spaces indent s))
648   (aif (obj-end-fmtstr fmt)
649           (apply #'format s it
650                  (multiple-value-list
651                   (funcall (obj-end-value-func fmt) x)))))
652   
653 ;;; Object Data 
654
655 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
656 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
657   (declare (ignore obj fieldname))
658   (format nil "~a\"~a?func=~a~akey=~a~a\"" 
659           (href-head ref) (make-url (page-name ref)) fieldfunc 
660           (ampersand ref) fieldvalue
661           (if refvars
662               (let ((varstr ""))
663                 (dolist (var refvars)
664                   (string-append varstr (format nil "~a~a=~a" 
665                                                 (ampersand ref) (car var) (cadr var))))
666                 varstr)
667             "")))
668
669 (defgeneric make-link-end (obj ref fieldname)) 
670 (defmethod make-link-end (obj (ref link-ref) fieldname)
671   (declare (ignore obj fieldname))
672   (format nil "~a" (href-end ref))
673   )
674
675 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
676 (defmethod fmt-obj-data (x (fmt textformat) s
677                          &optional (indent 0) (label nil) (refvars nil))
678   (if (obj-data-indent fmt)
679       (indent-spaces indent s))
680   (if (link-ref fmt)
681       (fmt-obj-data-with-ref x fmt s label refvars)
682     (fmt-obj-data-plain x fmt s label))
683   (aif (obj-data-end-fmtstr fmt)
684        (format s it)))
685
686 (defgeneric fmt-obj-data-plain (obj fmt s label))
687 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
688   (if label
689       (apply #'format s
690              (funcall (obj-data-fmtstr-labels fmt) x)
691              (multiple-value-list 
692               (funcall (funcall (obj-data-value-func fmt) x) x)))
693     (apply #'format s (funcall (obj-data-fmtstr fmt) x)
694            (multiple-value-list
695             (funcall (funcall (obj-data-value-func fmt) x) x)))))
696
697 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
698 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
699   (let ((refstr (make-ref-data-str x fmt label))
700         (refvalues nil)
701         (field-values 
702          (multiple-value-list
703           (funcall (funcall (obj-data-value-func fmt) x) x))))
704     
705     ;; make list of reference link fields for printing to refstr template
706     (dolist (ref (ho-references x))
707       (let ((link-start 
708              (make-link-start x (link-ref fmt) (name ref) (lookup ref)
709                               (nth (position (name ref) (ho-fields x)
710                                              :key #'(lambda (x) (name x)))
711                                    field-values)  
712                               (append (link-parameters ref) refvars)))
713             (link-end (make-link-end x (link-ref fmt) (name ref))))
714         (push link-start refvalues)
715         (push link-end refvalues)))
716     (setq refvalues (nreverse refvalues))
717     
718     (apply #'format s refstr refvalues)))
719
720 (defgeneric obj-data (obj))
721 (defmethod obj-data (x)
722   "Returns the objects data as a string. Used by common-graphics outline function"
723   (let ((fmt (make-format-instance :text)))
724     (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
725            (multiple-value-list 
726             (funcall (funcall (obj-data-value-func fmt) x) x)))))
727
728 (defgeneric make-ref-data-str (obj fmt &optional label))
729 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
730   "Return fmt string for that contains ~a slots for reference link start and end"
731   (unless (link-ref fmt)
732     (error "fmt does not contain a link-ref"))
733   (let ((refstr 
734          (if label
735              (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
736                     (multiple-value-list
737                       (funcall (funcall (obj-data-value-func fmt) x) x)))
738            (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
739                   (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
740     refstr))
741   
742 ;;; Display method for objects
743
744
745 (defgeneric load-all-subobjects (objs))
746 (defmethod load-all-subobjects (objs)
747   "Load all subobjects if they have not already been loaded."
748   (when objs
749     (let ((objlist (mklist objs)))
750       (dolist (obj objlist)
751         (awhen (ho-subobjects obj)  ;; access list of functions
752           (dolist (subobj it)   ;; for each child function
753             (awhen (funcall (reader subobj) obj)
754               (load-all-subobjects it))))))
755     objs))
756
757 (defgeneric print-hyperobject-class (objs fmt strm
758                                   &optional label english-only-function
759                                   indent subobjects refvars))
760
761 (defmethod print-hyperobject-class (objs (fmt dataformat) (strm stream) 
762                                  &optional (label nil) (indent 0)
763                                  (english-only-function nil)
764                                  (subobjects nil) (refvars nil))
765 "Display a single or list of hyperobject-class instances and their subobjects"
766   (when objs
767     (setq objs (mklist objs))
768     (let ((nobjs (length objs)))
769       (fmt-list-start (car objs) fmt strm indent nobjs)
770       (dolist (obj objs)
771         (unless (and english-only-function
772                   (multiple-value-bind (eng term) (funcall english-only-function obj)
773                     (and term (not eng))))
774           (fmt-obj-start obj fmt strm indent)
775           (fmt-obj-data obj fmt strm (1+ indent) label refvars)
776           (if subobjects
777               (awhen (ho-subobjects obj)  ;; access list of functions
778                      (dolist (subobj it)   ;; for each child function
779                        (awhen (funcall (reader subobj) obj) ;; access set of child objects
780                               (print-hyperobject-class it fmt strm label 
781                                                        (1+ indent) english-only-function
782                                                        subobjects refvars)))))
783           (fmt-obj-end obj fmt strm indent)))
784       (fmt-list-end (car objs) fmt strm indent nobjs))
785     t))
786
787
788
789 (defun print-hyperobject (objs &key (os *standard-output*) (format :text)
790                       (label nil) (english-only-function nil)
791                       (subobjects nil) (file-wrapper t) (refvars nil))
792   "EXPORTED Function: prints hyperobject-class objects. Simplies call to print-hyperobject-class"
793   (let ((fmt (make-format-instance format)))
794     (if file-wrapper
795         (fmt-file-start fmt os))
796     (when objs
797       (print-hyperobject-class objs fmt os label 0 english-only-function subobjects refvars))
798     (if file-wrapper
799         (fmt-file-end fmt os)))
800   objs)
801
802
803 (defmethod print-object ((obj hyperobject) (s stream))
804   (print-unreadable-object (obj s :type t :identity t)
805     (let ((fmt (make-instance 'hyperobject::textformat)))
806       (apply #'format 
807              s (funcall (hyperobject::obj-data-fmtstr fmt) obj)
808              (multiple-value-list 
809               (funcall (funcall (hyperobject::obj-data-value-func fmt) obj) obj))))))
810