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.15 2002/12/13 08:41:25 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 :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 :reader 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
32 :documentation "Source code for generating view.")
33 (country-language :initform :en :initarg :country-language
34 :documentation "Country's Language for this view.")
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"
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"))
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)))
96 (default-view obj-class)
97 (aif (find category (views obj-class) :key #'category)
100 (make-instance 'object-view :object-class (class-name obj-class)
103 (push view (views obj-class))
106 ;;;; *************************************************************************
107 ;;;; Metaclass Intialization
108 ;;;; *************************************************************************
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))))
116 (dolist (view-def (views cl))
117 (push (make-object-view cl view-def) views))
118 (setf (views cl) (nreverse views)))
121 (setf (default-view cl) (make-object-view cl (default-view cl))))
123 (setf (default-view cl) (make-object-view cl (car (views cl)))))
125 (setf (default-view cl) (make-object-view cl :default)))))
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"
130 ((typep view-def 'object-view)
132 ((eq view-def :default)
133 (let* ((name (class-name cl))
134 (view (make-instance 'object-view :name "automatic"
136 :category :compact-text)))
139 (eval `(make-instance ,view-def)))
141 (error "Invalid parameter to make-object-view: ~S" view-def))))
143 (defmethod initialize-instance :after ((view object-view)
144 &rest initargs &key &allow-other-keys)
145 (initialize-view (find-class (object-class view)) view))
147 (defun initialize-view (obj-cl view)
148 "Calculate all view slots for a hyperobject class"
153 (category (category view)))
155 (setf (slots view) (default-print-slots obj-cl)))
156 (dolist (slot-name (slots view))
157 (let ((slot (find-slot-by-name obj-cl slot-name)))
159 (error "Slot ~A is not found in class ~S" slot-name obj-cl))
160 (let ((name (slot-definition-name slot))
161 (namestr-lower (string-downcase (symbol-name (slot-definition-name slot))))
162 (type (slot-value slot 'type))
163 (print-formatter (esd-print-formatter slot)))
168 (setq first-field nil))
170 (string-append fmtstr " ")))
174 ((or :integer :fixnum)
182 (string-append fmtstr value-fmt))
183 (:compact-text-labels
184 (string-append fmtstr namestr-lower " " value-fmt))
186 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
188 (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
190 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
192 (string-append fmtstr (concatenate 'string "<span class=\"label\"><![CDATA[" namestr-lower "]]></span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
194 (string-append fmtstr (concatenate 'string "<label><[!CDATA[" namestr-lower "]]></label> <" namestr-lower ">" value-fmt "</" namestr-lower ">")))
195 ((or :html-link :xhtml-link)
197 (if (esd-hyperlink slot)
198 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
199 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))))
202 (if (esd-hyperlink slot)
203 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
204 (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))))
207 (if (esd-hyperlink slot)
208 (string-append fmtstr "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
209 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
212 (if (esd-hyperlink slot)
213 (string-append fmtstr "<span class=\"label\"><[!CDATA[" namestr-lower "]]></span> <~~a>" value-fmt "</~~a>")
214 (string-append fmtstr (concatenate 'string "<span class=\"label\"><![CDATA[" namestr-lower "]]></span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
217 (if (esd-hyperlink slot)
218 (string-append fmtstr "<label><[![CDATA[" namestr-lower "]]></label> <~~a>" value-fmt "</~~a>")
219 (string-append fmtstr (concatenate 'string "<label><![CDATA[" namestr-lower "]]></label> <" namestr-lower ">" value-fmt "</" namestr-lower ">")))))
222 (let ((func (if print-formatter
223 `(,print-formatter (slot-value x (quote ,name)))
224 `(slot-value x (quote ,name)))))
225 (when (and (in category :xml :xhtml :xml-link :xhtml-link
226 :xhtml-link-labels :xml-link-labels :ie-xml-link
229 (string-equal (write-to-string type) "string")))
230 (setq func `(kmrcl:xml-cdata ,func)))
231 (push func value-func))
236 (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
238 (setf (obj-data-fmtstr view) fmtstr)
239 (setf (obj-data-value-func view) value-func)
240 (setf (link-slots view) (nreverse links))
243 ((or :compact-text :compact-text-labels)
244 (initialize-text-view view))
245 ((or :html :xhtml :html-labels :xhtml-labels)
246 (initialize-html-view view))
247 ((or :xml :xml-labels)
248 (initialize-xml-view view))
249 ((or :html-link :html-link-labels)
250 (initialize-html-view view)
251 (setf (link-href-start view) "a href=")
252 (setf (link-href-end view) "a")
253 (setf (link-ampersand view) "&"))
254 ((or :xhtml-link :xhtml-link-labels)
255 (initialize-html-view view)
256 (setf (link-href-start view) "a href=")
257 (setf (link-href-end view) "a")
258 (setf (link-ampersand view) "&"))
259 ((or :xml-link :xml-link-labels)
260 (initialize-xml-view view)
261 (setf (link-href-start view)
262 "xmllink xlink:type=\"simple\" xlink:href=")
263 (setf (link-href-end view) "xmllink")
264 (setf (link-ampersand view) "&"))
265 ((or :ie-xml-link :ie-xml-link-labels)
266 (initialize-xml-view view)
267 (setf (link-href-start view) "html:a href=")
268 (setf (link-href-end view) "html:a")
269 (setf (link-ampersand view) "&"))))
273 ;;;; *************************************************************************
274 ;;;; View Data Format Section
275 ;;;; *************************************************************************
277 (defun class-name-of (obj)
278 (string-downcase (class-name (class-of obj))))
280 (defun text-list-start-value-func (obj nitems)
281 (values (hyperobject-class-user-name obj) nitems))
283 (defun htmlformat-list-start-value-func (x nitems)
284 (values (hyperobject-class-user-name x) nitems (class-name-of x)))
286 (defun initialize-text-view (view)
287 (setf (list-start-fmtstr view) "~a~P:~%")
288 (setf (list-start-value-func view) #'text-list-start-value-func)
289 (setf (list-start-indent view) t)
290 (setf (obj-data-indent view) t)
291 (setf (obj-data-end-fmtstr view) (format nil "~%"))
294 (defun initialize-html-view (view)
295 (initialize-text-view view)
296 (setf (file-start-str view) (format nil "<html><body>~%"))
297 (setf (file-end-str view) (format nil "</body><html>~%"))
298 (setf (list-start-indent view) t)
299 (setf (list-start-fmtstr view)
300 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
301 (setf (list-start-value-func view)
302 #'htmlformat-list-start-value-func)
303 (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
304 (setf (list-end-indent view) t)
305 (setf (list-end-value-func view) nil)
306 (setf (obj-start-indent view) t)
307 (setf (obj-start-fmtstr view) "<li>")
308 (setf (obj-start-value-func view) nil)
309 (setf (obj-end-indent view) t)
310 (setf (obj-end-fmtstr view) (format nil "</li>~%"))
311 (setf (obj-end-value-func view) nil)
312 (setf (obj-data-indent view) t))
314 (defun initialize-xhtml-view (view)
315 (initialize-text-view view)
316 (setf (file-start-str view) (format nil "<html><body>~%"))
317 (setf (file-end-str view) (format nil "</body><html>~%"))
318 (setf (list-start-indent view) t)
319 (setf (list-start-fmtstr view)
320 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
321 (setf (list-start-value-func view)
322 #'htmlformat-list-start-value-func)
323 (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
324 (setf (list-end-indent view) t)
325 (setf (list-end-value-func view) nil)
326 (setf (obj-start-indent view) t)
327 (setf (obj-start-fmtstr view) "<li>")
328 (setf (obj-start-value-func view) nil)
329 (setf (obj-end-indent view) t)
330 (setf (obj-end-fmtstr view) (format nil "</li>~%"))
331 (setf (obj-end-value-func view) nil)
332 (setf (obj-data-indent view) t))
334 (defun xmlformat-list-end-value-func (x)
335 (format nil "~alist" (class-name-of x)))
337 (defun xmlformat-list-start-value-func (x nitems)
338 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
340 (defun initialize-xml-view (view)
341 (initialize-text-view view)
342 (setf (file-start-str view) "") ; (std-xml-header)
343 (setf (list-start-indent view) t)
344 (setf (list-start-fmtstr view) "<~a><title>~a~p:</title> ~%")
345 (setf (list-start-value-func view)
346 #'xmlformat-list-start-value-func)
347 (setf (list-end-indent view) t)
348 (setf (list-end-fmtstr view) "</~a>~%")
349 (setf (list-end-value-func view) #'xmlformat-list-end-value-func)
350 (setf (obj-start-fmtstr view) (format nil "<~(~a~)>" (object-class view)))
351 (setf (obj-start-indent view) t)
352 (setf (obj-end-fmtstr view) (format nil "</~(~a~)>~%" (object-class view)))
353 (setf (obj-end-indent view) nil)
354 (setf (obj-data-indent view) nil))
357 ;;; File Start and Ends
359 (defun fmt-file-start (view strm)
360 (awhen (file-start-str view)
361 (write-string it strm)))
363 (defun fmt-file-end (view strm)
364 (awhen (file-end-str view)
365 (write-string it strm)))
367 ;;; List Start and Ends
369 (defun fmt-list-start (obj view strm indent num-items)
370 (when (list-start-indent view)
371 (indent-spaces indent strm))
372 (let-when (fmtstr (list-start-fmtstr view))
373 (let-if (value-func (list-start-value-func view))
374 (apply #'format strm fmtstr
375 (multiple-value-list (funcall value-func
377 (write-string fmtstr strm))))
379 (defun fmt-list-end (obj view strm indent num-items)
380 (declare (ignore num-items))
381 (when (list-end-indent view)
382 (indent-spaces indent strm))
383 (let-when (fmtstr (list-end-fmtstr view))
384 (let-if (value-func (list-end-value-func view))
385 (apply #'format strm fmtstr (multiple-value-list
386 (funcall value-func obj)))
387 (write-string fmtstr strm))))
389 ;;; Object Start and Ends
391 (defun fmt-obj-start (obj view strm indent)
392 (when (obj-start-indent view)
393 (indent-spaces indent strm))
394 (let-when (fmtstr (obj-start-fmtstr view))
395 (let-if (value-func (obj-start-value-func view))
396 (apply #'format strm fmtstr (multiple-value-list
397 (funcall value-func obj)))
398 (write-string fmtstr strm))))
400 (defun fmt-obj-end (obj view strm indent)
401 (when (obj-end-indent view)
402 (indent-spaces indent strm))
403 (let-when (fmtstr (obj-end-fmtstr view))
404 (let-if (value-func (obj-end-value-func view))
405 (apply #'format strm fmtstr (multiple-value-list
406 (funcall value-func obj)))
407 (write-string fmtstr strm))))
411 (defun make-link-start (view fieldfunc fieldvalue refvars)
412 (format nil "~a\"~a?func=~a~akey=~a~a\""
413 (link-href-start view)
414 (make-url (link-page-name view))
416 (link-ampersand view) fieldvalue
419 (dolist (var refvars)
421 varstr (link-ampersand view)
422 (format nil "~a=~a" (car var) (cadr var))))
426 (defun make-link-end (obj view fieldname)
427 (declare (ignore obj fieldname))
428 ;;(format nil "~a" (href-end ref))
432 (defun fmt-obj-data (obj view strm indent refvars)
433 (when (obj-data-indent view)
434 (indent-spaces indent strm))
435 (if (link-slots view)
436 (fmt-obj-data-with-link obj view strm refvars)
437 (fmt-obj-data-plain obj view strm))
438 (awhen (obj-data-end-fmtstr view)
441 (defun fmt-obj-data-plain (obj view strm)
442 (awhen (obj-data-value-func view)
443 (apply #'format strm (obj-data-fmtstr view)
444 (multiple-value-list (funcall it obj)))))
446 (defun fmt-obj-data-with-link (obj view strm refvars)
447 (let ((refvalues '()))
448 ;; make list of hyperlink link fields for printing to refstr template
449 (dolist (name (link-slots view))
451 (find name (hyperobject-class-hyperlinks obj) :key #'name))
452 (push (make-link-start view (lookup hyperlink) (slot-value obj name)
453 (append (link-parameters hyperlink) refvars))
455 (push (make-link-end obj view name) refvalues)))
456 (setq refvalues (nreverse refvalues))
457 (apply #'format strm (make-link-data-str obj view) refvalues)))
459 (defun obj-data (obj view)
460 "Returns the objects data as a string. Used by common-graphics outline function"
461 (awhen (obj-data-value-func view)
462 (apply #'format nil (funcall (obj-data-fmtstr view))
463 (multiple-value-list (funcall it obj)))))
465 (defun make-link-data-str (obj view)
466 "Return fmt string for that contains ~a slots for hyperlink link start and end"
467 (awhen (obj-data-value-func view)
468 (apply #'format nil (obj-data-fmtstr view)
469 (multiple-value-list (funcall it obj)))))
471 ;;; Display method for objects
474 (defun load-all-subobjects (objs)
475 "Load all subobjects if they have not already been loaded."
476 (dolist (obj (mklist objs))
477 (dolist (subobj (hyperobject-class-subobjects obj))
478 (awhen (slot-value obj (name-slot subobj))
479 (load-all-subobjects it))))
482 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
484 "Display a single or list of hyperobject-class instances and their subobjects"
485 (let-when (objlist (mklist objs))
486 (let ((nobjs (length objlist))
490 (*print-readably* nil)
493 (fmt-list-start (car objlist) view strm indent nobjs)
494 (dolist (obj objlist)
495 (unless (and filter (not (funcall filter obj)))
496 (fmt-obj-start obj view strm indent)
497 (fmt-obj-data obj view strm (1+ indent) refvars)
498 (when (and subobjects (hyperobject-class-subobjects obj))
499 (dolist (subobj (hyperobject-class-subobjects obj))
500 (aif (slot-value obj (name-slot subobj))
501 (view-hyperobject it (get-category-view (car (mklist it)) category)
502 category strm (1+ indent) filter
503 subobjects refvars))))
504 (fmt-obj-end obj view strm indent)))
505 (fmt-list-end (car objlist) view strm indent nobjs)))
509 (defun view (objs &key (stream *standard-output*) category view
510 filter subobjects refvars file-wrapper)
511 "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
512 (let-when (objlist (mklist objs))
514 (setq view (get-category-view (car objlist) category)))
516 (setq view (default-view (class-of (car objlist)))))
518 (fmt-file-start view stream))
519 (view-hyperobject objlist view category stream 0 filter subobjects refvars)
521 (fmt-file-end view stream)))
527 (defun fmt-comma-integer (i)
528 (format nil "~:d" i))