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.45 2003/05/16 07:35:09 kevin Exp $
12 ;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
13 ;;;; *************************************************************************
15 (in-package :hyperobject)
17 (eval-when (:compile-toplevel :execute)
18 (declaim (optimize (speed 2) (safety 2) (compilation-speed 0) (debug 2))))
21 (defclass object-view ()
22 ((object-class-name :initform nil :initarg :object-class-name
23 :accessor object-class-name
24 :documentation "Name of class of object to be viewed.")
25 (object-class :initform nil :initarg :object-class
26 :accessor object-class
27 :documentation "Class of object to be viewed.")
28 (slots :initform nil :initarg :slots :accessor slots
29 :documentation "List of effective slots for object to be viewed.")
30 (name :initform nil :initarg :name :accessor name
31 :documentation "Name for this view.")
32 (category :initform nil :initarg :category :accessor category
33 :documentation "Category for view. Helpful when want to find a view corresponding to a particular category.")
34 (source-code :initform nil :initarg :source-code :accessor source-code
35 :documentation "Source code for generating view.")
36 (country-language :initform :en :initarg :country-language
37 :documentation "Country's Language for this view.")
39 (file-start-str :type (or string null) :initform nil :initarg :file-start-str
40 :accessor file-start-str)
41 (file-end-str :type (or string null) :initform nil :initarg :file-end-str
42 :accessor file-end-str)
43 (list-start-str-or-func :type (or string function null) :initform nil
44 :initarg :list-start-str-or-func
45 :accessor list-start-str-or-func)
46 (list-start-indent :initform nil :initarg :list-start-indent
47 :accessor list-start-indent)
48 (list-end-str-or-func :type (or string function null) :initform nil
49 :initarg :list-end-str-or-func
50 :accessor list-end-str-or-func)
51 (list-end-indent :initform nil :initarg :list-end-indent
52 :accessor list-end-indent)
53 (obj-start-str-or-func :type (or string function null) :initform nil :initarg :obj-start-str-or-func
54 :accessor obj-start-str-or-func)
55 (obj-start-indent :initform nil :initarg :obj-start-indent
56 :accessor obj-start-indent)
57 (obj-end-str-or-func :type (or string function null) :initform nil :initarg :obj-end-str-or-func
58 :accessor obj-end-str-or-func)
59 (obj-end-indent :initform nil :initarg :obj-end-indent
60 :accessor obj-end-indent)
61 (obj-data-indent :initform nil :initarg :obj-data-indent
62 :accessor obj-data-indent)
63 (obj-data-func :type (or function null) :initform nil
64 :initarg :obj-data-func
65 :accessor obj-data-func)
66 (obj-data-print-code :type (or function null) :initform nil
67 :initarg :obj-data-print-code
68 :accessor obj-data-print-code)
69 (obj-data-end-str :type (or string null) :initform nil
70 :initarg :obj-data-end-str
71 :accessor obj-data-end-str)
72 (link-slots :type list :initform nil
73 :documentation "List of slot names that have hyperlinks"
75 (link-page-name :type (or string null) :initform nil :initarg :link-page-name
76 :accessor link-page-name)
77 (link-href-start :type (or string null) :initform nil :initarg :link-href-start
78 :accessor link-href-start)
79 (link-href-end :type (or string null) :initform nil :initarg :link-href-end
80 :accessor link-href-end)
81 (link-ampersand :type (or string null) :initform nil :initarg :link-ampersand
82 :accessor link-ampersand))
83 (:default-initargs :link-page-name "disp-func1")
84 (:documentation "View class for a hyperobject"))
87 (defun get-category-view (obj category &optional slots)
88 "Find or make a category view for an object"
89 (let ((obj-class (class-of obj)))
91 (default-view obj-class)
92 (aif (find category (views obj-class) :key #'category)
95 (make-instance 'object-view
96 :object-class-name (class-name obj-class)
97 :object-class obj-class
100 (push view (views obj-class))
103 ;;;; *************************************************************************
104 ;;;; Metaclass Intialization
105 ;;;; *************************************************************************
107 (defun finalize-views (cl)
108 "Finalize all views that are given on a objects initialization"
109 (unless (default-print-slots cl)
110 (setf (default-print-slots cl)
111 (mapcar #'slot-definition-name (class-slots cl))))
113 (dolist (view-def (views cl))
114 (push (make-object-view cl view-def) views))
115 (setf (views cl) (nreverse views)))
118 (setf (default-view cl) (make-object-view cl (default-view cl))))
120 (setf (default-view cl) (make-object-view cl (car (views cl)))))
122 (setf (default-view cl) (make-object-view cl :default)))))
124 (defun make-object-view (cl view-def)
125 "Make an object view from a definition. Do nothing if a class is passed so that reinitialization will be a no-op"
127 ((typep view-def 'object-view)
129 ((eq view-def :default)
130 (let* ((name (class-name cl))
131 (view (make-instance 'object-view :name "automatic"
132 :object-class-name name
134 :category :compact-text)))
137 (eval `(make-instance ,view-def)))
139 (error "Invalid parameter to make-object-view: ~S" view-def))))
141 (defmethod initialize-instance :after ((view object-view)
142 &rest initargs &key &allow-other-keys)
143 (initialize-view (object-class view) view))
145 (defun initialize-view (obj-cl view)
146 "Calculate all view slots for a hyperobject class"
149 (initialize-view-by-category obj-cl view))
151 (initialize-view-by-source-code obj-cl view))
153 (setf (category view) :compact-text)
154 (initialize-view-by-category obj-cl view))))
156 (defun initialize-view-by-source-code (obj-cl view)
157 "Initialize a view based upon a source code"
158 (let ((source-code (source-code view)))
159 (error "not implemented")
163 (defmacro write-simple (v s)
166 (write-string ,v ,s))
169 (excl::print-fixnum ,s 10 ,v))
171 (write-string (symbol-name ,v) ,s))
173 (write-string (write-to-string ,v) ,s))))
175 (defun write-ho-value (obj name type formatter cdata strm)
176 (declare (ignorable type))
177 (let* ((slot-data (slot-value obj name))
178 (fmt-data (if formatter
179 (funcall formatter slot-data)
182 (kmrcl:xml-cdata fmt-data)
184 (write-simple data strm)))
187 (defun ppfc-html (title name type formatter cdata print-func)
188 (vector-push-extend '(write-string "<span class=\"" s) print-func)
189 (vector-push-extend `(write-string ,title s) print-func)
190 (vector-push-extend '(write-string "\">" s) print-func)
191 (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
192 (vector-push-extend '(write-string "</span>" s) print-func))
194 (defun ppfc-xml (tag name type formatter cdata print-func)
195 (vector-push-extend '(write-char #\< s) print-func)
196 (vector-push-extend `(write-string ,tag s) print-func)
197 (vector-push-extend '(write-char #\> s) print-func)
198 (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
199 (vector-push-extend '(write-string "</" s) print-func)
200 (vector-push-extend `(write-string ,tag s) print-func)
201 (vector-push-extend '(write-char #\> s) print-func))
203 (defun ppfc-html-labels (label name type formatter cdata print-func)
204 (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
205 (vector-push-extend `(write-string ,label s) print-func)
206 (vector-push-extend '(write-string "</span> " s) print-func)
207 (ppfc-html label name type formatter cdata print-func))
209 (defun ppfc-xhtml-labels (label tag name type formatter cdata print-func)
210 (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
211 (vector-push-extend `(write-string ,label s) print-func)
212 (vector-push-extend '(write-string "</span> " s) print-func)
213 (ppfc-html tag name type formatter cdata print-func))
215 (defun ppfc-xml-labels (label tag name type formatter cdata print-func)
216 (vector-push-extend '(write-string "<label>" s) print-func)
217 (vector-push-extend `(write-string ,label s) print-func)
218 (vector-push-extend '(write-string "</label> " s) print-func)
219 (ppfc-xml tag name type formatter cdata print-func))
221 (defun ppfc-html-link (name type formatter cdata nlink print-func)
222 (declare (fixnum nlink))
223 (vector-push-extend '(write-char #\< s) print-func)
224 (vector-push-extend `(write-string (nth ,(+ nlink nlink) links) s) print-func)
225 (vector-push-extend '(write-char #\> s) print-func)
226 (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
227 (vector-push-extend '(write-string "</" s) print-func)
228 (vector-push-extend `(write-string (nth ,(+ nlink nlink 1) links) s) print-func)
229 (vector-push-extend '(write-char #\> s) print-func))
231 (defun ppfc-html-link-labels (label name type formatter cdata nlink print-func)
232 (vector-push-extend '(write-string "<label>" s) print-func)
233 (vector-push-extend `(write-string ,label s) print-func)
234 (vector-push-extend '(write-string "</label> " s) print-func)
235 (ppfc-html-link name type formatter cdata nlink print-func))
237 (defun push-print-fun-code (category slot nlink print-func)
238 (let* ((formatter (esd-print-formatter slot))
239 (name (slot-definition-name slot))
240 (namestr-lower (string-downcase (symbol-name name)))
241 (xml-namestr (escape-xml-string namestr-lower))
242 (xml-tag (escape-xml-string namestr-lower))
243 (type (slot-value slot 'type))
245 (and (in category :xml :xhtml :xml-link :xhtml-link
246 :xml-labels :ie-xml-labels
247 :xhtml-link-labels :xml-link-labels :ie-xml-link
250 (lisp-type-is-a-string type))))))
251 (hyperlink (esd-hyperlink slot)))
256 `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
257 (:compact-text-labels
258 (vector-push-extend `(write-string ,namestr-lower s) print-func)
259 (vector-push-extend '(write-char #\space s) print-func)
261 `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
263 (ppfc-html namestr-lower name type formatter cdata print-func))
265 (ppfc-xml xml-tag name type formatter cdata print-func))
267 (ppfc-html-labels namestr-lower name type formatter cdata print-func))
269 (ppfc-xhtml-labels xml-namestr namestr-lower name type formatter cdata print-func))
271 (ppfc-xml-labels xml-namestr xml-tag name type formatter cdata print-func))
272 ((or :html-link :xhtml-link)
274 (ppfc-html-link name type formatter cdata nlink print-func)
275 (ppfc-html namestr-lower name type formatter cdata print-func)))
276 ((or :xml-link :ie-xml-link)
278 (ppfc-html-link name type formatter cdata nlink print-func)
279 (ppfc-xml xml-tag name type formatter cdata print-func)))
282 (ppfc-html-link-labels namestr-lower name type formatter cdata nlink
284 (ppfc-html-labels namestr-lower name type formatter cdata print-func)))
287 (ppfc-html-link-labels xml-namestr name type formatter cdata nlink
289 (ppfc-xhtml-labels xml-tag namestr-lower name type formatter cdata
291 ((or :xml-link-labels :ie-xml-link-labels)
293 (ppfc-html-link-labels xml-namestr name type formatter cdata nlink
295 (ppfc-xml-labels xml-tag namestr-lower name type formatter cdata
299 (defun view-has-links-p (view)
300 (in (category view) :html-link :xhtml-link :xml-link :ie-xml-link
301 :html-link-labels :xhtml-link-labels :xml-link-labels
302 :ie-xml-link-labels))
304 (defun initialize-view-by-category (obj-cl view)
305 "Initialize a view based upon a preset category"
306 (unless (in (category view) :compact-text :compact-text-labels
307 :html :html-labels :html-link-labels
308 :xhtml :xhtml-labels :xhtml-link-labels
309 :xhtml-link :html-link
310 :xml :xml-labels :xml-link :ie-xml-link
311 :xml-link-labels :ie-xml-link-labels)
312 (error "Unknown view category ~A" (category view)))
314 (unless (slots view) (setf (slots view) (default-print-slots obj-cl)))
317 (print-func (make-array 10 :fill-pointer 0 :adjustable t)))
319 (do* ((slots (slots view) (cdr slots))
320 (slot-name (car slots) (car slots))
321 (slot (find-slot-by-name obj-cl slot-name)
322 (find-slot-by-name obj-cl slot-name)))
325 (error "Slot ~A is not found in class ~S" slot-name obj-cl))
327 (push-print-fun-code (category view) slot (length links) print-func)
328 (when (> (length slots) 1)
329 (vector-push-extend '(write-char #\space s) print-func))
331 (when (and (view-has-links-p view) (esd-hyperlink slot))
332 (push (slot-definition-name slot) links)))
334 (when (plusp (length print-func))
335 (setf (obj-data-print-code view) `(lambda (x s links)
336 (declare (ignorable links))
337 ,@(map 'list #'identity print-func)))
338 (setf (obj-data-func view)
339 (compile nil (eval (obj-data-print-code view)))))
341 (setf (link-slots view) (nreverse links)))
343 (finalize-view-by-category view)
346 (defun finalize-view-by-category (view)
347 (case (category view)
348 ((or :compact-text :compact-text-labels)
349 (initialize-text-view view))
350 ((or :html :xhtml :html-labels :xhtml-labels)
351 (initialize-html-view view))
352 ((or :xml :xml-labels)
353 (initialize-xml-view view))
354 ((or :html-link :html-link-labels)
355 (initialize-html-view view)
356 (setf (link-href-start view) "a href=")
357 (setf (link-href-end view) "a")
358 (setf (link-ampersand view) "&"))
359 ((or :xhtml-link :xhtml-link-labels)
360 (initialize-html-view view)
361 (setf (link-href-start view) "a href=")
362 (setf (link-href-end view) "a")
363 (setf (link-ampersand view) "&"))
364 ((or :xml-link :xml-link-labels)
365 (initialize-xml-view view)
366 (setf (link-href-start view)
367 "xmllink xlink:type=\"simple\" xlink:href=")
368 (setf (link-href-end view) "xmllink")
369 (setf (link-ampersand view) "&"))
370 ((or :ie-xml-link :ie-xml-link-labels)
371 (initialize-xml-view view)
372 (setf (link-href-start view) "html:a href=")
373 (setf (link-href-end view) "html:a")
374 (setf (link-ampersand view) "&"))))
377 ;;;; *************************************************************************
378 ;;;; View Data Format Section
379 ;;;; *************************************************************************
381 (defun class-name-of (obj)
382 (string-downcase (class-name (class-of obj))))
384 (defvar +newline-string+ (format nil "~%"))
386 (defun write-user-name-maybe-plural (obj nitems strm)
389 (hyperobject-class-user-name-plural obj)
390 (hyperobject-class-user-name obj))
393 (defun initialize-text-view (view)
394 (setf (list-start-str-or-func view)
396 (eval '(lambda (obj nitems strm)
397 (write-user-name-maybe-plural obj nitems strm)
398 (write-char #\: strm)
399 (write-char #\Newline strm)))))
400 (setf (list-start-indent view) t)
401 (setf (obj-data-indent view) t)
402 (setf (obj-data-end-str view) +newline-string+))
404 (defun html-list-start-func (obj nitems strm)
405 (write-string "<p><b>" strm)
406 (write-user-name-maybe-plural obj nitems strm)
407 (write-string ":</b></p><div class=\"" strm)
408 (write-string (class-name-of obj) strm)
409 (write-string "\"><ul>" strm)
410 (write-char #\newline strm))
412 (defun initialize-html-view (view)
413 (initialize-text-view view)
414 (setf (file-start-str view) (format nil "<html><body>~%"))
415 (setf (file-end-str view) (format nil "</body><html>~%"))
416 (setf (list-start-indent view) t)
417 (setf (list-start-str-or-func view) #'html-list-start-func)
418 (setf (list-end-str-or-func view) (format nil "</ul></div>~%"))
419 (setf (list-end-indent view) t)
420 (setf (obj-start-indent view) t)
421 (setf (obj-start-str-or-func view) "<li>")
422 (setf (obj-end-indent view) t)
423 (setf (obj-end-str-or-func view) (format nil "</li>~%"))
424 (setf (obj-data-indent view) nil))
426 (defun initialize-xhtml-view (view)
427 (initialize-text-view view)
428 (setf (file-start-str view) (format nil "<html><body>~%"))
429 (setf (file-end-str view) (format nil "</body><html>~%"))
430 (setf (list-start-indent view) t)
431 (setf (list-start-str-or-func view) #'html-list-start-func)
432 (setf (list-end-str-or-func view) (format nil "</ul></div>~%"))
433 (setf (list-end-indent view) t)
434 (setf (obj-start-indent view) t)
435 (setf (obj-start-str-or-func view) "<li>")
436 (setf (obj-end-indent view) t)
437 (setf (obj-end-str-or-func view) (format nil "</li>~%"))
438 (setf (obj-data-indent view) nil))
440 (defun xmlformat-list-end-func (x strm)
441 (write-string "</" strm)
442 (write-string (class-name-of x) strm)
443 (write-string "list" strm)
444 (write-string ">" strm)
445 (write-char #\newline strm))
447 (defun xmlformat-list-start-func (x nitems strm)
448 (write-char #\< strm)
449 (write-string (class-name-of x) strm)
450 (write-string "list><title>" strm)
451 (write-user-name-maybe-plural x nitems strm)
452 (write-string ":</title>" strm)
453 (write-char #\newline strm))
455 (defun initialize-xml-view (view)
456 (initialize-text-view view)
457 (setf (file-start-str view) "") ; (std-xml-header)
458 (setf (list-start-indent view) t)
459 (setf (list-start-str-or-func view) #'xmlformat-list-start-func)
460 (setf (list-end-indent view) t)
461 (setf (list-end-str-or-func view) #'xmlformat-list-end-func)
462 (setf (obj-start-str-or-func view) (format nil "<~(~a~)>" (object-class-name view)))
463 (setf (obj-start-indent view) t)
464 (setf (obj-end-str-or-func view) (format nil "</~(~a~)>~%" (object-class-name view)))
465 (setf (obj-end-indent view) nil)
466 (setf (obj-data-indent view) nil))
469 ;;; File Start and Ends
471 (defun fmt-file-start (view strm)
472 (awhen (file-start-str view)
473 (write-string it strm)))
475 (defun fmt-file-end (view strm)
476 (awhen (file-end-str view)
477 (write-string it strm)))
479 ;;; List Start and Ends
481 (defun fmt-list-start (obj view strm indent num-items)
482 (when (list-start-indent view)
483 (indent-spaces indent strm))
484 (awhen (list-start-str-or-func view)
486 (write-string it strm)
487 (funcall it obj num-items strm))))
489 (defun fmt-list-end (obj view strm indent num-items)
490 (declare (ignore num-items))
491 (when (list-end-indent view)
492 (indent-spaces indent strm))
493 (awhen (list-end-str-or-func view)
495 (write-string it strm)
496 (funcall it obj strm))))
498 ;;; Object Start and Ends
500 (defun fmt-obj-start (obj view strm indent)
501 (when (obj-start-indent view)
502 (indent-spaces indent strm))
503 (awhen (obj-start-str-or-func view)
505 (write-string it strm)
506 (funcall it obj strm))))
508 (defun fmt-obj-end (obj view strm indent)
509 (when (obj-end-indent view)
510 (indent-spaces indent strm))
511 (awhen (obj-end-str-or-func view)
513 (write-string it strm)
514 (funcall it obj strm))))
519 (defun make-link-start (view fieldfunc fieldvalue refvars)
520 (with-output-to-string (s)
521 (write-string (link-href-start view) s)
523 (write-string (make-url (link-page-name view)) s)
524 (write-string "?func=" s)
525 (write-simple fieldfunc s)
526 (write-string (link-ampersand view) s)
527 (write-string "key=" s)
528 (write-simple fieldvalue s)
529 (dolist (var refvars)
530 (write-string (link-ampersand view) s)
531 (write-simple (car var) s)
533 (write-simple (cdr var) s))
536 (defun make-link-end (obj view fieldname)
537 (declare (ignore obj fieldname))
538 (link-href-end view))
540 (defun fmt-obj-data (obj view strm indent refvars)
541 (when (obj-data-indent view)
542 (indent-spaces indent strm))
543 (if (link-slots view)
544 (fmt-obj-data-with-link obj view strm refvars)
545 (fmt-obj-data-plain obj view strm))
546 (awhen (obj-data-end-str view)
547 (write-string it strm)))
549 (defun fmt-obj-data-plain (obj view strm)
550 (awhen (obj-data-func view)
551 (funcall it obj strm nil)))
553 (defun fmt-obj-data-with-link (obj view strm refvars)
554 (let ((refvalues '()))
555 ;; make list of hyperlink link fields for printing to refstr template
556 (dolist (name (link-slots view))
557 (awhen (find name (hyperobject-class-hyperlinks obj) :key #'name)
558 (push (make-link-start view (lookup it) (slot-value obj name)
559 (append (link-parameters it) refvars))
561 (push (make-link-end obj view name) refvalues)))
562 (funcall (obj-data-func view) obj strm (nreverse refvalues))))
564 (defun obj-data (obj view)
565 "Returns the objects data as a string. Used by common-graphics outline function"
566 (with-output-to-string (s) (fmt-obj-data-plain obj view s)))
568 ;;; Display method for objects
571 (defun load-all-subobjects (objs)
572 "Load all subobjects if they have not already been loaded."
573 (dolist (obj (mklist objs))
574 (dolist (subobj (hyperobject-class-subobjects obj))
575 (awhen (slot-value obj (name-slot subobj))
576 (load-all-subobjects it))))
579 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
581 "Display a single or list of hyperobject-class instances and their subobjects"
582 (declare (fixnum indent))
583 (let-when (objlist (mklist objs))
584 (let ((nobjs (length objlist))
588 (*print-readably* nil)
591 (fmt-list-start (car objlist) view strm indent nobjs)
592 (dolist (obj objlist)
593 (unless (and filter (not (funcall filter obj)))
594 (fmt-obj-start obj view strm indent)
595 (fmt-obj-data obj view strm (1+ indent) refvars)
596 (when (and subobjects (hyperobject-class-subobjects obj))
597 (dolist (subobj (hyperobject-class-subobjects obj))
598 (aif (slot-value obj (name-slot subobj))
599 (view-hyperobject it (get-category-view (car (mklist it)) category)
600 category strm (1+ indent) filter
601 subobjects refvars))))
602 (fmt-obj-end obj view strm indent)))
603 (fmt-list-end (car objlist) view strm indent nobjs)))
607 (defun view (objs &key (stream *standard-output*) category view
608 filter subobjects refvars file-wrapper)
609 "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
610 (let-when (objlist (mklist objs))
612 (setq view (get-category-view (car objlist) category)))
614 (setq view (default-view (class-of (car objlist)))))
616 (fmt-file-start view stream))
617 (view-hyperobject objlist view category stream 0 filter subobjects refvars)
619 (fmt-file-end view stream)))
625 (defun fmt-comma-integer (i)
626 (format nil "~:d" i))