;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*- ;;;; ************************************************************************* ;;;; FILE IDENTIFICATION ;;;; ;;;; Name: views.lisp ;;;; Purpose: View methods for Hyperobjects ;;;; Programmer: Kevin M. Rosenberg ;;;; Date Started: Apr 2000 ;;;; ;;;; $Id: views.lisp,v 1.12 2002/12/13 05:44:19 kevin Exp $ ;;;; ;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg ;;;; ;;;; ************************************************************************* (in-package :hyperobject) (eval-when (:compile-toplevel :execute) (declaim (optimize (speed 2) (safety 2) (compilation-speed 0) (debug 2)))) (defclass object-view () ((object-class :initform nil :initarg :object-class :documentation "Name of class for object to be viewed.") (slots :initform nil :initarg :slots :documentation "List of effective slots for object to be viewed.") (name :initform nil :initarg :name :documentation "Name for this view.") (category :initform nil :initarg :category :reader category :documentation "Category for view. Helpful when want to find a view corresponding to a particular category.") (source-code :initform nil :initarg :source-code :documentation "Source code for generating view.") (country-language :initform :en :initarg :country-language :documentation "Country's Language for this view.") ;; (file-start-str :type string :initform nil :initarg :file-start-str) (file-end-str :type string :initform nil :initarg :file-end-str) (list-start-fmtstr :type string :initform nil :initarg :list-start-fmtstr) (list-start-value-func :type function :initform nil :initarg :list-start-value-func) (list-start-indent :initform nil :initarg :list-start-indent) (list-end-fmtstr :type string :initform nil :initarg :list-end-fmtstr) (list-end-value-func :type function :initform nil :initarg :list-end-value-func) (list-end-indent :initform nil :initarg :list-end-indent) (obj-start-fmtstr :type string :initform nil :initarg :obj-start-fmtstr) (obj-start-value-func :initform nil :initarg :obj-start-value-func) (obj-start-indent :initform nil :initarg :obj-start-indent) (obj-end-fmtstr :type string :initform nil :initarg :obj-end-fmtstr) (obj-end-value-func :type function :initform nil :initarg :obj-end-value-func) (obj-end-indent :initform nil :initarg :obj-end-indent) (obj-data-indent :initform nil :initarg :obj-data-indent) (obj-data-fmtstr :type string :initform nil :initarg :obj-data-fmtstr) (obj-data-end-fmtstr :type string :initform nil :initarg :obj-data-end-fmtstr) (obj-data-value-func :type function :initform nil :initarg :obj-data-value-func) (link-slots :type list :initform nil :documentation "List of slot names that have hyperlinks") (link-page-name :type string :initform nil :initarg :link-page-name) (link-href-start :type string :initform nil :initarg :link-href-start) (link-href-end :type string :initform nil :initarg :link-href-end) (link-ampersand :type string :initform nil :initarg :link-ampersand)) (:default-initargs :link-page-name "disp-func1") (:documentation "View class for a hyperobject")) (defun get-category-view (obj category &optional slots) "Find or make a category view for an object" (let ((obj-class (class-of obj))) (if (null category) (slot-value obj-class 'default-view) (aif (find category (views obj-class) :key #'category) it (let ((view (make-instance 'object-view :object-class (class-name obj-class) :category category :slots slots))) (push view (views obj-class)) view))))) ;;;; ************************************************************************* ;;;; Metaclass Intialization ;;;; ************************************************************************* (defun finalize-views (cl) "Finalize all views that are given on a objects initialization" (unless (slot-value cl 'default-print-slots) (setf (slot-value cl 'default-print-slots) (mapcar #'slot-definition-name (class-slots cl)))) (let ((views '())) (dolist (view-def (slot-value cl 'views)) (push (make-object-view cl view-def) views)) (setf (slot-value cl 'views) (nreverse views))) (cond ((default-view cl) (setf (slot-value cl 'default-view) (make-object-view cl (default-view cl)))) ((car (views cl)) (setf (slot-value cl 'default-view) (make-object-view cl (car (views cl))))) (t (setf (slot-value cl 'default-view) (make-object-view cl :default))))) (defun make-object-view (cl view-def) "Make an object view from a definition. Do nothing if a class is passed so that reinitialization will be a no-op" (cond ((typep view-def 'object-view) view-def) ((eq view-def :default) (let* ((name (class-name cl)) (view (make-instance 'object-view :name "automatic" :object-class name :category :compact-text))) view)) ((consp view-def) (eval `(make-instance ,view-def))) (t (error "Invalid parameter to make-object-view: ~S" view-def)))) (defmethod initialize-instance :after ((view object-view) &rest initargs &key &allow-other-keys) (initialize-view (find-class (slot-value view 'object-class)) view)) (defun initialize-view (obj-cl view) "Calculate all view slots for a hyperobject class" (let ((fmtstr nil) (first-field t) (value-func '()) (links '()) (category (category view))) (unless (slot-value view 'slots) (setf (slot-value view 'slots) (slot-value obj-cl 'default-print-slots))) (dolist (slot-name (slot-value view 'slots)) (let ((slot (find-slot-by-name obj-cl slot-name))) (unless slot (error "Slot ~A is not found in class ~S" slot-name obj-cl)) (let ((name (slot-definition-name slot)) (namestr-lower (string-downcase (symbol-name (slot-definition-name slot)))) (type (slot-value slot 'type)) (print-formatter (slot-value slot 'print-formatter))) (cond (first-field (setq fmtstr "") (setq first-field nil)) (t (string-append fmtstr " "))) (when (slot-value slot 'hyperlink) (push name links)) (let ((value-fmt (case type ((or :integer :fixnum) "~d") (:boolean "~a") (otherwise "~a")))) (case category (:compact-text (string-append fmtstr value-fmt)) (:compact-text-labels (string-append fmtstr namestr-lower " " value-fmt)) ((or :html :xhtml) (string-append fmtstr (concatenate 'string "" value-fmt ""))) (:xml (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "" namestr-lower ">"))) (:html-label (string-append fmtstr (concatenate 'string "" namestr-lower " " value-fmt ""))) (:xhtml-label (string-append fmtstr (concatenate 'string " " value-fmt ""))) (:xml-labels (string-append fmtstr (concatenate 'string " <" namestr-lower ">" value-fmt "" namestr-lower ">"))) ((or :html-link :xhtml-link) (if (slot-value slot 'hyperlink) (string-append fmtstr "<~~a>" value-fmt "~~a>") (string-append fmtstr (concatenate 'string "" value-fmt "")))) (:xml-link (if (slot-value slot 'hyperlink) (string-append fmtstr "<~~a>" value-fmt "~~a>") (string-append fmtstr (concatenate 'string "<" namestr-lower ">" value-fmt "" namestr-lower ">")))) (:html-link-labels (if (slot-value slot 'hyperlink) (string-append fmtstr "" namestr-lower " <~~a>" value-fmt "~~a>") (string-append fmtstr (concatenate 'string "" namestr-lower " " value-fmt "")))) (:xhtml-link-labels (if (slot-value slot 'hyperlink) (string-append fmtstr "<[!CDATA[" namestr-lower "]]> <~~a>" value-fmt "~~a>") (string-append fmtstr (concatenate 'string " " value-fmt "")))) (:xml-link-labels (if (slot-value slot 'hyperlink) (string-append fmtstr " <~~a>" value-fmt "~~a>") (string-append fmtstr (concatenate 'string " <" namestr-lower ">" value-fmt "" namestr-lower ">"))))) ) ;; let value-fmt (let ((func (if print-formatter `(,print-formatter (slot-value x (quote ,name))) `(slot-value x (quote ,name))))) (when (and (in category :xml :xhtml :xml-link :xhtml-link :xhtml-link-labels :xml-link-labels :ie-xml-link :ie-xml-link-labels) (or print-formatter (string-equal (write-to-string type) "string"))) (setq func `(kmrcl:xml-cdata ,func))) (push func value-func)) ))) (when value-func (setq value-func (compile nil (eval `(lambda (x) (values ,@(nreverse value-func))))))) (setf (slot-value view 'obj-data-fmtstr) fmtstr) (setf (slot-value view 'obj-data-value-func) value-func) (setf (slot-value view 'link-slots) (nreverse links)) (case category ((or :compact-text :compact-text-labels) (initialize-text-view view)) ((or :html :xhtml :html-labels :xhtml-labels) (initialize-html-view view)) ((or :xml :xml-labels) (initialize-xml-view view)) ((or :html-link :html-link-labels) (initialize-html-view view) (setf (slot-value view 'link-href-start) "a href=") (setf (slot-value view 'link-href-end) "a") (setf (slot-value view 'link-ampersand) "&")) ((or :xhtml-link :xhtml-link-labels) (initialize-html-view view) (setf (slot-value view 'link-href-start) "a href=") (setf (slot-value view 'link-href-end) "a") (setf (slot-value view 'link-ampersand) "&")) ((or :xml-link :xml-link-labels) (initialize-xml-view view) (setf (slot-value view 'link-href-start) "xmllink xlink:type=\"simple\" xlink:href=") (setf (slot-value view 'link-href-end) "xmllink") (setf (slot-value view 'link-ampersand) "&")) ((or :ie-xml-link :ie-xml-link-labels) (initialize-xml-view view) (setf (slot-value view 'link-href-start) "html:a href=") (setf (slot-value view 'link-href-end) "html:a") (setf (slot-value view 'link-ampersand) "&")))) view) ;;;; ************************************************************************* ;;;; View Data Format Section ;;;; ************************************************************************* (defun class-name-of (obj) (string-downcase (class-name (class-of obj)))) (defun text-list-start-value-func (obj nitems) (values (hyperobject-class-user-name obj) nitems)) (defun htmlformat-list-start-value-func (x nitems) (values (hyperobject-class-user-name x) nitems (class-name-of x))) (defun initialize-text-view (view) (setf (slot-value view 'list-start-fmtstr) "~a~P:~%") (setf (slot-value view 'list-start-value-func) #'text-list-start-value-func) (setf (slot-value view 'list-start-indent) t) (setf (slot-value view 'obj-data-indent) t) (setf (slot-value view 'obj-data-end-fmtstr) "~%")) (defun initialize-html-view (view) (initialize-text-view view) (setf (slot-value view 'file-start-str) "
~%") (setf (slot-value view 'file-end-str) "~%") (setf (slot-value view 'list-start-indent) t) (setf (slot-value view 'list-start-fmtstr) "~a~p:
~a~p: