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.12 2002/12/13 05:44:19 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
24 :documentation "Name of class for object to be viewed.")
25 (slots :initform nil :initarg :slots
26 :documentation "List of effective slots for object to be viewed.")
27 (name :initform nil :initarg :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 (file-end-str :type string :initform nil :initarg :file-end-str)
38 (list-start-fmtstr :type string :initform nil :initarg :list-start-fmtstr)
39 (list-start-value-func :type function :initform nil
40 :initarg :list-start-value-func)
41 (list-start-indent :initform nil :initarg :list-start-indent)
42 (list-end-fmtstr :type string :initform nil :initarg :list-end-fmtstr)
43 (list-end-value-func :type function :initform nil
44 :initarg :list-end-value-func)
45 (list-end-indent :initform nil :initarg :list-end-indent)
46 (obj-start-fmtstr :type string :initform nil :initarg :obj-start-fmtstr)
47 (obj-start-value-func :initform nil :initarg :obj-start-value-func)
48 (obj-start-indent :initform nil :initarg :obj-start-indent)
49 (obj-end-fmtstr :type string :initform nil :initarg :obj-end-fmtstr)
50 (obj-end-value-func :type function :initform nil
51 :initarg :obj-end-value-func)
52 (obj-end-indent :initform nil :initarg :obj-end-indent)
53 (obj-data-indent :initform nil :initarg :obj-data-indent)
54 (obj-data-fmtstr :type string :initform nil :initarg :obj-data-fmtstr)
55 (obj-data-end-fmtstr :type string :initform nil
56 :initarg :obj-data-end-fmtstr)
57 (obj-data-value-func :type function :initform nil
58 :initarg :obj-data-value-func)
60 (link-slots :type list :initform nil
61 :documentation "List of slot names that have hyperlinks")
62 (link-page-name :type string :initform nil :initarg :link-page-name)
63 (link-href-start :type string :initform nil :initarg :link-href-start)
64 (link-href-end :type string :initform nil :initarg :link-href-end)
65 (link-ampersand :type string :initform nil :initarg :link-ampersand))
66 (:default-initargs :link-page-name "disp-func1")
67 (:documentation "View class for a hyperobject"))
70 (defun get-category-view (obj category &optional slots)
71 "Find or make a category view for an object"
72 (let ((obj-class (class-of obj)))
74 (slot-value obj-class 'default-view)
75 (aif (find category (views obj-class) :key #'category)
78 (make-instance 'object-view :object-class (class-name obj-class)
81 (push view (views obj-class))
84 ;;;; *************************************************************************
85 ;;;; Metaclass Intialization
86 ;;;; *************************************************************************
88 (defun finalize-views (cl)
89 "Finalize all views that are given on a objects initialization"
90 (unless (slot-value cl 'default-print-slots)
91 (setf (slot-value cl 'default-print-slots)
92 (mapcar #'slot-definition-name (class-slots cl))))
94 (dolist (view-def (slot-value cl 'views))
95 (push (make-object-view cl view-def) views))
96 (setf (slot-value cl 'views) (nreverse views)))
99 (setf (slot-value cl 'default-view) (make-object-view cl (default-view cl))))
101 (setf (slot-value cl 'default-view) (make-object-view cl (car (views cl)))))
103 (setf (slot-value cl 'default-view) (make-object-view cl :default)))))
105 (defun make-object-view (cl view-def)
106 "Make an object view from a definition. Do nothing if a class is passed so that reinitialization will be a no-op"
108 ((typep view-def 'object-view)
110 ((eq view-def :default)
111 (let* ((name (class-name cl))
112 (view (make-instance 'object-view :name "automatic"
114 :category :compact-text)))
117 (eval `(make-instance ,view-def)))
119 (error "Invalid parameter to make-object-view: ~S" view-def))))
121 (defmethod initialize-instance :after ((view object-view)
122 &rest initargs &key &allow-other-keys)
123 (initialize-view (find-class (slot-value view 'object-class)) view))
125 (defun initialize-view (obj-cl view)
126 "Calculate all view slots for a hyperobject class"
131 (category (category view)))
132 (unless (slot-value view 'slots)
133 (setf (slot-value view 'slots) (slot-value obj-cl 'default-print-slots)))
134 (dolist (slot-name (slot-value view 'slots))
135 (let ((slot (find-slot-by-name obj-cl slot-name)))
137 (error "Slot ~A is not found in class ~S" slot-name obj-cl))
138 (let ((name (slot-definition-name slot))
139 (namestr-lower (string-downcase (symbol-name (slot-definition-name slot))))
140 (type (slot-value slot 'type))
141 (print-formatter (slot-value slot 'print-formatter)))
146 (setq first-field nil))
148 (string-append fmtstr " ")))
150 (when (slot-value slot 'hyperlink)
155 ((or :integer :fixnum)
163 (string-append fmtstr value-fmt))
164 (:compact-text-labels
165 (string-append fmtstr namestr-lower " " value-fmt))
167 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>")))
169 (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">")))
171 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
173 (string-append fmtstr (concatenate 'string "<span class=\"label\"><![CDATA[" namestr-lower "]]></span> <span class=\"" namestr-lower "\">" value-fmt "</span>")))
175 (string-append fmtstr (concatenate 'string "<label><[!CDATA[" namestr-lower "]]></label> <" namestr-lower ">" value-fmt "</" namestr-lower ">")))
176 ((or :html-link :xhtml-link)
177 (if (slot-value slot 'hyperlink)
178 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
179 (string-append fmtstr (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))))
181 (if (slot-value slot 'hyperlink)
182 (string-append fmtstr "<~~a>" value-fmt "</~~a>")
183 (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))))
185 (if (slot-value slot 'hyperlink)
186 (string-append fmtstr "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
187 (string-append fmtstr (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
189 (if (slot-value slot 'hyperlink)
190 (string-append fmtstr "<span class=\"label\"><[!CDATA[" namestr-lower "]]></span> <~~a>" value-fmt "</~~a>")
191 (string-append fmtstr (concatenate 'string "<span class=\"label\"><![CDATA[" namestr-lower "]]></span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))))
193 (if (slot-value slot 'hyperlink)
194 (string-append fmtstr "<label><[![CDATA[" namestr-lower "]]></label> <~~a>" value-fmt "</~~a>")
195 (string-append fmtstr (concatenate 'string "<label><![CDATA[" namestr-lower "]]></label> <" namestr-lower ">" value-fmt "</" namestr-lower ">")))))
198 (let ((func (if print-formatter
199 `(,print-formatter (slot-value x (quote ,name)))
200 `(slot-value x (quote ,name)))))
201 (when (and (in category :xml :xhtml :xml-link :xhtml-link
202 :xhtml-link-labels :xml-link-labels :ie-xml-link
205 (string-equal (write-to-string type) "string")))
206 (setq func `(kmrcl:xml-cdata ,func)))
207 (push func value-func))
212 (compile nil (eval `(lambda (x) (values ,@(nreverse value-func)))))))
214 (setf (slot-value view 'obj-data-fmtstr) fmtstr)
215 (setf (slot-value view 'obj-data-value-func) value-func)
216 (setf (slot-value view 'link-slots) (nreverse links))
219 ((or :compact-text :compact-text-labels)
220 (initialize-text-view view))
221 ((or :html :xhtml :html-labels :xhtml-labels)
222 (initialize-html-view view))
223 ((or :xml :xml-labels)
224 (initialize-xml-view view))
225 ((or :html-link :html-link-labels)
226 (initialize-html-view view)
227 (setf (slot-value view 'link-href-start) "a href=")
228 (setf (slot-value view 'link-href-end) "a")
229 (setf (slot-value view 'link-ampersand) "&"))
230 ((or :xhtml-link :xhtml-link-labels)
231 (initialize-html-view view)
232 (setf (slot-value view 'link-href-start) "a href=")
233 (setf (slot-value view 'link-href-end) "a")
234 (setf (slot-value view 'link-ampersand) "&"))
235 ((or :xml-link :xml-link-labels)
236 (initialize-xml-view view)
237 (setf (slot-value view 'link-href-start)
238 "xmllink xlink:type=\"simple\" xlink:href=")
239 (setf (slot-value view 'link-href-end) "xmllink")
240 (setf (slot-value view 'link-ampersand) "&"))
241 ((or :ie-xml-link :ie-xml-link-labels)
242 (initialize-xml-view view)
243 (setf (slot-value view 'link-href-start) "html:a href=")
244 (setf (slot-value view 'link-href-end) "html:a")
245 (setf (slot-value view 'link-ampersand) "&"))))
249 ;;;; *************************************************************************
250 ;;;; View Data Format Section
251 ;;;; *************************************************************************
253 (defun class-name-of (obj)
254 (string-downcase (class-name (class-of obj))))
256 (defun text-list-start-value-func (obj nitems)
257 (values (hyperobject-class-user-name obj) nitems))
259 (defun htmlformat-list-start-value-func (x nitems)
260 (values (hyperobject-class-user-name x) nitems (class-name-of x)))
262 (defun initialize-text-view (view)
263 (setf (slot-value view 'list-start-fmtstr) "~a~P:~%")
264 (setf (slot-value view 'list-start-value-func) #'text-list-start-value-func)
265 (setf (slot-value view 'list-start-indent) t)
266 (setf (slot-value view 'obj-data-indent) t)
267 (setf (slot-value view 'obj-data-end-fmtstr) "~%"))
269 (defun initialize-html-view (view)
270 (initialize-text-view view)
271 (setf (slot-value view 'file-start-str) "<html><body>~%")
272 (setf (slot-value view 'file-end-str) "</body><html>~%")
273 (setf (slot-value view 'list-start-indent) t)
274 (setf (slot-value view 'list-start-fmtstr)
275 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
276 (setf (slot-value view 'list-start-value-func)
277 #'htmlformat-list-start-value-func)
278 (setf (slot-value view 'list-end-fmtstr) "</ul></div>~%")
279 (setf (slot-value view 'list-end-indent) t)
280 (setf (slot-value view 'list-end-value-func) nil)
281 (setf (slot-value view 'obj-start-indent) t)
282 (setf (slot-value view 'obj-start-fmtstr) "<li>")
283 (setf (slot-value view 'obj-start-value-func) nil)
284 (setf (slot-value view 'obj-end-indent) t)
285 (setf (slot-value view 'obj-end-fmtstr) "</li>~%")
286 (setf (slot-value view 'obj-end-value-func) nil)
287 (setf (slot-value view 'obj-data-indent) t))
289 (defun initialize-xhtml-view (view)
290 (initialize-text-view view)
291 (setf (slot-value view 'file-start-str) "<html><body>~%")
292 (setf (slot-value view 'file-end-str) "</body><html>~%")
293 (setf (slot-value view 'list-start-indent) t)
294 (setf (slot-value view 'list-start-fmtstr)
295 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
296 (setf (slot-value view 'list-start-value-func)
297 #'htmlformat-list-start-value-func)
298 (setf (slot-value view 'list-end-fmtstr) "</ul></div>~%")
299 (setf (slot-value view 'list-end-indent) t)
300 (setf (slot-value view 'list-end-value-func) nil)
301 (setf (slot-value view 'obj-start-indent) t)
302 (setf (slot-value view 'obj-start-fmtstr) "<li>")
303 (setf (slot-value view 'obj-start-value-func) nil)
304 (setf (slot-value view 'obj-end-indent) t)
305 (setf (slot-value view 'obj-end-fmtstr) "</li>~%")
306 (setf (slot-value view 'obj-end-value-func) nil)
307 (setf (slot-value view 'obj-data-indent) t))
309 (defun xmlformat-list-end-value-func (x)
310 (format nil "~alist" (class-name-of x)))
312 (defun xmlformat-list-start-value-func (x nitems)
313 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
315 (defun initialize-xml-view (view)
316 (initialize-text-view view)
317 (setf (slot-value view 'file-start-str) "") ; (std-xml-header)
318 (setf (slot-value view 'list-start-indent) t)
319 (setf (slot-value view 'list-start-fmtstr) "<~a><title>~a~p:</title> ~%")
320 (setf (slot-value view 'list-start-value-func)
321 #'xmlformat-list-start-value-func)
322 (setf (slot-value view 'list-end-indent) t)
323 (setf (slot-value view 'list-end-fmtstr) "</~a>~%")
324 (setf (slot-value view 'list-end-value-func) #'xmlformat-list-end-value-func)
325 (setf (slot-value view 'obj-start-fmtstr) "<~a>")
326 (setf (slot-value view 'obj-start-value-func) #'class-name-of)
327 (setf (slot-value view 'obj-start-indent) t)
328 (setf (slot-value view 'obj-end-fmtstr) "</~a>~%")
329 (setf (slot-value view 'obj-end-value-func) #'class-name-of)
330 (setf (slot-value view 'obj-end-indent) nil)
331 (setf (slot-value view 'obj-data-indent) nil))
334 ;;; File Start and Ends
336 (defun fmt-file-start (view strm)
337 (awhen (slot-value view 'file-start-str)
340 (defun fmt-file-end (view strm)
341 (awhen (slot-value view 'file-end-str)
344 ;;; List Start and Ends
346 (defun fmt-list-start (obj view strm indent num-items)
347 (when (slot-value view 'list-start-indent)
348 (indent-spaces indent strm))
349 (let-when (fmtstr (slot-value view 'list-start-fmtstr))
350 (let-if (value-func (slot-value view 'list-start-value-func))
351 (apply #'format strm fmtstr
352 (multiple-value-list (funcall value-func
354 (format strm fmtstr))))
356 (defun fmt-list-end (obj view strm indent num-items)
357 (declare (ignore num-items))
358 (when (slot-value view 'list-end-indent)
359 (indent-spaces indent strm))
360 (let-when (fmtstr (slot-value view 'list-end-fmtstr))
361 (let-if (value-func (slot-value view 'list-end-value-func))
362 (apply #'format strm fmtstr (multiple-value-list
363 (funcall value-func obj)))
364 (format strm fmtstr))))
366 ;;; Object Start and Ends
368 (defun fmt-obj-start (obj view strm indent)
369 (when (slot-value view 'obj-start-indent)
370 (indent-spaces indent strm))
371 (let-when (fmtstr (slot-value view 'obj-start-fmtstr))
372 (let-if (value-func (slot-value view 'obj-start-value-func))
373 (apply #'format strm fmtstr (multiple-value-list
374 (funcall value-func obj)))
375 (format strm fmtstr))))
377 (defun fmt-obj-end (obj view strm indent)
378 (when (slot-value view 'obj-end-indent)
379 (indent-spaces indent strm))
380 (let-when (fmtstr (slot-value view 'obj-end-fmtstr))
381 (let-if (value-func (slot-value view 'obj-end-value-func))
382 (apply #'format strm fmtstr (multiple-value-list
383 (funcall value-func obj)))
384 (format strm fmtstr))))
388 (defun make-link-start (view fieldfunc fieldvalue refvars)
389 (format nil "~a\"~a?func=~a~akey=~a~a\""
390 (slot-value view 'link-href-start)
391 (make-url (slot-value view 'link-page-name))
393 (slot-value view 'link-ampersand) fieldvalue
396 (dolist (var refvars)
398 varstr (slot-value view 'ampersand)
399 (format nil "~a=~a" (car var) (cadr var))))
403 (defun make-link-end (obj view fieldname)
404 (declare (ignore obj fieldname))
405 ;;(format nil "~a" (href-end ref))
406 (slot-value view 'link-href-end)
409 (defun fmt-obj-data (obj view strm indent refvars)
410 (when (slot-value view 'obj-data-indent)
411 (indent-spaces indent strm))
412 (if (slot-value view 'link-slots)
413 (fmt-obj-data-with-link obj view strm refvars)
414 (fmt-obj-data-plain obj view strm))
415 (awhen (slot-value view 'obj-data-end-fmtstr)
418 (defun fmt-obj-data-plain (obj view strm)
419 (awhen (slot-value view 'obj-data-value-func)
420 (apply #'format strm (slot-value view 'obj-data-fmtstr)
421 (multiple-value-list (funcall it obj)))))
423 (defun fmt-obj-data-with-link (obj view strm refvars)
424 (let ((refvalues '()))
425 ;; make list of hyperlink link fields for printing to refstr template
426 (dolist (name (slot-value view 'link-slots))
428 (find name (hyperobject-class-hyperlinks obj) :key #'name)))
429 (push (make-link-start view (lookup hyperlink) (slot-value obj name)
430 (append (link-parameters hyperlink) refvars))
432 (push (make-link-end obj view name) refvalues)))
433 (setq refvalues (nreverse refvalues))
434 (apply #'format strm (make-link-data-str obj view) refvalues)))
436 (defun obj-data (obj view)
437 "Returns the objects data as a string. Used by common-graphics outline function"
438 (awhen (slot-value view 'obj-data-value-func)
439 (apply #'format nil (funcall (slot-value view 'obj-data-fmtstr))
440 (multiple-value-list (funcall it obj)))))
442 (defun make-link-data-str (obj view)
443 "Return fmt string for that contains ~a slots for hyperlink link start and end"
444 (awhen (slot-value view 'obj-data-value-func)
445 (apply #'format nil (slot-value view 'obj-data-fmtstr)
446 (multiple-value-list (funcall it obj)))))
448 ;;; Display method for objects
451 (defun load-all-subobjects (objs)
452 "Load all subobjects if they have not already been loaded."
453 (dolist (obj (mklist objs))
454 (dolist (subobj (hyperobject-class-subobjects obj))
455 (awhen (slot-value obj (name-slot subobj))
456 (load-all-subobjects it))))
459 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
461 "Display a single or list of hyperobject-class instances and their subobjects"
462 (let-when (objlist (mklist objs))
463 (let ((nobjs (length objlist)))
464 (fmt-list-start (car objlist) view strm indent nobjs)
465 (dolist (obj objlist)
466 (unless (and filter (not (funcall filter obj)))
467 (fmt-obj-start obj view strm indent)
468 (fmt-obj-data obj view strm (1+ indent) refvars)
469 (when (and subobjects (hyperobject-class-subobjects obj))
470 (dolist (subobj (hyperobject-class-subobjects obj))
471 (aif (slot-value obj (name-slot subobj))
472 (view-hyperobject it (get-category-view (car (mklist it)) category)
473 category strm (1+ indent) filter
474 subobjects refvars))))
475 (fmt-obj-end obj view strm indent)))
476 (fmt-list-end (car objlist) view strm indent nobjs)))
480 (defun view (objs &key (stream *standard-output*) category view
481 filter subobjects refvars file-wrapper)
482 "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
483 (let-when (objlist (mklist objs))
485 (setq view (get-category-view (car objlist) category)))
487 (setq view (default-view (class-of (car objlist)))))
489 (fmt-file-start view stream))
490 (view-hyperobject objlist view category stream 0 filter subobjects refvars)
492 (fmt-file-end view stream)))
498 (defun fmt-comma-integer (i)
499 (format nil "~:d" i))