r4933: *** empty log message ***
[hyperobject.git] / views.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          views.lisp
6 ;;;; Purpose:       View methods for Hyperobjects
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: views.lisp,v 1.42 2003/05/14 08:30:38 kevin Exp $
11 ;;;;
12 ;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
13 ;;;; *************************************************************************
14  
15 (in-package :hyperobject)
16
17 (eval-when (:compile-toplevel :execute)
18   (declaim (optimize (speed 2) (safety 2) (compilation-speed 0) (debug 2))))
19
20
21 (defclass object-view ()
22   ((object-class-name :initform nil :initarg :object-class-name
23                       :accessor object-class-name
24                  :documentation "Name of class of object to be viewed.")
25    (object-class :initform nil :initarg :object-class
26                  :accessor object-class
27                  :documentation "Class of object to be viewed.")
28    (slots :initform nil :initarg :slots :accessor slots
29           :documentation "List of effective slots for object to be viewed.")
30    (name :initform nil :initarg :name :accessor name
31          :documentation "Name for this view.")
32    (category :initform nil :initarg :category :accessor category
33              :documentation "Category for view. Helpful when want to find a view corresponding to a particular category.")
34    (source-code :initform nil :initarg :source-code :accessor source-code 
35                 :documentation "Source code for generating view.")
36    (country-language :initform :en :initarg :country-language
37                      :documentation "Country's Language for this view.")
38    ;;
39    (file-start-str :type (or string null) :initform nil :initarg :file-start-str
40                    :accessor file-start-str)
41    (file-end-str :type (or string null) :initform nil :initarg :file-end-str
42                  :accessor file-end-str)
43    (list-start-str-or-func :type (or string function null) :initform nil
44                            :initarg :list-start-str-or-func
45                       :accessor list-start-str-or-func)
46    (list-start-indent :initform nil :initarg :list-start-indent
47                       :accessor list-start-indent)
48    (list-end-str-or-func :type (or string function null) :initform nil
49                          :initarg :list-end-str-or-func
50                     :accessor list-end-str-or-func)
51    (list-end-indent :initform nil :initarg :list-end-indent
52                     :accessor list-end-indent)
53    (obj-start-str-or-func :type (or string function null) :initform nil :initarg :obj-start-str-or-func
54                      :accessor obj-start-str-or-func)
55    (obj-start-indent :initform nil :initarg :obj-start-indent
56                      :accessor obj-start-indent)
57    (obj-end-str-or-func :type (or string function null) :initform nil :initarg :obj-end-str-or-func
58                    :accessor obj-end-str-or-func)
59    (obj-end-indent :initform nil :initarg :obj-end-indent
60                    :accessor obj-end-indent)
61    (obj-data-indent :initform nil :initarg :obj-data-indent
62                     :accessor obj-data-indent)
63    (obj-data-func :type (or function null) :initform nil
64                         :initarg :obj-data-func
65                         :accessor obj-data-func)
66    (obj-data-print-code :type (or function null) :initform nil
67                   :initarg :obj-data-print-code
68                   :accessor obj-data-print-code)
69    (obj-data-end-str :type (or string null) :initform nil
70                         :initarg :obj-data-end-str
71                         :accessor obj-data-end-str)
72    (link-slots :type list :initform nil
73                :documentation "List of slot names that have hyperlinks"
74                :accessor link-slots)
75    (link-page-name :type (or string null) :initform nil :initarg :link-page-name
76                    :accessor link-page-name)
77    (link-href-start :type (or string null) :initform nil :initarg :link-href-start
78                     :accessor link-href-start)
79    (link-href-end :type (or string null) :initform nil :initarg :link-href-end
80                   :accessor link-href-end)
81    (link-ampersand :type (or string null) :initform nil :initarg :link-ampersand
82                    :accessor link-ampersand))
83   (:default-initargs :link-page-name "disp-func1")
84   (:documentation "View class for a hyperobject"))
85
86
87 (defun get-category-view (obj category &optional slots)
88   "Find or make a category view for an object"
89   (let ((obj-class (class-of obj)))
90     (if (null category)
91         (default-view obj-class)
92         (aif (find category (views obj-class) :key #'category)
93              it
94              (let ((view
95                     (make-instance 'object-view
96                                    :object-class-name (class-name obj-class)
97                                    :object-class obj-class
98                                    :category category
99                                    :slots slots)))
100                (push view (views obj-class))
101                view)))))
102                              
103 ;;;; *************************************************************************
104 ;;;;  Metaclass Intialization
105 ;;;; *************************************************************************
106
107 (defun finalize-views (cl)
108   "Finalize all views that are given on a objects initialization"
109   (unless (default-print-slots cl)
110     (setf (default-print-slots cl)
111           (mapcar #'slot-definition-name (class-slots cl))))
112   (let ((views '()))
113     (dolist (view-def (views cl))
114       (push (make-object-view cl view-def) views))
115     (setf (views cl) (nreverse views)))
116   (cond
117     ((default-view cl)
118      (setf (default-view cl) (make-object-view cl (default-view cl))))
119     ((car (views cl))
120      (setf (default-view cl) (make-object-view cl (car (views cl)))))
121     (t
122      (setf (default-view cl) (make-object-view cl :default)))))
123
124 (defun make-object-view (cl view-def)
125   "Make an object view from a definition. Do nothing if a class is passed so that reinitialization will be a no-op"
126   (cond
127     ((typep view-def 'object-view)
128      view-def)
129     ((eq view-def :default)
130      (let* ((name (class-name cl))
131             (view (make-instance 'object-view :name "automatic"
132                                  :object-class-name name
133                                  :object-class cl
134                                  :category :compact-text)))
135        view))
136     ((consp view-def)
137      (eval `(make-instance ,view-def)))
138     (t
139      (error "Invalid parameter to make-object-view: ~S" view-def))))
140
141 (defmethod initialize-instance :after ((view object-view)
142                                        &rest initargs &key &allow-other-keys)
143   (initialize-view (object-class view) view))
144   
145 (defun initialize-view (obj-cl view)
146   "Calculate all view slots for a hyperobject class"
147   (cond
148     ((category view)
149      (initialize-view-by-category obj-cl view))
150     ((source-code view)
151      (initialize-view-by-source-code obj-cl view))
152     (t
153      (setf (category view) :compact-text)
154      (initialize-view-by-category obj-cl view))))
155
156 (defun initialize-view-by-source-code (obj-cl view)
157   "Initialize a view based upon a source code"
158   (let ((source-code (source-code view)))
159     (error "not implemented")
160     )
161   )
162
163 (defmacro write-simple (v s)
164   `(if (stringp ,v)
165     (write-string ,v ,s)
166     (write-string (write-to-string ,v) ,s)))
167
168 (defun write-ho-value (obj name type formatter cdata strm)
169   (declare (ignorable type))
170   (let* ((slot-data (slot-value obj name))
171          (fmt-data (if formatter
172                        (funcall formatter slot-data)
173                    slot-data))
174          (data (if cdata
175                    (kmrcl:xml-cdata fmt-data)
176                    fmt-data)))
177     (write-simple data strm)))
178
179
180 (defun ppfc-html (title name type formatter cdata print-func)
181   (vector-push-extend '(write-string "<span class=\"" s) print-func)
182   (vector-push-extend `(write-string ,title s) print-func)
183   (vector-push-extend '(write-string "\">" s) print-func)
184   (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
185   (vector-push-extend '(write-string "</span>" s) print-func))
186
187 (defun ppfc-xml (tag name type formatter cdata print-func)
188   (vector-push-extend '(write-char #\< s) print-func)
189   (vector-push-extend `(write-string ,tag s) print-func)
190   (vector-push-extend '(write-char #\> s) print-func)
191   (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
192   (vector-push-extend '(write-string "</" s) print-func)
193   (vector-push-extend `(write-string ,tag s) print-func)
194   (vector-push-extend '(write-char #\> s) print-func))
195
196 (defun ppfc-html-labels (label name type formatter cdata print-func)
197   (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
198   (vector-push-extend `(write-string ,label s) print-func)
199   (vector-push-extend '(write-string "</span> " s) print-func)
200   (ppfc-html label name type formatter cdata print-func))
201
202 (defun ppfc-xhtml-labels (label tag name type formatter cdata print-func)
203   (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
204   (vector-push-extend `(write-string ,label s) print-func)
205   (vector-push-extend '(write-string "</span> " s) print-func)
206   (ppfc-html tag name type formatter cdata print-func))
207
208 (defun ppfc-xml-labels (label tag name type formatter cdata print-func)
209   (vector-push-extend '(write-string "<label>" s) print-func)
210   (vector-push-extend `(write-string ,label s) print-func)
211   (vector-push-extend '(write-string "</label> " s) print-func)
212   (ppfc-xml tag name type formatter cdata print-func))
213
214 (defun ppfc-html-link (name type formatter cdata nlink print-func)
215   (declare (fixnum nlink))
216   (vector-push-extend '(write-char #\< s) print-func)
217   (vector-push-extend `(write-string (nth ,(+ nlink nlink) links) s) print-func) 
218   (vector-push-extend '(write-char #\> s) print-func)
219   (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
220   (vector-push-extend '(write-string "</" s) print-func)
221   (vector-push-extend `(write-string (nth ,(+ nlink nlink 1) links) s) print-func) 
222   (vector-push-extend '(write-char #\> s) print-func))
223
224 (defun ppfc-html-link-labels (label name type formatter cdata nlink print-func)
225   (vector-push-extend '(write-string "<label>" s) print-func)
226   (vector-push-extend `(write-string ,label s) print-func)
227   (vector-push-extend '(write-string "</label> " s) print-func)
228   (ppfc-html-link name type formatter cdata nlink print-func))
229
230 (defun push-print-fun-code (category slot nlink print-func)
231   (let* ((formatter (esd-print-formatter slot))
232          (name (slot-definition-name slot))
233          (namestr-lower (string-downcase (symbol-name name)))
234          (xml-namestr (escape-xml-string namestr-lower))
235          (xml-tag (escape-xml-string namestr-lower))
236          (type (slot-value slot 'type))
237          (cdata (not (null
238                       (and (in category :xml :xhtml :xml-link :xhtml-link
239                                :xml-labels :ie-xml-labels
240                                :xhtml-link-labels :xml-link-labels :ie-xml-link
241                                :ie-xml-link-labels)
242                            (or formatter
243                                (lisp-type-is-a-string type))))))
244          (hyperlink (esd-hyperlink slot)))
245     
246     (case category
247       (:compact-text
248        (vector-push-extend
249         `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
250       (:compact-text-labels
251        (vector-push-extend `(write-string ,namestr-lower s) print-func)
252        (vector-push-extend '(write-char #\space s) print-func)
253        (vector-push-extend
254         `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
255       ((or :html :xhtml)
256        (ppfc-html namestr-lower name type formatter cdata print-func))
257       (:xml
258        (ppfc-xml xml-tag name type formatter cdata print-func))
259       (:html-labels
260        (ppfc-html-labels namestr-lower name type formatter cdata print-func))
261       (:xhtml-labels
262        (ppfc-xhtml-labels xml-namestr namestr-lower name type formatter cdata print-func))
263       (:xml-labels
264        (ppfc-xml-labels xml-namestr xml-tag name type formatter cdata print-func))
265       ((or :html-link :xhtml-link)
266        (if hyperlink
267            (ppfc-html-link name type formatter cdata nlink print-func)
268            (ppfc-html namestr-lower name type formatter cdata print-func)))
269       ((or :xml-link :ie-xml-link)
270        (if hyperlink
271            (ppfc-html-link name type formatter cdata nlink print-func)
272            (ppfc-xml xml-tag name type formatter cdata print-func)))
273       (:html-link-labels
274        (if hyperlink
275            (ppfc-html-link-labels namestr-lower name type formatter cdata nlink
276                                   print-func)
277            (ppfc-html-labels namestr-lower name type formatter cdata print-func)))
278       (:xhtml-link-labels
279        (if hyperlink
280            (ppfc-html-link-labels xml-namestr name type formatter cdata nlink
281                                   print-func)
282            (ppfc-xhtml-labels xml-tag namestr-lower name type formatter cdata
283                               print-func)))
284       ((or :xml-link-labels :ie-xml-link-labels)
285        (if hyperlink
286            (ppfc-html-link-labels xml-namestr name type formatter cdata nlink
287                                   print-func)
288            (ppfc-xml-labels xml-tag namestr-lower name type formatter cdata
289                             print-func))))))
290
291
292 (defun view-has-links-p (view)
293   (in (category view) :html-link :xhtml-link :xml-link :ie-xml-link
294       :html-link-labels :xhtml-link-labels :xml-link-labels
295       :ie-xml-link-labels))
296
297 (defun initialize-view-by-category (obj-cl view)
298   "Initialize a view based upon a preset category"
299   (unless (in (category view) :compact-text :compact-text-labels
300               :html :html-labels :html-link-labels
301               :xhtml :xhtml-labels :xhtml-link-labels
302               :xhtml-link :html-link
303               :xml :xml-labels :xml-link :ie-xml-link
304               :xml-link-labels :ie-xml-link-labels)
305     (error "Unknown view category ~A" (category view)))
306   
307   (unless (slots view) (setf (slots view) (default-print-slots obj-cl)))
308
309   (let ((links '())
310         (print-func (make-array 10 :fill-pointer 0 :adjustable t)))
311
312     (do* ((slots (slots view) (cdr slots))
313           (slot-name (car slots) (car slots))
314           (slot (find-slot-by-name obj-cl slot-name)
315                 (find-slot-by-name obj-cl slot-name)))
316          ((null slots))
317       (unless slot
318         (error "Slot ~A is not found in class ~S" slot-name obj-cl))
319       
320       (push-print-fun-code (category view) slot (length links) print-func)
321       (when (> (length slots) 1)
322         (vector-push-extend '(write-char #\space s) print-func))
323
324       (when (and (view-has-links-p view) (esd-hyperlink slot))
325         (push (slot-definition-name slot) links)))
326
327     (when (plusp (length print-func))
328       (setf (obj-data-print-code view) `(lambda (x s links)
329                                          (declare (ignorable links))
330                                          ,@(map 'list #'identity print-func)))
331       (setf (obj-data-func view)
332             (compile nil (eval (obj-data-print-code view)))))
333     
334     (setf (link-slots view) (nreverse links)))
335
336   (finalize-view-by-category view)
337   view)
338
339 (defun finalize-view-by-category (view)
340   (case (category view)
341     ((or :compact-text :compact-text-labels)
342      (initialize-text-view view))
343     ((or :html :xhtml :html-labels :xhtml-labels)
344      (initialize-html-view view))
345     ((or :xml :xml-labels)
346      (initialize-xml-view view))
347     ((or :html-link :html-link-labels)
348      (initialize-html-view view)
349      (setf (link-href-start view) "a href=")
350      (setf (link-href-end view) "a")
351      (setf (link-ampersand view) "&"))
352     ((or :xhtml-link :xhtml-link-labels)
353      (initialize-html-view view)
354      (setf (link-href-start view) "a href=")
355      (setf (link-href-end view) "a")
356      (setf (link-ampersand view) "&amp;"))
357     ((or :xml-link :xml-link-labels)
358      (initialize-xml-view view)
359      (setf (link-href-start view)
360            "xmllink xlink:type=\"simple\" xlink:href=")
361      (setf (link-href-end view) "xmllink")
362      (setf (link-ampersand view) "&amp;"))
363     ((or :ie-xml-link :ie-xml-link-labels)
364      (initialize-xml-view view)
365      (setf (link-href-start view) "html:a href=")
366      (setf (link-href-end view) "html:a")
367      (setf (link-ampersand view) "&amp;"))))
368
369 #+ignore
370 (defun initialize-view-by-category (obj-cl view)
371   "Initialize a view based upon a preset category"
372   (let ((fmtstr nil)
373         (first-field t)
374         (value-func '())
375         (links '())
376         (category (category view)))
377
378     (unless (in category :compact-text :compact-text-labels
379                 :html :html-labels :html-link-labels
380                 :xhtml :xhtml-labels :xhtml-link-labels
381                 :xml :xml-labels :xml-link :ie-xml-link
382                 :xml-link-labels :ie-xml-link-labels)
383       (error "Unknown view category ~A" category))
384     
385     (unless (slots view)
386       (setf (slots view) (default-print-slots obj-cl)))
387     (dolist (slot-name (slots view))
388       (let ((slot (find-slot-by-name obj-cl slot-name)))
389         (unless slot
390           (error "Slot ~A is not found in class ~S" slot-name obj-cl))
391         (let* ((name (slot-definition-name slot))
392                (namestr-lower (string-downcase (symbol-name name)))
393                (xml-namestr (escape-xml-string namestr-lower))
394                (xml-tag (escape-xml-string namestr-lower))
395                (type (slot-value slot 'type))
396                (print-formatter (esd-print-formatter slot)))
397
398           (cond
399             (first-field
400              (setq fmtstr "")
401              (setq first-field nil))
402             (t
403              (string-append fmtstr " ")))
404
405           (let ((value-fmt
406                  (case type
407                    ((or :integer :fixnum)
408                     "~d")
409                    (:boolean
410                     "~a")
411                    (otherwise
412                     "~a"))))
413             (case category
414               (:compact-text
415                (string-append fmtstr value-fmt))
416               (:compact-text-labels
417                (string-append fmtstr namestr-lower " " value-fmt))
418               ((or :html :xhtml)
419                (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
420               (:xml
421                (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
422               (:html-labels
423                (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
424               (:xhtml-labels
425                (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
426               (:xml-labels
427                (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))
428               ((or :html-link :xhtml-link)
429                (push name links)
430                (if (esd-hyperlink slot)
431                    (string-append fmtstr "<~~a>" value-fmt "</~~a>")
432                    (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))))
433               ((or :xml-link :ie-xml-link)
434                (push name links)
435                (if (esd-hyperlink slot)
436                    (string-append fmtstr "<~~a>" value-fmt "</~~a>")
437                    (string-append fmtstr (concatenate 'string "<" xml-tag ">" value-fmt "</" xml-tag ">"))))
438               (:html-link-labels
439                (push name links)
440                (if (esd-hyperlink slot)
441                    (string-append fmtstr "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
442                    (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
443               (:xhtml-link-labels
444                (push name links)
445                (if (esd-hyperlink slot)
446                    (string-append fmtstr "<span class=\"label\">" xml-namestr "</span> <~~a>" value-fmt "</~~a>")
447                    (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
448               ((or :xml-link-labels :ie-xml-link-labels)
449                (push name links)
450                (if (esd-hyperlink slot)
451                    (string-append fmtstr "<label>" xml-namestr "</label> <~~a>" value-fmt "</~~a>")
452                    (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))))
453             ) ;; let value-fmt
454             
455           (let ((func (if print-formatter
456                   `(,print-formatter (slot-value x (quote ,name)))
457                   `(slot-value x (quote ,name)))))
458             (when (and (in category :xml :xhtml :xml-link :xhtml-link
459                            :xml-labels :ie-xml-labels
460                            :xhtml-link-labels :xml-link-labels :ie-xml-link
461                            :ie-xml-link-labels)
462                        (or print-formatter
463                            (lisp-type-is-a-string type)))
464               (setq func `(kmrcl:xml-cdata ,func)))
465             (push func value-func))
466           )))
467           
468     (when value-func
469       (setq value-func
470             (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
471
472     (setf (obj-data-fmtstr view) fmtstr)
473     (setf (obj-data-value-func view) value-func)
474     (setf (link-slots view) (nreverse links))
475     
476     (case category
477       ((or :compact-text :compact-text-labels)
478        (initialize-text-view view))
479       ((or :html :xhtml :html-labels :xhtml-labels)
480        (initialize-html-view view))
481       ((or :xml :xml-labels)
482        (initialize-xml-view view))
483       ((or :html-link :html-link-labels)
484        (initialize-html-view view)
485        (setf (link-href-start view) "a href=")
486        (setf (link-href-end view) "a")
487        (setf (link-ampersand view) "&"))
488       ((or :xhtml-link :xhtml-link-labels)
489        (initialize-html-view view)
490        (setf (link-href-start view) "a href=")
491        (setf (link-href-end view) "a")
492        (setf (link-ampersand view) "&amp;"))
493       ((or :xml-link :xml-link-labels)
494        (initialize-xml-view view)
495        (setf (link-href-start view)
496              "xmllink xlink:type=\"simple\" xlink:href=")
497        (setf (link-href-end view) "xmllink")
498        (setf (link-ampersand view) "&amp;"))
499       ((or :ie-xml-link :ie-xml-link-labels)
500        (initialize-xml-view view)
501        (setf (link-href-start view) "html:a href=")
502        (setf (link-href-end view) "html:a")
503        (setf (link-ampersand view) "&amp;"))))
504   view)
505
506
507 ;;;; *************************************************************************
508 ;;;;  View Data Format Section
509 ;;;; *************************************************************************
510
511 (defun class-name-of (obj)
512   (string-downcase (class-name (class-of obj))))
513
514 (defvar +newline-string+ (format nil "~%"))
515
516 (defun write-user-name-maybe-plural (obj nitems strm)
517   (write-string
518    (if (> nitems 1)
519        (hyperobject-class-user-name-plural obj)
520        (hyperobject-class-user-name obj))
521    strm))
522
523 (defun initialize-text-view (view)
524   (setf (list-start-str-or-func view)
525         (compile nil
526                  (eval '(lambda (obj nitems strm)
527                          (write-user-name-maybe-plural obj nitems strm)
528                          (write-char #\: strm)
529                          (write-char #\Newline strm)))))
530   (setf (list-start-indent view) t)
531   (setf (obj-data-indent view) t)
532   (setf (obj-data-end-str view) +newline-string+))
533
534 (defun html-list-start-func (obj nitems strm)
535   (write-string "<p><b>" strm)
536   (write-user-name-maybe-plural obj nitems strm)
537   (write-string ":</b></p><div class=\"" strm)
538   (write-string (class-name-of obj) strm)
539   (write-string "\"><ul>" strm)
540   (write-char #\newline strm))
541
542 (defun initialize-html-view (view)
543   (initialize-text-view view)
544   (setf (file-start-str view) (format nil "<html><body>~%"))
545   (setf (file-end-str view) (format nil "</body><html>~%"))
546   (setf (list-start-indent view) t)
547   (setf (list-start-str-or-func view) #'html-list-start-func)
548   (setf (list-end-str-or-func view) (format nil "</ul></div>~%"))
549   (setf (list-end-indent view) t)
550   (setf (obj-start-indent view) t)
551   (setf (obj-start-str-or-func view) "<li>")
552   (setf (obj-end-indent view)  t)
553   (setf (obj-end-str-or-func view)  (format nil "</li>~%"))
554   (setf (obj-data-indent view) nil))
555
556 (defun initialize-xhtml-view (view)
557   (initialize-text-view view)
558   (setf (file-start-str view) (format nil "<html><body>~%"))
559   (setf (file-end-str view) (format nil "</body><html>~%"))
560   (setf (list-start-indent view) t)
561   (setf (list-start-str-or-func view) #'html-list-start-func)
562   (setf (list-end-str-or-func view) (format nil "</ul></div>~%"))
563   (setf (list-end-indent view) t)
564   (setf (obj-start-indent view) t)
565   (setf (obj-start-str-or-func view) "<li>")
566   (setf (obj-end-indent view)  t)
567   (setf (obj-end-str-or-func view) (format nil "</li>~%"))
568   (setf (obj-data-indent view) nil))
569
570 (defun xmlformat-list-end-func (x strm)
571   (write-string "</" strm)
572   (write-string (class-name-of x) strm)
573   (write-string "list" strm)
574   (write-string ">" strm)
575   (write-char #\newline strm))
576
577 (defun xmlformat-list-start-func (x nitems strm)
578   (write-char #\< strm)
579   (write-string (class-name-of x) strm)
580   (write-string "list><title>" strm)
581   (write-user-name-maybe-plural x nitems strm)
582   (write-string ":</title>" strm)
583   (write-char #\newline strm))
584
585 (defun initialize-xml-view (view)
586   (initialize-text-view view)
587   (setf (file-start-str view) "") ; (std-xml-header)
588   (setf (list-start-indent view)  t)
589   (setf (list-start-str-or-func view) #'xmlformat-list-start-func)
590   (setf (list-end-indent view) t)
591   (setf (list-end-str-or-func view) #'xmlformat-list-end-func)
592   (setf (obj-start-str-or-func view) (format nil "<~(~a~)>" (object-class-name view)))
593   (setf (obj-start-indent view) t)
594   (setf (obj-end-str-or-func view) (format nil "</~(~a~)>~%" (object-class-name view)))
595   (setf (obj-end-indent view) nil)
596   (setf (obj-data-indent view) nil))
597
598
599 ;;; File Start and Ends
600
601 (defun fmt-file-start (view strm)
602   (awhen (file-start-str view)
603          (write-string it strm)))
604
605 (defun fmt-file-end (view strm)
606   (awhen (file-end-str view)
607          (write-string it strm)))
608
609 ;;; List Start and Ends
610
611 (defun fmt-list-start (obj view strm indent num-items)
612   (when (list-start-indent view)
613     (indent-spaces indent strm))
614   (awhen (list-start-str-or-func view)
615          (if (stringp it)
616              (write-string it strm)
617              (funcall it obj num-items strm))))
618
619 (defun fmt-list-end (obj view strm indent num-items)
620   (declare (ignore num-items))
621   (when (list-end-indent view)
622       (indent-spaces indent strm))
623   (awhen (list-end-str-or-func view)
624          (if (stringp it)
625              (write-string it strm)
626              (funcall it obj strm))))
627
628 ;;; Object Start and Ends
629
630 (defun fmt-obj-start (obj view strm indent)
631   (when (obj-start-indent view)
632     (indent-spaces indent strm))
633   (awhen (obj-start-str-or-func view)
634          (if (stringp it)
635              (write-string it strm)
636              (funcall it obj strm))))
637
638 (defun fmt-obj-end (obj view strm indent)
639   (when (obj-end-indent view)
640     (indent-spaces indent strm))
641   (awhen (obj-end-str-or-func view)
642          (if (stringp it)
643              (write-string it strm)
644              (funcall it obj strm))))
645   
646 ;;; Object Data 
647
648
649 (defun make-link-start (view fieldfunc fieldvalue refvars)
650   (with-output-to-string (s)
651     (write-string (link-href-start view) s)
652     (write-char #\" s)
653     (write-string (make-url (link-page-name view)) s)
654     (write-string "?func=" s)
655     (write-simple fieldfunc s)
656     (write-string (link-ampersand view) s)
657     (write-string "key=" s)
658     (write-simple fieldvalue s)
659     (dolist (var refvars)
660       (write-string (link-ampersand view) s)
661       (write-simple (car var) s)
662       (write-char #\= s)
663       (write-simple (cdr var) s))
664     (write-char #\" s)))
665   
666 (defun make-link-end (obj view fieldname)
667   (declare (ignore obj fieldname))
668   (link-href-end view))
669
670 (defun fmt-obj-data (obj view strm indent refvars)
671   (when (obj-data-indent view)
672     (indent-spaces indent strm))
673   (if (link-slots view)
674       (fmt-obj-data-with-link obj view strm refvars)
675       (fmt-obj-data-plain obj view strm))
676   (awhen (obj-data-end-str view)
677          (write-string it strm)))
678
679 (defun fmt-obj-data-plain (obj view strm)
680   (awhen (obj-data-func view)
681          (funcall it obj strm nil)))
682
683 (defun fmt-obj-data-with-link (obj view strm refvars)
684   (let ((refvalues '()))
685     ;; make list of hyperlink link fields for printing to refstr template
686     (dolist (name (link-slots view))
687       (awhen (find name (hyperobject-class-hyperlinks obj) :key #'name)
688              (push (make-link-start view (lookup it) (slot-value obj name)
689                                     (append (link-parameters it) refvars))
690                    refvalues)
691              (push (make-link-end obj view name) refvalues)))
692     (funcall (obj-data-func view) obj strm (nreverse refvalues))))
693
694 (defun obj-data (obj view)
695   "Returns the objects data as a string. Used by common-graphics outline function"
696   (with-output-to-string (s) (fmt-obj-data-plain obj view s)))
697
698 ;;; Display method for objects
699
700
701 (defun load-all-subobjects (objs)
702   "Load all subobjects if they have not already been loaded."
703   (dolist (obj (mklist objs))
704     (dolist (subobj (hyperobject-class-subobjects obj))
705       (awhen (slot-value obj (name-slot subobj))
706              (load-all-subobjects it))))
707   objs)
708
709 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
710                          subobjects refvars)
711   "Display a single or list of hyperobject-class instances and their subobjects"
712   (declare (fixnum indent))
713   (let-when (objlist (mklist objs))
714     (let ((nobjs (length objlist))
715           (*print-pretty* nil)
716           (*print-circle* nil)
717           (*print-escape* nil)
718           (*print-readably* nil)
719           (*print-length* nil)
720           (*print-level* nil))
721       (fmt-list-start (car objlist) view strm indent nobjs)
722       (dolist (obj objlist)
723         (unless (and filter (not (funcall filter obj)))
724           (fmt-obj-start obj view strm indent)
725           (fmt-obj-data obj view strm (1+ indent) refvars)
726           (when (and subobjects (hyperobject-class-subobjects obj))
727             (dolist (subobj (hyperobject-class-subobjects obj))
728               (aif (slot-value obj (name-slot subobj))
729                    (view-hyperobject it (get-category-view (car (mklist it)) category)
730                                      category strm (1+ indent) filter
731                                      subobjects refvars))))
732           (fmt-obj-end obj view strm indent)))
733       (fmt-list-end (car objlist) view strm indent nobjs)))
734   objs)
735
736
737 (defun view (objs &key (stream *standard-output*) category view
738              filter subobjects refvars file-wrapper)
739   "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
740   (let-when (objlist (mklist objs))
741     (when category
742       (setq view (get-category-view (car objlist) category)))
743     (unless view
744       (setq view (default-view (class-of (car objlist)))))
745     (when file-wrapper
746       (fmt-file-start view stream))
747     (view-hyperobject objlist view category stream 0 filter subobjects refvars)
748     (when file-wrapper
749       (fmt-file-end view stream)))
750   objs)
751
752
753 ;;; Misc formatting
754
755 (defun fmt-comma-integer (i)
756   (format nil "~:d" i))