r3357: remove load-compile-op from .asd file
[pubmed.git] / pubmed.lisp
index c20d11138bfb04c8299a8fae9b3bc5992ae52e12..1cdd1f6956fdca2b77fa92a57dd3e6ce524e1878 100644 (file)
@@ -7,7 +7,7 @@
 ;;;; Programmer:    Kevin M. Rosenberg
 ;;;; Date Started:  Jun 2001
 ;;;;
-;;;; $Id: pubmed.lisp,v 1.3 2002/10/31 19:42:26 kevin Exp $
+;;;; $Id: pubmed.lisp,v 1.7 2002/11/06 19:36:53 kevin Exp $
 ;;;;
 ;;;; This file, part of cl-pubmed, is Copyright (c) 2002 by Kevin M. Rosenberg
 ;;;;
@@ -26,6 +26,7 @@
 
    ;; Conditions
    #:pubmed-condition
+   #:pubmed-query-error
    #:pubmed-server-error
    
    ;; Query functions
             (format stream "A PubMed server error occurred.")
             (awhen (pubmed-server-error-response c)
                    (format stream " The server response was:~&~S" it)))))
+
+(define-condition pubmed-query-error (error pubmed-condition)
+  ((response :initarg :response
+            :initform nil
+            :reader pubmed-query-error-response))
+  (:report (lambda (c stream)
+            (format stream "A PubMed server error occurred.")
+            (awhen (pubmed-query-error-response c)
+                   (format stream " The server response was:~&~S" it)))))
   
 ;;; Article-Set and Article Classes
 
 
 (defmethod print-object ((obj pm-article) (s stream))
   (print-unreadable-object (obj s :type t :identity t)
-    (format s "pmid: ~d" (article-pmid obj))))
+    (format s "pmid:~d, title:~S" (article-pmid obj)
+           (article-title obj))))
 
 (defun article-equal-p (a b)
   (check-type a pm-article)
   "Display an article set to specified stream in specified format"
   (dotimes (i (articles-count artset))
     (print-article (nth i (articles artset)) :os os :format format 
-                 :complete complete :print-link print-link)))
+                  :complete complete :print-link print-link))
+  artset)
 
 (defmethod print-article ((art pm-article) &key (os *standard-output*) (format :text)
                                              (complete nil) (print-link t))
   "Display an article"
-  (if (eql format :text)
-      (format os "~a~%~a~%~a~a ~a~%~a~%" 
-             (article-title art)
-             (list-to-delimited-string (article-authors art) ", ")
-             (aif (article-affiliation art)
-                  (format nil "~a~%" it) "")
-             (article-journal art) (article-ref art)
-             (aif (article-abstract art) 
+  (ecase format
+    (:text
+     (format os "~a~%~a~%~a~a ~a~%~a~%" 
+            (article-title art)
+            (list-to-delimited-string (article-authors art) ", ")
+            (aif (article-affiliation art)
+                 (format nil "~a~%" it) "")
+            (article-journal art) (article-ref art)
+            (aif (article-abstract art) 
                  (if complete
                      it
                    "Abstract available") 
-               "No abstract available")
-             (when complete
-                 (format os "~a~%" (article-mesh-headings art))))
-    
-    (let ((has-link (or (article-abstract art) (article-mesh-headings art))))
-      (when (and print-link has-link)
-       (format os "<a href=\"~a?key=~a\">" (make-url "print-article") (article-pmid art)))
-      (format os "<div class=\"article-title\">~a</div>~%" (article-title art))
-      (when (and print-link has-link)
-       (format os "</a>"))
-      (format os "<div class=\"article-authors\">~a</div>~%"
-             (list-to-delimited-string (article-authors art) ", "))
-      (format os "<div class=\"article-reference\">~a ~a</div>~%" 
-             (article-journal art) (article-ref art))
-      (when (and complete (article-abstract art))
-       (format os "<div class=\"article-abstract\">~a</div>~%" 
-               (article-abstract art)))
-      (when (and complete (article-mesh-headings art))
-       (format os "<div class=\"mesh-heading-title\">Mesh Headings:</div>")
-       (dolist (mh (article-mesh-headings art))
-         (format os "<div class=\"mesh-heading\">~a</div>~%" mh)))
-      (format os "<p/>~%"))))
+                 "No abstract available")
+            (when complete
+              (format os "~a~%" (article-mesh-headings art)))))
+     (:html
+      (let ((has-link (or (article-abstract art) (article-mesh-headings art))))
+       (when (and print-link has-link)
+         (format os "<a href=\"~a?key=~a\">" (make-url "print-article") (article-pmid art)))
+       (format os "<div class=\"article-title\">~a</div>~%" (article-title art))
+       (when (and print-link has-link)
+         (format os "</a>"))
+       (format os "<div class=\"article-authors\">~a</div>~%"
+               (list-to-delimited-string (article-authors art) ", "))
+       (format os "<div class=\"article-reference\">~a ~a</div>~%" 
+               (article-journal art) (article-ref art))
+       (when (and complete (article-abstract art))
+         (format os "<div class=\"article-abstract\">~a</div>~%" 
+                 (article-abstract art)))
+       (when (and complete (article-mesh-headings art))
+         (format os "<div class=\"mesh-heading-title\">Mesh Headings:</div>")
+         (dolist (mh (article-mesh-headings art))
+           (format os "<div class=\"mesh-heading\">~a</div>~%" mh)))
+       (format os "<p/>~%"))))
+  art)
 
 
 ;;; PubMed Query Functions
