r3471: *** empty log message ***
[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.5 2002/11/25 02:10:38 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         (let* ((name (name field))
218                (print-formatter (print-formatter field))
219                (type (ho-type field))
220                (reference (reference field))
221                (namestr (symbol-name name))
222                (namestr-lower (string-downcase (symbol-name name)))
223                (value-fmt "~a")
224                (plain-value-func nil)
225                html-str xml-str html-label-str xml-label-str)
226           (unless (subobject field)
227             (when (or (eql type :integer) (eql type :fixnum))
228               (setq value-fmt "~d"))
229             
230             (when (eql type :boolean)
231               (setq value-fmt "~a"))
232             
233             (if first-field
234                 (setq first-field nil)
235                 (progn
236                   (string-append fmtstr-text " ")
237                   (string-append fmtstr-html " ")
238                   (string-append fmtstr-xml " ")
239                   (string-append fmtstr-text-labels " ")
240                   (string-append fmtstr-html-labels " ")
241                   (string-append fmtstr-xml-labels " ")
242                   (string-append fmtstr-html-ref " ")
243                   (string-append fmtstr-xml-ref " ")
244                   (string-append fmtstr-html-ref-labels " ")
245                   (string-append fmtstr-xml-ref-labels " ")))
246             
247             (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
248             (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
249             (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
250             (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
251             
252             (string-append fmtstr-text value-fmt)
253             (string-append fmtstr-html html-str)
254             (string-append fmtstr-xml xml-str)
255             (string-append fmtstr-text-labels namestr-lower " " value-fmt)
256             (string-append fmtstr-html-labels html-label-str)
257             (string-append fmtstr-xml-labels xml-label-str)
258
259             (if reference
260                 (progn
261                   (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
262                   (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
263                   (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
264                   (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>"))
265                 (progn
266                   (string-append fmtstr-html-ref html-str)
267                   (string-append fmtstr-xml-ref xml-str)
268                   (string-append fmtstr-html-ref-labels html-label-str)
269                   (string-append fmtstr-xml-ref-labels xml-label-str)))
270
271             (if print-formatter
272                 (setq plain-value-func 
273                       (list `(,print-formatter (,(intern namestr package) x))))
274                 (setq plain-value-func 
275                       (list `(,(intern namestr package) x))))
276             (setq value-func (append value-func plain-value-func))
277             
278             (if (eql type :cdata)
279                 (setq xml-value-func (append xml-value-func (list `(xml-cdata ,@plain-value-func))))
280                 (setq xml-value-func (append xml-value-func plain-value-func)))
281             )))
282         
283       (if value-func
284           (setq value-func `(lambda (x) (values ,@value-func)))
285           (setq value-func `(lambda () (values))))
286       
287       (if xml-value-func
288           (setq xml-value-func `(lambda (x) (values ,@xml-value-func)))
289           (setq xml-value-func `(lambda () (values))))
290         
291       (setf (slot-value meta 'fmtstr-text) fmtstr-text)
292       (setf (slot-value meta 'fmtstr-html) fmtstr-html)
293       (setf (slot-value meta 'fmtstr-xml) fmtstr-xml)
294       (setf (slot-value meta 'fmtstr-text-labels) fmtstr-text-labels)
295       (setf (slot-value meta 'fmtstr-html-labels) fmtstr-html-labels)
296       (setf (slot-value meta 'fmtstr-xml-labels) fmtstr-xml-labels)
297       (setf (slot-value meta 'fmtstr-html-ref) fmtstr-html-ref)
298       (setf (slot-value meta 'fmtstr-xml-ref) fmtstr-xml-ref)
299       (setf (slot-value meta 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
300       (setf (slot-value meta 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
301       (setf (slot-value meta 'value-func) value-func)
302       (setf (slot-value meta 'xml-value-func) xml-value-func))
303     (values))
304
305 (defgeneric ho-title (obj) )
306 (defgeneric ho-name (obj) )
307 (defgeneric ho-value-func (obj) )
308 (defgeneric ho-xml-value-func (obj) )
309 (defgeneric ho-fmtstr-text (obj) )
310 (defgeneric ho-fmtstr-html (obj) )
311 (defgeneric ho-fmtstr-xml (obj) )
312 (defgeneric ho-fmtstr-text-labels (obj) )
313 (defgeneric ho-fmtstr-html-labels (obj) )
314 (defgeneric ho-fmtstr-xml-labels (obj) )
315 (defgeneric ho-fmtstr-html-ref (obj) )
316 (defgeneric ho-fmtstr-xml-ref (obj) )
317 (defgeneric ho-fmtstr-html-ref-labels (obj) )
318 (defgeneric ho-fmtstr-xml-ref-labels (obj) )
319 (defgeneric ho-fields (obj) )
320 (defgeneric ho-references (obj) )
321 (defgeneric ho-subobjects (obj) )
322   
323 (defmacro define-hyperobject (name parents fields &rest meta-fields)
324   (let* ((meta (make-instance 'hyperobject-meta))
325          (cl-fields (process-hyper-fields fields meta))
326          (title (process-title name meta-fields))
327          (documentation (process-documentation meta-fields))
328          (value-func (gensym))
329          (xml-value-func (gensym)))
330     (init-hyperobject-class name meta)
331     `(progn
332        (eval-when (:compile-toplevel :load-toplevel :execute)
333          (defclass ,name ,(append parents (list 'hyperobject)) ,cl-fields
334            ,@(and documentation (list (list :documentation documentation)))))
335        (let ((,value-func (compile nil (eval (slot-value ,meta 'value-func))))
336              (,xml-value-func (compile nil (eval (slot-value ,meta 'xml-value-func)))))
337          (defmethod ho-title ((obj ,name))
338            ,title)
339          (defmethod ho-name ((obj ,name))
340            ,(string-downcase (symbol-name name)))
341          (defmethod ho-fields ((obj ,name))
342            ',(slot-value meta 'fields))
343          (defmethod ho-references ((obj ,name))
344            ',(slot-value meta 'references))
345          (defmethod ho-subobjects ((obj ,name))
346            ',(slot-value meta 'subobjects))
347          (defmethod ho-value-func ((obj ,name))
348            ,value-func)
349          (defmethod ho-xml-value-func ((obj ,name))
350            ,xml-value-func)
351          (defmethod ho-fmtstr-text ((obj ,name))
352            ,(slot-value meta 'fmtstr-text))
353          (defmethod ho-fmtstr-html ((obj ,name))
354            ,(slot-value meta 'fmtstr-html))
355          (defmethod ho-fmtstr-xml ((obj ,name))
356            ,(slot-value meta 'fmtstr-xml))
357          (defmethod ho-fmtstr-text-labels ((obj ,name))
358            ,(slot-value meta 'fmtstr-text-labels))
359          (defmethod ho-fmtstr-html-labels ((obj ,name))
360            ,(slot-value meta 'fmtstr-html-labels))
361          (defmethod ho-fmtstr-xml-labels ((obj ,name))
362            ,(slot-value meta 'fmtstr-xml-labels))
363          (defmethod ho-fmtstr-html-ref ((obj ,name))
364            ,(slot-value meta 'fmtstr-html-ref))
365          (defmethod ho-fmtstr-xml-ref ((obj ,name))
366            ,(slot-value meta 'fmtstr-xml-ref))
367          (defmethod ho-fmtstr-html-ref-labels ((obj ,name))
368            ,(slot-value meta 'fmtstr-html-ref-labels))
369          (defmethod ho-fmtstr-xml-ref-labels ((obj ,name))
370            ,(slot-value meta 'fmtstr-xml-ref-labels))
371          ))))
372
373 ;;;; Generic Print functions
374
375 (defparameter *default-textformat* nil)
376 (defparameter *default-htmlformat* nil)
377 (defparameter *default-htmlrefformat* nil)
378 (defparameter *default-xhtmlformat* nil)
379 (defparameter *default-xhtmlrefformat* nil)
380 (defparameter *default-xmlformat* nil)
381 (defparameter *default-xmlrefformat* nil)
382 (defparameter *default-ie-xmlrefformat* nil)
383 (defparameter *default-nullformat* nil)
384 (defparameter *default-init-format?* nil)
385
386 (defun make-format-instance (fmt)
387   (unless *default-init-format?*
388     (setq *default-textformat* (make-instance 'textformat))
389     (setq *default-htmlformat* (make-instance 'htmlformat))
390     (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
391     (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
392     (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
393     (setq *default-xmlformat* (make-instance 'xmlformat))
394     (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
395     (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
396     (setq *default-nullformat* (make-instance 'nullformat))
397     (setq *default-init-format?* t))
398   
399   (case fmt
400       (:text *default-textformat*)
401       (:html *default-htmlformat*)
402       (:htmlref *default-htmlrefformat*)
403       (:xhtml  *default-xhtmlformat*)
404       (:xhtmlref *default-xhtmlrefformat*)
405       (:xml  *default-xmlformat*)
406       (:xmlref *default-xmlrefformat*)
407       (:ie-xmlref *default-ie-xmlrefformat*)
408       (:null *default-nullformat*)
409       (otherwise *default-textformat*)))
410     
411 ;;;; Output format classes for print hoes
412
413 (defclass dataformat ()
414   ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
415    (file-end-str :type string :initarg :file-end-str :reader file-end-str)
416    (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
417    (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
418    (list-start-indent :initarg :list-start-indent :reader list-start-indent)
419    (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
420    (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
421    (list-end-indent :initarg :list-end-indent :reader list-end-indent)
422    (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
423    (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
424    (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
425    (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
426    (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
427    (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
428    (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
429    (obj-data-fmtstr :initarg :obj-data-fmtstr :reader  obj-data-fmtstr)
430    (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader  obj-data-fmtstr-labels)
431    (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
432    (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
433    (link-ref :initarg :link-ref :reader link-ref))
434   (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
435                      :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
436                      :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
437                      :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
438                      :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
439                      :obj-data-value-func nil :link-ref nil)
440   (:documentation "Parent for all dataformat objects"))
441
442 (defclass binaryformat (dataformat)
443   ())
444
445 (defclass nullformat (dataformat)
446   ())
447
448 (defun text-list-start-value-func (obj nitems)
449   (values (ho-title obj) nitems))
450
451 (defclass textformat (dataformat) 
452   ()    
453   (:default-initargs :list-start-fmtstr "~a~P:~%"
454     :list-start-value-func #'text-list-start-value-func
455     :list-start-indent t
456     :obj-data-indent t
457     :obj-data-fmtstr #'ho-fmtstr-text
458     :obj-data-fmtstr-labels #'ho-fmtstr-text-labels
459     :obj-data-end-fmtstr "~%"
460     :obj-data-value-func #'ho-value-func))
461
462
463 (defun htmlformat-list-start-value-func (x nitems) 
464   (values (ho-title x) nitems (ho-name x)))
465
466 (defclass htmlformat (textformat) 
467   ()
468   (:default-initargs :file-start-str "<html><body>~%"
469     :file-end-str "</body><html>~%"
470     :list-start-indent t
471     :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
472     :list-start-value-func #'htmlformat-list-start-value-func
473     :list-end-fmtstr "</ul></div>~%"
474     :list-end-indent t
475     :list-end-value-func #'identity
476     :obj-start-indent t
477     :obj-start-fmtstr "<li>"
478     :obj-start-value-func #'identity
479     :obj-end-indent  t
480     :obj-end-fmtstr  "</li>~%"
481     :obj-end-value-func #'identity
482     :obj-data-indent t
483     :obj-data-fmtstr #'ho-fmtstr-html-labels
484     :obj-data-fmtstr-labels #'ho-fmtstr-html-labels
485     :obj-data-value-func #'ho-value-func))
486
487 (defclass xhtmlformat (textformat) 
488   ()
489   (:default-initargs :file-start-str "<html><body>~%"
490     :file-end-str "</body><html>~%"
491     :list-start-indent t
492     :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
493     :list-start-value-func #'htmlformat-list-start-value-func
494     :list-end-fmtstr "</ul></div>~%"
495     :list-end-indent t
496     :list-end-value-func #'identity
497     :obj-start-indent t
498     :obj-start-fmtstr "<li>"
499     :obj-start-value-func #'identity
500     :obj-end-indent  t
501     :obj-end-fmtstr  "</li>~%"
502     :obj-end-value-func #'identity
503     :obj-data-indent t
504     :obj-data-fmtstr #'ho-fmtstr-html-labels
505     :obj-data-fmtstr-labels #'ho-fmtstr-html-labels
506     :obj-data-value-func #'ho-xml-value-func))
507
508
509 (defun xmlformat-list-end-value-func (x)
510   (format nil "~alist" (ho-name x)))
511
512 (defun xmlformat-list-start-value-func (x nitems) 
513   (values (format nil "~alist" (ho-name x)) (ho-title x) nitems))
514
515 (defclass xmlformat (textformat) 
516   ()
517   (:default-initargs :file-start-str "" ; (std-xml-header)
518     :list-start-indent  t
519     :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
520     :list-start-value-func #'xmlformat-list-start-value-func
521     :list-end-indent  t
522     :list-end-fmtstr "</~a>~%"
523     :list-end-value-func #'xmlformat-list-end-value-func
524     :obj-start-fmtstr "<~a>"
525     :obj-start-value-func #'ho-name
526     :obj-start-indent t
527     :obj-end-fmtstr "</~a>~%"
528     :obj-end-value-func #'ho-name
529     :obj-end-indent nil
530     :obj-data-indent nil
531     :obj-data-fmtstr #'ho-fmtstr-xml
532     :obj-data-fmtstr-labels #'ho-fmtstr-xml-labels
533     :obj-data-value-func #'ho-xml-value-func))
534
535 (defclass link-ref ()
536   ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
537    (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
538    (page-name :type string :initarg :page-name :accessor page-name)
539    (href-head :type string :initarg :href-head :accessor href-head)
540    (href-end :type string :initarg :href-end :accessor href-end)
541    (ampersand :type string :initarg :ampersand :accessor ampersand))
542   (:default-initargs :fmtstr nil 
543     :fmtstr-labels nil 
544     :page-name "disp-func1" 
545     :href-head nil :href-end nil :ampersand nil)
546   (:documentation "Formatting for a linked reference"))
547
548 (defclass html-link-ref (link-ref)
549   ()
550   (:default-initargs :fmtstr #'ho-fmtstr-html-ref  
551     :fmtstr-labels #'ho-fmtstr-html-ref-labels
552     :href-head "a href=" 
553     :href-end "a" 
554     :ampersand "&"))
555
556 (defclass xhtml-link-ref (link-ref)
557   ()
558   (:default-initargs :fmtstr #'ho-fmtstr-html-ref  
559     :fmtstr-labels #'ho-fmtstr-html-ref-labels
560     :href-head "a href=" 
561     :href-end "a" 
562     :ampersand "&amp;"))
563
564 (defclass xml-link-ref (link-ref)
565   ()
566   (:default-initargs :fmtstr #'ho-fmtstr-xml-ref 
567                      :fmtstr-labels #'ho-fmtstr-xml-ref-labels
568                      :href-head "xmllink xlink:type=\"simple\" xlink:href=" 
569                      :href-end "xmllink" 
570                      :ampersand "&amp;")
571   (:documentation "Mozilla's and W3's idea of a link with XML"))
572
573 (defclass ie-xml-link-ref (xml-link-ref)
574   ()
575   (:default-initargs :href-head "html:a href=" 
576                      :href-end "html:a" )
577   (:documentation "Internet Explorer's idea of a link with XML"))
578
579
580 (defclass htmlrefformat (htmlformat)
581   ()
582   (:default-initargs :link-ref (make-instance 'html-link-ref)))
583
584 (defclass xhtmlrefformat (xhtmlformat)
585   ()
586   (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
587
588 (defclass xmlrefformat (xmlformat)
589   ()
590   (:default-initargs :link-ref (make-instance 'xml-link-ref)))
591
592 (defclass ie-xmlrefformat (xmlformat)
593   ()
594   (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
595
596
597 ;;; File Start and Ends
598
599 (defgeneric fmt-file-start (fmt s))
600 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
601
602 (defmethod fmt-file-start ((fmt textformat) (s stream))
603   (aif (file-start-str fmt)
604       (format s it)))
605
606 (defgeneric fmt-file-end (fmt s))
607 (defmethod fmt-file-end ((fmt textformat) (s stream))
608   (aif (file-end-str fmt)
609           (format s it)))
610
611 ;;; List Start and Ends
612
613 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
614 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
615   (if (list-start-indent fmt)
616       (indent-spaces indent s))
617   (aif (list-start-fmtstr fmt)
618           (apply #'format s it
619                  (multiple-value-list
620                   (funcall (list-start-value-func fmt) x num-items)))))
621
622 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
623 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
624   (declare (ignore num-items))
625   (if (list-end-indent fmt)
626       (indent-spaces indent s))
627   (aif (list-end-fmtstr fmt)
628           (apply #'format s it
629                  (multiple-value-list
630                   (funcall (list-end-value-func fmt) x)))))
631
632 ;;; Object Start and Ends
633
634 (defgeneric fmt-obj-start (obj fmt s &optional indent))
635 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
636   (if (obj-start-indent fmt)
637       (indent-spaces indent s))
638   (aif (obj-start-fmtstr fmt)
639           (apply #'format s it
640                  (multiple-value-list
641                   (funcall (obj-start-value-func fmt) x)))))
642
643 (defgeneric fmt-obj-end (obj fmt s &optional indent))
644 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
645   (if (obj-end-indent fmt)
646       (indent-spaces indent s))
647   (aif (obj-end-fmtstr fmt)
648           (apply #'format s it
649                  (multiple-value-list
650                   (funcall (obj-end-value-func fmt) x)))))
651   
652 ;;; Object Data 
653
654 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
655 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
656   (declare (ignore obj fieldname))
657   (format nil "~a\"~a?func=~a~akey=~a~a\"" 
658           (href-head ref) (make-url (page-name ref)) fieldfunc 
659           (ampersand ref) fieldvalue
660           (if refvars
661               (let ((varstr ""))
662                 (dolist (var refvars)
663                   (string-append varstr (format nil "~a~a=~a" 
664                                                 (ampersand ref) (car var) (cadr var))))
665                 varstr)
666             "")))
667
668 (defgeneric make-link-end (obj ref fieldname)) 
669 (defmethod make-link-end (obj (ref link-ref) fieldname)
670   (declare (ignore obj fieldname))
671   (format nil "~a" (href-end ref))
672   )
673
674 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
675 (defmethod fmt-obj-data (x (fmt textformat) s
676                          &optional (indent 0) (label nil) (refvars nil))
677   (if (obj-data-indent fmt)
678       (indent-spaces indent s))
679   (if (link-ref fmt)
680       (fmt-obj-data-with-ref x fmt s label refvars)
681     (fmt-obj-data-plain x fmt s label))
682   (aif (obj-data-end-fmtstr fmt)
683        (format s it)))
684
685 (defgeneric fmt-obj-data-plain (obj fmt s label))
686 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
687   (if label
688       (apply #'format s
689              (funcall (obj-data-fmtstr-labels fmt) x)
690              (multiple-value-list 
691               (funcall (funcall (obj-data-value-func fmt) x) x)))
692     (apply #'format s (funcall (obj-data-fmtstr fmt) x)
693            (multiple-value-list
694             (funcall (funcall (obj-data-value-func fmt) x) x)))))
695
696 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
697 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
698   (let ((refstr (make-ref-data-str x fmt label))
699         (refvalues nil)
700         (field-values 
701          (multiple-value-list
702           (funcall (funcall (obj-data-value-func fmt) x) x))))
703     
704     ;; make list of reference link fields for printing to refstr template
705     (dolist (ref (ho-references x))
706       (let ((link-start 
707              (make-link-start x (link-ref fmt) (name ref) (lookup ref)
708                               (nth (position (name ref) (ho-fields x)
709                                              :key #'(lambda (x) (name x)))
710                                    field-values)  
711                               (append (link-parameters ref) refvars)))
712             (link-end (make-link-end x (link-ref fmt) (name ref))))
713         (push link-start refvalues)
714         (push link-end refvalues)))
715     (setq refvalues (nreverse refvalues))
716     
717     (apply #'format s refstr refvalues)))
718
719 (defgeneric obj-data (obj))
720 (defmethod obj-data (x)
721   "Returns the objects data as a string. Used by common-graphics outline function"
722   (let ((fmt (make-format-instance :text)))
723     (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
724            (multiple-value-list 
725             (funcall (funcall (obj-data-value-func fmt) x) x)))))
726
727 (defgeneric make-ref-data-str (obj fmt &optional label))
728 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
729   "Return fmt string for that contains ~a slots for reference link start and end"
730   (unless (link-ref fmt)
731     (error "fmt does not contain a link-ref"))
732   (let ((refstr 
733          (if label
734              (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
735                     (multiple-value-list
736                       (funcall (funcall (obj-data-value-func fmt) x) x)))
737            (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
738                   (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
739     refstr))
740   
741 ;;; Display method for objects
742
743
744 (defgeneric load-all-subobjects (objs))
745 (defmethod load-all-subobjects (objs)
746   "Load all subobjects if they have not already been loaded."
747   (when objs
748     (let ((objlist (mklist objs)))
749       (dolist (obj objlist)
750         (awhen (ho-subobjects obj)  ;; access list of functions
751           (dolist (subobj it)   ;; for each child function
752             (awhen (funcall (reader subobj) obj)
753               (load-all-subobjects it))))))
754     objs))
755
756 (defgeneric print-hyperobject-class (objs fmt strm
757                                   &optional label english-only-function
758                                   indent subobjects refvars))
759
760 (defmethod print-hyperobject-class (objs (fmt dataformat) (strm stream) 
761                                  &optional (label nil) (indent 0)
762                                  (english-only-function nil)
763                                  (subobjects nil) (refvars nil))
764 "Display a single or list of hyperobject-class instances and their subobjects"
765   (when objs
766     (setq objs (mklist objs))
767     (let ((nobjs (length objs)))
768       (fmt-list-start (car objs) fmt strm indent nobjs)
769       (dolist (obj objs)
770         (unless (and english-only-function
771                   (multiple-value-bind (eng term) (funcall english-only-function obj)
772                     (and term (not eng))))
773           (fmt-obj-start obj fmt strm indent)
774           (fmt-obj-data obj fmt strm (1+ indent) label refvars)
775           (if subobjects
776               (awhen (ho-subobjects obj)  ;; access list of functions
777                      (dolist (subobj it)   ;; for each child function
778                        (awhen (funcall (reader subobj) obj) ;; access set of child objects
779                               (print-hyperobject-class it fmt strm label 
780                                                        (1+ indent) english-only-function
781                                                        subobjects refvars)))))
782           (fmt-obj-end obj fmt strm indent)))
783       (fmt-list-end (car objs) fmt strm indent nobjs))
784     t))
785
786
787
788 (defun print-hyperobject (objs &key (os *standard-output*) (format :text)
789                       (label nil) (english-only-function nil)
790                       (subobjects nil) (file-wrapper t) (refvars nil))
791   "EXPORTED Function: prints hyperobject-class objects. Simplies call to print-hyperobject-class"
792   (let ((fmt (make-format-instance format)))
793     (if file-wrapper
794         (fmt-file-start fmt os))
795     (when objs
796       (print-hyperobject-class objs fmt os label 0 english-only-function subobjects refvars))
797     (if file-wrapper
798         (fmt-file-end fmt os)))
799   objs)
800
801
802 (defmethod print-object ((obj hyperobject) (s stream))
803   (print-unreadable-object (obj s :type t :identity t)
804     (let ((fmt (make-instance 'hyperobject::textformat)))
805       (apply #'format 
806              s (funcall (hyperobject::obj-data-fmtstr fmt) obj)
807              (multiple-value-list 
808               (funcall (funcall (hyperobject::obj-data-value-func fmt) obj) obj))))))
809