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