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.4 2002/11/29 23:14:31 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 3) (safety 1) (compilation-speed 0) (debug 3))))
22 ;;;; *************************************************************************
23 ;;;; Metaclass Intialization
24 ;;;; *************************************************************************
26 (defun process-views (cl)
27 "Calculate all view slots for a hyperobject class"
28 (let ((fmtstr-text "")
31 (fmtstr-text-labels "")
32 (fmtstr-html-labels "")
33 (fmtstr-xml-labels "")
36 (fmtstr-html-ref-labels "")
37 (fmtstr-xml-ref-labels "")
41 (classname (class-name cl))
42 (package (symbol-package (class-name cl)))
44 (declare (ignore classname))
45 (check-type (slot-value cl 'print-slots) list)
46 (dolist (slot-name (slot-value cl 'print-slots))
47 (let ((slot (find-slot-by-name cl slot-name)))
49 (error "Slot ~A is not found in class ~S" slot-name cl))
50 (let ((name (slot-definition-name slot))
51 (namestr (symbol-name (slot-definition-name slot)))
52 (namestr-lower (string-downcase (symbol-name (slot-definition-name slot))))
53 (type (slot-value slot 'ho-type))
54 (print-formatter (slot-value slot 'print-formatter))
56 (plain-value-func nil)
57 html-str xml-str html-label-str xml-label-str)
59 (when (or (eql type :integer) (eql type :fixnum))
60 (setq value-fmt "~d"))
62 (when (eql type :boolean)
63 (setq value-fmt "~a"))
66 (setq first-field nil)
68 (string-append fmtstr-text " ")
69 (string-append fmtstr-html " ")
70 (string-append fmtstr-xml " ")
71 (string-append fmtstr-text-labels " ")
72 (string-append fmtstr-html-labels " ")
73 (string-append fmtstr-xml-labels " ")
74 (string-append fmtstr-html-ref " ")
75 (string-append fmtstr-xml-ref " ")
76 (string-append fmtstr-html-ref-labels " ")
77 (string-append fmtstr-xml-ref-labels " ")))
79 (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
80 (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
81 (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
82 (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
84 (string-append fmtstr-text value-fmt)
85 (string-append fmtstr-html html-str)
86 (string-append fmtstr-xml xml-str)
87 (string-append fmtstr-text-labels namestr-lower " " value-fmt)
88 (string-append fmtstr-html-labels html-label-str)
89 (string-append fmtstr-xml-labels xml-label-str)
91 (if (slot-value slot 'hyperlink)
93 (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
94 (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
95 (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
96 (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>")
97 (push (make-instance 'hyperlink :name name
98 :lookup (slot-value slot 'hyperlink))
101 (string-append fmtstr-html-ref html-str)
102 (string-append fmtstr-xml-ref xml-str)
103 (string-append fmtstr-html-ref-labels html-label-str)
104 (string-append fmtstr-xml-ref-labels xml-label-str)))
107 (setq plain-value-func
108 (list `(,print-formatter (slot-value x ',(intern namestr package)))))
109 (setq plain-value-func
110 (list `(slot-value x ',(intern namestr package)))))
111 (setq value-func (append value-func plain-value-func))
113 (if (eql type :cdata)
114 (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
115 (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
118 (setf (slot-value cl 'hyperlinks) hyperlinks)
121 (setq value-func `(lambda (x) (values ,@value-func)))
122 (setq value-func `(lambda () (values))))
123 (setq value-func (compile nil (eval value-func)))
126 (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
127 (setq xmlvalue-func `(lambda () (values))))
128 (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
130 (setf (slot-value cl 'fmtstr-text) fmtstr-text)
131 (setf (slot-value cl 'fmtstr-html) fmtstr-html)
132 (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
133 (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
134 (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
135 (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
136 (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
137 (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
138 (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
139 (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
140 (setf (slot-value cl 'value-func) value-func)
141 (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
145 ;;;; *************************************************************************
146 ;;;; View Data Format Section
147 ;;;; *************************************************************************
149 (defparameter *default-textformat* nil)
150 (defparameter *default-htmlformat* nil)
151 (defparameter *default-htmlrefformat* nil)
152 (defparameter *default-xhtmlformat* nil)
153 (defparameter *default-xhtmlrefformat* nil)
154 (defparameter *default-xmlformat* nil)
155 (defparameter *default-xmlrefformat* nil)
156 (defparameter *default-ie-xmlrefformat* nil)
157 (defparameter *default-nullformat* nil)
158 (defparameter *default-init-format?* nil)
160 (defun make-format-instance (fmt)
161 (unless *default-init-format?*
162 (setq *default-textformat* (make-instance 'textformat))
163 (setq *default-htmlformat* (make-instance 'htmlformat))
164 (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
165 (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
166 (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
167 (setq *default-xmlformat* (make-instance 'xmlformat))
168 (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
169 (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
170 (setq *default-nullformat* (make-instance 'nullformat))
171 (setq *default-init-format?* t))
174 (:text *default-textformat*)
175 (:html *default-htmlformat*)
176 (:htmlref *default-htmlrefformat*)
177 (:xhtml *default-xhtmlformat*)
178 (:xhtmlref *default-xhtmlrefformat*)
179 (:xml *default-xmlformat*)
180 (:xmlref *default-xmlrefformat*)
181 (:ie-xmlref *default-ie-xmlrefformat*)
182 (:null *default-nullformat*)
183 (otherwise *default-textformat*)))
185 ;;;; Output format classes for print hyperobject-classes
187 (defclass dataformat ()
188 ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
189 (file-end-str :type string :initarg :file-end-str :reader file-end-str)
190 (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
191 (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
192 (list-start-indent :initarg :list-start-indent :reader list-start-indent)
193 (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
194 (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
195 (list-end-indent :initarg :list-end-indent :reader list-end-indent)
196 (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
197 (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
198 (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
199 (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
200 (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
201 (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
202 (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
203 (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
204 (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
205 (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
206 (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
207 (link-ref :initarg :link-ref :reader link-ref))
208 (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
209 :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
210 :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
211 :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
212 :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
213 :obj-data-value-func nil :link-ref nil)
214 (:documentation "Parent for all dataformat objects"))
216 (defclass binaryformat (dataformat)
219 (defclass nullformat (dataformat)
222 (defun text-list-start-value-func (obj nitems)
223 (values (hyperobject-class-title obj) nitems))
225 (defclass textformat (dataformat)
227 (:default-initargs :list-start-fmtstr "~a~P:~%"
228 :list-start-value-func #'text-list-start-value-func
231 :obj-data-fmtstr #'hyperobject-class-fmtstr-text
232 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-text-labels
233 :obj-data-end-fmtstr "~%"
234 :obj-data-value-func #'hyperobject-class-value-func))
237 (defun class-name-of (obj)
238 (string-downcase (class-name (class-of obj))))
240 (defun htmlformat-list-start-value-func (x nitems)
241 (values (hyperobject-class-title x) nitems (class-name-of x)))
243 (defclass htmlformat (textformat)
245 (:default-initargs :file-start-str "<html><body>~%"
246 :file-end-str "</body><html>~%"
248 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
249 :list-start-value-func #'htmlformat-list-start-value-func
250 :list-end-fmtstr "</ul></div>~%"
252 :list-end-value-func #'identity
254 :obj-start-fmtstr "<li>"
255 :obj-start-value-func #'identity
257 :obj-end-fmtstr "</li>~%"
258 :obj-end-value-func #'identity
260 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
261 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
262 :obj-data-value-func #'hyperobject-class-value-func))
264 (defclass xhtmlformat (textformat)
266 (:default-initargs :file-start-str "<html><body>~%"
267 :file-end-str "</body><html>~%"
269 :list-start-fmtstr "<p><b>~a~p:</b></p><div class=\"~A\"><ul>~%"
270 :list-start-value-func #'htmlformat-list-start-value-func
271 :list-end-fmtstr "</ul></div>~%"
273 :list-end-value-func #'identity
275 :obj-start-fmtstr "<li>"
276 :obj-start-value-func #'identity
278 :obj-end-fmtstr "</li>~%"
279 :obj-end-value-func #'identity
281 :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
282 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
283 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
286 (defun xmlformat-list-end-value-func (x)
287 (format nil "~alist" (class-name-of x)))
289 (defun xmlformat-list-start-value-func (x nitems)
290 (values (format nil "~alist" (class-name-of x)) (hyperobject-class-title x) nitems))
292 (defclass xmlformat (textformat)
294 (:default-initargs :file-start-str "" ; (std-xml-header)
296 :list-start-fmtstr "<~a><title>~a~p:</title> ~%"
297 :list-start-value-func #'xmlformat-list-start-value-func
299 :list-end-fmtstr "</~a>~%"
300 :list-end-value-func #'xmlformat-list-end-value-func
301 :obj-start-fmtstr "<~a>"
302 :obj-start-value-func #'class-name-of
304 :obj-end-fmtstr "</~a>~%"
305 :obj-end-value-func #'class-name-of
308 :obj-data-fmtstr #'hyperobject-class-fmtstr-xml
309 :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-xml-labels
310 :obj-data-value-func #'hyperobject-class-xmlvalue-func))
312 (defclass link-ref ()
313 ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
314 (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
315 (page-name :type string :initarg :page-name :accessor page-name)
316 (href-head :type string :initarg :href-head :accessor href-head)
317 (href-end :type string :initarg :href-end :accessor href-end)
318 (ampersand :type string :initarg :ampersand :accessor ampersand))
319 (:default-initargs :fmtstr nil
321 :page-name "disp-func1"
322 :href-head nil :href-end nil :ampersand nil)
323 (:documentation "Formatting for a linked hyperlink"))
325 (defclass html-link-ref (link-ref)
327 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
328 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
333 (defclass xhtml-link-ref (link-ref)
335 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
336 :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
341 (defclass xml-link-ref (link-ref)
343 (:default-initargs :fmtstr #'hyperobject-class-fmtstr-xml-ref
344 :fmtstr-labels #'hyperobject-class-fmtstr-xml-ref-labels
345 :href-head "xmllink xlink:type=\"simple\" xlink:href="
348 (:documentation "Mozilla's and W3's idea of a link with XML"))
350 (defclass ie-xml-link-ref (xml-link-ref)
352 (:default-initargs :href-head "html:a href="
354 (:documentation "Internet Explorer's idea of a link with XML"))
357 (defclass htmlrefformat (htmlformat)
359 (:default-initargs :link-ref (make-instance 'html-link-ref)))
361 (defclass xhtmlrefformat (xhtmlformat)
363 (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
365 (defclass xmlrefformat (xmlformat)
367 (:default-initargs :link-ref (make-instance 'xml-link-ref)))
369 (defclass ie-xmlrefformat (xmlformat)
371 (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
374 ;;; File Start and Ends
376 (defgeneric fmt-file-start (fmt s))
377 (defmethod fmt-file-start ((fmt dataformat) (s stream)))
379 (defmethod fmt-file-start ((fmt textformat) (s stream))
380 (aif (file-start-str fmt)
383 (defgeneric fmt-file-end (fmt s))
384 (defmethod fmt-file-end ((fmt textformat) (s stream))
385 (aif (file-end-str fmt)
388 ;;; List Start and Ends
390 (defgeneric fmt-list-start (obj fmt s &optional indent num-items))
391 (defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
392 (if (list-start-indent fmt)
393 (indent-spaces indent s))
394 (aif (list-start-fmtstr fmt)
397 (funcall (list-start-value-func fmt) x num-items)))))
399 (defgeneric fmt-list-end (obj fmt s &optional indent num-items))
400 (defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
401 (declare (ignore num-items))
402 (if (list-end-indent fmt)
403 (indent-spaces indent s))
404 (aif (list-end-fmtstr fmt)
407 (funcall (list-end-value-func fmt) x)))))
409 ;;; Object Start and Ends
411 (defgeneric fmt-obj-start (obj fmt s &optional indent))
412 (defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
413 (if (obj-start-indent fmt)
414 (indent-spaces indent s))
415 (aif (obj-start-fmtstr fmt)
418 (funcall (obj-start-value-func fmt) x)))))
420 (defgeneric fmt-obj-end (obj fmt s &optional indent))
421 (defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
422 (if (obj-end-indent fmt)
423 (indent-spaces indent s))
424 (aif (obj-end-fmtstr fmt)
427 (funcall (obj-end-value-func fmt) x)))))
431 (defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
432 (defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
433 (declare (ignore obj fieldname))
434 (format nil "~a\"~a?func=~a~akey=~a~a\""
435 (href-head ref) (make-url (page-name ref)) fieldfunc
436 (ampersand ref) fieldvalue
439 (dolist (var refvars)
440 (string-append varstr (format nil "~a~a=~a"
441 (ampersand ref) (car var) (cadr var))))
445 (defgeneric make-link-end (obj ref fieldname))
446 (defmethod make-link-end (obj (ref link-ref) fieldname)
447 (declare (ignore obj fieldname))
448 (format nil "~a" (href-end ref))
451 (defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
452 (defmethod fmt-obj-data (x (fmt textformat) s
453 &optional (indent 0) (label nil) (refvars nil))
454 (if (obj-data-indent fmt)
455 (indent-spaces indent s))
457 (fmt-obj-data-with-ref x fmt s label refvars)
458 (fmt-obj-data-plain x fmt s label))
459 (aif (obj-data-end-fmtstr fmt)
462 (defgeneric fmt-obj-data-plain (obj fmt s label))
463 (defmethod fmt-obj-data-plain (x (fmt textformat) s label)
466 (funcall (obj-data-fmtstr-labels fmt) x)
468 (funcall (funcall (obj-data-value-func fmt) x) x)))
469 (apply #'format s (funcall (obj-data-fmtstr fmt) x)
471 (funcall (funcall (obj-data-value-func fmt) x) x)))))
473 (defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
474 (defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
475 (let ((refstr (make-ref-data-str x fmt label))
479 (funcall (funcall (obj-data-value-func fmt) x) x))))
481 ;; make list of hyperlink link fields for printing to refstr template
482 (dolist (ref (hyperobject-class-hyperlinks x))
484 (make-link-start x (link-ref fmt) (name ref) (lookup ref)
485 (nth (position (name ref)
486 (hyperobject-class-fields x)
488 (slot-definition-name x)))
490 (append (link-parameters ref) refvars)))
491 (link-end (make-link-end x (link-ref fmt) (name ref))))
492 (push link-start refvalues)
493 (push link-end refvalues)))
494 (setq refvalues (nreverse refvalues))
496 (apply #'format s refstr refvalues)))
498 (defgeneric obj-data (obj))
499 (defmethod obj-data (x)
500 "Returns the objects data as a string. Used by common-graphics outline function"
501 (let ((fmt (make-format-instance :text)))
502 (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
504 (funcall (funcall (obj-data-value-func fmt) x) x)))))
506 (defgeneric make-ref-data-str (obj fmt &optional label))
507 (defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
508 "Return fmt string for that contains ~a slots for hyperlink link start and end"
509 (unless (link-ref fmt)
510 (error "fmt does not contain a link-ref"))
513 (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
515 (funcall (funcall (obj-data-value-func fmt) x) x)))
516 (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
517 (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
520 ;;; Display method for objects
523 (defgeneric load-all-subobjects (objs))
524 (defmethod load-all-subobjects (objs)
525 "Load all subobjects if they have not already been loaded."
527 (let ((objlist (mklist objs)))
528 (dolist (obj objlist)
529 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
530 (dolist (child-obj it) ;; for each child function
531 (awhen (funcall (reader child-obj) obj)
532 (load-all-subobjects it))))))
535 (defgeneric view-hyperobject (objs fmt strm
536 &optional label english-only-function
537 indent subobjects refvars))
539 (defmethod view-hyperobject (objs (fmt dataformat) (strm stream)
540 &optional (label nil) (indent 0)
541 (english-only-function nil)
542 (subobjects nil) (refvars nil))
543 "Display a single or list of hyperobject-class instances and their subobjects"
545 (setq objs (mklist objs))
546 (let ((nobjs (length objs)))
547 (fmt-list-start (car objs) fmt strm indent nobjs)
549 (unless (and english-only-function
550 (multiple-value-bind (eng term) (funcall english-only-function obj)
551 (and term (not eng))))
552 (fmt-obj-start obj fmt strm indent)
553 (fmt-obj-data obj fmt strm (1+ indent) label refvars)
555 (awhen (hyperobject-class-subobjects obj) ;; access list of functions
556 (dolist (child-obj it) ;; for each child function
557 (awhen (funcall (reader child-obj) obj) ;; access set of child objects
558 (view-hyperobject it fmt strm label
559 (1+ indent) english-only-function
560 subobjects refvars)))))
561 (fmt-obj-end obj fmt strm indent)))
562 (fmt-list-end (car objs) fmt strm indent nobjs))
566 (defun view (objs &key (os *standard-output*) (format :text)
567 (label nil) (english-only-function nil)
568 (subobjects nil) (file-wrapper t) (refvars nil))
569 "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
570 (let ((fmt (make-format-instance format)))
572 (fmt-file-start fmt os))
574 (view-hyperobject objs fmt os label 0 english-only-function subobjects refvars))
576 (fmt-file-end fmt os)))