1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: View methods for Hyperobjects
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Apr 2000
10 ;;;; $Id: views.lisp,v 1.24 2003/03/26 21:01:27 kevin Exp $
12 ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
14 ;;;; *************************************************************************
16 (in-package :hyperobject)
18 (eval-when (:compile-toplevel :execute)
19 (declaim (optimize (speed 2) (safety 2) (compilation-speed 0) (debug 2))))
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.")
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"
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"))
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)))
100 (default-view obj-class)
101 (aif (find category (views obj-class) :key #'category)
104 (make-instance 'object-view :object-class-name (class-name obj-class)
107 (push view (views obj-class))
110 ;;;; *************************************************************************
111 ;;;; Metaclass Intialization
112 ;;;; *************************************************************************
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))))
120 (dolist (view-def (views cl))
121 (push (make-object-view cl view-def) views))
122 (setf (views cl) (nreverse views)))
125 (setf (default-view cl) (make-object-view cl (default-view cl))))
127 (setf (default-view cl) (make-object-view cl (car (views cl)))))
129 (setf (default-view cl) (make-object-view cl :default)))))
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"
134 ((typep view-def 'object-view)
136 ((eq view-def :default)
137 (let* ((name (class-name cl))
138 (view (make-instance 'object-view :name "automatic"
139 :object-class-name name
141 :category :compact-text)))
144 (eval `(make-instance ,view-def)))
146 (error "Invalid parameter to make-object-view: ~S" view-def))))
148 (defmethod initialize-instance :after ((view object-view)
149 &rest initargs &key &allow-other-keys)
150 (initialize-view (find-class (object-class-name view)) view))
152 (defun initialize-view (obj-cl view)
153 "Calculate all view slots for a hyperobject class"
156 (initialize-view-by-category obj-cl view))
158 (initialize-view-by-source-code obj-cl view))
160 (setf (category view) :compact-text)
161 (initialize-view-by-category obj-cl view))))
163 (defun initialize-view-by-source-code (obj-cl view)
164 "Initialize a view based upon a source code"
165 (let ((source-code (source-code view)))
166 (error "not implemented")
171 (defun initialize-view-by-category (obj-cl view)
172 "Initialize a view based upon a preset category"
177 (category (category view)))
179 (unless (in category :compact-text :compact-text-labels
180 :html :html-labels :html-link-labels
181 :xhtml :xhtml-labels :xhtml-link-labels
182 :xml :xml-labels :xml-link :ie-xml-link
183 :xml-link-labels :ie-xml-link-labels)
184 (error "Unknown view category ~A" category))
187 (setf (slots view) (default-print-slots obj-cl)))
188 (dolist (slot-name (slots view))
189 (let ((slot (find-slot-by-name obj-cl slot-name)))
191 (error "Slot ~A is not found in class ~S" slot-name obj-cl))
192 (let* ((name (slot-definition-name slot))
193 (namestr-lower (string-downcase (symbol-name name)))
194 (xml-namestr (escape-xml-string namestr-lower))
195 (xml-tag (escape-xml-string namestr-lower))
196 (type (slot-value slot 'type))
197 (print-formatter (esd-print-formatter slot)))
202 (setq first-field nil))
204 (string-append fmtstr " ")))
208 ((or :integer :fixnum)
216 (string-append fmtstr value-fmt))
217 (:compact-text-labels
218 (string-append fmtstr namestr-lower " " value-fmt))
220 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
222 (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
224 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
226 (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
228 (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))
229 ((or :html-link :xhtml-link)
231 (if (esd-hyperlink slot)
232 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
233 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))))
234 ((or :xml-link :ie-xml-link)
236 (if (esd-hyperlink slot)
237 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
238 (string-append fmtstr (concatenate 'string "<" xml-tag ">" value-fmt "</" xml-tag ">"))))
241 (if (esd-hyperlink slot)
242 (string-append fmtstr "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
243 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
246 (if (esd-hyperlink slot)
247 (string-append fmtstr "<span class=\"label\">" xml-namestr "</span> <~~a>" value-fmt "</~~a>")
248 (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
249 ((or :xml-link-labels :ie-xml-link-labels)
251 (if (esd-hyperlink slot)
252 (string-append fmtstr "<label>" xml-namestr "</label> <~~a>" value-fmt "</~~a>")
253 (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))))
256 (let ((func (if print-formatter
257 `(,print-formatter (slot-value x (quote ,name)))
258 `(slot-value x (quote ,name)))))
259 (when (and (in category :xml :xhtml :xml-link :xhtml-link
260 :xml-labels :ie-xml-labels
261 :xhtml-link-labels :xml-link-labels :ie-xml-link
264 (string-equal (write-to-string type) "string")))
265 (setq func `(kmrcl:xml-cdata ,func)))
266 (push func value-func))
271 (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
273 (setf (obj-data-fmtstr view) fmtstr)
274 (setf (obj-data-value-func view) value-func)
275 (setf (link-slots view) (nreverse links))
278 ((or :compact-text :compact-text-labels)
279 (initialize-text-view view))
280 ((or :html :xhtml :html-labels :xhtml-labels)
281 (initialize-html-view view))
282 ((or :xml :xml-labels)
283 (initialize-xml-view view))
284 ((or :html-link :html-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) "&"))
289 ((or :xhtml-link :xhtml-link-labels)
290 (initialize-html-view view)
291 (setf (link-href-start view) "a href=")
292 (setf (link-href-end view) "a")
293 (setf (link-ampersand view) "&"))
294 ((or :xml-link :xml-link-labels)
295 (initialize-xml-view view)
296 (setf (link-href-start view)
297 "xmllink xlink:type=\"simple\" xlink:href=")
298 (setf (link-href-end view) "xmllink")
299 (setf (link-ampersand view) "&"))
300 ((or :ie-xml-link :ie-xml-link-labels)
301 (initialize-xml-view view)
302 (setf (link-href-start view) "html:a href=")
303 (setf (link-href-end view) "html:a")
304 (setf (link-ampersand view) "&"))))
308 ;;;; *************************************************************************
309 ;;;; View Data Format Section
310 ;;;; *************************************************************************
312 (defun class-name-of (obj)
313 (string-downcase (class-name (class-of obj))))
315 (defun text-list-start-value-func (obj nitems)
316 (values (hyperobject-class-user-name obj) nitems))
318 (defun htmlformat-list-start-value-func (x nitems)
319 (values (hyperobject-class-user-name x) nitems (class-name-of x)))
321 (defun initialize-text-view (view)
322 (setf (list-start-fmtstr view) "~a~P:~%")
323 (setf (list-start-value-func view) #'text-list-start-value-func)
324 (setf (list-start-indent view) t)
325 (setf (obj-data-indent view) t)
326 (setf (obj-data-end-fmtstr view) (format nil "~%"))
329 (defun initialize-html-view (view)
330 (initialize-text-view view)
331 (setf (file-start-str view) (format nil "<html><body>~%"))
332 (setf (file-end-str view) (format nil "</body><html>~%"))
333 (setf (list-start-indent view) t)
334 (setf (list-start-fmtstr view)
335 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
336 (setf (list-start-value-func view)
337 #'htmlformat-list-start-value-func)
338 (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
339 (setf (list-end-indent view) t)
340 (setf (list-end-value-func view) nil)
341 (setf (obj-start-indent view) t)
342 (setf (obj-start-fmtstr view) "<li>")
343 (setf (obj-start-value-func view) nil)
344 (setf (obj-end-indent view) t)
345 (setf (obj-end-fmtstr view) (format nil "</li>~%"))
346 (setf (obj-end-value-func view) nil)
347 (setf (obj-data-indent view) t))
349 (defun initialize-xhtml-view (view)
350 (initialize-text-view view)
351 (setf (file-start-str view) (format nil "<html><body>~%"))
352 (setf (file-end-str view) (format nil "</body><html>~%"))
353 (setf (list-start-indent view) t)
354 (setf (list-start-fmtstr view)
355 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
356 (setf (list-start-value-func view)
357 #'htmlformat-list-start-value-func)
358 (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
359 (setf (list-end-indent view) t)
360 (setf (list-end-value-func view) nil)
361 (setf (obj-start-indent view) t)
362 (setf (obj-start-fmtstr view) "<li>")
363 (setf (obj-start-value-func view) nil)
364 (setf (obj-end-indent view) t)
365 (setf (obj-end-fmtstr view) (format nil "</li>~%"))
366 (setf (obj-end-value-func view) nil)
367 (setf (obj-data-indent view) t))
369 (defun xmlformat-list-end-value-func (x)
370 (format nil "~alist" (class-name-of x)))
372 (defun xmlformat-list-start-value-func (x nitems)
373 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
375 (defun initialize-xml-view (view)
376 (initialize-text-view view)
377 (setf (file-start-str view) "") ; (std-xml-header)
378 (setf (list-start-indent view) t)
379 (setf (list-start-fmtstr view) "<~a><title>~a~p:</title> ~%")
380 (setf (list-start-value-func view)
381 #'xmlformat-list-start-value-func)
382 (setf (list-end-indent view) t)
383 (setf (list-end-fmtstr view) "</~a>~%")
384 (setf (list-end-value-func view) #'xmlformat-list-end-value-func)
385 (setf (obj-start-fmtstr view) (format nil "<~(~a~)>" (object-class-name view)))
386 (setf (obj-start-indent view) t)
387 (setf (obj-end-fmtstr view) (format nil "</~(~a~)>~%" (object-class-name view)))
388 (setf (obj-end-indent view) nil)
389 (setf (obj-data-indent view) nil))
392 ;;; File Start and Ends
394 (defun fmt-file-start (view strm)
395 (awhen (file-start-str view)
396 (write-string it strm)))
398 (defun fmt-file-end (view strm)
399 (awhen (file-end-str view)
400 (write-string it strm)))
402 ;;; List Start and Ends
404 (defun fmt-list-start (obj view strm indent num-items)
405 (when (list-start-indent view)
406 (indent-spaces indent strm))
407 (let-when (fmtstr (list-start-fmtstr view))
408 (let-if (value-func (list-start-value-func view))
409 (apply #'format strm fmtstr
410 (multiple-value-list (funcall value-func
412 (write-string fmtstr strm))))
414 (defun fmt-list-end (obj view strm indent num-items)
415 (declare (ignore num-items))
416 (when (list-end-indent view)
417 (indent-spaces indent strm))
418 (let-when (fmtstr (list-end-fmtstr view))
419 (let-if (value-func (list-end-value-func view))
420 (apply #'format strm fmtstr (multiple-value-list
421 (funcall value-func obj)))
422 (write-string fmtstr strm))))
424 ;;; Object Start and Ends
426 (defun fmt-obj-start (obj view strm indent)
427 (when (obj-start-indent view)
428 (indent-spaces indent strm))
429 (let-when (fmtstr (obj-start-fmtstr view))
430 (let-if (value-func (obj-start-value-func view))
431 (apply #'format strm fmtstr (multiple-value-list
432 (funcall value-func obj)))
433 (write-string fmtstr strm))))
435 (defun fmt-obj-end (obj view strm indent)
436 (when (obj-end-indent view)
437 (indent-spaces indent strm))
438 (let-when (fmtstr (obj-end-fmtstr view))
439 (let-if (value-func (obj-end-value-func view))
440 (apply #'format strm fmtstr (multiple-value-list
441 (funcall value-func obj)))
442 (write-string fmtstr strm))))
446 (defun make-link-start (view fieldfunc fieldvalue refvars)
447 (format nil "~a\"~a?func=~a~akey=~a~a\""
448 (link-href-start view)
449 (make-url (link-page-name view))
451 (link-ampersand view) fieldvalue
454 (dolist (var refvars)
456 varstr (link-ampersand view)
457 (format nil "~a=~a" (car var) (cadr var))))
461 (defun make-link-end (obj view fieldname)
462 (declare (ignore obj fieldname))
463 (link-href-end view))
465 (defun fmt-obj-data (obj view strm indent refvars)
466 (when (obj-data-indent view)
467 (indent-spaces indent strm))
468 (if (link-slots view)
469 (fmt-obj-data-with-link obj view strm refvars)
470 (fmt-obj-data-plain obj view strm))
471 (awhen (obj-data-end-fmtstr view)
472 (write-string it strm)))
474 (defun fmt-obj-data-plain (obj view strm)
475 (awhen (obj-data-value-func view)
476 (apply #'format strm (obj-data-fmtstr view)
477 (multiple-value-list (funcall it obj)))))
479 (defun fmt-obj-data-with-link (obj view strm refvars)
480 (let ((refvalues '()))
481 ;; make list of hyperlink link fields for printing to refstr template
482 (dolist (name (link-slots view))
484 (find name (hyperobject-class-hyperlinks obj) :key #'name))
485 (push (make-link-start view (lookup hyperlink) (slot-value obj name)
486 (append (link-parameters hyperlink) refvars))
488 (push (make-link-end obj view name) refvalues)))
489 (setq refvalues (nreverse refvalues))
490 (apply #'format strm (make-link-data-str obj view) refvalues)))
492 (defun obj-data (obj view)
493 "Returns the objects data as a string. Used by common-graphics outline function"
494 (awhen (obj-data-value-func view)
495 (apply #'format nil (funcall (obj-data-fmtstr view))
496 (multiple-value-list (funcall it obj)))))
498 (defun make-link-data-str (obj view)
499 "Return fmt string for that contains ~a slots for hyperlink link start and end"
500 (awhen (obj-data-value-func view)
501 (apply #'format nil (obj-data-fmtstr view)
502 (multiple-value-list (funcall it obj)))))
504 ;;; Display method for objects
507 (defun load-all-subobjects (objs)
508 "Load all subobjects if they have not already been loaded."
509 (dolist (obj (mklist objs))
510 (dolist (subobj (hyperobject-class-subobjects obj))
511 (awhen (slot-value obj (name-slot subobj))
512 (load-all-subobjects it))))
515 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
517 "Display a single or list of hyperobject-class instances and their subobjects"
518 (let-when (objlist (mklist objs))
519 (let ((nobjs (length objlist))
523 (*print-readably* nil)
526 (fmt-list-start (car objlist) view strm indent nobjs)
527 (dolist (obj objlist)
528 (unless (and filter (not (funcall filter obj)))
529 (fmt-obj-start obj view strm indent)
530 (fmt-obj-data obj view strm (1+ indent) refvars)
531 (when (and subobjects (hyperobject-class-subobjects obj))
532 (dolist (subobj (hyperobject-class-subobjects obj))
533 (aif (slot-value obj (name-slot subobj))
534 (view-hyperobject it (get-category-view (car (mklist it)) category)
535 category strm (1+ indent) filter
536 subobjects refvars))))
537 (fmt-obj-end obj view strm indent)))
538 (fmt-list-end (car objlist) view strm indent nobjs)))
542 (defun view (objs &key (stream *standard-output*) category view
543 filter subobjects refvars file-wrapper)
544 "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
545 (let-when (objlist (mklist objs))
547 (setq view (get-category-view (car objlist) category)))
549 (setq view (default-view (class-of (car objlist)))))
551 (fmt-file-start view stream))
552 (view-hyperobject objlist view category stream 0 filter subobjects refvars)
554 (fmt-file-end view stream)))
560 (defun fmt-comma-integer (i)
561 (format nil "~:d" i))