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.13 2002/12/13 07:33:54 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) (format nil "~%"))
270 (defun initialize-html-view (view)
271 (initialize-text-view view)
272 (setf (slot-value view 'file-start-str) (format nil "<html><body>~%"))
273 (setf (slot-value view 'file-end-str) (format nil "</body><html>~%"))
274 (setf (slot-value view 'list-start-indent) t)
275 (setf (slot-value view 'list-start-fmtstr)
276 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
277 (setf (slot-value view 'list-start-value-func)
278 #'htmlformat-list-start-value-func)
279 (setf (slot-value view 'list-end-fmtstr) (format nil "</ul></div>~%"))
280 (setf (slot-value view 'list-end-indent) t)
281 (setf (slot-value view 'list-end-value-func) nil)
282 (setf (slot-value view 'obj-start-indent) t)
283 (setf (slot-value view 'obj-start-fmtstr) "<li>")
284 (setf (slot-value view 'obj-start-value-func) nil)
285 (setf (slot-value view 'obj-end-indent) t)
286 (setf (slot-value view 'obj-end-fmtstr) (format nil "</li>~%"))
287 (setf (slot-value view 'obj-end-value-func) nil)
288 (setf (slot-value view 'obj-data-indent) t))
290 (defun initialize-xhtml-view (view)
291 (initialize-text-view view)
292 (setf (slot-value view 'file-start-str) (format nil "<html><body>~%"))
293 (setf (slot-value view 'file-end-str) (format nil "</body><html>~%"))
294 (setf (slot-value view 'list-start-indent) t)
295 (setf (slot-value view 'list-start-fmtstr)
296 "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%")
297 (setf (slot-value view 'list-start-value-func)
298 #'htmlformat-list-start-value-func)
299 (setf (slot-value view 'list-end-fmtstr) (format nil "</ul></div>~%"))
300 (setf (slot-value view 'list-end-indent) t)
301 (setf (slot-value view 'list-end-value-func) nil)
302 (setf (slot-value view 'obj-start-indent) t)
303 (setf (slot-value view 'obj-start-fmtstr) "<li>")
304 (setf (slot-value view 'obj-start-value-func) nil)
305 (setf (slot-value view 'obj-end-indent) t)
306 (setf (slot-value view 'obj-end-fmtstr) (format nil "</li>~%"))
307 (setf (slot-value view 'obj-end-value-func) nil)
308 (setf (slot-value view 'obj-data-indent) t))
310 (defun xmlformat-list-end-value-func (x)
311 (format nil "~alist" (class-name-of x)))
313 (defun xmlformat-list-start-value-func (x nitems)
314 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-user-name x) nitems))
316 (defun initialize-xml-view (view)
317 (initialize-text-view view)
318 (setf (slot-value view 'file-start-str) "") ; (std-xml-header)
319 (setf (slot-value view 'list-start-indent) t)
320 (setf (slot-value view 'list-start-fmtstr) "<~a><title>~a~p:</title> ~%")
321 (setf (slot-value view 'list-start-value-func)
322 #'xmlformat-list-start-value-func)
323 (setf (slot-value view 'list-end-indent) t)
324 (setf (slot-value view 'list-end-fmtstr) "</~a>~%")
325 (setf (slot-value view 'list-end-value-func) #'xmlformat-list-end-value-func)
326 (setf (slot-value view 'obj-start-fmtstr) (format nil "<~(~a~)>" (slot-value view 'object-class)))
327 (setf (slot-value view 'obj-start-indent) t)
328 (setf (slot-value view 'obj-end-fmtstr) (format nil "</~(~a~)>~%" (slot-value view 'object-class)))
329 (setf (slot-value view 'obj-end-indent) nil)
330 (setf (slot-value view 'obj-data-indent) nil))
333 ;;; File Start and Ends
335 (defun fmt-file-start (view strm)
336 (awhen (slot-value view 'file-start-str)
337 (write-string it strm)))
339 (defun fmt-file-end (view strm)
340 (awhen (slot-value view 'file-end-str)
341 (write-string it strm)))
343 ;;; List Start and Ends
345 (defun fmt-list-start (obj view strm indent num-items)
346 (when (slot-value view 'list-start-indent)
347 (indent-spaces indent strm))
348 (let-when (fmtstr (slot-value view 'list-start-fmtstr))
349 (let-if (value-func (slot-value view 'list-start-value-func))
350 (apply #'format strm fmtstr
351 (multiple-value-list (funcall value-func
353 (write-string fmtstr strm))))
355 (defun fmt-list-end (obj view strm indent num-items)
356 (declare (ignore num-items))
357 (when (slot-value view 'list-end-indent)
358 (indent-spaces indent strm))
359 (let-when (fmtstr (slot-value view 'list-end-fmtstr))
360 (let-if (value-func (slot-value view 'list-end-value-func))
361 (apply #'format strm fmtstr (multiple-value-list
362 (funcall value-func obj)))
363 (write-string fmtstr strm))))
365 ;;; Object Start and Ends
367 (defun fmt-obj-start (obj view strm indent)
368 (when (slot-value view 'obj-start-indent)
369 (indent-spaces indent strm))
370 (let-when (fmtstr (slot-value view 'obj-start-fmtstr))
371 (let-if (value-func (slot-value view 'obj-start-value-func))
372 (apply #'format strm fmtstr (multiple-value-list
373 (funcall value-func obj)))
374 (write-string fmtstr strm))))
376 (defun fmt-obj-end (obj view strm indent)
377 (when (slot-value view 'obj-end-indent)
378 (indent-spaces indent strm))
379 (let-when (fmtstr (slot-value view 'obj-end-fmtstr))
380 (let-if (value-func (slot-value view 'obj-end-value-func))
381 (apply #'format strm fmtstr (multiple-value-list
382 (funcall value-func obj)))
383 (write-string fmtstr strm))))
387 (defun make-link-start (view fieldfunc fieldvalue refvars)
388 (format nil "~a\"~a?func=~a~akey=~a~a\""
389 (slot-value view 'link-href-start)
390 (make-url (slot-value view 'link-page-name))
392 (slot-value view 'link-ampersand) fieldvalue
395 (dolist (var refvars)
397 varstr (slot-value view 'ampersand)
398 (format nil "~a=~a" (car var) (cadr var))))
402 (defun make-link-end (obj view fieldname)
403 (declare (ignore obj fieldname))
404 ;;(format nil "~a" (href-end ref))
405 (slot-value view 'link-href-end)
408 (defun fmt-obj-data (obj view strm indent refvars)
409 (when (slot-value view 'obj-data-indent)
410 (indent-spaces indent strm))
411 (if (slot-value view 'link-slots)
412 (fmt-obj-data-with-link obj view strm refvars)
413 (fmt-obj-data-plain obj view strm))
414 (awhen (slot-value view 'obj-data-end-fmtstr)
417 (defun fmt-obj-data-plain (obj view strm)
418 (awhen (slot-value view 'obj-data-value-func)
419 (apply #'format strm (slot-value view 'obj-data-fmtstr)
420 (multiple-value-list (funcall it obj)))))
422 (defun fmt-obj-data-with-link (obj view strm refvars)
423 (let ((refvalues '()))
424 ;; make list of hyperlink link fields for printing to refstr template
425 (dolist (name (slot-value view 'link-slots))
427 (find name (hyperobject-class-hyperlinks obj) :key #'name)))
428 (push (make-link-start view (lookup hyperlink) (slot-value obj name)
429 (append (link-parameters hyperlink) refvars))
431 (push (make-link-end obj view name) refvalues)))
432 (setq refvalues (nreverse refvalues))
433 (apply #'format strm (make-link-data-str obj view) refvalues)))
435 (defun obj-data (obj view)
436 "Returns the objects data as a string. Used by common-graphics outline function"
437 (awhen (slot-value view 'obj-data-value-func)
438 (apply #'format nil (funcall (slot-value view 'obj-data-fmtstr))
439 (multiple-value-list (funcall it obj)))))
441 (defun make-link-data-str (obj view)
442 "Return fmt string for that contains ~a slots for hyperlink link start and end"
443 (awhen (slot-value view 'obj-data-value-func)
444 (apply #'format nil (slot-value view 'obj-data-fmtstr)
445 (multiple-value-list (funcall it obj)))))
447 ;;; Display method for objects
450 (defun load-all-subobjects (objs)
451 "Load all subobjects if they have not already been loaded."
452 (dolist (obj (mklist objs))
453 (dolist (subobj (hyperobject-class-subobjects obj))
454 (awhen (slot-value obj (name-slot subobj))
455 (load-all-subobjects it))))
458 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
460 "Display a single or list of hyperobject-class instances and their subobjects"
461 (let-when (objlist (mklist objs))
462 (let ((nobjs (length objlist))
466 (*print-readably* nil)
469 (fmt-list-start (car objlist) view strm indent nobjs)
470 (dolist (obj objlist)
471 (unless (and filter (not (funcall filter obj)))
472 (fmt-obj-start obj view strm indent)
473 (fmt-obj-data obj view strm (1+ indent) refvars)
474 (when (and subobjects (hyperobject-class-subobjects obj))
475 (dolist (subobj (hyperobject-class-subobjects obj))
476 (aif (slot-value obj (name-slot subobj))
477 (view-hyperobject it (get-category-view (car (mklist it)) category)
478 category strm (1+ indent) filter
479 subobjects refvars))))
480 (fmt-obj-end obj view strm indent)))
481 (fmt-list-end (car objlist) view strm indent nobjs)))
485 (defun view (objs &key (stream *standard-output*) category view
486 filter subobjects refvars file-wrapper)
487 "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
488 (let-when (objlist (mklist objs))
490 (setq view (get-category-view (car objlist) category)))
492 (setq view (default-view (class-of (car objlist)))))
494 (fmt-file-start view stream))
495 (view-hyperobject objlist view category stream 0 filter subobjects refvars)
497 (fmt-file-end view stream)))
503 (defun fmt-comma-integer (i)
504 (format nil "~:d" i))