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.28 2003/05/06 22:00:42 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 (or string null) :initform nil :initarg :file-start-str
41 :accessor file-start-str)
42 (file-end-str :type (or string null) :initform nil :initarg :file-end-str
43 :accessor file-end-str)
44 (list-start-fmtstr :type (or string null) :initform nil :initarg :list-start-fmtstr
45 :accessor list-start-fmtstr)
46 (list-start-value-func :type (or function symbol null) :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 (or string null) :initform nil :initarg :list-end-fmtstr
52 :accessor list-end-fmtstr)
53 (list-end-value-func :type (or function symbol null) :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 (or string symbol null) :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 (or string null) :initform nil :initarg :obj-end-fmtstr
65 :accessor obj-end-fmtstr)
66 (obj-end-value-func :type (or function symbol null) :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 (or string null) :initform nil :initarg :obj-data-fmtstr
74 :accessor obj-data-fmtstr)
75 (obj-data-end-fmtstr :type (or string null) :initform nil
76 :initarg :obj-data-end-fmtstr
77 :accessor obj-data-end-fmtstr)
78 (obj-data-value-func :type (or function symbol null) :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 (or string null) :initform nil :initarg :link-page-name
85 :accessor link-page-name)
86 (link-href-start :type (or string null) :initform nil :initarg :link-href-start
87 :accessor link-href-start)
88 (link-href-end :type (or string null) :initform nil :initarg :link-href-end
89 :accessor link-href-end)
90 (link-ampersand :type (or string null) :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
105 :object-class-name (class-name obj-class)
106 :object-class obj-class
109 (push view (views obj-class))
112 ;;;; *************************************************************************
113 ;;;; Metaclass Intialization
114 ;;;; *************************************************************************
116 (defun finalize-views (cl)
117 "Finalize all views that are given on a objects initialization"
118 (unless (default-print-slots cl)
119 (setf (default-print-slots cl)
120 (mapcar #'slot-definition-name (class-slots cl))))
122 (dolist (view-def (views cl))
123 (push (make-object-view cl view-def) views))
124 (setf (views cl) (nreverse views)))
127 (setf (default-view cl) (make-object-view cl (default-view cl))))
129 (setf (default-view cl) (make-object-view cl (car (views cl)))))
131 (setf (default-view cl) (make-object-view cl :default)))))
133 (defun make-object-view (cl view-def)
134 "Make an object view from a definition. Do nothing if a class is passed so that reinitialization will be a no-op"
136 ((typep view-def 'object-view)
138 ((eq view-def :default)
139 (let* ((name (class-name cl))
140 (view (make-instance 'object-view :name "automatic"
141 :object-class-name name
143 :category :compact-text)))
146 (eval `(make-instance ,view-def)))
148 (error "Invalid parameter to make-object-view: ~S" view-def))))
150 (defmethod initialize-instance :after ((view object-view)
151 &rest initargs &key &allow-other-keys)
152 (initialize-view (object-class view) view))
154 (defun initialize-view (obj-cl view)
155 "Calculate all view slots for a hyperobject class"
158 (initialize-view-by-category obj-cl view))
160 (initialize-view-by-source-code obj-cl view))
162 (setf (category view) :compact-text)
163 (initialize-view-by-category obj-cl view))))
165 (defun initialize-view-by-source-code (obj-cl view)
166 "Initialize a view based upon a source code"
167 (let ((source-code (source-code view)))
168 (error "not implemented")
173 (defun initialize-view-by-category (obj-cl view)
174 "Initialize a view based upon a preset category"
179 (category (category view)))
181 (unless (in category :compact-text :compact-text-labels
182 :html :html-labels :html-link-labels
183 :xhtml :xhtml-labels :xhtml-link-labels
184 :xml :xml-labels :xml-link :ie-xml-link
185 :xml-link-labels :ie-xml-link-labels)
186 (error "Unknown view category ~A" category))
189 (setf (slots view) (default-print-slots obj-cl)))
190 (dolist (slot-name (slots view))
191 (let ((slot (find-slot-by-name obj-cl slot-name)))
193 (error "Slot ~A is not found in class ~S" slot-name obj-cl))
194 (let* ((name (slot-definition-name slot))
195 (namestr-lower (string-downcase (symbol-name name)))
196 (xml-namestr (escape-xml-string namestr-lower))
197 (xml-tag (escape-xml-string namestr-lower))
198 (type (slot-value slot 'type))
199 (print-formatter (esd-print-formatter slot)))
204 (setq first-field nil))
206 (string-append fmtstr " ")))
210 ((or :integer :fixnum)
218 (string-append fmtstr value-fmt))
219 (:compact-text-labels
220 (string-append fmtstr namestr-lower " " value-fmt))
222 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
224 (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
226 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
228 (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
230 (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))
231 ((or :html-link :xhtml-link)
233 (if (esd-hyperlink slot)
234 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
235 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))))
236 ((or :xml-link :ie-xml-link)
238 (if (esd-hyperlink slot)
239 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
240 (string-append fmtstr (concatenate 'string "<" xml-tag ">" value-fmt "</" xml-tag ">"))))
243 (if (esd-hyperlink slot)
244 (string-append fmtstr "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
245 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
248 (if (esd-hyperlink slot)
249 (string-append fmtstr "<span class=\"label\">" xml-namestr "</span> <~~a>" value-fmt "</~~a>")
250 (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
251 ((or :xml-link-labels :ie-xml-link-labels)
253 (if (esd-hyperlink slot)
254 (string-append fmtstr "<label>" xml-namestr "</label> <~~a>" value-fmt "</~~a>")
255 (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))))
258 (let ((func (if print-formatter
259 `(,print-formatter (slot-value x (quote ,name)))
260 `(slot-value x (quote ,name)))))
261 (when (and (in category :xml :xhtml :xml-link :xhtml-link
262 :xml-labels :ie-xml-labels
263 :xhtml-link-labels :xml-link-labels :ie-xml-link
266 (string-equal (write-to-string type) "cdata")
267 (string-equal (write-to-string type) "string")))
268 (setq func `(kmrcl:xml-cdata ,func)))
269 (push func value-func))
274 (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
276 (setf (obj-data-fmtstr view) fmtstr)
277 (setf (obj-data-value-func view) value-func)
278 (setf (link-slots view) (nreverse links))
281 ((or :compact-text :compact-text-labels)
282 (initialize-text-view view))
283 ((or :html :xhtml :html-labels :xhtml-labels)
284 (initialize-html-view view))
285 ((or :xml :xml-labels)
286 (initialize-xml-view view))
287 ((or :html-link :html-link-labels)
288 (initialize-html-view view)
289 (setf (link-href-start view) "a href=")
290 (setf (link-href-end view) "a")
291 (setf (link-ampersand view) "&"))
292 ((or :xhtml-link :xhtml-link-labels)
293 (initialize-html-view view)
294 (setf (link-href-start view) "a href=")
295 (setf (link-href-end view) "a")
296 (setf (link-ampersand view) "&"))
297 ((or :xml-link :xml-link-labels)
298 (initialize-xml-view view)
299 (setf (link-href-start view)
300 "xmllink xlink:type=\"simple\" xlink:href=")
301 (setf (link-href-end view) "xmllink")
302 (setf (link-ampersand view) "&"))
303 ((or :ie-xml-link :ie-xml-link-labels)
304 (initialize-xml-view view)
305 (setf (link-href-start view) "html:a href=")
306 (setf (link-href-end view) "html:a")
307 (setf (link-ampersand view) "&"))))
311 ;;;; *************************************************************************
312 ;;;; View Data Format Section
313 ;;;; *************************************************************************
315 (defun class-name-of (obj)
316 (string-downcase (class-name (class-of obj))))
318 (defun text-list-start-value-func (obj nitems)
319 (values (hyperobject-class-user-name obj) nitems))
321 (defun htmlformat-list-start-value-func (x nitems)
322 (values (hyperobject-class-user-name x) nitems (class-name-of x)))
324 (defun initialize-text-view (view)
325 (setf (list-start-fmtstr view) "~a~P:~%")
326 (setf (list-start-value-func view) #'text-list-start-value-func)
327 (setf (list-start-indent view) t)
328 (setf (obj-data-indent view) t)
329 (setf (obj-data-end-fmtstr view) (format nil "~%"))
332 (defun initialize-html-view (view)
333 (initialize-text-view view)
334 (setf (file-start-str view) (format nil "<html><body>~%"))
335 (setf (file-end-str view) (format nil "</body><html>~%"))
336 (setf (list-start-indent view) t)
337 (setf (list-start-fmtstr view)
338 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
339 (setf (list-start-value-func view)
340 #'htmlformat-list-start-value-func)
341 (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
342 (setf (list-end-indent view) t)
343 (setf (list-end-value-func view) nil)
344 (setf (obj-start-indent view) t)
345 (setf (obj-start-fmtstr view) "<li>")
346 (setf (obj-start-value-func view) nil)
347 (setf (obj-end-indent view) t)
348 (setf (obj-end-fmtstr view) (format nil "</li>~%"))
349 (setf (obj-end-value-func view) nil)
350 (setf (obj-data-indent view) t))
352 (defun initialize-xhtml-view (view)
353 (initialize-text-view view)
354 (setf (file-start-str view) (format nil "<html><body>~%"))
355 (setf (file-end-str view) (format nil "</body><html>~%"))
356 (setf (list-start-indent view) t)
357 (setf (list-start-fmtstr view)
358 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
359 (setf (list-start-value-func view)
360 #'htmlformat-list-start-value-func)
361 (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
362 (setf (list-end-indent view) t)
363 (setf (list-end-value-func view) nil)
364 (setf (obj-start-indent view) t)
365 (setf (obj-start-fmtstr view) "<li>")
366 (setf (obj-start-value-func view) nil)
367 (setf (obj-end-indent view) t)
368 (setf (obj-end-fmtstr view) (format nil "</li>~%"))
369 (setf (obj-end-value-func view) nil)
370 (setf (obj-data-indent view) t))
372 (defun xmlformat-list-end-value-func (x)
373 (format nil "~alist" (class-name-of x)))
375 (defun xmlformat-list-start-value-func (x nitems)
376 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
378 (defun initialize-xml-view (view)
379 (initialize-text-view view)
380 (setf (file-start-str view) "") ; (std-xml-header)
381 (setf (list-start-indent view) t)
382 (setf (list-start-fmtstr view) "<~a><title>~a~p:</title> ~%")
383 (setf (list-start-value-func view)
384 #'xmlformat-list-start-value-func)
385 (setf (list-end-indent view) t)
386 (setf (list-end-fmtstr view) "</~a>~%")
387 (setf (list-end-value-func view) #'xmlformat-list-end-value-func)
388 (setf (obj-start-fmtstr view) (format nil "<~(~a~)>" (object-class-name view)))
389 (setf (obj-start-indent view) t)
390 (setf (obj-end-fmtstr view) (format nil "</~(~a~)>~%" (object-class-name view)))
391 (setf (obj-end-indent view) nil)
392 (setf (obj-data-indent view) nil))
395 ;;; File Start and Ends
397 (defun fmt-file-start (view strm)
398 (awhen (file-start-str view)
399 (write-string it strm)))
401 (defun fmt-file-end (view strm)
402 (awhen (file-end-str view)
403 (write-string it strm)))
405 ;;; List Start and Ends
407 (defun fmt-list-start (obj view strm indent num-items)
408 (when (list-start-indent view)
409 (indent-spaces indent strm))
410 (let-when (fmtstr (list-start-fmtstr view))
411 (let-if (value-func (list-start-value-func view))
412 (apply #'format strm fmtstr
413 (multiple-value-list (funcall value-func
415 (write-string fmtstr strm))))
417 (defun fmt-list-end (obj view strm indent num-items)
418 (declare (ignore num-items))
419 (when (list-end-indent view)
420 (indent-spaces indent strm))
421 (let-when (fmtstr (list-end-fmtstr view))
422 (let-if (value-func (list-end-value-func view))
423 (apply #'format strm fmtstr (multiple-value-list
424 (funcall value-func obj)))
425 (write-string fmtstr strm))))
427 ;;; Object Start and Ends
429 (defun fmt-obj-start (obj view strm indent)
430 (when (obj-start-indent view)
431 (indent-spaces indent strm))
432 (let-when (fmtstr (obj-start-fmtstr view))
433 (let-if (value-func (obj-start-value-func view))
434 (apply #'format strm fmtstr (multiple-value-list
435 (funcall value-func obj)))
436 (write-string fmtstr strm))))
438 (defun fmt-obj-end (obj view strm indent)
439 (when (obj-end-indent view)
440 (indent-spaces indent strm))
441 (let-when (fmtstr (obj-end-fmtstr view))
442 (let-if (value-func (obj-end-value-func view))
443 (apply #'format strm fmtstr (multiple-value-list
444 (funcall value-func obj)))
445 (write-string fmtstr strm))))
449 (defun make-link-start (view fieldfunc fieldvalue refvars)
450 (format nil "~a\"~a?func=~a~akey=~a~a\""
451 (link-href-start view)
452 (make-url (link-page-name view))
454 (link-ampersand view) fieldvalue
457 (dolist (var refvars)
459 varstr (link-ampersand view)
460 (format nil "~a=~a" (car var) (cadr var))))
464 (defun make-link-end (obj view fieldname)
465 (declare (ignore obj fieldname))
466 (link-href-end view))
468 (defun fmt-obj-data (obj view strm indent refvars)
469 (when (obj-data-indent view)
470 (indent-spaces indent strm))
471 (if (link-slots view)
472 (fmt-obj-data-with-link obj view strm refvars)
473 (fmt-obj-data-plain obj view strm))
474 (awhen (obj-data-end-fmtstr view)
475 (write-string it strm)))
477 (defun fmt-obj-data-plain (obj view strm)
478 (awhen (obj-data-value-func view)
479 (apply #'format strm (obj-data-fmtstr view)
480 (multiple-value-list (funcall it obj)))))
482 (defun fmt-obj-data-with-link (obj view strm refvars)
483 (let ((refvalues '()))
484 ;; make list of hyperlink link fields for printing to refstr template
485 (dolist (name (link-slots view))
487 (find name (hyperobject-class-hyperlinks obj) :key #'name))
488 (push (make-link-start view (lookup hyperlink) (slot-value obj name)
489 (append (link-parameters hyperlink) refvars))
491 (push (make-link-end obj view name) refvalues)))
492 (setq refvalues (nreverse refvalues))
493 (apply #'format strm (make-link-data-str obj view) refvalues)))
495 (defun obj-data (obj view)
496 "Returns the objects data as a string. Used by common-graphics outline function"
497 (awhen (obj-data-value-func view)
498 (apply #'format nil (funcall (obj-data-fmtstr view))
499 (multiple-value-list (funcall it obj)))))
501 (defun make-link-data-str (obj view)
502 "Return fmt string for that contains ~a slots for hyperlink link start and end"
503 (awhen (obj-data-value-func view)
504 (apply #'format nil (obj-data-fmtstr view)
505 (multiple-value-list (funcall it obj)))))
507 ;;; Display method for objects
510 (defun load-all-subobjects (objs)
511 "Load all subobjects if they have not already been loaded."
512 (dolist (obj (mklist objs))
513 (dolist (subobj (hyperobject-class-subobjects obj))
514 (awhen (slot-value obj (name-slot subobj))
515 (load-all-subobjects it))))
518 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
520 "Display a single or list of hyperobject-class instances and their subobjects"
521 (let-when (objlist (mklist objs))
522 (let ((nobjs (length objlist))
526 (*print-readably* nil)
529 (fmt-list-start (car objlist) view strm indent nobjs)
530 (dolist (obj objlist)
531 (unless (and filter (not (funcall filter obj)))
532 (fmt-obj-start obj view strm indent)
533 (fmt-obj-data obj view strm (1+ indent) refvars)
534 (when (and subobjects (hyperobject-class-subobjects obj))
535 (dolist (subobj (hyperobject-class-subobjects obj))
536 (aif (slot-value obj (name-slot subobj))
537 (view-hyperobject it (get-category-view (car (mklist it)) category)
538 category strm (1+ indent) filter
539 subobjects refvars))))
540 (fmt-obj-end obj view strm indent)))
541 (fmt-list-end (car objlist) view strm indent nobjs)))
545 (defun view (objs &key (stream *standard-output*) category view
546 filter subobjects refvars file-wrapper)
547 "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
548 (let-when (objlist (mklist objs))
550 (setq view (get-category-view (car objlist) category)))
552 (setq view (default-view (class-of (car objlist)))))
554 (fmt-file-start view stream))
555 (view-hyperobject objlist view category stream 0 filter subobjects refvars)
557 (fmt-file-end view stream)))
563 (defun fmt-comma-integer (i)
564 (format nil "~:d" i))