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