- (let ((fmtstr-text "")
- (fmtstr-html "")
- (fmtstr-xml "")
- (fmtstr-text-labels "")
- (fmtstr-html-labels "")
- (fmtstr-xml-labels "")
- (fmtstr-html-ref "")
- (fmtstr-xml-ref "")
- (fmtstr-html-ref-labels "")
- (fmtstr-xml-ref-labels "")
- (first-field t)
- (value-func '())
- (xmlvalue-func '())
- (classname (class-name cl))
- (package (symbol-package (class-name cl)))
- (hyperlinks nil))
- (declare (ignore classname))
- (check-type (slot-value cl 'print-slots) list)
- (dolist (slot-name (slot-value cl 'print-slots))
- (let ((slot (find-slot-by-name cl slot-name)))
- (unless slot
- (error "Slot ~A is not found in class ~S" slot-name cl))
- (let ((name (slot-definition-name slot))
- (namestr (symbol-name (slot-definition-name slot)))
- (namestr-lower (string-downcase (symbol-name (slot-definition-name slot))))
- (type (slot-value slot 'ho-type))
- (print-formatter (slot-value slot 'print-formatter))
- (value-fmt "~a")
- (plain-value-func nil)
- html-str xml-str html-label-str xml-label-str)
-
- (when (or (eql type :integer) (eql type :fixnum))
- (setq value-fmt "~d"))
-
- (when (eql type :boolean)
- (setq value-fmt "~a"))
-
- (if first-field
- (setq first-field nil)
- (progn
- (string-append fmtstr-text " ")
- (string-append fmtstr-html " ")
- (string-append fmtstr-xml " ")
- (string-append fmtstr-text-labels " ")
- (string-append fmtstr-html-labels " ")
- (string-append fmtstr-xml-labels " ")
- (string-append fmtstr-html-ref " ")
- (string-append fmtstr-xml-ref " ")
- (string-append fmtstr-html-ref-labels " ")
- (string-append fmtstr-xml-ref-labels " ")))
-
- (setq html-str (concatenate 'string "<span class=\"" namestr-lower "\">" value-fmt "</span>"))
- (setq xml-str (concatenate 'string "<" namestr-lower ">" value-fmt "</" namestr-lower ">"))
- (setq html-label-str (concatenate 'string "<span class=\"label\">" namestr-lower "</span> <span class=\"" namestr-lower "\">" value-fmt "</span>"))
- (setq xml-label-str (concatenate 'string "<label>" namestr-lower "</label> <" namestr-lower ">" value-fmt "</" namestr-lower ">"))
-
- (string-append fmtstr-text value-fmt)
- (string-append fmtstr-html html-str)
- (string-append fmtstr-xml xml-str)
- (string-append fmtstr-text-labels namestr-lower " " value-fmt)
- (string-append fmtstr-html-labels html-label-str)
- (string-append fmtstr-xml-labels xml-label-str)
-
- (if (slot-value slot 'hyperlink)
- (progn
- (string-append fmtstr-html-ref "<~~a>" value-fmt "</~~a>")
- (string-append fmtstr-xml-ref "<~~a>" value-fmt "</~~a>")
- (string-append fmtstr-html-ref-labels "<span class=\"label\">" namestr-lower "</span> <~~a>" value-fmt "</~~a>")
- (string-append fmtstr-xml-ref-labels "<label>" namestr-lower "</label> <~~a>" value-fmt "</~~a>")
- (push (make-instance 'hyperlink :name name
- :lookup (slot-value slot 'hyperlink))
- hyperlinks))
- (progn
- (string-append fmtstr-html-ref html-str)
- (string-append fmtstr-xml-ref xml-str)
- (string-append fmtstr-html-ref-labels html-label-str)
- (string-append fmtstr-xml-ref-labels xml-label-str)))
-
- (if print-formatter
- (setq plain-value-func
- (list `(,print-formatter (slot-value x ',(intern namestr package)))))
- (setq plain-value-func
- (list `(slot-value x ',(intern namestr package)))))
- (setq value-func (append value-func plain-value-func))
-
- (if (eql type :cdata)
- (setq xmlvalue-func (append xmlvalue-func (list `(xml-cdata ,@plain-value-func))))
- (setq xmlvalue-func (append xmlvalue-func plain-value-func)))
- )))
-
- (setf (slot-value cl 'hyperlinks) hyperlinks)
-
- (if value-func
- (setq value-func `(lambda (x) (values ,@value-func)))
- (setq value-func `(lambda () (values))))
- (setq value-func (compile nil (eval value-func)))
-
- (if xmlvalue-func
- (setq xmlvalue-func `(lambda (x) (values ,@xmlvalue-func)))
- (setq xmlvalue-func `(lambda () (values))))
- (setq xmlvalue-func (compile nil (eval xmlvalue-func)))
-
- (setf (slot-value cl 'fmtstr-text) fmtstr-text)
- (setf (slot-value cl 'fmtstr-html) fmtstr-html)
- (setf (slot-value cl 'fmtstr-xml) fmtstr-xml)
- (setf (slot-value cl 'fmtstr-text-labels) fmtstr-text-labels)
- (setf (slot-value cl 'fmtstr-html-labels) fmtstr-html-labels)
- (setf (slot-value cl 'fmtstr-xml-labels) fmtstr-xml-labels)
- (setf (slot-value cl 'fmtstr-html-ref) fmtstr-html-ref)
- (setf (slot-value cl 'fmtstr-xml-ref) fmtstr-xml-ref)
- (setf (slot-value cl 'fmtstr-html-ref-labels) fmtstr-html-ref-labels)
- (setf (slot-value cl 'fmtstr-xml-ref-labels) fmtstr-xml-ref-labels)
- (setf (slot-value cl 'value-func) value-func)
- (setf (slot-value cl 'xmlvalue-func) xmlvalue-func))
- (values))
+ (let ((obj-cl (find-class (object-class view))))
+ (cond
+ ((source-code view)
+ (initialize-view-by-source-code view))
+ ((id view)
+ (initialize-view-by-id obj-cl view))
+ (t
+ (setf (id view) :compact-text)
+ (initialize-view-by-id obj-cl view)))))
+
+
+
+(defun initialize-view-by-source-code (view)
+ "Initialize a view based upon a source code"
+ (let* ((source-code (source-code view))
+ (printer `(lambda
+ (,(intern (symbol-name '#:self)
+ (symbol-package (object-class view)))
+ ,(intern (symbol-name '#:s)
+ (symbol-package (object-class view))))
+ (declare (ignorable
+ ,(intern (symbol-name '#:self)
+ (symbol-package (object-class view)))
+ ,(intern (symbol-name '#:s)
+ (symbol-package (object-class view)))))
+ (with-slots ,(slots view)
+ ,(intern (symbol-name '#:self)
+ (symbol-package (object-class view)))
+ ,@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-cdata fmt-data strm)
+ (write-simple fmt-data strm))))
+
+(defun ppfc-html (title name type formatter cdata print-func)
+ (vector-push-extend '(write-string "<span class=\"" s) print-func)
+ (vector-push-extend `(write-string ,title s) 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 "</span>" 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-display-table (title name type formatter cdata print-func)
+ (vector-push-extend '(write-string "<td>" s) print-func)
+ (ppfc-html title name type formatter cdata print-func)
+ (vector-push-extend '(write-string "</td>" s) print-func))
+
+(defun ppfc-html-labels (label name type formatter cdata print-func)
+ (vector-push-extend '(write-string "<span class=\"label\">" s) print-func)
+ (vector-push-extend `(write-string ,label s) print-func)
+ (vector-push-extend '(write-string "</span> " 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 "<span class=\"label\">" s) print-func)
+ (vector-push-extend `(write-string ,label s) print-func)
+ (vector-push-extend '(write-string "</span> " 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 "<label>" s) print-func)
+ (vector-push-extend `(write-string ,label s) print-func)
+ (vector-push-extend '(write-string "</label> " 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 "<span class=\"label\">" s) print-func)
+ (vector-push-extend `(write-string ,label s) print-func)
+ (vector-push-extend '(write-string "</span> " s) print-func)
+ (ppfc-html-link name type formatter cdata nlink print-func))
+
+(defun push-print-fun-code (vid 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-definition-type slot))
+
+ (cdata (not (null
+ (and (in vid :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)))
+
+ (case vid
+ (: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))
+ ((:display-table :display-table-labels)
+ (ppfc-display-table 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 (id view) :html-link :xhtml-link :xml-link :ie-xml-link
+ :html-link-labels :xhtml-link-labels :xml-link-labels
+ :ie-xml-link-labels))
+
+(defun creatable-view-id-p (obj-cl vid)
+ "Returns T if a view id can be created for this class"
+ (declare (ignore obj-cl))
+ (in vid :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 :display-table-labels :edit-table :edit-table-labels))
+
+(defun initialize-view-by-id (obj-cl view)
+ "Initialize a view based upon a preset vid"
+ (unless (creatable-view-id-p obj-cl (id view))
+ (error "Unable to automatically create view id ~A" (id view)))
+
+ (unless (slots view) (setf (slots view) (default-print-slots obj-cl)))
+
+ (let ((links '())
+ (print-func (make-array 20 :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 (id 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-id view)
+ view)
+
+(defun finalize-view-by-id (view)
+ (case (id 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 :display-table :display-table-labels :edit-tables)
+ (initialize-table-view view)
+ (when (in (id view) :display-table-labels :edit-table-labels)
+ (setf (list-start-printer view) #'table-label-list-start-func))
+ (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) "&"))))