Revision: 24771
          http://sourceforge.net/p/bibdesk/svn/24771
Author:   hofman
Date:     2020-05-12 22:12:04 +0000 (Tue, 12 May 2020)
Log Message:
-----------
check import command input before creating the handler

Modified Paths:
--------------
    trunk/bibdesk/BibDocument+Scripting.m

Modified: trunk/bibdesk/BibDocument+Scripting.m
===================================================================
--- trunk/bibdesk/BibDocument+Scripting.m       2020-05-12 21:45:10 UTC (rev 
24770)
+++ trunk/bibdesk/BibDocument+Scripting.m       2020-05-12 22:12:04 UTC (rev 
24771)
@@ -1079,6 +1079,7 @@
     id overwrite = [params objectForKey:@"overwrite"];
     BOOL synchronous = [wait respondsToSelector:@selector(boolValue)] ? [wait 
boolValue] : (mergePub == nil);
     NSArray *pubs = nil;
+    BDSKServerInfo *serverInfo = nil;
     
     if ([url isKindOfClass:[NSString class]]) {
         url = [url hasPrefix:@"/"] ? [NSURL fileURLWithPath:url 
isDirectory:NO] : [NSURL URLWithString:url];
@@ -1103,6 +1104,45 @@
         return nil;
     }
     
+    // make sure we get something valid as input
+    if (string == nil) {
+        if (searchTerm) {
+            id server = [params objectForKey:@"server"];
+            if (server || synchronous == NO) {
+                // the server can be a scriptingServerInfo dictionary, a 
.bdsksearch file URL, a x-bdsk-search URL, or a default server name
+                if ([server isKindOfClass:[NSDictionary class]]) {
+                    if ([server count] == 1 && [server objectForKey:@"name"])
+                        server = [server objectForKey:@"name"];
+                    else
+                        serverInfo = [[BDSKSearchGroup newServerInfo:nil 
withScriptingServerInfo:server] autorelease];
+                } else if ([server isKindOfClass:[NSURL class]]) {
+                    serverInfo = [[[BDSKServerInfo alloc] 
initWithDictionary:[NSDictionary dictionaryWithContentsOfURL:[NSURL 
fileURLWithPath:server isDirectory:NO]]] autorelease];
+                } else if ([server isKindOfClass:[NSString class]]) {
+                    if ([server hasPrefix:@"x-bdsk-search://"]) {
+                        serverInfo = [[[BDSKServerInfo alloc] 
initWithDictionary:[BDSKSearchGroup dictionaryWithBdsksearchURL:[NSURL 
URLWithString:server]]] autorelease];
+                    } else {
+                        NSArray *servers = [[BDSKSearchGroupServerManager 
sharedManager] servers];
+                        NSUInteger i = [[servers valueForKey:@"name"] 
indexOfObject:server];
+                        if (i != NSNotFound)
+                            serverInfo = [servers objectAtIndex:i];
+                    }
+                } else if (server == nil && synchronous == NO) {
+                    serverInfo = [[BDSKServerInfo alloc] 
initWithType:BDSKSearchGroupEntrez name:@"PubMed" database:@"pubmed" host:nil 
port:nil options:nil];
+                }
+                if (serverInfo == nil) {
+                    [command setScriptErrorNumber:NSArgumentsWrongScriptError];
+                    [command setScriptErrorString:@"Invalid search server 
data."];
+                    return nil;
+                }
+            }
+        } else if (url == nil) {
+            [command 
setScriptErrorNumber:NSRequiredArgumentsMissingScriptError];
+            [command setScriptErrorString:@"Either 'from', 'with, or 'for 
search term' must be specified."];
+            return nil;
+        }
+    }
+    
+    // construct the handler to process the pubs when we get them
     void (^handler)(NSArray *) = NULL;
     if (mergePub) {
         handler = ^(NSArray *result){
@@ -1141,38 +1181,11 @@
         };
     }
     
-    // make sure we get something
+    // finally get the pubs and process them
     if (string) {
         pubs = [BDSKStringParser itemsFromString:string 
ofType:BDSKStringTypeUnknown owner:self error:NULL];
     } else if (searchTerm) {
-        id server = [params objectForKey:@"server"];
-        if (server || synchronous == NO) {
-            BDSKServerInfo *serverInfo = nil;
-            // the server can be a scriptingServerInfo dictionary, a 
.bdsksearch file URL, a x-bdsk-search URL, or a default server name
-            if ([server isKindOfClass:[NSDictionary class]]) {
-                if ([server count] == 1 && [server objectForKey:@"name"])
-                    server = [server objectForKey:@"name"];
-                else
-                    serverInfo = [[BDSKSearchGroup newServerInfo:nil 
withScriptingServerInfo:server] autorelease];
-            } else if ([server isKindOfClass:[NSURL class]]) {
-                serverInfo = [[[BDSKServerInfo alloc] 
initWithDictionary:[NSDictionary dictionaryWithContentsOfURL:[NSURL 
fileURLWithPath:server isDirectory:NO]]] autorelease];
-            } else if ([server isKindOfClass:[NSString class]]) {
-                if ([server hasPrefix:@"x-bdsk-search://"]) {
-                    serverInfo = [[[BDSKServerInfo alloc] 
initWithDictionary:[BDSKSearchGroup dictionaryWithBdsksearchURL:[NSURL 
URLWithString:server]]] autorelease];
-                } else {
-                    NSArray *servers = [[BDSKSearchGroupServerManager 
sharedManager] servers];
-                    NSUInteger i = [[servers valueForKey:@"name"] 
indexOfObject:server];
-                    if (i != NSNotFound)
-                        serverInfo = [servers objectAtIndex:i];
-                }
-            } else if (server == nil && synchronous == NO) {
-                serverInfo = [[BDSKServerInfo alloc] 
initWithType:BDSKSearchGroupEntrez name:@"PubMed" database:@"pubmed" host:nil 
port:nil options:nil];
-            }
-            if (serverInfo == nil) {
-                [command setScriptErrorNumber:NSArgumentsWrongScriptError];
-                [command setScriptErrorString:@"Invalid search server data."];
-                return nil;
-            }
+        if (serverInfo) {
             BDSKGroupSearch *search = [[[BDSKGroupSearch alloc] 
initWithMacroResolver:[self macroResolver]] autorelease];
             if (synchronous) {
                 pubs = [search searchUsingSearchTerm:searchTerm 
serverInfo:serverInfo];
@@ -1184,19 +1197,10 @@
             pubs = [BDSKEntrezGroupServer itemsForSearchTerm:searchTerm 
usingDatabase:@"pubmed" allowMultiple:YES];
         }
     } else if (url) {
-        if ([url isKindOfClass:[NSURL class]]) {
-            if ([url isFileURL])
-                pubs = [NSArray arrayWithObjects:[BibItem itemWithFileURL:url 
owner:self], nil];
-            else
-                pubs = [NSArray arrayWithObjects:[BibItem itemWithURL:url 
title:nil owner:self], nil];
-        } else {
-            [command setScriptErrorNumber:NSArgumentsWrongScriptError];
-            return nil;
-        }
-    } else {
-        [command setScriptErrorNumber:NSRequiredArgumentsMissingScriptError];
-        [command setScriptErrorString:@"Either 'from', 'with, or 'for search 
term' must be specified."];
-        return nil;
+        if ([url isFileURL])
+            pubs = [NSArray arrayWithObjects:[BibItem itemWithFileURL:url 
owner:self], nil];
+        else
+            pubs = [NSArray arrayWithObjects:[BibItem itemWithURL:url 
title:nil owner:self], nil];
     }
     handler(pubs);
     

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



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to