r3662: *** empty log message ***
[hyperobject.git] / views.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          views.lisp
6 ;;;; Purpose:       View methods for Hyperobjects
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Apr 2000
9 ;;;;
10 ;;;; $Id: views.lisp,v 1.18 2002/12/26 11:57:15 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-namestring (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                            :xhtml-link-labels :xml-link-labels :ie-xml-link
256                            :ie-xml-link-labels)
257                        (or print-formatter
258                            (string-equal (write-to-string type) "string")))
259               (setq func `(kmrcl:xml-cdata ,func)))
260             (push func value-func))
261           )))
262           
263     (when value-func
264       (setq value-func
265             (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
266
267     (setf (obj-data-fmtstr view) fmtstr)
268     (setf (obj-data-value-func view) value-func)
269     (setf (link-slots view) (nreverse links))
270     
271     (case category
272       ((or :compact-text :compact-text-labels)
273        (initialize-text-view view))
274       ((or :html :xhtml :html-labels :xhtml-labels)
275        (initialize-html-view view))
276       ((or :xml :xml-labels)
277        (initialize-xml-view view))
278       ((or :html-link :html-link-labels)
279        (initialize-html-view view)
280        (setf (link-href-start view) "a href=")
281        (setf (link-href-end view) "a")
282        (setf (link-ampersand view) "&"))
283       ((or :xhtml-link :xhtml-link-labels)
284        (initialize-html-view view)
285        (setf (link-href-start view) "a href=")
286        (setf (link-href-end view) "a")
287        (setf (link-ampersand view) "&amp;"))
288       ((or :xml-link :xml-link-labels)
289        (initialize-xml-view view)
290        (setf (link-href-start view)
291              "xmllink xlink:type=\"simple\" xlink:href=")
292        (setf (link-href-end view) "xmllink")
293        (setf (link-ampersand view) "&amp;"))
294       ((or :ie-xml-link :ie-xml-link-labels)
295        (initialize-xml-view view)
296        (setf (link-href-start view) "html:a href=")
297        (setf (link-href-end view) "html:a")
298        (setf (link-ampersand view) "&amp;"))))
299   view)
300
301
302 ;;;; *************************************************************************
303 ;;;;  View Data Format Section
304 ;;;; *************************************************************************
305
306 (defun class-name-of (obj)
307   (string-downcase (class-name (class-of obj))))
308
309 (defun text-list-start-value-func (obj nitems)
310   (values (hyperobject-class-user-name obj) nitems))
311
312 (defun htmlformat-list-start-value-func (x nitems) 
313   (values (hyperobject-class-user-name x) nitems (class-name-of x)))
314
315 (defun initialize-text-view (view)
316   (setf (list-start-fmtstr view) "~a~P:~%")
317   (setf (list-start-value-func view) #'text-list-start-value-func)
318   (setf (list-start-indent view) t)
319   (setf (obj-data-indent view) t)
320   (setf (obj-data-end-fmtstr view) (format nil "~%"))
321   )
322
323 (defun initialize-html-view (view)
324   (initialize-text-view view)
325   (setf (file-start-str view) (format nil "<html><body>~%"))
326   (setf (file-end-str view) (format nil "</body><html>~%"))
327   (setf (list-start-indent view) t)
328   (setf (list-start-fmtstr view)
329         "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
330   (setf (list-start-value-func view)
331         #'htmlformat-list-start-value-func)
332   (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
333   (setf (list-end-indent view) t)
334   (setf (list-end-value-func view) nil)
335   (setf (obj-start-indent view) t)
336   (setf (obj-start-fmtstr view) "<li>")
337   (setf (obj-start-value-func view) nil)
338   (setf (obj-end-indent view)  t)
339   (setf (obj-end-fmtstr view)  (format nil "</li>~%"))
340   (setf (obj-end-value-func view) nil)
341   (setf (obj-data-indent view) t))
342
343 (defun initialize-xhtml-view (view)
344   (initialize-text-view view)
345   (setf (file-start-str view) (format nil "<html><body>~%"))
346   (setf (file-end-str view) (format nil "</body><html>~%"))
347   (setf (list-start-indent view) t)
348   (setf (list-start-fmtstr view)
349         "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
350   (setf (list-start-value-func view)
351                     #'htmlformat-list-start-value-func)
352   (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
353   (setf (list-end-indent view) t)
354   (setf (list-end-value-func view) nil)
355   (setf (obj-start-indent view) t)
356   (setf (obj-start-fmtstr view) "<li>")
357   (setf (obj-start-value-func view) nil)
358   (setf (obj-end-indent view)  t)
359   (setf (obj-end-fmtstr view) (format nil "</li>~%"))
360   (setf (obj-end-value-func view) nil)
361   (setf (obj-data-indent view) t))
362
363 (defun xmlformat-list-end-value-func (x)
364   (format nil "~alist" (class-name-of x)))
365
366 (defun xmlformat-list-start-value-func (x nitems) 
367   (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
368
369 (defun initialize-xml-view (view)
370   (initialize-text-view view)
371   (setf (file-start-str view) "") ; (std-xml-header)
372   (setf (list-start-indent view)  t)
373   (setf (list-start-fmtstr view) "<~a><title>~a~p:</title> ~%")
374   (setf (list-start-value-func view)
375         #'xmlformat-list-start-value-func)
376   (setf (list-end-indent view) t)
377   (setf (list-end-fmtstr view) "</~a>~%")
378   (setf (list-end-value-func view) #'xmlformat-list-end-value-func)
379   (setf (obj-start-fmtstr view) (format nil "<~(~a~)>" (object-class view)))
380   (setf (obj-start-indent view) t)
381   (setf (obj-end-fmtstr view) (format nil "</~(~a~)>~%" (object-class view)))
382   (setf (obj-end-indent view) nil)
383   (setf (obj-data-indent view) nil))
384
385
386 ;;; File Start and Ends
387
388 (defun fmt-file-start (view strm)
389   (awhen (file-start-str view)
390          (write-string it strm)))
391
392 (defun fmt-file-end (view strm)
393   (awhen (file-end-str view)
394          (write-string it strm)))
395
396 ;;; List Start and Ends
397
398 (defun fmt-list-start (obj view strm indent num-items)
399   (when (list-start-indent view)
400     (indent-spaces indent strm))
401   (let-when (fmtstr (list-start-fmtstr view))
402             (let-if (value-func (list-start-value-func view))
403                     (apply #'format strm fmtstr
404                            (multiple-value-list (funcall value-func
405                                                          obj num-items)))
406                     (write-string fmtstr strm))))
407
408 (defun fmt-list-end (obj view strm indent num-items)
409   (declare (ignore num-items))
410   (when (list-end-indent view)
411       (indent-spaces indent strm))
412   (let-when (fmtstr (list-end-fmtstr view))
413             (let-if (value-func (list-end-value-func view))
414                     (apply #'format strm fmtstr (multiple-value-list
415                                                  (funcall value-func obj)))
416                     (write-string fmtstr strm))))
417
418 ;;; Object Start and Ends
419
420 (defun fmt-obj-start (obj view strm indent)
421   (when (obj-start-indent view)
422     (indent-spaces indent strm))
423   (let-when (fmtstr (obj-start-fmtstr view))
424             (let-if (value-func (obj-start-value-func view))
425                     (apply #'format strm fmtstr (multiple-value-list
426                                                  (funcall value-func obj)))
427                     (write-string fmtstr strm))))
428
429 (defun fmt-obj-end (obj view strm indent)
430   (when (obj-end-indent view)
431     (indent-spaces indent strm))
432   (let-when (fmtstr (obj-end-fmtstr view))
433             (let-if (value-func (obj-end-value-func view))
434                     (apply #'format strm fmtstr (multiple-value-list
435                                                  (funcall value-func obj)))
436                     (write-string fmtstr strm))))
437   
438 ;;; Object Data 
439
440 (defun make-link-start (view fieldfunc fieldvalue refvars)
441   (format nil "~a\"~a?func=~a~akey=~a~a\"" 
442           (link-href-start view)
443           (make-url (link-page-name view))
444           fieldfunc 
445           (link-ampersand view) fieldvalue
446           (if refvars
447               (let ((varstr ""))
448                 (dolist (var refvars)
449                   (string-append
450                    varstr (link-ampersand view)
451                    (format nil "~a=~a" (car var) (cadr var))))
452                 varstr)
453               "")))
454
455 (defun make-link-end (obj view fieldname)
456   (declare (ignore obj fieldname))
457   (link-href-end view))
458
459 (defun fmt-obj-data (obj view strm indent refvars)
460   (when (obj-data-indent view)
461     (indent-spaces indent strm))
462   (if (link-slots view)
463       (fmt-obj-data-with-link obj view strm refvars)
464       (fmt-obj-data-plain obj view strm))
465   (awhen (obj-data-end-fmtstr view)
466          (write-string it strm)))
467
468 (defun fmt-obj-data-plain (obj view strm)
469   (awhen (obj-data-value-func view)
470          (apply #'format strm (obj-data-fmtstr view)
471                 (multiple-value-list (funcall it obj)))))
472
473 (defun fmt-obj-data-with-link (obj view strm refvars)
474   (let ((refvalues '()))
475     ;; make list of hyperlink link fields for printing to refstr template
476     (dolist (name (link-slots view))
477       (let-when (hyperlink
478                  (find name (hyperobject-class-hyperlinks obj) :key #'name))
479                 (push  (make-link-start view (lookup hyperlink) (slot-value obj name)
480                                         (append (link-parameters hyperlink) refvars))
481                        refvalues)
482                 (push (make-link-end obj view name) refvalues)))
483     (setq refvalues (nreverse refvalues))
484     (apply #'format strm (make-link-data-str obj view) refvalues)))
485
486 (defun obj-data (obj view)
487   "Returns the objects data as a string. Used by common-graphics outline function"
488   (awhen (obj-data-value-func view)
489          (apply #'format nil (funcall (obj-data-fmtstr view))
490                 (multiple-value-list (funcall it obj)))))
491
492 (defun make-link-data-str (obj view)
493   "Return fmt string for that contains ~a slots for hyperlink link start and end"
494   (awhen (obj-data-value-func view)
495          (apply #'format nil (obj-data-fmtstr view)
496                 (multiple-value-list (funcall it obj)))))
497
498 ;;; Display method for objects
499
500
501 (defun load-all-subobjects (objs)
502   "Load all subobjects if they have not already been loaded."
503   (dolist (obj (mklist objs))
504     (dolist (subobj (hyperobject-class-subobjects obj))
505       (awhen (slot-value obj (name-slot subobj))
506              (load-all-subobjects it))))
507   objs)
508
509 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
510                          subobjects refvars)
511   "Display a single or list of hyperobject-class instances and their subobjects"
512   (let-when (objlist (mklist objs))
513     (let ((nobjs (length objlist))
514           (*print-pretty* nil)
515           (*print-circle* nil)
516           (*print-escape* nil)
517           (*print-readably* nil)
518           (*print-length* nil)
519           (*print-level* nil))
520       (fmt-list-start (car objlist) view strm indent nobjs)
521       (dolist (obj objlist)
522         (unless (and filter (not (funcall filter obj)))
523           (fmt-obj-start obj view strm indent)
524           (fmt-obj-data obj view strm (1+ indent) refvars)
525           (when (and subobjects (hyperobject-class-subobjects obj))
526             (dolist (subobj (hyperobject-class-subobjects obj))
527               (aif (slot-value obj (name-slot subobj))
528                    (view-hyperobject it (get-category-view (car (mklist it)) category)
529                                      category strm (1+ indent) filter
530                                      subobjects refvars))))
531           (fmt-obj-end obj view strm indent)))
532       (fmt-list-end (car objlist) view strm indent nobjs)))
533   objs)
534
535
536 (defun view (objs &key (stream *standard-output*) category view
537              filter subobjects refvars file-wrapper)
538   "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
539   (let-when (objlist (mklist objs))
540     (when category
541       (setq view (get-category-view (car objlist) category)))
542     (unless view
543       (setq view (default-view (class-of (car objlist)))))
544     (when file-wrapper
545       (fmt-file-start view stream))
546     (view-hyperobject objlist view category stream 0 filter subobjects refvars)
547     (when file-wrapper
548       (fmt-file-end view stream)))
549   objs)
550
551
552 ;;; Misc formatting
553
554 (defun fmt-comma-integer (i)
555   (format nil "~:d" i))
556