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.19 2002/12/26 12:04:00 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 :accessor 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 :accessor 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"
151 (initialize-view-by-category obj-cl view))
153 (initialize-view-by-source-code obj-cl view))
155 (setf (category view) :compact-text)
156 (initialize-view-by-category obj-cl view))))
158 (defun initialize-view-by-source-code (obj-cl view)
159 "Initialize a view based upon a source code"
160 (let ((source-code (source-code view)))
161 (error "not implemented")
166 (defun initialize-view-by-category (obj-cl view)
167 "Initialize a view based upon a preset category"
172 (category (category view)))
174 (unless (in category :compact-text :compact-text-labels
175 :html :html-labels :html-link-labels
176 :xhtml :xhtml-labels :xhtml-link-labels
177 :xml :xml-labels :xml-link :ie-xml-link
178 :xml-link-labels :ie-xml-link-labels)
179 (error "Unknown view category ~A" category))
182 (setf (slots view) (default-print-slots obj-cl)))
183 (dolist (slot-name (slots view))
184 (let ((slot (find-slot-by-name obj-cl slot-name)))
186 (error "Slot ~A is not found in class ~S" slot-name obj-cl))
187 (let* ((name (slot-definition-name slot))
188 (namestr-lower (string-downcase (symbol-name name)))
189 (xml-namestr (escape-xml-string namestr-lower))
190 (xml-tag (escape-xml-string namestr-lower))
191 (type (slot-value slot 'type))
192 (print-formatter (esd-print-formatter slot)))
197 (setq first-field nil))
199 (string-append fmtstr " ")))
203 ((or :integer :fixnum)
211 (string-append fmtstr value-fmt))
212 (:compact-text-labels
213 (string-append fmtstr namestr-lower " " value-fmt))
215 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
217 (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
219 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
221 (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
223 (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))
224 ((or :html-link :xhtml-link)
226 (if (esd-hyperlink slot)
227 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
228 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))))
229 ((or :xml-link :ie-xml-link)
231 (if (esd-hyperlink slot)
232 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
233 (string-append fmtstr (concatenate 'string "<" xml-tag ">" value-fmt "</" xml-tag ">"))))
236 (if (esd-hyperlink slot)
237 (string-append fmtstr "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
238 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
241 (if (esd-hyperlink slot)
242 (string-append fmtstr "<span class=\"label\">" xml-namestr "</span> <~~a>" value-fmt "</~~a>")
243 (string-append fmtstr (concatenate 'string "<span class=\"label\">" xml-namestr "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
244 ((or :xml-link-labels :ie-xml-link-labels)
246 (if (esd-hyperlink slot)
247 (string-append fmtstr "<label>" xml-namestr "></label> <~~a>" value-fmt "</~~a>")
248 (string-append fmtstr (concatenate 'string "<label>" xml-namestr "</label> <" xml-tag ">" value-fmt "</" xml-tag ">")))))
251 (let ((func (if print-formatter
252 `(,print-formatter (slot-value x (quote ,name)))
253 `(slot-value x (quote ,name)))))
254 (when (and (in category :xml :xhtml :xml-link :xhtml-link
255 :xml-labels :ie-xml-labels
256 :xhtml-link-labels :xml-link-labels :ie-xml-link
259 (string-equal (write-to-string type) "string")))
260 (setq func `(kmrcl:xml-cdata ,func)))
261 (push func value-func))
266 (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
268 (setf (obj-data-fmtstr view) fmtstr)
269 (setf (obj-data-value-func view) value-func)
270 (setf (link-slots view) (nreverse links))
273 ((or :compact-text :compact-text-labels)
274 (initialize-text-view view))
275 ((or :html :xhtml :html-labels :xhtml-labels)
276 (initialize-html-view view))
277 ((or :xml :xml-labels)
278 (initialize-xml-view view))
279 ((or :html-link :html-link-labels)
280 (initialize-html-view view)
281 (setf (link-href-start view) "a href=")
282 (setf (link-href-end view) "a")
283 (setf (link-ampersand view) "&"))
284 ((or :xhtml-link :xhtml-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 :xml-link :xml-link-labels)
290 (initialize-xml-view view)
291 (setf (link-href-start view)
292 "xmllink xlink:type=\"simple\" xlink:href=")
293 (setf (link-href-end view) "xmllink")
294 (setf (link-ampersand view) "&"))
295 ((or :ie-xml-link :ie-xml-link-labels)
296 (initialize-xml-view view)
297 (setf (link-href-start view) "html:a href=")
298 (setf (link-href-end view) "html:a")
299 (setf (link-ampersand view) "&"))))
303 ;;;; *************************************************************************
304 ;;;; View Data Format Section
305 ;;;; *************************************************************************
307 (defun class-name-of (obj)
308 (string-downcase (class-name (class-of obj))))
310 (defun text-list-start-value-func (obj nitems)
311 (values (hyperobject-class-user-name obj) nitems))
313 (defun htmlformat-list-start-value-func (x nitems)
314 (values (hyperobject-class-user-name x) nitems (class-name-of x)))
316 (defun initialize-text-view (view)
317 (setf (list-start-fmtstr view) "~a~P:~%")
318 (setf (list-start-value-func view) #'text-list-start-value-func)
319 (setf (list-start-indent view) t)
320 (setf (obj-data-indent view) t)
321 (setf (obj-data-end-fmtstr view) (format nil "~%"))
324 (defun initialize-html-view (view)
325 (initialize-text-view view)
326 (setf (file-start-str view) (format nil "<html><body>~%"))
327 (setf (file-end-str view) (format nil "</body><html>~%"))
328 (setf (list-start-indent view) t)
329 (setf (list-start-fmtstr view)
330 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
331 (setf (list-start-value-func view)
332 #'htmlformat-list-start-value-func)
333 (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
334 (setf (list-end-indent view) t)
335 (setf (list-end-value-func view) nil)
336 (setf (obj-start-indent view) t)
337 (setf (obj-start-fmtstr view) "<li>")
338 (setf (obj-start-value-func view) nil)
339 (setf (obj-end-indent view) t)
340 (setf (obj-end-fmtstr view) (format nil "</li>~%"))
341 (setf (obj-end-value-func view) nil)
342 (setf (obj-data-indent view) t))
344 (defun initialize-xhtml-view (view)
345 (initialize-text-view view)
346 (setf (file-start-str view) (format nil "<html><body>~%"))
347 (setf (file-end-str view) (format nil "</body><html>~%"))
348 (setf (list-start-indent view) t)
349 (setf (list-start-fmtstr view)
350 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
351 (setf (list-start-value-func view)
352 #'htmlformat-list-start-value-func)
353 (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
354 (setf (list-end-indent view) t)
355 (setf (list-end-value-func view) nil)
356 (setf (obj-start-indent view) t)
357 (setf (obj-start-fmtstr view) "<li>")
358 (setf (obj-start-value-func view) nil)
359 (setf (obj-end-indent view) t)
360 (setf (obj-end-fmtstr view) (format nil "</li>~%"))
361 (setf (obj-end-value-func view) nil)
362 (setf (obj-data-indent view) t))
364 (defun xmlformat-list-end-value-func (x)
365 (format nil "~alist" (class-name-of x)))
367 (defun xmlformat-list-start-value-func (x nitems)
368 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
370 (defun initialize-xml-view (view)
371 (initialize-text-view view)
372 (setf (file-start-str view) "") ; (std-xml-header)
373 (setf (list-start-indent view) t)
374 (setf (list-start-fmtstr view) "<~a><title>~a~p:</title> ~%")
375 (setf (list-start-value-func view)
376 #'xmlformat-list-start-value-func)
377 (setf (list-end-indent view) t)
378 (setf (list-end-fmtstr view) "</~a>~%")
379 (setf (list-end-value-func view) #'xmlformat-list-end-value-func)
380 (setf (obj-start-fmtstr view) (format nil "<~(~a~)>" (object-class view)))
381 (setf (obj-start-indent view) t)
382 (setf (obj-end-fmtstr view) (format nil "</~(~a~)>~%" (object-class view)))
383 (setf (obj-end-indent view) nil)
384 (setf (obj-data-indent view) nil))
387 ;;; File Start and Ends
389 (defun fmt-file-start (view strm)
390 (awhen (file-start-str view)
391 (write-string it strm)))
393 (defun fmt-file-end (view strm)
394 (awhen (file-end-str view)
395 (write-string it strm)))
397 ;;; List Start and Ends
399 (defun fmt-list-start (obj view strm indent num-items)
400 (when (list-start-indent view)
401 (indent-spaces indent strm))
402 (let-when (fmtstr (list-start-fmtstr view))
403 (let-if (value-func (list-start-value-func view))
404 (apply #'format strm fmtstr
405 (multiple-value-list (funcall value-func
407 (write-string fmtstr strm))))
409 (defun fmt-list-end (obj view strm indent num-items)
410 (declare (ignore num-items))
411 (when (list-end-indent view)
412 (indent-spaces indent strm))
413 (let-when (fmtstr (list-end-fmtstr view))
414 (let-if (value-func (list-end-value-func view))
415 (apply #'format strm fmtstr (multiple-value-list
416 (funcall value-func obj)))
417 (write-string fmtstr strm))))
419 ;;; Object Start and Ends
421 (defun fmt-obj-start (obj view strm indent)
422 (when (obj-start-indent view)
423 (indent-spaces indent strm))
424 (let-when (fmtstr (obj-start-fmtstr view))
425 (let-if (value-func (obj-start-value-func view))
426 (apply #'format strm fmtstr (multiple-value-list
427 (funcall value-func obj)))
428 (write-string fmtstr strm))))
430 (defun fmt-obj-end (obj view strm indent)
431 (when (obj-end-indent view)
432 (indent-spaces indent strm))
433 (let-when (fmtstr (obj-end-fmtstr view))
434 (let-if (value-func (obj-end-value-func view))
435 (apply #'format strm fmtstr (multiple-value-list
436 (funcall value-func obj)))
437 (write-string fmtstr strm))))
441 (defun make-link-start (view fieldfunc fieldvalue refvars)
442 (format nil "~a\"~a?func=~a~akey=~a~a\""
443 (link-href-start view)
444 (make-url (link-page-name view))
446 (link-ampersand view) fieldvalue
449 (dolist (var refvars)
451 varstr (link-ampersand view)
452 (format nil "~a=~a" (car var) (cadr var))))
456 (defun make-link-end (obj view fieldname)
457 (declare (ignore obj fieldname))
458 (link-href-end view))
460 (defun fmt-obj-data (obj view strm indent refvars)
461 (when (obj-data-indent view)
462 (indent-spaces indent strm))
463 (if (link-slots view)
464 (fmt-obj-data-with-link obj view strm refvars)
465 (fmt-obj-data-plain obj view strm))
466 (awhen (obj-data-end-fmtstr view)
467 (write-string it strm)))
469 (defun fmt-obj-data-plain (obj view strm)
470 (awhen (obj-data-value-func view)
471 (apply #'format strm (obj-data-fmtstr view)
472 (multiple-value-list (funcall it obj)))))
474 (defun fmt-obj-data-with-link (obj view strm refvars)
475 (let ((refvalues '()))
476 ;; make list of hyperlink link fields for printing to refstr template
477 (dolist (name (link-slots view))
479 (find name (hyperobject-class-hyperlinks obj) :key #'name))
480 (push (make-link-start view (lookup hyperlink) (slot-value obj name)
481 (append (link-parameters hyperlink) refvars))
483 (push (make-link-end obj view name) refvalues)))
484 (setq refvalues (nreverse refvalues))
485 (apply #'format strm (make-link-data-str obj view) refvalues)))
487 (defun obj-data (obj view)
488 "Returns the objects data as a string. Used by common-graphics outline function"
489 (awhen (obj-data-value-func view)
490 (apply #'format nil (funcall (obj-data-fmtstr view))
491 (multiple-value-list (funcall it obj)))))
493 (defun make-link-data-str (obj view)
494 "Return fmt string for that contains ~a slots for hyperlink link start and end"
495 (awhen (obj-data-value-func view)
496 (apply #'format nil (obj-data-fmtstr view)
497 (multiple-value-list (funcall it obj)))))
499 ;;; Display method for objects
502 (defun load-all-subobjects (objs)
503 "Load all subobjects if they have not already been loaded."
504 (dolist (obj (mklist objs))
505 (dolist (subobj (hyperobject-class-subobjects obj))
506 (awhen (slot-value obj (name-slot subobj))
507 (load-all-subobjects it))))
510 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
512 "Display a single or list of hyperobject-class instances and their subobjects"
513 (let-when (objlist (mklist objs))
514 (let ((nobjs (length objlist))
518 (*print-readably* nil)
521 (fmt-list-start (car objlist) view strm indent nobjs)
522 (dolist (obj objlist)
523 (unless (and filter (not (funcall filter obj)))
524 (fmt-obj-start obj view strm indent)
525 (fmt-obj-data obj view strm (1+ indent) refvars)
526 (when (and subobjects (hyperobject-class-subobjects obj))
527 (dolist (subobj (hyperobject-class-subobjects obj))
528 (aif (slot-value obj (name-slot subobj))
529 (view-hyperobject it (get-category-view (car (mklist it)) category)
530 category strm (1+ indent) filter
531 subobjects refvars))))
532 (fmt-obj-end obj view strm indent)))
533 (fmt-list-end (car objlist) view strm indent nobjs)))
537 (defun view (objs &key (stream *standard-output*) category view
538 filter subobjects refvars file-wrapper)
539 "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
540 (let-when (objlist (mklist objs))
542 (setq view (get-category-view (car objlist) category)))
544 (setq view (default-view (class-of (car objlist)))))
546 (fmt-file-start view stream))
547 (view-hyperobject objlist view category stream 0 filter subobjects refvars)
549 (fmt-file-end view stream)))
555 (defun fmt-comma-integer (i)
556 (format nil "~:d" i))