r3792: 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.21 2003/01/17 19:16:28 kevin Exp $
11 ;;;;
12 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15  
16 (in-package :hyperobject)
17
18 (eval-when (:compile-toplevel :execute)
19   (declaim (optimize (speed 2) (safety 2) (compilation-speed 0) (debug 2))))
20
21
22 (defclass object-view ()
23   ((object-class :initform nil :initarg :object-class :accessor object-class
24                  :documentation "Name of class for object to be viewed.")
25    (slots :initform nil :initarg :slots :accessor slots
26           :documentation "List of effective slots for object to be viewed.")
27    (name :initform nil :initarg :name :accessor name
28          :documentation "Name for this view.")
29    (category :initform nil :initarg :category :accessor category
30              :documentation "Category for view. Helpful when want to find a view corresponding to a particular category.")
31    (source-code :initform nil :initarg :source-code :accessor source-code 
32                 :documentation "Source code for generating view.")
33    (country-language :initform :en :initarg :country-language
34                      :documentation "Country's Language for this view.")
35    ;;
36    (file-start-str :type string :initform nil :initarg :file-start-str
37                    :accessor file-start-str)
38    (file-end-str :type string :initform nil :initarg :file-end-str
39                  :accessor file-end-str)
40    (list-start-fmtstr :type string :initform nil :initarg :list-start-fmtstr
41                       :accessor list-start-fmtstr)
42    (list-start-value-func :type function :initform nil
43                           :initarg :list-start-value-func
44                           :accessor list-start-value-func)
45    (list-start-indent :initform nil :initarg :list-start-indent
46                       :accessor list-start-indent)
47    (list-end-fmtstr :type string :initform nil :initarg :list-end-fmtstr
48                     :accessor list-end-fmtstr)
49    (list-end-value-func :type function :initform nil
50                         :initarg :list-end-value-func
51                         :accessor list-end-value-func)
52    (list-end-indent :initform nil :initarg :list-end-indent
53                     :accessor list-end-indent)
54    (obj-start-fmtstr :type string :initform nil :initarg :obj-start-fmtstr
55                      :accessor obj-start-fmtstr)
56    (obj-start-value-func :initform nil :initarg :obj-start-value-func
57                          :accessor obj-start-value-func)
58    (obj-start-indent :initform nil :initarg :obj-start-indent
59                      :accessor obj-start-indent)
60    (obj-end-fmtstr :type string :initform nil :initarg :obj-end-fmtstr
61                    :accessor obj-end-fmtstr)
62    (obj-end-value-func :type function :initform nil
63                        :initarg :obj-end-value-func
64                        :accessor obj-end-value-func)
65    (obj-end-indent :initform nil :initarg :obj-end-indent
66                    :accessor obj-end-indent)
67    (obj-data-indent :initform nil :initarg :obj-data-indent
68                     :accessor obj-data-indent)
69    (obj-data-fmtstr :type string :initform nil :initarg :obj-data-fmtstr
70                     :accessor obj-data-fmtstr)
71    (obj-data-end-fmtstr :type string :initform nil
72                         :initarg :obj-data-end-fmtstr
73                         :accessor obj-data-end-fmtstr)
74    (obj-data-value-func :type function :initform nil
75                         :initarg :obj-data-value-func
76                         :accessor obj-data-value-func)
77    (link-slots :type list :initform nil
78                :documentation "List of slot names that have hyperlinks"
79                :accessor link-slots)
80    (link-page-name :type string :initform nil :initarg :link-page-name
81                    :accessor link-page-name)
82    (link-href-start :type string :initform nil :initarg :link-href-start
83                     :accessor link-href-start)
84    (link-href-end :type string :initform nil :initarg :link-href-end
85                   :accessor link-href-end)
86    (link-ampersand :type string :initform nil :initarg :link-ampersand
87                    :accessor link-ampersand))
88   (:default-initargs :link-page-name "disp-func1")
89   (:documentation "View class for a hyperobject"))
90
91
92 (defun get-category-view (obj category &optional slots)
93   "Find or make a category view for an object"
94   (let ((obj-class (class-of obj)))
95     (if (null category)
96         (default-view obj-class)
97         (aif (find category (views obj-class) :key #'category)
98              it
99              (let ((view
100                     (make-instance 'object-view :object-class (class-name obj-class)
101                                    :category category
102                                    :slots slots)))
103                (push view (views obj-class))
104                view)))))
105                              
106 ;;;; *************************************************************************
107 ;;;;  Metaclass Intialization
108 ;;;; *************************************************************************
109
110 (defun finalize-views (cl)
111   "Finalize all views that are given on a objects initialization"
112   (unless (default-print-slots cl)
113     (setf (default-print-slots cl)
114           (mapcar #'slot-definition-name (class-slots cl))))
115   (let ((views '()))
116     (dolist (view-def (views cl))
117       (push (make-object-view cl view-def) views))
118     (setf (views cl) (nreverse views)))
119   (cond
120     ((default-view cl)
121      (setf (default-view cl) (make-object-view cl (default-view cl))))
122     ((car (views cl))
123      (setf (default-view cl) (make-object-view cl (car (views cl)))))
124     (t
125      (setf (default-view cl) (make-object-view cl :default)))))
126
127 (defun make-object-view (cl view-def)
128   "Make an object view from a definition. Do nothing if a class is passed so that reinitialization will be a no-op"
129   (cond
130     ((typep view-def 'object-view)
131      view-def)
132     ((eq view-def :default)
133      (let* ((name (class-name cl))
134             (view (make-instance 'object-view :name "automatic"
135                                  :object-class name
136                                  :category :compact-text)))
137        view))
138     ((consp view-def)
139      (eval `(make-instance ,view-def)))
140     (t
141      (error "Invalid parameter to make-object-view: ~S" view-def))))
142
143 (defmethod initialize-instance :after ((view object-view)
144                                        &rest initargs &key &allow-other-keys)
145   (initialize-view (find-class (object-class view)) view))
146   
147 (defun initialize-view (obj-cl view)
148   "Calculate all view slots for a hyperobject class"
149   (cond
150     ((category view)
151      (initialize-view-by-category obj-cl view))
152     ((source-code view)
153      (initialize-view-by-source-code obj-cl view))
154     (t
155      (setf (category view) :compact-text)
156      (initialize-view-by-category obj-cl view))))
157
158 (defun initialize-view-by-source-code (obj-cl view)
159   "Initialize a view based upon a source code"
160   (let ((source-code (source-code view)))
161     (error "not implemented")
162     )
163   )
164
165
166 (defun initialize-view-by-category (obj-cl view)
167   "Initialize a view based upon a preset category"
168   (let ((fmtstr nil)
169         (first-field t)
170         (value-func '())
171         (links '())
172         (category (category view)))
173
174     (unless (in category :compact-text :compact-text-labels
175                 :html :html-labels :html-link-labels
176                 :xhtml :xhtml-labels :xhtml-link-labels
177                 :xml :xml-labels :xml-link :ie-xml-link
178                 :xml-link-labels :ie-xml-link-labels)
179       (error "Unknown view category ~A" category))
180     
181     (unless (slots view)
182       (setf (slots view) (default-print-slots obj-cl)))
183     (dolist (slot-name (slots view))
184       (let ((slot (find-slot-by-name obj-cl slot-name)))
185         (unless slot
186           (error "Slot ~A is not found in class ~S" slot-name obj-cl))
187         (let* ((name (slot-definition-name slot))
188                (namestr-lower (string-downcase (symbol-name name)))
189                (xml-namestr (escape-xml-string namestr-lower))
190                (xml-tag (escape-xml-string namestr-lower))
191                (type (slot-value slot 'type))
192                (print-formatter (esd-print-formatter slot)))
193
194           (cond
195             (first-field
196              (setq fmtstr "")
197              (setq first-field nil))
198             (t
199              (string-append fmtstr " ")))
200
201           (let ((value-fmt
202                  (case type
203                    ((or :integer :fixnum)
204                     "~d")
205                    (:boolean
206                     "~a")
207                    (otherwise
208                     "~a"))))
209             (case category
210               (:compact-text
211                (string-append fmtstr value-fmt))
212               (:compact-text-labels
213                (string-append fmtstr namestr-lower " " value-fmt))
214               ((or :html :xhtml)
215                (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
216               (:xml
217                (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
218               (:html-labels
219                (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
220               (:xhtml-labels
221                (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
222               (:xml-labels
223                (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))
224               ((or :html-link :xhtml-link)
225                (push name links)
226                (if (esd-hyperlink slot)
227                    (string-append fmtstr "<~~a>" value-fmt "</~~a>")
228                    (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))))
229               ((or :xml-link :ie-xml-link)
230                (push name links)
231                (if (esd-hyperlink slot)
232                    (string-append fmtstr "<~~a>" value-fmt "</~~a>")
233                    (string-append fmtstr (concatenate 'string "<" xml-tag ">" value-fmt "</" xml-tag ">"))))
234               (:html-link-labels
235                (push name links)
236                (if (esd-hyperlink slot)
237                    (string-append fmtstr "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
238                    (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
239               (:xhtml-link-labels
240                (push name links)
241                (if (esd-hyperlink slot)
242                    (string-append fmtstr "<span class=\"label\">" xml-namestr "</span> <~~a>" value-fmt "</~~a>")
243                    (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
244               ((or :xml-link-labels :ie-xml-link-labels)
245                (push name links)
246                (if (esd-hyperlink slot)
247                    (string-append fmtstr "<label>" xml-namestr "</label> <~~a>" value-fmt "</~~a>")
248                    (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))))
249             ) ;; let value-fmt
250             
251           (let ((func (if print-formatter
252                   `(,print-formatter (slot-value x (quote ,name)))
253                   `(slot-value x (quote ,name)))))
254             (when (and (in category :xml :xhtml :xml-link :xhtml-link
255                            :xml-labels :ie-xml-labels
256                            :xhtml-link-labels :xml-link-labels :ie-xml-link
257                            :ie-xml-link-labels)
258                        (or print-formatter
259                            (string-equal (write-to-string type) "string")))
260               (setq func `(kmrcl:xml-cdata ,func)))
261             (push func value-func))
262           )))
263           
264     (when value-func
265       (setq value-func
266             (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
267
268     (setf (obj-data-fmtstr view) fmtstr)
269     (setf (obj-data-value-func view) value-func)
270     (setf (link-slots view) (nreverse links))
271     
272     (case category
273       ((or :compact-text :compact-text-labels)
274        (initialize-text-view view))
275       ((or :html :xhtml :html-labels :xhtml-labels)
276        (initialize-html-view view))
277       ((or :xml :xml-labels)
278        (initialize-xml-view view))
279       ((or :html-link :html-link-labels)
280        (initialize-html-view view)
281        (setf (link-href-start view) "a href=")
282        (setf (link-href-end view) "a")
283        (setf (link-ampersand view) "&"))
284       ((or :xhtml-link :xhtml-link-labels)
285        (initialize-html-view view)
286        (setf (link-href-start view) "a href=")
287        (setf (link-href-end view) "a")
288        (setf (link-ampersand view) "&amp;"))
289       ((or :xml-link :xml-link-labels)
290        (initialize-xml-view view)
291        (setf (link-href-start view)
292              "xmllink xlink:type=\"simple\" xlink:href=")
293        (setf (link-href-end view) "xmllink")
294        (setf (link-ampersand view) "&amp;"))
295       ((or :ie-xml-link :ie-xml-link-labels)
296        (initialize-xml-view view)
297        (setf (link-href-start view) "html:a href=")
298        (setf (link-href-end view) "html:a")
299        (setf (link-ampersand view) "&amp;"))))
300   view)
301
302
303 ;;;; *************************************************************************
304 ;;;;  View Data Format Section
305 ;;;; *************************************************************************
306
307 (defun class-name-of (obj)
308   (string-downcase (class-name (class-of obj))))
309
310 (defun text-list-start-value-func (obj nitems)
311   (values (hyperobject-class-user-name obj) nitems))
312
313 (defun htmlformat-list-start-value-func (x nitems) 
314   (values (hyperobject-class-user-name x) nitems (class-name-of x)))
315
316 (defun initialize-text-view (view)
317   (setf (list-start-fmtstr view) "~a~P:~%")
318   (setf (list-start-value-func view) #'text-list-start-value-func)
319   (setf (list-start-indent view) t)
320   (setf (obj-data-indent view) t)
321   (setf (obj-data-end-fmtstr view) (format nil "~%"))
322   )
323
324 (defun initialize-html-view (view)
325   (initialize-text-view view)
326   (setf (file-start-str view) (format nil "<html><body>~%"))
327   (setf (file-end-str view) (format nil "</body><html>~%"))
328   (setf (list-start-indent view) t)
329   (setf (list-start-fmtstr view)
330         "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
331   (setf (list-start-value-func view)
332         #'htmlformat-list-start-value-func)
333   (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
334   (setf (list-end-indent view) t)
335   (setf (list-end-value-func view) nil)
336   (setf (obj-start-indent view) t)
337   (setf (obj-start-fmtstr view) "<li>")
338   (setf (obj-start-value-func view) nil)
339   (setf (obj-end-indent view)  t)
340   (setf (obj-end-fmtstr view)  (format nil "</li>~%"))
341   (setf (obj-end-value-func view) nil)
342   (setf (obj-data-indent view) t))
343
344 (defun initialize-xhtml-view (view)
345   (initialize-text-view view)
346   (setf (file-start-str view) (format nil "<html><body>~%"))
347   (setf (file-end-str view) (format nil "</body><html>~%"))
348   (setf (list-start-indent view) t)
349   (setf (list-start-fmtstr view)
350         "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
351   (setf (list-start-value-func view)
352                     #'htmlformat-list-start-value-func)
353   (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
354   (setf (list-end-indent view) t)
355   (setf (list-end-value-func view) nil)
356   (setf (obj-start-indent view) t)
357   (setf (obj-start-fmtstr view) "<li>")
358   (setf (obj-start-value-func view) nil)
359   (setf (obj-end-indent view)  t)
360   (setf (obj-end-fmtstr view) (format nil "</li>~%"))
361   (setf (obj-end-value-func view) nil)
362   (setf (obj-data-indent view) t))
363
364 (defun xmlformat-list-end-value-func (x)
365   (format nil "~alist" (class-name-of x)))
366
367 (defun xmlformat-list-start-value-func (x nitems) 
368   (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
369
370 (defun initialize-xml-view (view)
371   (initialize-text-view view)
372   (setf (file-start-str view) "") ; (std-xml-header)
373   (setf (list-start-indent view)  t)
374   (setf (list-start-fmtstr view) "<~a><title>~a~p:</title> ~%")
375   (setf (list-start-value-func view)
376         #'xmlformat-list-start-value-func)
377   (setf (list-end-indent view) t)
378   (setf (list-end-fmtstr view) "</~a>~%")
379   (setf (list-end-value-func view) #'xmlformat-list-end-value-func)
380   (setf (obj-start-fmtstr view) (format nil "<~(~a~)>" (object-class view)))
381   (setf (obj-start-indent view) t)
382   (setf (obj-end-fmtstr view) (format nil "</~(~a~)>~%" (object-class view)))
383   (setf (obj-end-indent view) nil)
384   (setf (obj-data-indent view) nil))
385
386
387 ;;; File Start and Ends
388
389 (defun fmt-file-start (view strm)
390   (awhen (file-start-str view)
391          (write-string it strm)))
392
393 (defun fmt-file-end (view strm)
394   (awhen (file-end-str view)
395          (write-string it strm)))
396
397 ;;; List Start and Ends
398
399 (defun fmt-list-start (obj view strm indent num-items)
400   (when (list-start-indent view)
401     (indent-spaces indent strm))
402   (let-when (fmtstr (list-start-fmtstr view))
403             (let-if (value-func (list-start-value-func view))
404                     (apply #'format strm fmtstr
405                            (multiple-value-list (funcall value-func
406                                                          obj num-items)))
407                     (write-string fmtstr strm))))
408
409 (defun fmt-list-end (obj view strm indent num-items)
410   (declare (ignore num-items))
411   (when (list-end-indent view)
412       (indent-spaces indent strm))
413   (let-when (fmtstr (list-end-fmtstr view))
414             (let-if (value-func (list-end-value-func view))
415                     (apply #'format strm fmtstr (multiple-value-list
416                                                  (funcall value-func obj)))
417                     (write-string fmtstr strm))))
418
419 ;;; Object Start and Ends
420
421 (defun fmt-obj-start (obj view strm indent)
422   (when (obj-start-indent view)
423     (indent-spaces indent strm))
424   (let-when (fmtstr (obj-start-fmtstr view))
425             (let-if (value-func (obj-start-value-func view))
426                     (apply #'format strm fmtstr (multiple-value-list
427                                                  (funcall value-func obj)))
428                     (write-string fmtstr strm))))
429
430 (defun fmt-obj-end (obj view strm indent)
431   (when (obj-end-indent view)
432     (indent-spaces indent strm))
433   (let-when (fmtstr (obj-end-fmtstr view))
434             (let-if (value-func (obj-end-value-func view))
435                     (apply #'format strm fmtstr (multiple-value-list
436                                                  (funcall value-func obj)))
437                     (write-string fmtstr strm))))
438   
439 ;;; Object Data 
440
441 (defun make-link-start (view fieldfunc fieldvalue refvars)
442   (format nil "~a\"~a?func=~a~akey=~a~a\"" 
443           (link-href-start view)
444           (make-url (link-page-name view))
445           fieldfunc 
446           (link-ampersand view) fieldvalue
447           (if refvars
448               (let ((varstr ""))
449                 (dolist (var refvars)
450                   (string-append
451                    varstr (link-ampersand view)
452                    (format nil "~a=~a" (car var) (cadr var))))
453                 varstr)
454               "")))
455
456 (defun make-link-end (obj view fieldname)
457   (declare (ignore obj fieldname))
458   (link-href-end view))
459
460 (defun fmt-obj-data (obj view strm indent refvars)
461   (when (obj-data-indent view)
462     (indent-spaces indent strm))
463   (if (link-slots view)
464       (fmt-obj-data-with-link obj view strm refvars)
465       (fmt-obj-data-plain obj view strm))
466   (awhen (obj-data-end-fmtstr view)
467          (write-string it strm)))
468
469 (defun fmt-obj-data-plain (obj view strm)
470   (awhen (obj-data-value-func view)
471          (apply #'format strm (obj-data-fmtstr view)
472                 (multiple-value-list (funcall it obj)))))
473
474 (defun fmt-obj-data-with-link (obj view strm refvars)
475   (let ((refvalues '()))
476     ;; make list of hyperlink link fields for printing to refstr template
477     (dolist (name (link-slots view))
478       (let-when (hyperlink
479                  (find name (hyperobject-class-hyperlinks obj) :key #'name))
480                 (push  (make-link-start view (lookup hyperlink) (slot-value obj name)
481                                         (append (link-parameters hyperlink) refvars))
482                        refvalues)
483                 (push (make-link-end obj view name) refvalues)))
484     (setq refvalues (nreverse refvalues))
485     (apply #'format strm (make-link-data-str obj view) refvalues)))
486
487 (defun obj-data (obj view)
488   "Returns the objects data as a string. Used by common-graphics outline function"
489   (awhen (obj-data-value-func view)
490          (apply #'format nil (funcall (obj-data-fmtstr view))
491                 (multiple-value-list (funcall it obj)))))
492
493 (defun make-link-data-str (obj view)
494   "Return fmt string for that contains ~a slots for hyperlink link start and end"
495   (awhen (obj-data-value-func view)
496          (apply #'format nil (obj-data-fmtstr view)
497                 (multiple-value-list (funcall it obj)))))
498
499 ;;; Display method for objects
500
501
502 (defun load-all-subobjects (objs)
503   "Load all subobjects if they have not already been loaded."
504   (dolist (obj (mklist objs))
505     (dolist (subobj (hyperobject-class-subobjects obj))
506       (awhen (slot-value obj (name-slot subobj))
507              (load-all-subobjects it))))
508   objs)
509
510 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
511                          subobjects refvars)
512   "Display a single or list of hyperobject-class instances and their subobjects"
513   (let-when (objlist (mklist objs))
514     (let ((nobjs (length objlist))
515           (*print-pretty* nil)
516           (*print-circle* nil)
517           (*print-escape* nil)
518           (*print-readably* nil)
519           (*print-length* nil)
520           (*print-level* nil))
521       (fmt-list-start (car objlist) view strm indent nobjs)
522       (dolist (obj objlist)
523         (unless (and filter (not (funcall filter obj)))
524           (fmt-obj-start obj view strm indent)
525           (fmt-obj-data obj view strm (1+ indent) refvars)
526           (when (and subobjects (hyperobject-class-subobjects obj))
527             (dolist (subobj (hyperobject-class-subobjects obj))
528               (aif (slot-value obj (name-slot subobj))
529                    (view-hyperobject it (get-category-view (car (mklist it)) category)
530                                      category strm (1+ indent) filter
531                                      subobjects refvars))))
532           (fmt-obj-end obj view strm indent)))
533       (fmt-list-end (car objlist) view strm indent nobjs)))
534   objs)
535
536
537 (defun view (objs &key (stream *standard-output*) category view
538              filter subobjects refvars file-wrapper)
539   "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
540   (let-when (objlist (mklist objs))
541     (when category
542       (setq view (get-category-view (car objlist) category)))
543     (unless view
544       (setq view (default-view (class-of (car objlist)))))
545     (when file-wrapper
546       (fmt-file-start view stream))
547     (view-hyperobject objlist view category stream 0 filter subobjects refvars)
548     (when file-wrapper
549       (fmt-file-end view stream)))
550   objs)
551
552
553 ;;; Misc formatting
554
555 (defun fmt-comma-integer (i)
556   (format nil "~:d" i))