X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;ds=inline;f=views.lisp;h=6c102965ef1cbfef5b86a54fe61c4777d48c93bc;hb=6ff5ef506bb52b4a98cb7a0b3726f5d3a9637944;hp=e9a89e3cd9c9d02eef285ec640d85546ca73a5b5;hpb=a922259ecd7b9d02cccb8cc735e06ed8483f270a;p=hyperobject.git
diff --git a/views.lisp b/views.lisp
index e9a89e3..6c10296 100644
--- a/views.lisp
+++ b/views.lisp
@@ -7,444 +7,712 @@
;;;; Programmer: Kevin M. Rosenberg
;;;; Date Started: Apr 2000
;;;;
-;;;; $Id: views.lisp,v 1.2 2002/11/25 07:45:35 kevin Exp $
-;;;;
-;;;; This file is Copyright (c) 2000-2002 by Kevin M. Rosenberg
+;;;; $Id: views.lisp,v 1.60 2003/07/11 18:02:41 kevin Exp $
;;;;
+;;;; This file is Copyright (c) 2000-2003 by Kevin M. Rosenberg
;;;; *************************************************************************
-(in-package :hyperobject)
-
-(eval-when (:compile-toplevel :execute)
- (declaim (optimize (speed 3) (safety 1) (compilation-speed 0) (debug 3))))
-
-(defparameter *default-textformat* nil)
-(defparameter *default-htmlformat* nil)
-(defparameter *default-htmlrefformat* nil)
-(defparameter *default-xhtmlformat* nil)
-(defparameter *default-xhtmlrefformat* nil)
-(defparameter *default-xmlformat* nil)
-(defparameter *default-xmlrefformat* nil)
-(defparameter *default-ie-xmlrefformat* nil)
-(defparameter *default-nullformat* nil)
-(defparameter *default-init-format?* nil)
-
-(defun make-format-instance (fmt)
- (unless *default-init-format?*
- (setq *default-textformat* (make-instance 'textformat))
- (setq *default-htmlformat* (make-instance 'htmlformat))
- (setq *default-htmlrefformat* (make-instance 'htmlrefformat))
- (setq *default-xhtmlformat* (make-instance 'xhtmlformat))
- (setq *default-xhtmlrefformat* (make-instance 'xhtmlrefformat))
- (setq *default-xmlformat* (make-instance 'xmlformat))
- (setq *default-xmlrefformat* (make-instance 'xmlrefformat))
- (setq *default-ie-xmlrefformat* (make-instance 'ie-xmlrefformat))
- (setq *default-nullformat* (make-instance 'nullformat))
- (setq *default-init-format?* t))
+(in-package #:hyperobject)
+
+
+(defclass object-view ()
+ ((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.")
+ (printer :initform nil :initarg :printer :accessor printer
+ :documentation "default function that prints the object")
+ ;;
+ (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-printer :type (or string function null) :initform nil
+ :initarg :list-start-printer
+ :accessor list-start-printer)
+ (list-start-indent :initform nil :initarg :list-start-indent
+ :accessor list-start-indent)
+ (list-end-printer :type (or string function null) :initform nil
+ :initarg :list-end-printer
+ :accessor list-end-printer)
+ (list-end-indent :initform nil :initarg :list-end-indent
+ :accessor list-end-indent)
+ (obj-start-printer :type (or string function null) :initform nil :initarg :obj-start-printer
+ :accessor obj-start-printer)
+ (obj-start-indent :initform nil :initarg :obj-start-indent
+ :accessor obj-start-indent)
+ (obj-end-printer :type (or string function null) :initform nil :initarg :obj-end-printer
+ :accessor obj-end-printer)
+ (obj-end-indent :initform nil :initarg :obj-end-indent
+ :accessor obj-end-indent)
+ (subobj-start-printer :type (or string function null) :initform nil :initarg :subobj-start-printer
+ :accessor subobj-start-printer)
+ (subobj-start-indent :initform nil :initarg :subobj-start-indent
+ :accessor subobj-start-indent)
+ (subobj-end-printer :type (or string function null) :initform nil :initarg :subobj-end-printer
+ :accessor subobj-end-printer)
+ (subobj-end-indent :initform nil :initarg :subobj-end-indent
+ :accessor subobj-end-indent)
+ (obj-data-indent :initform nil :initarg :obj-data-indent
+ :accessor obj-data-indent)
+ (obj-data-printer :type (or function null) :initform nil
+ :initarg :obj-data-printer
+ :accessor obj-data-printer)
+ (obj-data-print-code :type (or function null) :initform nil
+ :initarg :obj-data-print-code
+ :accessor obj-data-print-code)
+ (obj-data-start-printer :type (or function string null) :initform nil
+ :initarg :obj-data-start-printer
+ :accessor obj-data-start-printer)
+ (obj-data-end-printer :type (or string null) :initform nil
+ :initarg :obj-data-end-printer
+ :accessor obj-data-end-printer)
+ (indenter :type (or function null) :initform nil
+ :accessor indenter
+ :documentation "Function that performs hierarchical indenting")
+ (link-slots :type list :initform nil
+ :documentation "List of slot names that have hyperlinks"
+ :accessor link-slots)
+ (link-page :type (or string null) :initform nil
+ :initarg :link-page
+ :accessor link-page)
+ (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 "lookup-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 (find-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 (direct-views cl))
+ (push (make-object-view cl view-def) views))
+ (setf (views cl) (nreverse views)))
+ (cond
+ ((views cl)
+ (setf (default-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 (class-name cl)
+ :category :compact-text)))
+ view))
+ ((consp view-def)
+ (make-instance 'object-view
+ :object-class (class-name cl)
+ :name (getf view-def :name)
+ :source-code (getf view-def :source-code)))
+ (t
+ (error "Invalid parameter to make-object-view: ~S" view-def))))
+
+(defmethod initialize-instance :after ((self object-view)
+ &rest initargs
+ &key
+ &allow-other-keys)
+ (initialize-view self))
- (case fmt
- (:text *default-textformat*)
- (:html *default-htmlformat*)
- (:htmlref *default-htmlrefformat*)
- (:xhtml *default-xhtmlformat*)
- (:xhtmlref *default-xhtmlrefformat*)
- (:xml *default-xmlformat*)
- (:xmlref *default-xmlrefformat*)
- (:ie-xmlref *default-ie-xmlrefformat*)
- (:null *default-nullformat*)
- (otherwise *default-textformat*)))
+(defun initialize-view (view)
+ "Calculate all view slots for a hyperobject class"
+ (let ((obj-cl (find-class (object-class view))))
+ (cond
+ ((category view)
+ (initialize-view-by-category obj-cl view))
+ ((source-code view)
+ (initialize-view-by-source-code view))
+ (t
+ (setf (category view) :compact-text)
+ (initialize-view-by-category obj-cl view)))))
+
+(defun initialize-view-by-source-code (view)
+ "Initialize a view based upon a source code"
+ (let* ((source-code (source-code view))
+ (*package* (symbol-package (object-class view)))
+ (printer `(lambda (x s)
+ (declare (ignorable x s))
+ ,@source-code)))
+ (setf (printer view)
+ (compile nil (eval printer)))))
+
+(defmacro write-simple (v s)
+ `(typecase ,v
+ (string
+ (write-string ,v ,s))
+ (fixnum
+ (write-fixnum ,v ,s))
+ (symbol
+ (write-string (symbol-name ,v) ,s))
+ (t
+ (write-string (write-to-string ,v) ,s))))
+
+(defun write-ho-value (obj name type formatter cdata strm)
+ (declare (ignorable type))
+ (let* ((slot-data (slot-value obj name))
+ (fmt-data (if formatter
+ (funcall formatter slot-data)
+ slot-data)))
+ (if cdata
+ (write-xml-cdata fmt-data strm)
+ (write-simple fmt-data strm))))
+
+(defun ppfc-html (title name type formatter cdata print-func)
+ (vector-push-extend '(write-string "" s) print-func)
+ (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
+ (vector-push-extend '(write-string "" s) print-func))
+
+(defun ppfc-xml (tag name type formatter cdata print-func)
+ (vector-push-extend '(write-char #\< s) print-func)
+ (vector-push-extend `(write-string ,tag s) print-func)
+ (vector-push-extend '(write-char #\> s) print-func)
+ (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
+ (vector-push-extend '(write-string "" s) print-func)
+ (vector-push-extend `(write-string ,tag s) print-func)
+ (vector-push-extend '(write-char #\> s) print-func))
+
+(defun ppfc-html-labels (label name type formatter cdata print-func)
+ (vector-push-extend '(write-string "" s) print-func)
+ (vector-push-extend `(write-string ,label s) print-func)
+ (vector-push-extend '(write-string " " s) print-func)
+ (ppfc-html label name type formatter cdata print-func))
+
+(defun ppfc-xhtml-labels (label tag name type formatter cdata print-func)
+ (vector-push-extend '(write-string "" s) print-func)
+ (vector-push-extend `(write-string ,label s) print-func)
+ (vector-push-extend '(write-string " " s) print-func)
+ (ppfc-html tag name type formatter cdata print-func))
+
+(defun ppfc-xml-labels (label tag name type formatter cdata print-func)
+ (vector-push-extend '(write-string " " s) print-func)
+ (ppfc-xml tag name type formatter cdata print-func))
+
+(defun ppfc-html-link (name type formatter cdata nlink print-func)
+ (declare (fixnum nlink))
+ (vector-push-extend '(write-char #\< s) print-func)
+ (vector-push-extend `(write-string (nth ,(+ nlink nlink) links) s) print-func)
+ (vector-push-extend '(write-char #\> s) print-func)
+ (vector-push-extend `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func)
+ (vector-push-extend '(write-string "" s) print-func)
+ (vector-push-extend `(write-string (nth ,(+ nlink nlink 1) links) s) print-func)
+ (vector-push-extend '(write-char #\> s) print-func))
+
+(defun ppfc-html-link-labels (label name type formatter cdata nlink print-func)
+ (vector-push-extend '(write-string "" s) print-func)
+ (vector-push-extend `(write-string ,label s) print-func)
+ (vector-push-extend '(write-string " " s) print-func)
+ (ppfc-html-link name type formatter cdata nlink print-func))
+
+(defun push-print-fun-code (category slot nlink print-func)
+ (let* ((formatter (esd-print-formatter slot))
+ (name (slot-definition-name slot))
+ (user-name (esd-user-name slot))
+ (xml-user-name (escape-xml-string user-name))
+ (xml-tag (escape-xml-string user-name))
+ (type (slot-value slot 'type))
+ (cdata (not (null
+ (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 formatter
+ (lisp-type-is-a-string type))))))
+ (hyperlink (esd-hyperlink slot)))
-;;;; Output format classes for print hyperobject-classes
-
-(defclass dataformat ()
- ((file-start-str :type string :initarg :file-start-str :reader file-start-str)
- (file-end-str :type string :initarg :file-end-str :reader file-end-str)
- (list-start-fmtstr :type string :initarg :list-start-fmtstr :reader list-start-fmtstr)
- (list-start-value-func :type function :initarg :list-start-value-func :reader list-start-value-func)
- (list-start-indent :initarg :list-start-indent :reader list-start-indent)
- (list-end-fmtstr :type string :initarg :list-end-fmtstr :reader list-end-fmtstr)
- (list-end-value-func :type function :initarg :list-end-value-func :reader list-end-value-func)
- (list-end-indent :initarg :list-end-indent :reader list-end-indent)
- (obj-start-fmtstr :type string :initarg :obj-start-fmtstr :reader obj-start-fmtstr)
- (obj-start-value-func :initarg :obj-start-value-func :reader obj-start-value-func)
- (obj-start-indent :initarg :obj-start-indent :reader obj-start-indent)
- (obj-end-fmtstr :type string :initarg :obj-end-fmtstr :reader obj-end-fmtstr)
- (obj-end-value-func :initarg :obj-end-value-func :reader obj-end-value-func)
- (obj-end-indent :initarg :obj-end-indent :reader obj-end-indent)
- (obj-data-indent :initarg :obj-data-indent :reader obj-data-indent)
- (obj-data-fmtstr :initarg :obj-data-fmtstr :reader obj-data-fmtstr)
- (obj-data-fmtstr-labels :initarg :obj-data-fmtstr-labels :reader obj-data-fmtstr-labels)
- (obj-data-end-fmtstr :initarg :obj-data-end-fmtstr :reader obj-data-end-fmtstr)
- (obj-data-value-func :initarg :obj-data-value-func :reader obj-data-value-func)
- (link-ref :initarg :link-ref :reader link-ref))
- (:default-initargs :file-start-str nil :file-end-str nil :list-start-fmtstr nil :list-start-value-func nil
- :list-start-indent nil :list-end-fmtstr nil :list-end-value-func nil :list-end-indent nil
- :obj-start-fmtstr nil :obj-start-value-func nil :obj-start-indent nil
- :obj-end-fmtstr nil :obj-end-value-func nil :obj-end-indent nil
- :obj-data-indent nil :obj-data-fmtstr nil :obj-data-fmtstr-labels nil :obj-data-end-fmtstr nil
- :obj-data-value-func nil :link-ref nil)
- (:documentation "Parent for all dataformat objects"))
-
-(defclass binaryformat (dataformat)
- ())
-
-(defclass nullformat (dataformat)
- ())
-
-(defun text-list-start-value-func (obj nitems)
- (values (hyperobject-class-title obj) nitems))
-
-(defclass textformat (dataformat)
- ()
- (:default-initargs :list-start-fmtstr "~a~P:~%"
- :list-start-value-func #'text-list-start-value-func
- :list-start-indent t
- :obj-data-indent t
- :obj-data-fmtstr #'hyperobject-class-fmtstr-text
- :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-text-labels
- :obj-data-end-fmtstr "~%"
- :obj-data-value-func #'hyperobject-class-value-func))
+ (case category
+ (:compact-text
+ (vector-push-extend
+ `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
+ (:compact-text-labels
+ (vector-push-extend `(write-string ,user-name s) print-func)
+ (vector-push-extend '(write-char #\space s) print-func)
+ (vector-push-extend
+ `(write-ho-value x ',name ',type ',formatter ,cdata s) print-func))
+ ((or :html :xhtml)
+ (ppfc-html user-name name type formatter cdata print-func))
+ (:xml
+ (ppfc-xml xml-tag name type formatter cdata print-func))
+ (:html-labels
+ (ppfc-html-labels user-name name type formatter cdata print-func))
+ (:xhtml-labels
+ (ppfc-xhtml-labels xml-user-name user-name name type formatter cdata print-func))
+ (:xml-labels
+ (ppfc-xml-labels xml-user-name xml-tag name type formatter cdata print-func))
+ ((or :html-link :xhtml-link)
+ (if hyperlink
+ (ppfc-html-link name type formatter cdata nlink print-func)
+ (ppfc-html user-name name type formatter cdata print-func)))
+ ((or :xml-link :ie-xml-link)
+ (if hyperlink
+ (ppfc-html-link name type formatter cdata nlink print-func)
+ (ppfc-xml xml-tag name type formatter cdata print-func)))
+ (:html-link-labels
+ (if hyperlink
+ (ppfc-html-link-labels user-name name type formatter cdata nlink
+ print-func)
+ (ppfc-html-labels user-name name type formatter cdata print-func)))
+ (:xhtml-link-labels
+ (if hyperlink
+ (ppfc-html-link-labels xml-user-name name type formatter cdata nlink
+ print-func)
+ (ppfc-xhtml-labels xml-tag user-name name type formatter cdata
+ print-func)))
+ ((or :xml-link-labels :ie-xml-link-labels)
+ (if hyperlink
+ (ppfc-html-link-labels xml-user-name name type formatter cdata nlink
+ print-func)
+ (ppfc-xml-labels xml-tag user-name name type formatter cdata
+ print-func))))))
+
+
+(defun view-has-links-p (view)
+ (in (category view) :html-link :xhtml-link :xml-link :ie-xml-link
+ :html-link-labels :xhtml-link-labels :xml-link-labels
+ :ie-xml-link-labels))
+
+(defun initialize-view-by-category (obj-cl view)
+ "Initialize a view based upon a preset category"
+ (unless (in (category view) :compact-text :compact-text-labels
+ :html :html-labels :html-link-labels
+ :xhtml :xhtml-labels :xhtml-link-labels
+ :xhtml-link :html-link
+ :xml :xml-labels :xml-link :ie-xml-link
+ :xml-link-labels :ie-xml-link-labels
+ :display-table :edit-table)
+ (error "Unknown view category ~A" (category view)))
+
+ (unless (slots view) (setf (slots view) (default-print-slots obj-cl)))
+
+ (let ((links '())
+ (print-func (make-array 10 :fill-pointer 0 :adjustable t)))
+
+ (do* ((slots (slots view) (cdr slots))
+ (slot-name (car slots) (car slots))
+ (slot (find-slot-by-name obj-cl slot-name)
+ (find-slot-by-name obj-cl slot-name)))
+ ((null slots))
+ (unless slot
+ (error "Slot ~A is not found in class ~S" slot-name obj-cl))
+
+ (push-print-fun-code (category view) slot (length links) print-func)
+ (when (> (length slots) 1)
+ (vector-push-extend '(write-char #\space s) print-func))
+
+ (when (and (view-has-links-p view) (esd-hyperlink slot))
+ (push (slot-definition-name slot) links)))
+
+ (vector-push-extend 'x print-func) ;; return object
+ (setf (obj-data-print-code view) `(lambda (x s links)
+ (declare (ignorable s links))
+ ,@(map 'list #'identity print-func)))
+ (setf (obj-data-printer view)
+ (compile nil (eval (obj-data-print-code view))))
+
+ (setf (link-slots view) (nreverse links)))
+
+ (finalize-view-by-category view)
+ view)
+
+(defun finalize-view-by-category (view)
+ (case (category view)
+ ((or :compact-text :compact-text-labels)
+ (initialize-text-view view))
+ ((or :html :html-labels)
+ (initialize-html-view view))
+ ((or :xhtml :xhtml-labels)
+ (initialize-xhtml-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-xhtml-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) "&"))))
+
+
+(defun make-std-object-slots-view (class-name slots)
+ #'(lambda (obj strm)
+ )
+
+ )
+;;;; *************************************************************************
+;;;; View Data Format Section
+;;;; *************************************************************************
(defun class-name-of (obj)
(string-downcase (class-name (class-of obj))))
-(defun htmlformat-list-start-value-func (x nitems)
- (values (hyperobject-class-title x) nitems (class-name-of x)))
-
-(defclass htmlformat (textformat)
- ()
- (:default-initargs :file-start-str "
~%"
- :file-end-str "~%"
- :list-start-indent t
- :list-start-fmtstr "~a~p:
~%"
- :list-start-value-func #'htmlformat-list-start-value-func
- :list-end-fmtstr "
~%"
- :list-end-indent t
- :list-end-value-func #'identity
- :obj-start-indent t
- :obj-start-fmtstr ""
- :obj-start-value-func #'identity
- :obj-end-indent t
- :obj-end-fmtstr "~%"
- :obj-end-value-func #'identity
- :obj-data-indent t
- :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
- :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
- :obj-data-value-func #'hyperobject-class-value-func))
-
-(defclass xhtmlformat (textformat)
- ()
- (:default-initargs :file-start-str "~%"
- :file-end-str "~%"
- :list-start-indent t
- :list-start-fmtstr "~a~p:
~%"
- :list-start-value-func #'htmlformat-list-start-value-func
- :list-end-fmtstr "
~%"
- :list-end-indent t
- :list-end-value-func #'identity
- :obj-start-indent t
- :obj-start-fmtstr ""
- :obj-start-value-func #'identity
- :obj-end-indent t
- :obj-end-fmtstr "~%"
- :obj-end-value-func #'identity
- :obj-data-indent t
- :obj-data-fmtstr #'hyperobject-class-fmtstr-html-labels
- :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-html-labels
- :obj-data-value-func #'hyperobject-class-xmlvalue-func))
-
-
-(defun xmlformat-list-end-value-func (x)
- (format nil "~alist" (class-name-of x)))
-
-(defun xmlformat-list-start-value-func (x nitems)
- (values (format nil "~alist" (class-name-of x)) (hyperobject-class-title x) nitems))
-
-(defclass xmlformat (textformat)
- ()
- (:default-initargs :file-start-str "" ; (std-xml-header)
- :list-start-indent t
- :list-start-fmtstr "<~a>~a~p: ~%"
- :list-start-value-func #'xmlformat-list-start-value-func
- :list-end-indent t
- :list-end-fmtstr "~a>~%"
- :list-end-value-func #'xmlformat-list-end-value-func
- :obj-start-fmtstr "<~a>"
- :obj-start-value-func #'class-name-of
- :obj-start-indent t
- :obj-end-fmtstr "~a>~%"
- :obj-end-value-func #'class-name-of
- :obj-end-indent nil
- :obj-data-indent nil
- :obj-data-fmtstr #'hyperobject-class-fmtstr-xml
- :obj-data-fmtstr-labels #'hyperobject-class-fmtstr-xml-labels
- :obj-data-value-func #'hyperobject-class-xmlvalue-func))
-
-(defclass link-ref ()
- ((fmtstr :type function :initarg :fmtstr :accessor fmtstr)
- (fmtstr-labels :type function :initarg :fmtstr-labels :accessor fmtstr-labels)
- (page-name :type string :initarg :page-name :accessor page-name)
- (href-head :type string :initarg :href-head :accessor href-head)
- (href-end :type string :initarg :href-end :accessor href-end)
- (ampersand :type string :initarg :ampersand :accessor ampersand))
- (:default-initargs :fmtstr nil
- :fmtstr-labels nil
- :page-name "disp-func1"
- :href-head nil :href-end nil :ampersand nil)
- (:documentation "Formatting for a linked reference"))
-
-(defclass html-link-ref (link-ref)
- ()
- (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
- :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
- :href-head "a href="
- :href-end "a"
- :ampersand "&"))
-
-(defclass xhtml-link-ref (link-ref)
- ()
- (:default-initargs :fmtstr #'hyperobject-class-fmtstr-html-ref
- :fmtstr-labels #'hyperobject-class-fmtstr-html-ref-labels
- :href-head "a href="
- :href-end "a"
- :ampersand "&"))
-
-(defclass xml-link-ref (link-ref)
- ()
- (:default-initargs :fmtstr #'hyperobject-class-fmtstr-xml-ref
- :fmtstr-labels #'hyperobject-class-fmtstr-xml-ref-labels
- :href-head "xmllink xlink:type=\"simple\" xlink:href="
- :href-end "xmllink"
- :ampersand "&")
- (:documentation "Mozilla's and W3's idea of a link with XML"))
-
-(defclass ie-xml-link-ref (xml-link-ref)
- ()
- (:default-initargs :href-head "html:a href="
- :href-end "html:a" )
- (:documentation "Internet Explorer's idea of a link with XML"))
-
-
-(defclass htmlrefformat (htmlformat)
- ()
- (:default-initargs :link-ref (make-instance 'html-link-ref)))
-
-(defclass xhtmlrefformat (xhtmlformat)
- ()
- (:default-initargs :link-ref (make-instance 'xhtml-link-ref)))
-
-(defclass xmlrefformat (xmlformat)
- ()
- (:default-initargs :link-ref (make-instance 'xml-link-ref)))
-
-(defclass ie-xmlrefformat (xmlformat)
- ()
- (:default-initargs :link-ref (make-instance 'ie-xml-link-ref)))
+(defvar +newline-string+ (format nil "~%"))
+
+(defun write-user-name-maybe-plural (obj nitems strm)
+ (write-string
+ (if (> nitems 1)
+ (hyperobject-class-user-name-plural obj)
+ (hyperobject-class-user-name obj))
+ strm))
+
+(defun initialize-text-view (view)
+ (setf (list-start-printer view)
+ (compile nil
+ (eval '(lambda (obj nitems indent strm)
+ (declare (ignore indent))
+ (write-user-name-maybe-plural obj nitems strm)
+ (write-char #\: strm)
+ (write-char #\Newline strm)))))
+ (setf (list-start-indent view) t)
+ (setf (obj-data-indent view) t)
+ (setf (obj-data-end-printer view) +newline-string+)
+ (setf (indenter view) #'indent-spaces))
+
+(defun html-list-start-func (obj nitems indent strm)
+ (write-string "" strm)
+ (write-user-name-maybe-plural obj nitems strm)
+ (write-string "
" strm)
+ (write-char #\newline strm)
+ (write-string "" strm)
+ (write-char #\newline strm))
+
+(defun initialize-html-view (view)
+ (initialize-text-view view)
+ (setf (indenter view) #'indent-spaces)
+ (setf (file-start-str view) (format nil "~%"))
+ (setf (file-end-str view) (format nil "~%"))
+ (setf (list-start-indent view) t)
+ (setf (list-start-printer view) #'html-list-start-func)
+ (setf (list-end-printer view) (format nil "
~%"))
+ (setf (list-end-indent view) t)
+ (setf (obj-start-indent view) nil)
+ (setf (obj-start-printer view) "")
+ (setf (obj-end-indent view) nil)
+ (setf (obj-end-printer view) (format nil "~%"))
+ (setf (obj-data-end-printer view) nil)
+ (setf (obj-data-indent view) nil))
+
+(defun xhtml-list-start-func (obj nitems indent strm)
+ (write-string "" strm)
+ (write-user-name-maybe-plural obj nitems strm)
+ (write-string "
" strm)
+ (write-string "" strm)
+ (write-char #\newline strm))
+
+(defun html-obj-start (obj indent strm)
+ (declare (ignore obj indent))
+ (write-string "
" strm))
+
+(defun initialize-xhtml-view (view)
+ (initialize-text-view view)
+ (setf (indenter view) #'indent-spaces)
+ (setf (file-start-str view) (format nil "~%"))
+ (setf (file-end-str view) (format nil "~%"))
+ (setf (list-start-indent view) nil)
+ (setf (list-start-printer view) #'xhtml-list-start-func)
+ (setf (list-end-printer view) (format nil "
~%"))
+ (setf (list-end-indent view) nil)
+ (setf (obj-start-indent view) nil)
+ (setf (obj-start-printer view) #'html-obj-start)
+ (setf (obj-end-printer view) (format nil "
~%"))
+ (setf (obj-data-indent view) nil))
+
+(defun xmlformat-list-end-func (x strm)
+ (write-string "" strm)
+ (write-string (class-name-of x) strm)
+ (write-string "list" strm)
+ (write-string ">" strm)
+ (write-char #\newline strm))
+
+(defun xmlformat-list-start-func (x nitems indent strm)
+ (declare (ignore indent))
+ (write-char #\< strm)
+ (write-string (class-name-of x) strm)
+ (write-string "list>" strm)
+ (write-user-name-maybe-plural x nitems strm)
+ (write-string ":" strm)
+ (write-char #\newline strm))
+
+(defun initialize-xml-view (view)
+ (setf (file-start-str view) "") ; (std-xml-header)
+ (setf (list-start-indent view) t)
+ (setf (list-start-printer view) #'xmlformat-list-start-func)
+ (setf (list-end-indent view) t)
+ (setf (list-end-printer view) #'xmlformat-list-end-func)
+ (setf (obj-start-printer view) (format nil "<~(~a~)>" (object-class-name view)))
+ (setf (obj-start-indent view) t)
+ (setf (subobj-end-printer view) (format nil "~(~a~)>~%" (object-class-name view)))
+ (setf (subobj-end-indent view) nil)
+ (setf (obj-data-indent view) nil))
;;; File Start and Ends
-(defgeneric fmt-file-start (fmt s))
-(defmethod fmt-file-start ((fmt dataformat) (s stream)))
+(defun fmt-file-start (view strm)
+ (awhen (file-start-str view)
+ (write-string it strm)))
-(defmethod fmt-file-start ((fmt textformat) (s stream))
- (aif (file-start-str fmt)
- (format s it)))
-
-(defgeneric fmt-file-end (fmt s))
-(defmethod fmt-file-end ((fmt textformat) (s stream))
- (aif (file-end-str fmt)
- (format s it)))
+(defun fmt-file-end (view strm)
+ (awhen (file-end-str view)
+ (write-string it strm)))
;;; List Start and Ends
-(defgeneric fmt-list-start (obj fmt s &optional indent num-items))
-(defmethod fmt-list-start (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
- (if (list-start-indent fmt)
- (indent-spaces indent s))
- (aif (list-start-fmtstr fmt)
- (apply #'format s it
- (multiple-value-list
- (funcall (list-start-value-func fmt) x num-items)))))
-
-(defgeneric fmt-list-end (obj fmt s &optional indent num-items))
-(defmethod fmt-list-end (x (fmt textformat) (s stream) &optional (indent 0) (num-items 1))
+(defun fmt-list-start (obj view strm indent num-items)
+ (when (list-start-indent view)
+ (awhen (indenter view)
+ (funcall it indent strm)))
+ (awhen (list-start-printer view)
+ (if (stringp it)
+ (write-string it strm)
+ (funcall it obj num-items indent strm))))
+
+(defun fmt-list-end (obj view strm indent num-items)
(declare (ignore num-items))
- (if (list-end-indent fmt)
- (indent-spaces indent s))
- (aif (list-end-fmtstr fmt)
- (apply #'format s it
- (multiple-value-list
- (funcall (list-end-value-func fmt) x)))))
+ (when (list-end-indent view)
+ (awhen (indenter view)
+ (funcall it indent strm)))
+ (awhen (list-end-printer view)
+ (if (stringp it)
+ (write-string it strm)
+ (funcall it obj strm))))
;;; Object Start and Ends
-(defgeneric fmt-obj-start (obj fmt s &optional indent))
-(defmethod fmt-obj-start (x (fmt textformat) (s stream) &optional (indent 0))
- (if (obj-start-indent fmt)
- (indent-spaces indent s))
- (aif (obj-start-fmtstr fmt)
- (apply #'format s it
- (multiple-value-list
- (funcall (obj-start-value-func fmt) x)))))
-
-(defgeneric fmt-obj-end (obj fmt s &optional indent))
-(defmethod fmt-obj-end (x (fmt textformat) (s stream) &optional (indent 0))
- (if (obj-end-indent fmt)
- (indent-spaces indent s))
- (aif (obj-end-fmtstr fmt)
- (apply #'format s it
- (multiple-value-list
- (funcall (obj-end-value-func fmt) x)))))
+
+(defun fmt-obj-start (obj view strm indent)
+ (when (obj-start-indent view)
+ (awhen (indenter view)
+ (funcall it indent strm)))
+ (awhen (obj-start-printer view)
+ (if (stringp it)
+ (write-string it strm)
+ (funcall it obj indent strm))))
+
+(defun fmt-obj-end (obj view strm indent)
+ (when (obj-end-indent view)
+ (awhen (indenter view)
+ (funcall it indent strm)))
+ (awhen (obj-end-printer view)
+ (if (stringp it)
+ (write-string it strm)
+ (funcall it obj strm))))
+
+(defun fmt-subobj-start (obj view strm indent)
+ (when (subobj-start-indent view)
+ (awhen (indenter view)
+ (funcall it indent strm)))
+ (awhen (subobj-start-printer view)
+ (if (stringp it)
+ (write-string it strm)
+ (funcall it obj indent strm))))
+
+(defun fmt-subobj-end (obj view strm indent)
+ (when (subobj-end-indent view)
+ (awhen (indenter view)
+ (funcall it indent strm)))
+ (awhen (subobj-end-printer view)
+ (if (stringp it)
+ (write-string it strm)
+ (funcall it obj strm))))
;;; Object Data
-(defgeneric make-link-start (obj ref fieldname fieldfunc fieldvalue refvars))
-(defmethod make-link-start (obj (ref link-ref) fieldname fieldfunc fieldvalue refvars)
- (declare (ignore obj fieldname))
- (format nil "~a\"~a?func=~a~akey=~a~a\""
- (href-head ref) (make-url (page-name ref)) fieldfunc
- (ampersand ref) fieldvalue
- (if refvars
- (let ((varstr ""))
- (dolist (var refvars)
- (string-append varstr (format nil "~a~a=~a"
- (ampersand ref) (car var) (cadr var))))
- varstr)
- "")))
-
-(defgeneric make-link-end (obj ref fieldname))
-(defmethod make-link-end (obj (ref link-ref) fieldname)
- (declare (ignore obj fieldname))
- (format nil "~a" (href-end ref))
- )
-
-(defgeneric fmt-obj-data (obj fmt s &optional indent label refvars))
-(defmethod fmt-obj-data (x (fmt textformat) s
- &optional (indent 0) (label nil) (refvars nil))
- (if (obj-data-indent fmt)
- (indent-spaces indent s))
- (if (link-ref fmt)
- (fmt-obj-data-with-ref x fmt s label refvars)
- (fmt-obj-data-plain x fmt s label))
- (aif (obj-data-end-fmtstr fmt)
- (format s it)))
-
-(defgeneric fmt-obj-data-plain (obj fmt s label))
-(defmethod fmt-obj-data-plain (x (fmt textformat) s label)
- (if label
- (apply #'format s
- (funcall (obj-data-fmtstr-labels fmt) x)
- (multiple-value-list
- (funcall (funcall (obj-data-value-func fmt) x) x)))
- (apply #'format s (funcall (obj-data-fmtstr fmt) x)
- (multiple-value-list
- (funcall (funcall (obj-data-value-func fmt) x) x)))))
-
-(defgeneric fmt-obj-data-with-ref (obj fmt s label refvars))
-(defmethod fmt-obj-data-with-ref (x (fmt textformat) s label refvars)
- (let ((refstr (make-ref-data-str x fmt label))
- (refvalues nil)
- (field-values
- (multiple-value-list
- (funcall (funcall (obj-data-value-func fmt) x) x))))
-
- ;; make list of reference link fields for printing to refstr template
- (dolist (ref (hyperobject-class-references x))
- (let ((link-start
- (make-link-start x (link-ref fmt) (name ref) (lookup ref)
- (nth (position (name ref)
- (hyperobject-class-fields x)
- :key #'(lambda (x)
- (slot-definition-name x)))
- field-values)
- (append (link-parameters ref) refvars)))
- (link-end (make-link-end x (link-ref fmt) (name ref))))
- (push link-start refvalues)
- (push link-end refvalues)))
- (setq refvalues (nreverse refvalues))
-
- (apply #'format s refstr refvalues)))
-(defgeneric obj-data (obj))
-(defmethod obj-data (x)
- "Returns the objects data as a string. Used by common-graphics outline function"
- (let ((fmt (make-format-instance :text)))
- (apply #'format nil (funcall (obj-data-fmtstr fmt) x)
- (multiple-value-list
- (funcall (funcall (obj-data-value-func fmt) x) x)))))
-
-(defgeneric make-ref-data-str (obj fmt &optional label))
-(defmethod make-ref-data-str (x (fmt textformat) &optional (label nil))
- "Return fmt string for that contains ~a slots for reference link start and end"
- (unless (link-ref fmt)
- (error "fmt does not contain a link-ref"))
- (let ((refstr
- (if label
- (apply #'format nil (funcall (fmtstr-labels (link-ref fmt)) x)
- (multiple-value-list
- (funcall (funcall (obj-data-value-func fmt) x) x)))
- (apply #'format nil (funcall (fmtstr (link-ref fmt)) x)
- (multiple-value-list (funcall (funcall (obj-data-value-func fmt) x) x))))))
- refstr))
+(defun make-link-start (view fieldfunc fieldvalue refvars link-printer)
+ (with-output-to-string (s)
+ (write-string (link-href-start view) s)
+ (write-char #\" s)
+ (let ((link-page (link-page view)))
+ (cond
+ ((null link-printer)
+ (write-string (make-url link-page) s)
+ (write-string "?func=" s)
+ (write-simple fieldfunc s)
+ (write-string (link-ampersand view) s)
+ (write-string "key=" s)
+ (write-simple fieldvalue s)
+ (dolist (var refvars)
+ (write-string (link-ampersand view) s)
+ (write-simple (car var) s)
+ (write-char #\= s)
+ (write-simple (cdr var) s)))
+ (link-printer
+ (funcall link-printer link-page fieldfunc fieldvalue refvars s))))
+ (write-char #\" s)))
+(defun make-link-end (obj view fieldname)
+ (declare (ignore obj fieldname))
+ (link-href-end view))
+
+(defun fmt-obj-data (obj view strm indent refvars link-printer)
+ (awhen (obj-data-start-printer view)
+ (if (stringp it)
+ (write-string it strm)
+ (funcall it obj strm)))
+ (when (obj-data-indent view)
+ (awhen (indenter view)
+ (funcall it indent strm)))
+ (if (link-slots view)
+ (fmt-obj-data-with-link obj view strm refvars link-printer)
+ (fmt-obj-data-plain obj view strm))
+ (awhen (obj-data-end-printer view)
+ (if (stringp it)
+ (write-string it strm)
+ (funcall it obj strm))))
+
+(defun fmt-obj-data-plain (obj view strm)
+ (awhen (obj-data-printer view)
+ (funcall it obj strm nil)))
+
+(defun fmt-obj-data-with-link (obj view strm refvars link-printer)
+ (let ((refvalues '()))
+ (declare (dynamic-extent refvalues))
+ ;; make list of hyperlink link fields for printing to refstr template
+ (dolist (name (link-slots view))
+ (awhen (find name (hyperobject-class-hyperlinks obj) :key #'name)
+ (push (make-link-start view (lookup it) (slot-value obj name)
+ (append (link-parameters it) refvars)
+ link-printer)
+ refvalues)
+ (push (make-link-end obj view name) refvalues)))
+ (funcall (obj-data-printer view) obj strm (nreverse refvalues))))
+
+(defun obj-data (obj view)
+ "Returns the objects data as a string. Used by common-graphics outline function"
+ (with-output-to-string (s) (fmt-obj-data-plain obj view s)))
+
;;; Display method for objects
-(defgeneric load-all-subobjects (objs))
-(defmethod load-all-subobjects (objs)
+(defun load-all-subobjects (objs)
"Load all subobjects if they have not already been loaded."
- (when objs
- (let ((objlist (mklist objs)))
+ (dolist (obj (mklist objs))
+ (dolist (subobj (hyperobject-class-subobjects obj))
+ (awhen (slot-value obj (name-slot subobj))
+ (load-all-subobjects it))))
+ objs)
+
+(defun view-subobjects (obj strm &optional category (indent 0) filter
+ subobjects refvars link-printer)
+ (when (hyperobject-class-subobjects obj)
+ (dolist (subobj (hyperobject-class-subobjects obj))
+ (aif (slot-value obj (name-slot subobj))
+ (view-hyperobject
+ it (get-category-view (car (mklist it)) category)
+ category strm (1+ indent) filter subobjects refvars
+ link-printer)))))
+
+
+(defun view-hyperobject (objs view strm &optional category (indent 0) filter
+ subobjects refvars link-printer)
+ "Display a single or list of hyperobject-class instances and their subobjects"
+ (let-when (objlist (mklist objs))
+ (let ((nobjs (length objlist))
+ (*print-pretty* nil)
+ (*print-circle* nil)
+ (*print-escape* nil)
+ (*print-readably* nil)
+ (*print-length* nil)
+ (*print-level* nil))
+ (fmt-list-start (car objlist) view strm indent nobjs)
(dolist (obj objlist)
- (awhen (hyperobject-class-subobjects obj) ;; access list of functions
- (dolist (child-obj it) ;; for each child function
- (awhen (funcall (reader child-obj) obj)
- (load-all-subobjects it))))))
- objs))
-
-(defgeneric view-hyperobject (objs fmt strm
- &optional label english-only-function
- indent subobjects refvars))
-
-(defmethod view-hyperobject (objs (fmt dataformat) (strm stream)
- &optional (label nil) (indent 0)
- (english-only-function nil)
- (subobjects nil) (refvars nil))
-"Display a single or list of hyperobject-class instances and their subobjects"
- (when objs
- (setq objs (mklist objs))
- (let ((nobjs (length objs)))
- (fmt-list-start (car objs) fmt strm indent nobjs)
- (dolist (obj objs)
- (unless (and english-only-function
- (multiple-value-bind (eng term) (funcall english-only-function obj)
- (and term (not eng))))
- (fmt-obj-start obj fmt strm indent)
- (fmt-obj-data obj fmt strm (1+ indent) label refvars)
- (if subobjects
- (awhen (hyperobject-class-subobjects obj) ;; access list of functions
- (dolist (child-obj it) ;; for each child function
- (awhen (funcall (reader child-obj) obj) ;; access set of child objects
- (view-hyperobject it fmt strm label
- (1+ indent) english-only-function
- subobjects refvars)))))
- (fmt-obj-end obj fmt strm indent)))
- (fmt-list-end (car objs) fmt strm indent nobjs))
- t))
-
-
-(defun view (objs &key (os *standard-output*) (format :text)
- (label nil) (english-only-function nil)
- (subobjects nil) (file-wrapper t) (refvars nil))
- "EXPORTED Function: prints hyperobject-class objects. Simplies call to view-hyperobject"
- (let ((fmt (make-format-instance format)))
- (if file-wrapper
- (fmt-file-start fmt os))
- (when objs
- (view-hyperobject objs fmt os label 0 english-only-function subobjects refvars))
- (if file-wrapper
- (fmt-file-end fmt os)))
+ (awhen (printer view)
+ (funcall it obj strm))
+ (unless (and filter (not (funcall filter obj)))
+ (fmt-obj-start obj view strm indent)
+ (fmt-obj-data obj view strm (1+ indent) refvars link-printer)
+ (fmt-obj-end obj view strm indent)
+ (when subobjects
+ (fmt-subobj-start obj view strm indent)
+ (view-subobjects obj category strm indent filter subobjects
+ refvars link-printer)
+ (fmt-subobj-end obj view strm indent))))
+ (fmt-list-end (car objlist) view strm indent nobjs)))
objs)
+
+(defun view (objs &key (stream *standard-output*) category view
+ filter subobjects refvars file-wrapper link-printer)
+ "EXPORTED Function: prints hyperobject-class objects. Calls view-hyperobject"
+ (let-when (objlist (mklist objs))
+ (when category
+ (setq view (get-category-view (car objlist) category)))
+ (unless view
+ (setq view (default-view (class-of (car objlist)))))
+ (when file-wrapper
+ (fmt-file-start view stream))
+ (view-hyperobject objlist view stream category 0 filter subobjects refvars
+ link-printer)
+ (when file-wrapper
+ (fmt-file-end view stream)))
+ objs)
+
+
+;;; Misc formatting
+
+(defun fmt-comma-integer (i)
+ (format nil "~:d" i))