@@ -290,12 +304,12 @@ XML string of PubMed search results and XML search status"
   "Extract article contents from PubMed XML string and return results in pm-article class"
   (let ((article (make-instance 'pm-article)))
     (setf 
-       (article-pmid article) (parse-integer (xml-tag-contents "PMID" xmlstr a-start))
-       (article-title article) (xml-tag-contents "ArticleTitle" xmlstr a-start)
-       (article-journal article) (xml-tag-contents "MedlineTA" xmlstr a-start)
-       (article-pages article) (xml-tag-contents "MedlinePgn" xmlstr a-start)
-       (article-affiliation article) (xml-tag-contents "Affiliation" xmlstr a-start)
-       (article-abstract article) (xml-tag-contents "AbstractText" xmlstr a-start))
+       (article-pmid article) (parse-integer (xml-tag-contents "PMID" xmlstr a-start a-end))
+       (article-title article) (xml-tag-contents "ArticleTitle" xmlstr a-start a-end)
+       (article-journal article) (xml-tag-contents "MedlineTA" xmlstr a-start a-end)
+       (article-pages article) (xml-tag-contents "MedlinePgn" xmlstr a-start a-end)
+       (article-affiliation article) (xml-tag-contents "Affiliation" xmlstr a-start a-end)
+       (article-abstract article) (xml-tag-contents "AbstractText" xmlstr a-start a-end))
     (multiple-value-bind (ji-start ji-end ji-next)
        (positions-xml-tag-contents "JournalIssue" xmlstr a-start a-end)
       (declare (ignore ji-next))
@@ -367,11 +381,14 @@ XML string of PubMed search results and XML search status"
 
 (defun extract-pmid-list (results)
   "Returns list of PubMed ID's from XML result string"
-  (if (or (search "<ERROR>" results)
-         (search "<H1>Server Error</H1>" results))
-      (error 'pubmed-server-error :response results)
+  (cond
+   ((search "<ERROR>" results)
+    (error 'pubmed-query-error :response results))
+   ((search "<H1>Server Error</H1>" results)
+    (error 'pubmed-server-error :response results))
+   (t
     (awhen (xml-tag-contents "Id" results)
-          (delimited-string-to-list it #\space))))
+          (delimited-string-to-list it #\space)))))