r4930: Auto commit for Debian build
[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.40 2003/05/14 07:00:47 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
164 (defun write-ho-value (obj name type formatter cdata strm)
165   (declare (ignorable type))
166   (let* ((slot-data (slot-value obj name))
167          (fmt-data (if formatter
168                        (funcall formatter slot-data)
169                    slot-data))
170          (data (if cdata
171                    (kmrcl:xml-cdata fmt-data)
172                    fmt-data)))
173     (typecase data
174       (string
175        (write-string data strm))
176       (t
177        (write-string (write-to-string data) strm)))))
178
179 (defun ppfc-html (title name type formatter cdata print-func)
180   (vector-push-extend '(write-string "<span class=\"" s) print-func)
181   (vector-push-extend `(write-string ,title s) print-func)
182   (vector-push-extend '(write-string "\">" s) print-func)
183   (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
184   (vector-push-extend '(write-string "</span>" s) print-func))
185
186 (defun ppfc-xml (tag name type formatter cdata print-func)
187   (vector-push-extend '(write-char #\< s) print-func)
188   (vector-push-extend `(write-string ,tag s) print-func)
189   (vector-push-extend '(write-char #\> s) print-func)
190   (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
191   (vector-push-extend '(write-string "</" s) print-func)
192   (vector-push-extend `(write-string ,tag s) print-func)
193   (vector-push-extend '(write-char #\> s) print-func))
194
195 (defun ppfc-html-labels (label name type formatter cdata print-func)
196   (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
197   (vector-push-extend `(write-string ,label s) print-func)
198   (vector-push-extend '(write-string "</span> " s) print-func)
199   (ppfc-html label name type formatter cdata print-func))
200
201 (defun ppfc-xhtml-labels (label tag name type formatter cdata print-func)
202   (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
203   (vector-push-extend `(write-string ,label s) print-func)
204   (vector-push-extend '(write-string "</span> " s) print-func)
205   (ppfc-html tag name type formatter cdata print-func))
206
207 (defun ppfc-xml-labels (label tag name type formatter cdata print-func)
208   (vector-push-extend '(write-string "<label>" s) print-func)
209   (vector-push-extend `(write-string ,label s) print-func)
210   (vector-push-extend '(write-string "</label> " s) print-func)
211   (ppfc-xml tag name type formatter cdata print-func))
212
213 (defun ppfc-html-link (name type formatter cdata nlink print-func)
214   (declare (fixnum nlink))
215   (vector-push-extend '(write-char #\< s) print-func)
216   (vector-push-extend `(write-string (nth ,(+ nlink nlink) links) s) print-func) 
217   (vector-push-extend '(write-char #\> s) print-func)
218   (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
219   (vector-push-extend '(write-string "</" s) print-func)
220   (vector-push-extend `(write-string (nth ,(+ nlink nlink 1) links) s) print-func) 
221   (vector-push-extend '(write-char #\> s) print-func))
222
223 (defun ppfc-html-link-labels (label name type formatter cdata nlink print-func)
224   (vector-push-extend '(write-string "<label>" s) print-func)
225   (vector-push-extend `(write-string ,label s) print-func)
226   (vector-push-extend '(write-string "</label> " s) print-func)
227   (ppfc-html-link name type formatter cdata nlink print-func))
228
229 (defun push-print-fun-code (category slot nlink print-func)
230   (let* ((formatter (esd-print-formatter slot))
231          (name (slot-definition-name slot))
232          (namestr-lower (string-downcase (symbol-name name)))
233          (xml-namestr (escape-xml-string namestr-lower))
234          (xml-tag (escape-xml-string namestr-lower))
235          (type (slot-value slot 'type))
236          (cdata (not (null
237                       (and (in category :xml :xhtml :xml-link :xhtml-link
238                                :xml-labels :ie-xml-labels
239                                :xhtml-link-labels :xml-link-labels :ie-xml-link
240                                :ie-xml-link-labels)
241                            (or formatter
242                                (lisp-type-is-a-string type))))))
243          (hyperlink (esd-hyperlink slot)))
244     
245     (case category
246       (:compact-text
247        (vector-push-extend
248         `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
249       (:compact-text-labels
250        (vector-push-extend `(write-string ,namestr-lower s) print-func)
251        (vector-push-extend '(write-char #\space s) print-func)
252        (vector-push-extend
253         `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
254       ((or :html :xhtml)
255        (ppfc-html namestr-lower name type formatter cdata print-func))
256       (:xml
257        (ppfc-xml xml-tag name type formatter cdata print-func))
258       (:html-labels
259        (ppfc-html-labels namestr-lower name type formatter cdata print-func))
260       (:xhtml-labels
261        (ppfc-xhtml-labels xml-namestr namestr-lower name type formatter cdata print-func))
262       (:xml-labels
263        (ppfc-xml-labels xml-namestr xml-tag name type formatter cdata print-func))
264       ((or :html-link :xhtml-link)
265        (if hyperlink
266            (ppfc-html-link name type formatter cdata nlink print-func)
267            (ppfc-html namestr-lower name type formatter cdata print-func)))
268       ((or :xml-link :ie-xml-link)
269        (if hyperlink
270            (ppfc-html-link name type formatter cdata nlink print-func)
271            (ppfc-xml xml-tag name type formatter cdata print-func)))
272       (:html-link-labels
273        (if hyperlink
274            (ppfc-html-labels namestr-lower name type formatter cdata print-func)))
275       (:xhtml-link-labels
276        (if hyperlink
277            (ppfc-html-link-labels xml-namestr name type formatter cdata nlink
278                                   print-func)
279            (ppfc-xhtml-labels xml-tag namestr-lower name type formatter cdata
280                               print-func)))
281       ((or :xml-link-labels :ie-xml-link-labels)
282        (if hyperlink
283            (ppfc-html-link-labels xml-namestr name type formatter cdata nlink
284                                   print-func)
285            (ppfc-xml-labels xml-tag namestr-lower name type formatter cdata
286                             print-func))))))
287
288
289 (defun view-has-links-p (view)
290   (in (category view) :html-link :xhtml-link :xml-link :ie-xml-link
291       :html-link-labels :xhtml-links-labels :xml-link-labels
292       :ie-xml-link-labels))
293
294 (defun initialize-view-by-category (obj-cl view)
295   "Initialize a view based upon a preset category"
296   (unless (in (category view) :compact-text :compact-text-labels
297               :html :html-labels :html-link-labels
298               :xhtml :xhtml-labels :xhtml-link-labels
299               :xml :xml-labels :xml-link :ie-xml-link
300               :xml-link-labels :ie-xml-link-labels)
301     (error "Unknown view category ~A" (category view)))
302   
303   (unless (slots view) (setf (slots view) (default-print-slots obj-cl)))
304
305   (let ((links '())
306         (print-func (make-array 10 :fill-pointer 0 :adjustable t)))
307
308     (do* ((slots (slots view) (cdr slots))
309           (slot-name (car slots) (car slots))
310           (slot (find-slot-by-name obj-cl slot-name)
311                 (find-slot-by-name obj-cl slot-name)))
312          ((null slots))
313       (unless slot
314         (error "Slot ~A is not found in class ~S" slot-name obj-cl))
315       
316       (push-print-fun-code (category view) slot (length links) print-func)
317       (when (> (length slots) 1)
318         (vector-push-extend '(write-char #\space s) print-func))
319
320       (when (and (view-has-links-p view) (esd-hyperlink slot))
321         (push (slot-definition-name slot) links)))
322
323     (when (plusp (length print-func))
324       (setf (obj-data-print-code view) `(lambda (x s links)
325                                          (declare (ignorable links))
326                                          ,@(map 'list #'identity print-func)))
327       (setf (obj-data-func view)
328             (compile nil (eval (obj-data-print-code view)))))
329     
330     (setf (link-slots view) (nreverse links)))
331
332   (finalize-view-by-category view)
333   view)
334
335 (defun finalize-view-by-category (view)
336   (case (category view)
337     ((or :compact-text :compact-text-labels)
338      (initialize-text-view view))
339     ((or :html :xhtml :html-labels :xhtml-labels)
340      (initialize-html-view view))
341     ((or :xml :xml-labels)
342      (initialize-xml-view view))
343     ((or :html-link :html-link-labels)
344      (initialize-html-view view)
345      (setf (link-href-start view) "a href=")
346      (setf (link-href-end view) "a")
347      (setf (link-ampersand view) "&"))
348     ((or :xhtml-link :xhtml-link-labels)
349      (initialize-html-view view)
350      (setf (link-href-start view) "a href=")
351      (setf (link-href-end view) "a")
352      (setf (link-ampersand view) "&amp;"))
353     ((or :xml-link :xml-link-labels)
354      (initialize-xml-view view)
355      (setf (link-href-start view)
356            "xmllink xlink:type=\"simple\" xlink:href=")
357      (setf (link-href-end view) "xmllink")
358      (setf (link-ampersand view) "&amp;"))
359     ((or :ie-xml-link :ie-xml-link-labels)
360      (initialize-xml-view view)
361      (setf (link-href-start view) "html:a href=")
362      (setf (link-href-end view) "html:a")
363      (setf (link-ampersand view) "&amp;"))))
364
365 #+ignore
366 (defun initialize-view-by-category (obj-cl view)
367   "Initialize a view based upon a preset category"
368   (let ((fmtstr nil)
369         (first-field t)
370         (value-func '())
371         (links '())
372         (category (category view)))
373
374     (unless (in category :compact-text :compact-text-labels
375                 :html :html-labels :html-link-labels
376                 :xhtml :xhtml-labels :xhtml-link-labels
377                 :xml :xml-labels :xml-link :ie-xml-link
378                 :xml-link-labels :ie-xml-link-labels)
379       (error "Unknown view category ~A" category))
380     
381     (unless (slots view)
382       (setf (slots view) (default-print-slots obj-cl)))
383     (dolist (slot-name (slots view))
384       (let ((slot (find-slot-by-name obj-cl slot-name)))
385         (unless slot
386           (error "Slot ~A is not found in class ~S" slot-name obj-cl))
387         (let* ((name (slot-definition-name slot))
388                (namestr-lower (string-downcase (symbol-name name)))
389                (xml-namestr (escape-xml-string namestr-lower))
390                (xml-tag (escape-xml-string namestr-lower))
391                (type (slot-value slot 'type))
392                (print-formatter (esd-print-formatter slot)))
393
394           (cond
395             (first-field
396              (setq fmtstr "")
397              (setq first-field nil))
398             (t
399              (string-append fmtstr " ")))
400
401           (let ((value-fmt
402                  (case type
403                    ((or :integer :fixnum)
404                     "~d")
405                    (:boolean
406                     "~a")
407                    (otherwise
408                     "~a"))))
409             (case category
410               (:compact-text
411                (string-append fmtstr value-fmt))
412               (:compact-text-labels
413                (string-append fmtstr namestr-lower " " value-fmt))
414               ((or :html :xhtml)
415                (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
416               (:xml
417                (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
418               (:html-labels
419                (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
420               (:xhtml-labels
421                (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
422               (:xml-labels
423                (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))
424               ((or :html-link :xhtml-link)
425                (push name links)
426                (if (esd-hyperlink slot)
427                    (string-append fmtstr "<~~a>" value-fmt "</~~a>")
428                    (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))))
429               ((or :xml-link :ie-xml-link)
430                (push name links)
431                (if (esd-hyperlink slot)
432                    (string-append fmtstr "<~~a>" value-fmt "</~~a>")
433                    (string-append fmtstr (concatenate 'string "<" xml-tag ">" value-fmt "</" xml-tag ">"))))
434               (:html-link-labels
435                (push name links)
436                (if (esd-hyperlink slot)
437                    (string-append fmtstr "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
438                    (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
439               (:xhtml-link-labels
440                (push name links)
441                (if (esd-hyperlink slot)
442                    (string-append fmtstr "<span class=\"label\">" xml-namestr "</span> <~~a>" value-fmt "</~~a>")
443                    (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
444               ((or :xml-link-labels :ie-xml-link-labels)
445                (push name links)
446                (if (esd-hyperlink slot)
447                    (string-append fmtstr "<label>" xml-namestr "</label> <~~a>" value-fmt "</~~a>")
448                    (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))))
449             ) ;; let value-fmt
450             
451           (let ((func (if print-formatter
452                   `(,print-formatter (slot-value x (quote ,name)))
453                   `(slot-value x (quote ,name)))))
454             (when (and (in category :xml :xhtml :xml-link :xhtml-link
455                            :xml-labels :ie-xml-labels
456                            :xhtml-link-labels :xml-link-labels :ie-xml-link
457                            :ie-xml-link-labels)
458                        (or print-formatter
459                            (lisp-type-is-a-string type)))
460               (setq func `(kmrcl:xml-cdata ,func)))
461             (push func value-func))
462           )))
463           
464     (when value-func
465       (setq value-func
466             (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
467
468     (setf (obj-data-fmtstr view) fmtstr)
469     (setf (obj-data-value-func view) value-func)
470     (setf (link-slots view) (nreverse links))
471     
472     (case category
473       ((or :compact-text :compact-text-labels)
474        (initialize-text-view view))
475       ((or :html :xhtml :html-labels :xhtml-labels)
476        (initialize-html-view view))
477       ((or :xml :xml-labels)
478        (initialize-xml-view view))
479       ((or :html-link :html-link-labels)
480        (initialize-html-view view)
481        (setf (link-href-start view) "a href=")
482        (setf (link-href-end view) "a")
483        (setf (link-ampersand view) "&"))
484       ((or :xhtml-link :xhtml-link-labels)
485        (initialize-html-view view)
486        (setf (link-href-start view) "a href=")
487        (setf (link-href-end view) "a")
488        (setf (link-ampersand view) "&amp;"))
489       ((or :xml-link :xml-link-labels)
490        (initialize-xml-view view)
491        (setf (link-href-start view)
492              "xmllink xlink:type=\"simple\" xlink:href=")
493        (setf (link-href-end view) "xmllink")
494        (setf (link-ampersand view) "&amp;"))
495       ((or :ie-xml-link :ie-xml-link-labels)
496        (initialize-xml-view view)
497        (setf (link-href-start view) "html:a href=")
498        (setf (link-href-end view) "html:a")
499        (setf (link-ampersand view) "&amp;"))))
500   view)
501
502
503 ;;;; *************************************************************************
504 ;;;;  View Data Format Section
505 ;;;; *************************************************************************
506
507 (defun class-name-of (obj)
508   (string-downcase (class-name (class-of obj))))
509
510 (defvar +newline-string+ (format nil "~%"))
511
512 (defun write-user-name-maybe-plural (obj nitems strm)
513   (write-string
514    (if (> nitems 1)
515        (hyperobject-class-user-name-plural obj)
516        (hyperobject-class-user-name obj))
517    strm))
518
519 (defun initialize-text-view (view)
520   (setf (list-start-str-or-func view)
521         (compile nil
522                  (eval '(lambda (obj nitems strm)
523                          (write-user-name-maybe-plural obj nitems strm)
524                          (write-char #\: strm)
525                          (write-char #\Newline strm)))))
526   (setf (list-start-indent view) t)
527   (setf (obj-data-indent view) t)
528   (setf (obj-data-end-str view) +newline-string+))
529
530 (defun html-list-start-func (obj nitems strm)
531   (write-string "<p><b>" strm)
532   (write-user-name-maybe-plural obj nitems strm)
533   (write-string ":</b></p><div class=\"" strm)
534   (write-string (class-name-of obj) strm)
535   (write-string "\"><ul>" strm)
536   (write-char #\newline strm))
537
538 (defun initialize-html-view (view)
539   (initialize-text-view view)
540   (setf (file-start-str view) (format nil "<html><body>~%"))
541   (setf (file-end-str view) (format nil "</body><html>~%"))
542   (setf (list-start-indent view) t)
543   (setf (list-start-str-or-func view) #'html-list-start-func)
544   (setf (list-end-str-or-func view) (format nil "</ul></div>~%"))
545   (setf (list-end-indent view) t)
546   (setf (obj-start-indent view) t)
547   (setf (obj-start-str-or-func view) "<li>")
548   (setf (obj-end-indent view)  t)
549   (setf (obj-end-str-or-func view)  (format nil "</li>~%"))
550   (setf (obj-data-indent view) t))
551
552 (defun initialize-xhtml-view (view)
553   (initialize-text-view view)
554   (setf (file-start-str view) (format nil "<html><body>~%"))
555   (setf (file-end-str view) (format nil "</body><html>~%"))
556   (setf (list-start-indent view) t)
557   (setf (list-start-str-or-func view) #'html-list-start-func)
558   (setf (list-end-str-or-func view) (format nil "</ul></div>~%"))
559   (setf (list-end-indent view) t)
560   (setf (obj-start-indent view) t)
561   (setf (obj-start-str-or-func view) "<li>")
562   (setf (obj-end-indent view)  t)
563   (setf (obj-end-str-or-func view) (format nil "</li>~%"))
564   (setf (obj-data-indent view) t))
565
566 (defun xmlformat-list-end-func (x strm)
567   (write-string "</" strm)
568   (write-string (class-name-of x) strm)
569   (write-string "list" strm)
570   (write-string ">" strm)
571   (write-char #\newline strm))
572
573 (defun xmlformat-list-start-func (x nitems strm)
574   (write-char #\< strm)
575   (write-string (class-name-of x) strm)
576   (write-string "list><title>" strm)
577   (write-user-name-maybe-plural x nitems strm)
578   (write-string ":</title>" strm)
579   (write-char #\newline strm))
580
581 (defun initialize-xml-view (view)
582   (initialize-text-view view)
583   (setf (file-start-str view) "") ; (std-xml-header)
584   (setf (list-start-indent view)  t)
585   (setf (list-start-str-or-func view) #'xmlformat-list-start-func)
586   (setf (list-end-indent view) t)
587   (setf (list-end-str-or-func view) #'xmlformat-list-end-func)
588   (setf (obj-start-str-or-func view) (format nil "<~(~a~)>" (object-class-name view)))
589   (setf (obj-start-indent view) t)
590   (setf (obj-end-str-or-func view) (format nil "</~(~a~)>~%" (object-class-name view)))
591   (setf (obj-end-indent view) nil)
592   (setf (obj-data-indent view) nil))
593
594
595 ;;; File Start and Ends
596
597 (defun fmt-file-start (view strm)
598   (awhen (file-start-str view)
599          (write-string it strm)))
600
601 (defun fmt-file-end (view strm)
602   (awhen (file-end-str view)
603          (write-string it strm)))
604
605 ;;; List Start and Ends
606
607 (defun fmt-list-start (obj view strm indent num-items)
608   (when (list-start-indent view)
609     (indent-spaces indent strm))
610   (awhen (list-start-str-or-func view)
611          (if (stringp it)
612              (write-string it strm)
613              (funcall it obj num-items strm))))
614
615 (defun fmt-list-end (obj view strm indent num-items)
616   (declare (ignore num-items))
617   (when (list-end-indent view)
618       (indent-spaces indent strm))
619   (awhen (list-end-str-or-func view)
620          (if (stringp it)
621              (write-string it strm)
622              (funcall it obj strm))))
623
624 ;;; Object Start and Ends
625
626 (defun fmt-obj-start (obj view strm indent)
627   (when (obj-start-indent view)
628     (indent-spaces indent strm))
629   (awhen (obj-start-str-or-func view)
630          (if (stringp it)
631              (write-string it strm)
632              (funcall it obj strm))))
633
634 (defun fmt-obj-end (obj view strm indent)
635   (when (obj-end-indent view)
636     (indent-spaces indent strm))
637   (awhen (obj-end-str-or-func view)
638          (if (stringp it)
639              (write-string it strm)
640              (funcall it obj strm))))
641   
642 ;;; Object Data 
643
644 (defun make-link-start (view fieldfunc fieldvalue refvars)
645   (with-output-to-string (s)
646     (write-string (link-href-start view) s)
647     (write-char #\" s)
648     (write-string (make-url (link-page-name view)) s)
649     (write-string "?func=" s)
650     (write-string fieldfunc s)
651     (write-string (link-ampersand view) s)
652     (write-string "key=" s)
653     (write-string fieldvalue s)
654     (dolist (var refvars)
655       (write-string (link-ampersand view) s)
656       (write-string (car var) s)
657       (write-char #\= s)
658       (write-string (cdr var) s))
659     (write-char #\" s)))
660   
661 (defun make-link-end (obj view fieldname)
662   (declare (ignore obj fieldname))
663   (link-href-end view))
664
665 (defun fmt-obj-data (obj view strm indent refvars)
666   (when (obj-data-indent view)
667     (indent-spaces indent strm))
668   (if (link-slots view)
669       (fmt-obj-data-with-link obj view strm refvars)
670       (fmt-obj-data-plain obj view strm))
671   (awhen (obj-data-end-str view)
672          (write-string it strm)))
673
674 (defun fmt-obj-data-plain (obj view strm)
675   (awhen (obj-data-func view)
676          (funcall it obj strm nil)))
677
678 (defun fmt-obj-data-with-link (obj view strm refvars)
679   (let ((refvalues '()))
680     ;; make list of hyperlink link fields for printing to refstr template
681     (dolist (name (link-slots view))
682       (awhen (find name (hyperobject-class-hyperlinks obj) :key #'name)
683              (push (make-link-start view (lookup it) (slot-value obj name)
684                                     (append (link-parameters it) refvars))
685                    refvalues)
686              (push (make-link-end obj view name) refvalues)))
687     (funcall (obj-data-func view) obj strm (nreverse refvalues))))
688
689 (defun obj-data (obj view)
690   "Returns the objects data as a string. Used by common-graphics outline function"
691   (with-output-to-string (s) (fmt-obj-data-plain obj view s)))
692
693 ;;; Display method for objects
694
695
696 (defun load-all-subobjects (objs)
697   "Load all subobjects if they have not already been loaded."
698   (dolist (obj (mklist objs))
699     (dolist (subobj (hyperobject-class-subobjects obj))
700       (awhen (slot-value obj (name-slot subobj))
701              (load-all-subobjects it))))
702   objs)
703
704 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
705                          subobjects refvars)
706   "Display a single or list of hyperobject-class instances and their subobjects"
707   (declare (fixnum indent))
708   (let-when (objlist (mklist objs))
709     (let ((nobjs (length objlist))
710           (*print-pretty* nil)
711           (*print-circle* nil)
712           (*print-escape* nil)
713           (*print-readably* nil)
714           (*print-length* nil)
715           (*print-level* nil))
716       (fmt-list-start (car objlist) view strm indent nobjs)
717       (dolist (obj objlist)
718         (unless (and filter (not (funcall filter obj)))
719           (fmt-obj-start obj view strm indent)
720           (fmt-obj-data obj view strm (1+ indent) refvars)
721           (when (and subobjects (hyperobject-class-subobjects obj))
722             (dolist (subobj (hyperobject-class-subobjects obj))
723               (aif (slot-value obj (name-slot subobj))
724                    (view-hyperobject it (get-category-view (car (mklist it)) category)
725                                      category strm (1+ indent) filter
726                                      subobjects refvars))))
727           (fmt-obj-end obj view strm indent)))
728       (fmt-list-end (car objlist) view strm indent nobjs)))
729   objs)
730
731
732 (defun view (objs &key (stream *standard-output*) category view
733              filter subobjects refvars file-wrapper)
734   "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
735   (let-when (objlist (mklist objs))
736     (when category
737       (setq view (get-category-view (car objlist) category)))
738     (unless view
739       (setq view (default-view (class-of (car objlist)))))
740     (when file-wrapper
741       (fmt-file-start view stream))
742     (view-hyperobject objlist view category stream 0 filter subobjects refvars)
743     (when file-wrapper
744       (fmt-file-end view stream)))
745   objs)
746
747
748 ;;; Misc formatting
749
750 (defun fmt-comma-integer (i)
751   (format nil "~:d" i))