r3620: *** 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.16 2002/12/13 12:23:17 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 (defun initialize-view-by-category (obj-cl view)
166   "Initialize a view based upon a preset category"
167   (let ((fmtstr nil)
168         (first-field t)
169         (value-func '())
170         (links '())
171         (category (category view)))
172
173     (unless (in category :compact-text :compact-text-labels
174                 :html :html-labels :html-link-labels
175                 :xhtml :xhtml-labels :xhtml-link-labels
176                 :xml :xml-labels :xml-link-labels)
177       (error "Unknown view category ~A" category))
178     
179     (unless (slots view)
180       (setf (slots view) (default-print-slots obj-cl)))
181     (dolist (slot-name (slots view))
182       (let ((slot (find-slot-by-name obj-cl slot-name)))
183         (unless slot
184           (error "Slot ~A is not found in class ~S" slot-name obj-cl))
185         (let ((name (slot-definition-name slot))
186               (namestr-lower (string-downcase (symbol-name (slot-definition-name slot))))
187               (type (slot-value slot 'type))
188               (print-formatter (esd-print-formatter slot)))
189
190           (cond
191             (first-field
192              (setq fmtstr "")
193              (setq first-field nil))
194             (t
195              (string-append fmtstr " ")))
196
197           (let ((value-fmt
198                  (case type
199                    ((or :integer :fixnum)
200                     "~d")
201                    (:boolean
202                     "~a")
203                    (otherwise
204                     "~a"))))
205             (case category
206               (:compact-text
207                (string-append fmtstr value-fmt))
208               (:compact-text-labels
209                (string-append fmtstr namestr-lower " " value-fmt))
210               ((or :html :xhtml)
211                (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
212               (:xml
213                (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
214               (:html-labels
215                (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
216               (:xhtml-labels
217                (string-append fmtstr (concatenate 'string "<span class=\"label\"><![CDATA[" namestr-lower "]]></span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
218               (:xml-labels
219                (string-append fmtstr (concatenate 'string "<label><[!CDATA[" namestr-lower "]]></label> <" namestr-lower ">" value-fmt "</" namestr-lower ">")))
220               ((or :html-link :xhtml-link)
221                (push name links)
222                (if (esd-hyperlink slot)
223                    (string-append fmtstr "<~~a>" value-fmt "</~~a>")
224                    (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))))
225               (:xml-link
226                (push name links)
227                (if (esd-hyperlink slot)
228                    (string-append fmtstr "<~~a>" value-fmt "</~~a>")
229                    (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))))
230               (:html-link-labels
231                (push name links)
232                (if (esd-hyperlink slot)
233                    (string-append fmtstr "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
234                    (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
235               (:xhtml-link-labels
236                (push name links)
237                (if (esd-hyperlink slot)
238                    (string-append fmtstr "<span class=\"label\"><[!CDATA[" namestr-lower "]]></span> <~~a>" value-fmt "</~~a>")
239                    (string-append fmtstr (concatenate 'string "<span class=\"label\"><![CDATA[" namestr-lower "]]></span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
240               (:xml-link-labels
241                (push name links)
242                (if (esd-hyperlink slot)
243                    (string-append fmtstr "<label><[![CDATA[" namestr-lower "]]></label> <~~a>" value-fmt "</~~a>")
244                    (string-append fmtstr (concatenate 'string "<label><![CDATA[" namestr-lower "]]></label> <" namestr-lower ">" value-fmt "</" namestr-lower ">")))))
245             ) ;; let value-fmt
246           
247           (let ((func (if print-formatter
248                   `(,print-formatter (slot-value x (quote ,name)))
249                   `(slot-value x (quote ,name)))))
250             (when (and (in category :xml :xhtml :xml-link :xhtml-link
251                            :xhtml-link-labels :xml-link-labels :ie-xml-link
252                            :ie-xml-link-labels)
253                        (or print-formatter
254                            (string-equal (write-to-string type) "string")))
255               (setq func `(kmrcl:xml-cdata ,func)))
256             (push func value-func))
257           )))
258           
259     (when value-func
260       (setq value-func
261             (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
262
263     (setf (obj-data-fmtstr view) fmtstr)
264     (setf (obj-data-value-func view) value-func)
265     (setf (link-slots view) (nreverse links))
266     
267     (case category
268       ((or :compact-text :compact-text-labels)
269        (initialize-text-view view))
270       ((or :html :xhtml :html-labels :xhtml-labels)
271        (initialize-html-view view))
272       ((or :xml :xml-labels)
273        (initialize-xml-view view))
274       ((or :html-link :html-link-labels)
275        (initialize-html-view view)
276        (setf (link-href-start view) "a href=")
277        (setf (link-href-end view) "a")
278        (setf (link-ampersand view) "&"))
279       ((or :xhtml-link :xhtml-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) "&amp;"))
284       ((or :xml-link :xml-link-labels)
285        (initialize-xml-view view)
286        (setf (link-href-start view)
287              "xmllink xlink:type=\"simple\" xlink:href=")
288        (setf (link-href-end view) "xmllink")
289        (setf (link-ampersand view) "&amp;"))
290       ((or :ie-xml-link :ie-xml-link-labels)
291        (initialize-xml-view view)
292        (setf (link-href-start view) "html:a href=")
293        (setf (link-href-end view) "html:a")
294        (setf (link-ampersand view) "&amp;"))))
295   view)
296
297
298 ;;;; *************************************************************************
299 ;;;;  View Data Format Section
300 ;;;; *************************************************************************
301
302 (defun class-name-of (obj)
303   (string-downcase (class-name (class-of obj))))
304
305 (defun text-list-start-value-func (obj nitems)
306   (values (hyperobject-class-user-name obj) nitems))
307
308 (defun htmlformat-list-start-value-func (x nitems) 
309   (values (hyperobject-class-user-name x) nitems (class-name-of x)))
310
311 (defun initialize-text-view (view)
312   (setf (list-start-fmtstr view) "~a~P:~%")
313   (setf (list-start-value-func view) #'text-list-start-value-func)
314   (setf (list-start-indent view) t)
315   (setf (obj-data-indent view) t)
316   (setf (obj-data-end-fmtstr view) (format nil "~%"))
317   )
318
319 (defun initialize-html-view (view)
320   (initialize-text-view view)
321   (setf (file-start-str view) (format nil "<html><body>~%"))
322   (setf (file-end-str view) (format nil "</body><html>~%"))
323   (setf (list-start-indent view) t)
324   (setf (list-start-fmtstr view)
325         "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
326   (setf (list-start-value-func view)
327         #'htmlformat-list-start-value-func)
328   (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
329   (setf (list-end-indent view) t)
330   (setf (list-end-value-func view) nil)
331   (setf (obj-start-indent view) t)
332   (setf (obj-start-fmtstr view) "<li>")
333   (setf (obj-start-value-func view) nil)
334   (setf (obj-end-indent view)  t)
335   (setf (obj-end-fmtstr view)  (format nil "</li>~%"))
336   (setf (obj-end-value-func view) nil)
337   (setf (obj-data-indent view) t))
338
339 (defun initialize-xhtml-view (view)
340   (initialize-text-view view)
341   (setf (file-start-str view) (format nil "<html><body>~%"))
342   (setf (file-end-str view) (format nil "</body><html>~%"))
343   (setf (list-start-indent view) t)
344   (setf (list-start-fmtstr view)
345         "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
346   (setf (list-start-value-func view)
347                     #'htmlformat-list-start-value-func)
348   (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
349   (setf (list-end-indent view) t)
350   (setf (list-end-value-func view) nil)
351   (setf (obj-start-indent view) t)
352   (setf (obj-start-fmtstr view) "<li>")
353   (setf (obj-start-value-func view) nil)
354   (setf (obj-end-indent view)  t)
355   (setf (obj-end-fmtstr view) (format nil "</li>~%"))
356   (setf (obj-end-value-func view) nil)
357   (setf (obj-data-indent view) t))
358
359 (defun xmlformat-list-end-value-func (x)
360   (format nil "~alist" (class-name-of x)))
361
362 (defun xmlformat-list-start-value-func (x nitems) 
363   (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
364
365 (defun initialize-xml-view (view)
366   (initialize-text-view view)
367   (setf (file-start-str view) "") ; (std-xml-header)
368   (setf (list-start-indent view)  t)
369   (setf (list-start-fmtstr view) "<~a><title>~a~p:</title> ~%")
370   (setf (list-start-value-func view)
371         #'xmlformat-list-start-value-func)
372   (setf (list-end-indent view) t)
373   (setf (list-end-fmtstr view) "</~a>~%")
374   (setf (list-end-value-func view) #'xmlformat-list-end-value-func)
375   (setf (obj-start-fmtstr view) (format nil "<~(~a~)>" (object-class view)))
376   (setf (obj-start-indent view) t)
377   (setf (obj-end-fmtstr view) (format nil "</~(~a~)>~%" (object-class view)))
378   (setf (obj-end-indent view) nil)
379   (setf (obj-data-indent view) nil))
380
381
382 ;;; File Start and Ends
383
384 (defun fmt-file-start (view strm)
385   (awhen (file-start-str view)
386          (write-string it strm)))
387
388 (defun fmt-file-end (view strm)
389   (awhen (file-end-str view)
390          (write-string it strm)))
391
392 ;;; List Start and Ends
393
394 (defun fmt-list-start (obj view strm indent num-items)
395   (when (list-start-indent view)
396     (indent-spaces indent strm))
397   (let-when (fmtstr (list-start-fmtstr view))
398             (let-if (value-func (list-start-value-func view))
399                     (apply #'format strm fmtstr
400                            (multiple-value-list (funcall value-func
401                                                          obj num-items)))
402                     (write-string fmtstr strm))))
403
404 (defun fmt-list-end (obj view strm indent num-items)
405   (declare (ignore num-items))
406   (when (list-end-indent view)
407       (indent-spaces indent strm))
408   (let-when (fmtstr (list-end-fmtstr view))
409             (let-if (value-func (list-end-value-func view))
410                     (apply #'format strm fmtstr (multiple-value-list
411                                                  (funcall value-func obj)))
412                     (write-string fmtstr strm))))
413
414 ;;; Object Start and Ends
415
416 (defun fmt-obj-start (obj view strm indent)
417   (when (obj-start-indent view)
418     (indent-spaces indent strm))
419   (let-when (fmtstr (obj-start-fmtstr view))
420             (let-if (value-func (obj-start-value-func view))
421                     (apply #'format strm fmtstr (multiple-value-list
422                                                  (funcall value-func obj)))
423                     (write-string fmtstr strm))))
424
425 (defun fmt-obj-end (obj view strm indent)
426   (when (obj-end-indent view)
427     (indent-spaces indent strm))
428   (let-when (fmtstr (obj-end-fmtstr view))
429             (let-if (value-func (obj-end-value-func view))
430                     (apply #'format strm fmtstr (multiple-value-list
431                                                  (funcall value-func obj)))
432                     (write-string fmtstr strm))))
433   
434 ;;; Object Data 
435
436 (defun make-link-start (view fieldfunc fieldvalue refvars)
437   (format nil "~a\"~a?func=~a~akey=~a~a\"" 
438           (link-href-start view)
439           (make-url (link-page-name view))
440           fieldfunc 
441           (link-ampersand view) fieldvalue
442           (if refvars
443               (let ((varstr ""))
444                 (dolist (var refvars)
445                   (string-append
446                    varstr (link-ampersand view)
447                    (format nil "~a=~a" (car var) (cadr var))))
448                 varstr)
449               "")))
450
451 (defun make-link-end (obj view fieldname)
452   (declare (ignore obj fieldname))
453   (link-href-end view))
454
455 (defun fmt-obj-data (obj view strm indent refvars)
456   (when (obj-data-indent view)
457     (indent-spaces indent strm))
458   (if (link-slots view)
459       (fmt-obj-data-with-link obj view strm refvars)
460       (fmt-obj-data-plain obj view strm))
461   (awhen (obj-data-end-fmtstr view)
462          (write-string it strm)))
463
464 (defun fmt-obj-data-plain (obj view strm)
465   (awhen (obj-data-value-func view)
466          (apply #'format strm (obj-data-fmtstr view)
467                 (multiple-value-list (funcall it obj)))))
468
469 (defun fmt-obj-data-with-link (obj view strm refvars)
470   (let ((refvalues '()))
471     ;; make list of hyperlink link fields for printing to refstr template
472     (dolist (name (link-slots view))
473       (let-when (hyperlink
474                  (find name (hyperobject-class-hyperlinks obj) :key #'name))
475                 (push  (make-link-start view (lookup hyperlink) (slot-value obj name)
476                                         (append (link-parameters hyperlink) refvars))
477                        refvalues)
478                 (push (make-link-end obj view name) refvalues)))
479     (setq refvalues (nreverse refvalues))
480     (apply #'format strm (make-link-data-str obj view) refvalues)))
481
482 (defun obj-data (obj view)
483   "Returns the objects data as a string. Used by common-graphics outline function"
484   (awhen (obj-data-value-func view)
485          (apply #'format nil (funcall (obj-data-fmtstr view))
486                 (multiple-value-list (funcall it obj)))))
487
488 (defun make-link-data-str (obj view)
489   "Return fmt string for that contains ~a slots for hyperlink link start and end"
490   (awhen (obj-data-value-func view)
491          (apply #'format nil (obj-data-fmtstr view)
492                 (multiple-value-list (funcall it obj)))))
493
494 ;;; Display method for objects
495
496
497 (defun load-all-subobjects (objs)
498   "Load all subobjects if they have not already been loaded."
499   (dolist (obj (mklist objs))
500     (dolist (subobj (hyperobject-class-subobjects obj))
501       (awhen (slot-value obj (name-slot subobj))
502              (load-all-subobjects it))))
503   objs)
504
505 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
506                          subobjects refvars)
507   "Display a single or list of hyperobject-class instances and their subobjects"
508   (let-when (objlist (mklist objs))
509     (let ((nobjs (length objlist))
510           (*print-pretty* nil)
511           (*print-circle* nil)
512           (*print-escape* nil)
513           (*print-readably* nil)
514           (*print-length* nil)
515           (*print-level* nil))
516       (fmt-list-start (car objlist) view strm indent nobjs)
517       (dolist (obj objlist)
518         (unless (and filter (not (funcall filter obj)))
519           (fmt-obj-start obj view strm indent)
520           (fmt-obj-data obj view strm (1+ indent) refvars)
521           (when (and subobjects (hyperobject-class-subobjects obj))
522             (dolist (subobj (hyperobject-class-subobjects obj))
523               (aif (slot-value obj (name-slot subobj))
524                    (view-hyperobject it (get-category-view (car (mklist it)) category)
525                                      category strm (1+ indent) filter
526                                      subobjects refvars))))
527           (fmt-obj-end obj view strm indent)))
528       (fmt-list-end (car objlist) view strm indent nobjs)))
529   objs)
530
531
532 (defun view (objs &key (stream *standard-output*) category view
533              filter subobjects refvars file-wrapper)
534   "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
535   (let-when (objlist (mklist objs))
536     (when category
537       (setq view (get-category-view (car objlist) category)))
538     (unless view
539       (setq view (default-view (class-of (car objlist)))))
540     (when file-wrapper
541       (fmt-file-start view stream))
542     (view-hyperobject objlist view category stream 0 filter subobjects refvars)
543     (when file-wrapper
544       (fmt-file-end view stream)))
545   objs)
546
547
548 ;;; Misc formatting
549
550 (defun fmt-comma-integer (i)
551   (format nil "~:d" i))
552