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.22 2003/03/25 06:45:57 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
140 :category :compact-text)))
143 (eval `(make-instance ,view-def)))
145 (error "Invalid parameter to make-object-view: ~S" view-def))))
147 (defmethod initialize-instance :after ((view object-view)
148 &rest initargs &key &allow-other-keys)
149 (initialize-view (object-class view) view))
151 (defun initialize-view (obj-cl view)
152 "Calculate all view slots for a hyperobject class"
155 (initialize-view-by-category obj-cl view))
157 (initialize-view-by-source-code obj-cl view))
159 (setf (category view) :compact-text)
160 (initialize-view-by-category obj-cl view))))
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")
170 (defun initialize-view-by-category (obj-cl view)
171 "Initialize a view based upon a preset category"
176 (category (category view)))
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))
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)))
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)))
201 (setq first-field nil))
203 (string-append fmtstr " ")))
207 ((or :integer :fixnum)
215 (string-append fmtstr value-fmt))
216 (:compact-text-labels
217 (string-append fmtstr namestr-lower " " value-fmt))
219 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
221 (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
223 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
225 (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
227 (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))
228 ((or :html-link :xhtml-link)
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)
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 ">"))))
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>"))))
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)
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 ">")))))
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
263 (string-equal (write-to-string type) "string")))
264 (setq func `(kmrcl:xml-cdata ,func)))
265 (push func value-func))
270 (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
272 (setf (obj-data-fmtstr view) fmtstr)
273 (setf (obj-data-value-func view) value-func)
274 (setf (link-slots view) (nreverse links))
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) "&"))
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) "&"))
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) "&"))))
307 ;;;; *************************************************************************
308 ;;;; View Data Format Section
309 ;;;; *************************************************************************
311 (defun class-name-of (obj)
312 (string-downcase (class-name (class-of obj))))
314 (defun text-list-start-value-func (obj nitems)
315 (values (hyperobject-class-user-name obj) nitems))
317 (defun htmlformat-list-start-value-func (x nitems)
318 (values (hyperobject-class-user-name x) nitems (class-name-of x)))
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 "~%"))
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))
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))
368 (defun xmlformat-list-end-value-func (x)
369 (format nil "~alist" (class-name-of x)))
371 (defun xmlformat-list-start-value-func (x nitems)
372 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
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))
391 ;;; File Start and Ends
393 (defun fmt-file-start (view strm)
394 (awhen (file-start-str view)
395 (write-string it strm)))
397 (defun fmt-file-end (view strm)
398 (awhen (file-end-str view)
399 (write-string it strm)))
401 ;;; List Start and Ends
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
411 (write-string fmtstr strm))))
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))))
423 ;;; Object Start and Ends
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))))
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))))
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))
450 (link-ampersand view) fieldvalue
453 (dolist (var refvars)
455 varstr (link-ampersand view)
456 (format nil "~a=~a" (car var) (cadr var))))
460 (defun make-link-end (obj view fieldname)
461 (declare (ignore obj fieldname))
462 (link-href-end view))
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)))
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)))))
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))
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))
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)))
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)))))
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)))))
503 ;;; Display method for objects
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))))
514 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
516 "Display a single or list of hyperobject-class instances and their subobjects"
517 (let-when (objlist (mklist objs))
518 (let ((nobjs (length objlist))
522 (*print-readably* 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)))
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))
546 (setq view (get-category-view (car objlist) category)))
548 (setq view (default-view (class-of (car objlist)))))
550 (fmt-file-start view stream))
551 (view-hyperobject objlist view category stream 0 filter subobjects refvars)
553 (fmt-file-end view stream)))
559 (defun fmt-comma-integer (i)
560 (format nil "~:d" i))