Revision: 11513
          http://bibdesk.svn.sourceforge.net/bibdesk/?rev=11513&view=rev
Author:   amaxwell
Date:     2007-11-09 12:29:24 -0800 (Fri, 09 Nov 2007)

Log Message:
-----------
Fix some error handling in the web group, mainly in Google Scholar.  The others 
should be checked as well.

One of these was a potential crash (returning nil without setting the 
NSError**).

Modified Paths:
--------------
    trunk/bibdesk/BDSKGoogleScholarParser.m
    trunk/bibdesk/BDSKWebGroupViewController.m
    trunk/bibdesk/BDSKWebParser.m

Modified: trunk/bibdesk/BDSKGoogleScholarParser.m
===================================================================
--- trunk/bibdesk/BDSKGoogleScholarParser.m     2007-11-09 20:28:45 UTC (rev 
11512)
+++ trunk/bibdesk/BDSKGoogleScholarParser.m     2007-11-09 20:29:24 UTC (rev 
11513)
@@ -39,6 +39,7 @@
 #import <WebKit/WebKit.h>
 #import "BibItem.h"
 #import "BDSKBibTeXParser.h"
+#import "NSError_BDSKExtensions.h"
 
 @implementation BDSKGoogleScholarParser
 
@@ -75,11 +76,24 @@
     NSArray *BibTeXLinkNodes = [[xmlDocument rootElement] 
nodesForXPath:BibTexLinkNodePath
                                                     error:&error];
     
+    // bail out with an XML error if the Xpath query fails
+    if (nil == BibTeXLinkNodes) {
+        if (outError) *outError = error;
+        return nil;
+    }
     
+    unsigned int i, iMax = [BibTeXLinkNodes count];
+    
+    // check the number of nodes first
+    if (0 == iMax) {
+        error = [NSError mutableLocalErrorWithCode:kBDSKUnknownError 
localizedDescription:NSLocalizedString(@"No BibTeX links found", @"Google 
scholar error")];
+        [error setValue:NSLocalizedString(@"Unable to parse this page.  Please 
report this to BibDesk's developers and provide the URL.", @"Google scholar 
error")];
+        if (outError) *outError = error;
+        return nil;
+    }
+    
+    for(i=0; i < iMax; i++){
         
-    unsigned int i;
-    for(i=0; i < [BibTeXLinkNodes count]; i++){
-        
         NSXMLNode *btlinknode = [BibTeXLinkNodes objectAtIndex:i];
         
         NSString *hrefValue = [btlinknode stringValueOfAttribute:@"href"];
@@ -140,7 +154,8 @@
     }
     
     if (0 == [items count]) {
-        // signal an error condition
+        // signal an error condition; this page had BibTeX links, but we were 
unable to parse anything
+        // the BibTeX parser /should/ have set the NSError
         items = nil;
         if (outError)
             *outError = error;

Modified: trunk/bibdesk/BDSKWebGroupViewController.m
===================================================================
--- trunk/bibdesk/BDSKWebGroupViewController.m  2007-11-09 20:28:45 UTC (rev 
11512)
+++ trunk/bibdesk/BDSKWebGroupViewController.m  2007-11-09 20:29:24 UTC (rev 
11513)
@@ -180,8 +180,11 @@
     
     NSError *error = nil;
     NSArray *newPubs = [BDSKWebParser itemsFromDocument:domDocument 
fromURL:url error:&error];
-    if (nil == newPubs)
+    if (nil == newPubs) {
+#warning remove for release
+        NSLog(@"-[%@ [EMAIL PROTECTED] %@", [self class], 
NSStringFromSelector(_cmd), error);
         [NSApp presentError:error];
+    }
         
     if (frame == loadingWebFrame) {
         [self setRetrieving:NO];
@@ -193,12 +196,14 @@
 }
 
 - (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError 
*)error forFrame:(WebFrame *)frame{
+#warning is this really a failure?
     if (frame == loadingWebFrame) {
         [self setRetrieving:NO];
         [group addPublications:nil];
         loadingWebFrame = nil;
     }
-    [NSApp presentError:error];
+#warning remove for release
+    NSLog(@"-[%@ [EMAIL PROTECTED] %@", [self class], 
NSStringFromSelector(_cmd), error);
 }
 
 - (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error 
forFrame:(WebFrame *)frame{
@@ -207,6 +212,9 @@
         [group addPublications:nil];
         loadingWebFrame = nil;
     }
+#warning remove for release
+    NSLog(@"-[%@ [EMAIL PROTECTED] %@", [self class], 
NSStringFromSelector(_cmd), error);
+    [NSApp presentError:error];
 }
 
 - (void)webView:(WebView *)sender 
didReceiveServerRedirectForProvisionalLoadForFrame:(WebFrame *)frame{

Modified: trunk/bibdesk/BDSKWebParser.m
===================================================================
--- trunk/bibdesk/BDSKWebParser.m       2007-11-09 20:28:45 UTC (rev 11512)
+++ trunk/bibdesk/BDSKWebParser.m       2007-11-09 20:29:24 UTC (rev 11513)
@@ -13,6 +13,7 @@
 #import "BDSKACMDLParser.h"
 #import "BDSKHubmedParser.h"
 #import "BDSKGoogleScholarParser.h"
+#import "NSError_BDSKExtensions.h"
 
 @implementation BDSKWebParser
 
@@ -68,16 +69,27 @@
     
     parserClass = webParserClassForType(webType);
     
+    // don't return nil here; this may be the Google Scholar homepage or 
something, and we don't want to display an error message for it
+    // this may lead to some false negatives if the heuristics for 
canParseDocument::: change.
+    if (Nil == parserClass)
+        return [NSArray array];
     
     return [parserClass itemsFromDocument:domDocument xmlDocument:xmlDocument 
fromURL:url error:outError];
 }
 
+// entry point from view controller
 + (NSArray *)itemsFromDocument:(DOMDocument *)domDocument fromURL:(NSURL *)url 
error:(NSError **)outError{
     NSError *error = nil;    
     
     NSString *htmlString = [(id)[domDocument documentElement] outerHTML];
-    if (nil == htmlString)
+    if (nil == htmlString) {
+        if (outError) {
+            *outError = [NSError mutableLocalErrorWithCode:kBDSKUnknownError 
localizedDescription:NSLocalizedString(@"Failed to read HTML string from 
document", @"web view error; should never occur")];
+            [*outError setValue:NSLocalizedString(@"Please inform the 
developer of this error and provide the URL.", @"web view error") 
+                         forKey:NSLocalizedRecoverySuggestionErrorKey];
+        }
         return nil;
+    }
     
     NSXMLDocument *xmlDoc = [[NSXMLDocument alloc] initWithXMLString:htmlString
                                                              
options:NSXMLDocumentTidyHTML error:&error];


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to