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.16 2002/12/13 12:23:17 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")
165 (defun initialize-view-by-category (obj-cl view)
166 "Initialize a view based upon a preset category"
171 (category (category view)))
173 (unless (in category :compact-text :compact-text-labels
174 :html :html-labels :html-link-labels
175 :xhtml :xhtml-labels :xhtml-link-labels
176 :xml :xml-labels :xml-link-labels)
177 (error "Unknown view category ~A" category))
180 (setf (slots view) (default-print-slots obj-cl)))
181 (dolist (slot-name (slots view))
182 (let ((slot (find-slot-by-name obj-cl slot-name)))
184 (error "Slot ~A is not found in class ~S" slot-name obj-cl))
185 (let ((name (slot-definition-name slot))
186 (namestr-lower (string-downcase (symbol-name (slot-definition-name slot))))
187 (type (slot-value slot 'type))
188 (print-formatter (esd-print-formatter slot)))
193 (setq first-field nil))
195 (string-append fmtstr " ")))
199 ((or :integer :fixnum)
207 (string-append fmtstr value-fmt))
208 (:compact-text-labels
209 (string-append fmtstr namestr-lower " " value-fmt))
211 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
213 (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
215 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
217 (string-append fmtstr (concatenate 'string "<span class=\"label\"><![CDATA[" namestr-lower "]]></span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
219 (string-append fmtstr (concatenate 'string "<label><[!CDATA[" namestr-lower "]]></label> <" namestr-lower ">" value-fmt "</" namestr-lower ">")))
220 ((or :html-link :xhtml-link)
222 (if (esd-hyperlink slot)
223 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
224 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))))
227 (if (esd-hyperlink slot)
228 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
229 (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))))
232 (if (esd-hyperlink slot)
233 (string-append fmtstr "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
234 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
237 (if (esd-hyperlink slot)
238 (string-append fmtstr "<span class=\"label\"><[!CDATA[" namestr-lower "]]></span> <~~a>" value-fmt "</~~a>")
239 (string-append fmtstr (concatenate 'string "<span class=\"label\"><![CDATA[" namestr-lower "]]></span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
242 (if (esd-hyperlink slot)
243 (string-append fmtstr "<label><[![CDATA[" namestr-lower "]]></label> <~~a>" value-fmt "</~~a>")
244 (string-append fmtstr (concatenate 'string "<label><![CDATA[" namestr-lower "]]></label> <" namestr-lower ">" value-fmt "</" namestr-lower ">")))))
247 (let ((func (if print-formatter
248 `(,print-formatter (slot-value x (quote ,name)))
249 `(slot-value x (quote ,name)))))
250 (when (and (in category :xml :xhtml :xml-link :xhtml-link
251 :xhtml-link-labels :xml-link-labels :ie-xml-link
254 (string-equal (write-to-string type) "string")))
255 (setq func `(kmrcl:xml-cdata ,func)))
256 (push func value-func))
261 (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
263 (setf (obj-data-fmtstr view) fmtstr)
264 (setf (obj-data-value-func view) value-func)
265 (setf (link-slots view) (nreverse links))
268 ((or :compact-text :compact-text-labels)
269 (initialize-text-view view))
270 ((or :html :xhtml :html-labels :xhtml-labels)
271 (initialize-html-view view))
272 ((or :xml :xml-labels)
273 (initialize-xml-view view))
274 ((or :html-link :html-link-labels)
275 (initialize-html-view view)
276 (setf (link-href-start view) "a href=")
277 (setf (link-href-end view) "a")
278 (setf (link-ampersand view) "&"))
279 ((or :xhtml-link :xhtml-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 :xml-link :xml-link-labels)
285 (initialize-xml-view view)
286 (setf (link-href-start view)
287 "xmllink xlink:type=\"simple\" xlink:href=")
288 (setf (link-href-end view) "xmllink")
289 (setf (link-ampersand view) "&"))
290 ((or :ie-xml-link :ie-xml-link-labels)
291 (initialize-xml-view view)
292 (setf (link-href-start view) "html:a href=")
293 (setf (link-href-end view) "html:a")
294 (setf (link-ampersand view) "&"))))
298 ;;;; *************************************************************************
299 ;;;; View Data Format Section
300 ;;;; *************************************************************************
302 (defun class-name-of (obj)
303 (string-downcase (class-name (class-of obj))))
305 (defun text-list-start-value-func (obj nitems)
306 (values (hyperobject-class-user-name obj) nitems))
308 (defun htmlformat-list-start-value-func (x nitems)
309 (values (hyperobject-class-user-name x) nitems (class-name-of x)))
311 (defun initialize-text-view (view)
312 (setf (list-start-fmtstr view) "~a~P:~%")
313 (setf (list-start-value-func view) #'text-list-start-value-func)
314 (setf (list-start-indent view) t)
315 (setf (obj-data-indent view) t)
316 (setf (obj-data-end-fmtstr view) (format nil "~%"))
319 (defun initialize-html-view (view)
320 (initialize-text-view view)
321 (setf (file-start-str view) (format nil "<html><body>~%"))
322 (setf (file-end-str view) (format nil "</body><html>~%"))
323 (setf (list-start-indent view) t)
324 (setf (list-start-fmtstr view)
325 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
326 (setf (list-start-value-func view)
327 #'htmlformat-list-start-value-func)
328 (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
329 (setf (list-end-indent view) t)
330 (setf (list-end-value-func view) nil)
331 (setf (obj-start-indent view) t)
332 (setf (obj-start-fmtstr view) "<li>")
333 (setf (obj-start-value-func view) nil)
334 (setf (obj-end-indent view) t)
335 (setf (obj-end-fmtstr view) (format nil "</li>~%"))
336 (setf (obj-end-value-func view) nil)
337 (setf (obj-data-indent view) t))
339 (defun initialize-xhtml-view (view)
340 (initialize-text-view view)
341 (setf (file-start-str view) (format nil "<html><body>~%"))
342 (setf (file-end-str view) (format nil "</body><html>~%"))
343 (setf (list-start-indent view) t)
344 (setf (list-start-fmtstr view)
345 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
346 (setf (list-start-value-func view)
347 #'htmlformat-list-start-value-func)
348 (setf (list-end-fmtstr view) (format nil "</ul></div>~%"))
349 (setf (list-end-indent view) t)
350 (setf (list-end-value-func view) nil)
351 (setf (obj-start-indent view) t)
352 (setf (obj-start-fmtstr view) "<li>")
353 (setf (obj-start-value-func view) nil)
354 (setf (obj-end-indent view) t)
355 (setf (obj-end-fmtstr view) (format nil "</li>~%"))
356 (setf (obj-end-value-func view) nil)
357 (setf (obj-data-indent view) t))
359 (defun xmlformat-list-end-value-func (x)
360 (format nil "~alist" (class-name-of x)))
362 (defun xmlformat-list-start-value-func (x nitems)
363 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
365 (defun initialize-xml-view (view)
366 (initialize-text-view view)
367 (setf (file-start-str view) "") ; (std-xml-header)
368 (setf (list-start-indent view) t)
369 (setf (list-start-fmtstr view) "<~a><title>~a~p:</title> ~%")
370 (setf (list-start-value-func view)
371 #'xmlformat-list-start-value-func)
372 (setf (list-end-indent view) t)
373 (setf (list-end-fmtstr view) "</~a>~%")
374 (setf (list-end-value-func view) #'xmlformat-list-end-value-func)
375 (setf (obj-start-fmtstr view) (format nil "<~(~a~)>" (object-class view)))
376 (setf (obj-start-indent view) t)
377 (setf (obj-end-fmtstr view) (format nil "</~(~a~)>~%" (object-class view)))
378 (setf (obj-end-indent view) nil)
379 (setf (obj-data-indent view) nil))
382 ;;; File Start and Ends
384 (defun fmt-file-start (view strm)
385 (awhen (file-start-str view)
386 (write-string it strm)))
388 (defun fmt-file-end (view strm)
389 (awhen (file-end-str view)
390 (write-string it strm)))
392 ;;; List Start and Ends
394 (defun fmt-list-start (obj view strm indent num-items)
395 (when (list-start-indent view)
396 (indent-spaces indent strm))
397 (let-when (fmtstr (list-start-fmtstr view))
398 (let-if (value-func (list-start-value-func view))
399 (apply #'format strm fmtstr
400 (multiple-value-list (funcall value-func
402 (write-string fmtstr strm))))
404 (defun fmt-list-end (obj view strm indent num-items)
405 (declare (ignore num-items))
406 (when (list-end-indent view)
407 (indent-spaces indent strm))
408 (let-when (fmtstr (list-end-fmtstr view))
409 (let-if (value-func (list-end-value-func view))
410 (apply #'format strm fmtstr (multiple-value-list
411 (funcall value-func obj)))
412 (write-string fmtstr strm))))
414 ;;; Object Start and Ends
416 (defun fmt-obj-start (obj view strm indent)
417 (when (obj-start-indent view)
418 (indent-spaces indent strm))
419 (let-when (fmtstr (obj-start-fmtstr view))
420 (let-if (value-func (obj-start-value-func view))
421 (apply #'format strm fmtstr (multiple-value-list
422 (funcall value-func obj)))
423 (write-string fmtstr strm))))
425 (defun fmt-obj-end (obj view strm indent)
426 (when (obj-end-indent view)
427 (indent-spaces indent strm))
428 (let-when (fmtstr (obj-end-fmtstr view))
429 (let-if (value-func (obj-end-value-func view))
430 (apply #'format strm fmtstr (multiple-value-list
431 (funcall value-func obj)))
432 (write-string fmtstr strm))))
436 (defun make-link-start (view fieldfunc fieldvalue refvars)
437 (format nil "~a\"~a?func=~a~akey=~a~a\""
438 (link-href-start view)
439 (make-url (link-page-name view))
441 (link-ampersand view) fieldvalue
444 (dolist (var refvars)
446 varstr (link-ampersand view)
447 (format nil "~a=~a" (car var) (cadr var))))
451 (defun make-link-end (obj view fieldname)
452 (declare (ignore obj fieldname))
453 (link-href-end view))
455 (defun fmt-obj-data (obj view strm indent refvars)
456 (when (obj-data-indent view)
457 (indent-spaces indent strm))
458 (if (link-slots view)
459 (fmt-obj-data-with-link obj view strm refvars)
460 (fmt-obj-data-plain obj view strm))
461 (awhen (obj-data-end-fmtstr view)
462 (write-string it strm)))
464 (defun fmt-obj-data-plain (obj view strm)
465 (awhen (obj-data-value-func view)
466 (apply #'format strm (obj-data-fmtstr view)
467 (multiple-value-list (funcall it obj)))))
469 (defun fmt-obj-data-with-link (obj view strm refvars)
470 (let ((refvalues '()))
471 ;; make list of hyperlink link fields for printing to refstr template
472 (dolist (name (link-slots view))
474 (find name (hyperobject-class-hyperlinks obj) :key #'name))
475 (push (make-link-start view (lookup hyperlink) (slot-value obj name)
476 (append (link-parameters hyperlink) refvars))
478 (push (make-link-end obj view name) refvalues)))
479 (setq refvalues (nreverse refvalues))
480 (apply #'format strm (make-link-data-str obj view) refvalues)))
482 (defun obj-data (obj view)
483 "Returns the objects data as a string. Used by common-graphics outline function"
484 (awhen (obj-data-value-func view)
485 (apply #'format nil (funcall (obj-data-fmtstr view))
486 (multiple-value-list (funcall it obj)))))
488 (defun make-link-data-str (obj view)
489 "Return fmt string for that contains ~a slots for hyperlink link start and end"
490 (awhen (obj-data-value-func view)
491 (apply #'format nil (obj-data-fmtstr view)
492 (multiple-value-list (funcall it obj)))))
494 ;;; Display method for objects
497 (defun load-all-subobjects (objs)
498 "Load all subobjects if they have not already been loaded."
499 (dolist (obj (mklist objs))
500 (dolist (subobj (hyperobject-class-subobjects obj))
501 (awhen (slot-value obj (name-slot subobj))
502 (load-all-subobjects it))))
505 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
507 "Display a single or list of hyperobject-class instances and their subobjects"
508 (let-when (objlist (mklist objs))
509 (let ((nobjs (length objlist))
513 (*print-readably* nil)
516 (fmt-list-start (car objlist) view strm indent nobjs)
517 (dolist (obj objlist)
518 (unless (and filter (not (funcall filter obj)))
519 (fmt-obj-start obj view strm indent)
520 (fmt-obj-data obj view strm (1+ indent) refvars)
521 (when (and subobjects (hyperobject-class-subobjects obj))
522 (dolist (subobj (hyperobject-class-subobjects obj))
523 (aif (slot-value obj (name-slot subobj))
524 (view-hyperobject it (get-category-view (car (mklist it)) category)
525 category strm (1+ indent) filter
526 subobjects refvars))))
527 (fmt-obj-end obj view strm indent)))
528 (fmt-list-end (car objlist) view strm indent nobjs)))
532 (defun view (objs &key (stream *standard-output*) category view
533 filter subobjects refvars file-wrapper)
534 "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
535 (let-when (objlist (mklist objs))
537 (setq view (get-category-view (car objlist) category)))
539 (setq view (default-view (class-of (car objlist)))))
541 (fmt-file-start view stream))
542 (view-hyperobject objlist view category stream 0 filter subobjects refvars)
544 (fmt-file-end view stream)))
550 (defun fmt-comma-integer (i)
551 (format nil "~:d" i))