;;;; -*- 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.27 2003/04/16 19:35:35 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-name :initform nil :initarg :object-class-name :accessor object-class-name :documentation "Name of class of object to be viewed.") (object-class :initform nil :initarg :object-class :accessor object-class :documentation "Class of object to be viewed.") (slots :initform nil :initarg :slots :accessor slots :documentation "List of effective slots for object to be viewed.") (name :initform nil :initarg :name :accessor name :documentation "Name for this view.") (category :initform nil :initarg :category :accessor category :documentation "Category for view. Helpful when want to find a view corresponding to a particular category.") (source-code :initform nil :initarg :source-code :accessor 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 (or string null) :initform nil :initarg :file-start-str :accessor file-start-str) (file-end-str :type (or string null) :initform nil :initarg :file-end-str :accessor file-end-str) (list-start-fmtstr :type (or string null) :initform nil :initarg :list-start-fmtstr :accessor list-start-fmtstr) (list-start-value-func :type (or function symbol null) :initform nil :initarg :list-start-value-func :accessor list-start-value-func) (list-start-indent :initform nil :initarg :list-start-indent :accessor list-start-indent) (list-end-fmtstr :type (or string null) :initform nil :initarg :list-end-fmtstr :accessor list-end-fmtstr) (list-end-value-func :type (or function symbol null) :initform nil :initarg :list-end-value-func :accessor list-end-value-func) (list-end-indent :initform nil :initarg :list-end-indent :accessor list-end-indent) (obj-start-fmtstr :type (or string symbol null) :initform nil :initarg :obj-start-fmtstr :accessor obj-start-fmtstr) (obj-start-value-func :initform nil :initarg :obj-start-value-func :accessor obj-start-value-func) (obj-start-indent :initform nil :initarg :obj-start-indent :accessor obj-start-indent) (obj-end-fmtstr :type (or string null) :initform nil :initarg :obj-end-fmtstr :accessor obj-end-fmtstr) (obj-end-value-func :type (or function symbol null) :initform nil :initarg :obj-end-value-func :accessor obj-end-value-func) (obj-end-indent :initform nil :initarg :obj-end-indent :accessor obj-end-indent) (obj-data-indent :initform nil :initarg :obj-data-indent :accessor obj-data-indent) (obj-data-fmtstr :type (or string null) :initform nil :initarg :obj-data-fmtstr :accessor obj-data-fmtstr) (obj-data-end-fmtstr :type (or string null) :initform nil :initarg :obj-data-end-fmtstr :accessor obj-data-end-fmtstr) (obj-data-value-func :type (or function symbol null) :initform nil :initarg :obj-data-value-func :accessor obj-data-value-func) (link-slots :type list :initform nil :documentation "List of slot names that have hyperlinks" :accessor link-slots) (link-page-name :type (or string null) :initform nil :initarg :link-page-name :accessor link-page-name) (link-href-start :type (or string null) :initform nil :initarg :link-href-start :accessor link-href-start) (link-href-end :type (or string null) :initform nil :initarg :link-href-end :accessor link-href-end) (link-ampersand :type (or string null) :initform nil :initarg :link-ampersand :accessor 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) (default-view obj-class) (aif (find category (views obj-class) :key #'category) it (let ((view (make-instance 'object-view :object-class-name (class-name obj-class) :object-class 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 (default-print-slots cl) (setf (default-print-slots cl) (mapcar #'slot-definition-name (class-slots cl)))) (let ((views '())) (dolist (view-def (views cl)) (push (make-object-view cl view-def) views)) (setf (views cl) (nreverse views))) (cond ((default-view cl) (setf (default-view cl) (make-object-view cl (default-view cl)))) ((car (views cl)) (setf (default-view cl) (make-object-view cl (car (views cl))))) (t (setf (default-view cl) (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 name :object-class cl :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 (object-class view) view)) (defun initialize-view (obj-cl view) "Calculate all view slots for a hyperobject class" (cond ((category view) (initialize-view-by-category obj-cl view)) ((source-code view) (initialize-view-by-source-code obj-cl view)) (t (setf (category view) :compact-text) (initialize-view-by-category obj-cl view)))) (defun initialize-view-by-source-code (obj-cl view) "Initialize a view based upon a source code" (let ((source-code (source-code view))) (error "not implemented") ) ) (defun initialize-view-by-category (obj-cl view) "Initialize a view based upon a preset category" (let ((fmtstr nil) (first-field t) (value-func '()) (links '()) (category (category view))) (unless (in category :compact-text :compact-text-labels :html :html-labels :html-link-labels :xhtml :xhtml-labels :xhtml-link-labels :xml :xml-labels :xml-link :ie-xml-link :xml-link-labels :ie-xml-link-labels) (error "Unknown view category ~A" category)) (unless (slots view) (setf (slots view) (default-print-slots obj-cl))) (dolist (slot-name (slots view)) (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 name))) (xml-namestr (escape-xml-string namestr-lower)) (xml-tag (escape-xml-string namestr-lower)) (type (slot-value slot 'type)) (print-formatter (esd-print-formatter slot))) (cond (first-field (setq fmtstr "") (setq first-field nil)) (t (string-append fmtstr " "))) (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-labels (string-append fmtstr (concatenate 'string "" namestr-lower " " value-fmt ""))) (:xhtml-labels (string-append fmtstr (concatenate 'string "" xml-namestr " " value-fmt ""))) (:xml-labels (string-append fmtstr (concatenate 'string " <" xml-tag ">" value-fmt "" xml-tag ">"))) ((or :html-link :xhtml-link) (push name links) (if (esd-hyperlink slot) (string-append fmtstr "<~~a>" value-fmt "~~a>") (string-append fmtstr (concatenate 'string "" value-fmt "")))) ((or :xml-link :ie-xml-link) (push name links) (if (esd-hyperlink slot) (string-append fmtstr "<~~a>" value-fmt "~~a>") (string-append fmtstr (concatenate 'string "<" xml-tag ">" value-fmt "" xml-tag ">")))) (:html-link-labels (push name links) (if (esd-hyperlink slot) (string-append fmtstr "" namestr-lower " <~~a>" value-fmt "~~a>") (string-append fmtstr (concatenate 'string "" namestr-lower " " value-fmt "")))) (:xhtml-link-labels (push name links) (if (esd-hyperlink slot) (string-append fmtstr "" xml-namestr " <~~a>" value-fmt "~~a>") (string-append fmtstr (concatenate 'string "" xml-namestr " " value-fmt "")))) ((or :xml-link-labels :ie-xml-link-labels) (push name links) (if (esd-hyperlink slot) (string-append fmtstr " <~~a>" value-fmt "~~a>") (string-append fmtstr (concatenate 'string " <" xml-tag ">" value-fmt "" xml-tag ">"))))) ) ;; 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 :xml-labels :ie-xml-labels :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 (obj-data-fmtstr view) fmtstr) (setf (obj-data-value-func view) value-func) (setf (link-slots view) (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 (link-href-start view) "a href=") (setf (link-href-end view) "a") (setf (link-ampersand view) "&")) ((or :xhtml-link :xhtml-link-labels) (initialize-html-view view) (setf (link-href-start view) "a href=") (setf (link-href-end view) "a") (setf (link-ampersand view) "&")) ((or :xml-link :xml-link-labels) (initialize-xml-view view) (setf (link-href-start view) "xmllink xlink:type=\"simple\" xlink:href=") (setf (link-href-end view) "xmllink") (setf (link-ampersand view) "&")) ((or :ie-xml-link :ie-xml-link-labels) (initialize-xml-view view) (setf (link-href-start view) "html:a href=") (setf (link-href-end view) "html:a") (setf (link-ampersand view) "&")))) 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 (list-start-fmtstr view) "~a~P:~%") (setf (list-start-value-func view) #'text-list-start-value-func) (setf (list-start-indent view) t) (setf (obj-data-indent view) t) (setf (obj-data-end-fmtstr view) (format nil "~%")) ) (defun initialize-html-view (view) (initialize-text-view view) (setf (file-start-str view) (format nil "
~%")) (setf (file-end-str view) (format nil "~%")) (setf (list-start-indent view) t) (setf (list-start-fmtstr view) "~a~p:
~a~p: