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.49 2003/05/30 18:46:35 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-printer :type (or string function null) :initform nil
44 :initarg :list-start-printer
45 :accessor list-start-printer)
46 (list-start-indent :initform nil :initarg :list-start-indent
47 :accessor list-start-indent)
48 (list-end-printer :type (or string function null) :initform nil
49 :initarg :list-end-printer
50 :accessor list-end-printer)
51 (list-end-indent :initform nil :initarg :list-end-indent
52 :accessor list-end-indent)
53 (obj-start-printer :type (or string function null) :initform nil :initarg :obj-start-printer
54 :accessor obj-start-printer)
55 (obj-start-indent :initform nil :initarg :obj-start-indent
56 :accessor obj-start-indent)
57 (obj-end-printer :type (or string function null) :initform nil :initarg :obj-end-printer
58 :accessor obj-end-printer)
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-printer :type (or function null) :initform nil
64 :initarg :obj-data-printer
65 :accessor obj-data-printer)
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 (apply #'make-instance 'object-view 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 (write-xml-cdata fmt-data strm)
183 (write-simple fmt-data strm))))
185 (defun ppfc-html (title name type formatter cdata print-func)
186 (vector-push-extend '(write-string "<span class=\"" s) print-func)
187 (vector-push-extend `(write-string ,title s) print-func)
188 (vector-push-extend '(write-string "\">" s) print-func)
189 (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
190 (vector-push-extend '(write-string "</span>" s) print-func))
192 (defun ppfc-xml (tag name type formatter cdata print-func)
193 (vector-push-extend '(write-char #\< s) print-func)
194 (vector-push-extend `(write-string ,tag s) print-func)
195 (vector-push-extend '(write-char #\> s) print-func)
196 (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
197 (vector-push-extend '(write-string "</" s) print-func)
198 (vector-push-extend `(write-string ,tag s) print-func)
199 (vector-push-extend '(write-char #\> s) print-func))
201 (defun ppfc-html-labels (label name type formatter cdata print-func)
202 (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
203 (vector-push-extend `(write-string ,label s) print-func)
204 (vector-push-extend '(write-string "</span> " s) print-func)
205 (ppfc-html label name type formatter cdata print-func))
207 (defun ppfc-xhtml-labels (label tag name type formatter cdata print-func)
208 (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
209 (vector-push-extend `(write-string ,label s) print-func)
210 (vector-push-extend '(write-string "</span> " s) print-func)
211 (ppfc-html tag name type formatter cdata print-func))
213 (defun ppfc-xml-labels (label tag name type formatter cdata print-func)
214 (vector-push-extend '(write-string "<label>" s) print-func)
215 (vector-push-extend `(write-string ,label s) print-func)
216 (vector-push-extend '(write-string "</label> " s) print-func)
217 (ppfc-xml tag name type formatter cdata print-func))
219 (defun ppfc-html-link (name type formatter cdata nlink print-func)
220 (declare (fixnum nlink))
221 (vector-push-extend '(write-char #\< s) print-func)
222 (vector-push-extend `(write-string (nth ,(+ nlink nlink) links) s) print-func)
223 (vector-push-extend '(write-char #\> s) print-func)
224 (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
225 (vector-push-extend '(write-string "</" s) print-func)
226 (vector-push-extend `(write-string (nth ,(+ nlink nlink 1) links) s) print-func)
227 (vector-push-extend '(write-char #\> s) print-func))
229 (defun ppfc-html-link-labels (label name type formatter cdata nlink print-func)
230 (vector-push-extend '(write-string "<label>" s) print-func)
231 (vector-push-extend `(write-string ,label s) print-func)
232 (vector-push-extend '(write-string "</label> " s) print-func)
233 (ppfc-html-link name type formatter cdata nlink print-func))
235 (defun push-print-fun-code (category slot nlink print-func)
236 (let* ((formatter (esd-print-formatter slot))
237 (name (slot-definition-name slot))
238 (user-name (esd-user-name slot))
239 (xml-user-name (escape-xml-string user-name))
240 (xml-tag (escape-xml-string user-name))
241 (type (slot-value slot 'type))
243 (and (in category :xml :xhtml :xml-link :xhtml-link
244 :xml-labels :ie-xml-labels
245 :xhtml-link-labels :xml-link-labels :ie-xml-link
248 (lisp-type-is-a-string type))))))
249 (hyperlink (esd-hyperlink slot)))
254 `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
255 (:compact-text-labels
256 (vector-push-extend `(write-string ,user-name s) print-func)
257 (vector-push-extend '(write-char #\space s) print-func)
259 `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
261 (ppfc-html user-name name type formatter cdata print-func))
263 (ppfc-xml xml-tag name type formatter cdata print-func))
265 (ppfc-html-labels user-name name type formatter cdata print-func))
267 (ppfc-xhtml-labels xml-user-name user-name name type formatter cdata print-func))
269 (ppfc-xml-labels xml-user-name xml-tag name type formatter cdata print-func))
270 ((or :html-link :xhtml-link)
272 (ppfc-html-link name type formatter cdata nlink print-func)
273 (ppfc-html user-name name type formatter cdata print-func)))
274 ((or :xml-link :ie-xml-link)
276 (ppfc-html-link name type formatter cdata nlink print-func)
277 (ppfc-xml xml-tag name type formatter cdata print-func)))
280 (ppfc-html-link-labels user-name name type formatter cdata nlink
282 (ppfc-html-labels user-name name type formatter cdata print-func)))
285 (ppfc-html-link-labels xml-user-name name type formatter cdata nlink
287 (ppfc-xhtml-labels xml-tag user-name name type formatter cdata
289 ((or :xml-link-labels :ie-xml-link-labels)
291 (ppfc-html-link-labels xml-user-name name type formatter cdata nlink
293 (ppfc-xml-labels xml-tag user-name name type formatter cdata
297 (defun view-has-links-p (view)
298 (in (category view) :html-link :xhtml-link :xml-link :ie-xml-link
299 :html-link-labels :xhtml-link-labels :xml-link-labels
300 :ie-xml-link-labels))
302 (defun initialize-view-by-category (obj-cl view)
303 "Initialize a view based upon a preset category"
304 (unless (in (category view) :compact-text :compact-text-labels
305 :html :html-labels :html-link-labels
306 :xhtml :xhtml-labels :xhtml-link-labels
307 :xhtml-link :html-link
308 :xml :xml-labels :xml-link :ie-xml-link
309 :xml-link-labels :ie-xml-link-labels)
310 (error "Unknown view category ~A" (category view)))
312 (unless (slots view) (setf (slots view) (default-print-slots obj-cl)))
315 (print-func (make-array 10 :fill-pointer 0 :adjustable t)))
317 (do* ((slots (slots view) (cdr slots))
318 (slot-name (car slots) (car slots))
319 (slot (find-slot-by-name obj-cl slot-name)
320 (find-slot-by-name obj-cl slot-name)))
323 (error "Slot ~A is not found in class ~S" slot-name obj-cl))
325 (push-print-fun-code (category view) slot (length links) print-func)
326 (when (> (length slots) 1)
327 (vector-push-extend '(write-char #\space s) print-func))
329 (when (and (view-has-links-p view) (esd-hyperlink slot))
330 (push (slot-definition-name slot) links)))
332 (vector-push-extend 'x print-func) ;; return object
333 (setf (obj-data-print-code view) `(lambda (x s links)
334 (declare (ignorable s links))
335 ,@(map 'list #'identity print-func)))
336 (setf (obj-data-printer view)
337 (compile nil (eval (obj-data-print-code view))))
339 (setf (link-slots view) (nreverse links)))
341 (finalize-view-by-category view)
344 (defun finalize-view-by-category (view)
345 (case (category view)
346 ((or :compact-text :compact-text-labels)
347 (initialize-text-view view))
348 ((or :html :xhtml :html-labels :xhtml-labels)
349 (initialize-html-view view))
350 ((or :xml :xml-labels)
351 (initialize-xml-view view))
352 ((or :html-link :html-link-labels)
353 (initialize-html-view view)
354 (setf (link-href-start view) "a href=")
355 (setf (link-href-end view) "a")
356 (setf (link-ampersand view) "&"))
357 ((or :xhtml-link :xhtml-link-labels)
358 (initialize-html-view view)
359 (setf (link-href-start view) "a href=")
360 (setf (link-href-end view) "a")
361 (setf (link-ampersand view) "&"))
362 ((or :xml-link :xml-link-labels)
363 (initialize-xml-view view)
364 (setf (link-href-start view)
365 "xmllink xlink:type=\"simple\" xlink:href=")
366 (setf (link-href-end view) "xmllink")
367 (setf (link-ampersand view) "&"))
368 ((or :ie-xml-link :ie-xml-link-labels)
369 (initialize-xml-view view)
370 (setf (link-href-start view) "html:a href=")
371 (setf (link-href-end view) "html:a")
372 (setf (link-ampersand view) "&"))))
375 ;;;; *************************************************************************
376 ;;;; View Data Format Section
377 ;;;; *************************************************************************
379 (defun class-name-of (obj)
380 (string-downcase (class-name (class-of obj))))
382 (defvar +newline-string+ (format nil "~%"))
384 (defun write-user-name-maybe-plural (obj nitems strm)
387 (hyperobject-class-user-name-plural obj)
388 (hyperobject-class-user-name obj))
391 (defun initialize-text-view (view)
392 (setf (list-start-printer view)
394 (eval '(lambda (obj nitems strm)
395 (write-user-name-maybe-plural obj nitems strm)
396 (write-char #\: strm)
397 (write-char #\Newline strm)))))
398 (setf (list-start-indent view) t)
399 (setf (obj-data-indent view) t)
400 (setf (obj-data-end-str view) +newline-string+))
402 (defun html-list-start-func (obj nitems strm)
403 (write-string "<p><b>" strm)
404 (write-user-name-maybe-plural obj nitems strm)
405 (write-string ":</b></p><div class=\"" strm)
406 (write-string (class-name-of obj) strm)
407 (write-string "\"><ul>" strm)
408 (write-char #\newline strm))
410 (defun initialize-html-view (view)
411 (initialize-text-view view)
412 (setf (file-start-str view) (format nil "<html><body>~%"))
413 (setf (file-end-str view) (format nil "</body><html>~%"))
414 (setf (list-start-indent view) t)
415 (setf (list-start-printer view) #'html-list-start-func)
416 (setf (list-end-printer view) (format nil "</ul></div>~%"))
417 (setf (list-end-indent view) t)
418 (setf (obj-start-indent view) t)
419 (setf (obj-start-printer view) "<li>")
420 (setf (obj-end-indent view) t)
421 (setf (obj-end-printer view) (format nil "</li>~%"))
422 (setf (obj-data-indent view) nil))
424 (defun initialize-xhtml-view (view)
425 (initialize-text-view view)
426 (setf (file-start-str view) (format nil "<html><body>~%"))
427 (setf (file-end-str view) (format nil "</body><html>~%"))
428 (setf (list-start-indent view) t)
429 (setf (list-start-printer view) #'html-list-start-func)
430 (setf (list-end-printer view) (format nil "</ul></div>~%"))
431 (setf (list-end-indent view) t)
432 (setf (obj-start-indent view) t)
433 (setf (obj-start-printer view) "<li>")
434 (setf (obj-end-indent view) t)
435 (setf (obj-end-printer view) (format nil "</li>~%"))
436 (setf (obj-data-indent view) nil))
438 (defun xmlformat-list-end-func (x strm)
439 (write-string "</" strm)
440 (write-string (class-name-of x) strm)
441 (write-string "list" strm)
442 (write-string ">" strm)
443 (write-char #\newline strm))
445 (defun xmlformat-list-start-func (x nitems strm)
446 (write-char #\< strm)
447 (write-string (class-name-of x) strm)
448 (write-string "list><title>" strm)
449 (write-user-name-maybe-plural x nitems strm)
450 (write-string ":</title>" strm)
451 (write-char #\newline strm))
453 (defun initialize-xml-view (view)
454 (initialize-text-view view)
455 (setf (file-start-str view) "") ; (std-xml-header)
456 (setf (list-start-indent view) t)
457 (setf (list-start-printer view) #'xmlformat-list-start-func)
458 (setf (list-end-indent view) t)
459 (setf (list-end-printer view) #'xmlformat-list-end-func)
460 (setf (obj-start-printer view) (format nil "<~(~a~)>" (object-class-name view)))
461 (setf (obj-start-indent view) t)
462 (setf (obj-end-printer view) (format nil "</~(~a~)>~%" (object-class-name view)))
463 (setf (obj-end-indent view) nil)
464 (setf (obj-data-indent view) nil))
467 ;;; File Start and Ends
469 (defun fmt-file-start (view strm)
470 (awhen (file-start-str view)
471 (write-string it strm)))
473 (defun fmt-file-end (view strm)
474 (awhen (file-end-str view)
475 (write-string it strm)))
477 ;;; List Start and Ends
479 (defun fmt-list-start (obj view strm indent num-items)
480 (when (list-start-indent view)
481 (indent-spaces indent strm))
482 (awhen (list-start-printer view)
484 (write-string it strm)
485 (funcall it obj num-items strm))))
487 (defun fmt-list-end (obj view strm indent num-items)
488 (declare (ignore num-items))
489 (when (list-end-indent view)
490 (indent-spaces indent strm))
491 (awhen (list-end-printer view)
493 (write-string it strm)
494 (funcall it obj strm))))
496 ;;; Object Start and Ends
498 (defun fmt-obj-start (obj view strm indent)
499 (when (obj-start-indent view)
500 (indent-spaces indent strm))
501 (awhen (obj-start-printer view)
503 (write-string it strm)
504 (funcall it obj strm))))
506 (defun fmt-obj-end (obj view strm indent)
507 (when (obj-end-indent view)
508 (indent-spaces indent strm))
509 (awhen (obj-end-printer view)
511 (write-string it strm)
512 (funcall it obj strm))))
517 (defun make-link-start (view fieldfunc fieldvalue refvars)
518 (with-output-to-string (s)
519 (write-string (link-href-start view) s)
521 (write-string (make-url (link-page-name view)) s)
522 (write-string "?func=" s)
523 (write-simple fieldfunc s)
524 (write-string (link-ampersand view) s)
525 (write-string "key=" s)
526 (write-simple fieldvalue s)
527 (dolist (var refvars)
528 (write-string (link-ampersand view) s)
529 (write-simple (car var) s)
531 (write-simple (cdr var) s))
534 (defun make-link-end (obj view fieldname)
535 (declare (ignore obj fieldname))
536 (link-href-end view))
538 (defun fmt-obj-data (obj view strm indent refvars)
539 (when (obj-data-indent view)
540 (indent-spaces indent strm))
541 (if (link-slots view)
542 (fmt-obj-data-with-link obj view strm refvars)
543 (fmt-obj-data-plain obj view strm))
544 (awhen (obj-data-end-str view)
545 (write-string it strm)))
547 (defun fmt-obj-data-plain (obj view strm)
548 (awhen (obj-data-printer view)
549 (funcall it obj strm nil)))
551 (defun fmt-obj-data-with-link (obj view strm refvars)
552 (let ((refvalues '()))
553 ;; make list of hyperlink link fields for printing to refstr template
554 (dolist (name (link-slots view))
555 (awhen (find name (hyperobject-class-hyperlinks obj) :key #'name)
556 (push (make-link-start view (lookup it) (slot-value obj name)
557 (append (link-parameters it) refvars))
559 (push (make-link-end obj view name) refvalues)))
560 (funcall (obj-data-printer view) obj strm (nreverse refvalues))))
562 (defun obj-data (obj view)
563 "Returns the objects data as a string. Used by common-graphics outline function"
564 (with-output-to-string (s) (fmt-obj-data-plain obj view s)))
566 ;;; Display method for objects
569 (defun load-all-subobjects (objs)
570 "Load all subobjects if they have not already been loaded."
571 (dolist (obj (mklist objs))
572 (dolist (subobj (hyperobject-class-subobjects obj))
573 (awhen (slot-value obj (name-slot subobj))
574 (load-all-subobjects it))))
577 (defun view-hyperobject (objs view category strm &optional (indent 0) filter
579 "Display a single or list of hyperobject-class instances and their subobjects"
580 (declare (fixnum indent))
581 (let-when (objlist (mklist objs))
582 (let ((nobjs (length objlist))
586 (*print-readably* nil)
589 (fmt-list-start (car objlist) view strm indent nobjs)
590 (dolist (obj objlist)
591 (unless (and filter (not (funcall filter obj)))
592 (fmt-obj-start obj view strm indent)
593 (fmt-obj-data obj view strm (1+ indent) refvars)
594 (when (and subobjects (hyperobject-class-subobjects obj))
595 (dolist (subobj (hyperobject-class-subobjects obj))
596 (aif (slot-value obj (name-slot subobj))
597 (view-hyperobject it (get-category-view (car (mklist it)) category)
598 category strm (1+ indent) filter
599 subobjects refvars))))
600 (fmt-obj-end obj view strm indent)))
601 (fmt-list-end (car objlist) view strm indent nobjs)))
605 (defun view (objs &key (stream *standard-output*) category view
606 filter subobjects refvars file-wrapper)
607 "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
608 (let-when (objlist (mklist objs))
610 (setq view (get-category-view (car objlist) category)))
612 (setq view (default-view (class-of (car objlist)))))
614 (fmt-file-start view stream))
615 (view-hyperobject objlist view category stream 0 filter subobjects refvars)
617 (fmt-file-end view stream)))
623 (defun fmt-comma-integer (i)
624 (format nil "~:d" i))