Revision: 28020
          http://sourceforge.net/p/bibdesk/svn/28020
Author:   hofman
Date:     2022-10-13 14:07:55 +0000 (Thu, 13 Oct 2022)
Log Message:
-----------
allow requesting monitor for URL from web parser without parsing, pass flag to 
monitor in error userinfo. No need to return dummy error item to let the user 
reload for INSPIRE parser.

Modified Paths:
--------------
    trunk/bibdesk/BDSKInspireParser.m
    trunk/bibdesk/BDSKWebGroup.m
    trunk/bibdesk/BDSKWebParser.h
    trunk/bibdesk/BDSKWebParser.m
    trunk/bibdesk/de.lproj/Localizable.strings
    trunk/bibdesk/en.lproj/Localizable.strings
    trunk/bibdesk/fr.lproj/Localizable.strings

Modified: trunk/bibdesk/BDSKInspireParser.m
===================================================================
--- trunk/bibdesk/BDSKInspireParser.m   2022-10-13 06:30:20 UTC (rev 28019)
+++ trunk/bibdesk/BDSKInspireParser.m   2022-10-13 14:07:55 UTC (rev 28020)
@@ -65,46 +65,31 @@
 }
 
 + (BOOL)canParseDocument:(DOMDocument *)domDocument fromURL:(NSURL *)url {
-    NSString *host = [[url host] lowercaseString];
-    if ([host isEqualToString:@"inspirehep.net"]) {
-        return YES;
-        //return [url hasFirstPathComponent:@"literature"] && ([[url query] 
length] > 0 || [[url path] length] > 12);
-    }
-    return NO;
+    return [[[url host] lowercaseString] isEqualToString:@"inspirehep.net"] &&
+            [url hasFirstPathComponent:@"literature"] &&
+            ([[url query] length] > 0 || [[url path] length] > 12);
 }
 
++ (BOOL)wantsMonitoringURL:(NSURL *)url {
+    return [[[url host] lowercaseString] isEqualToString:@"inspirehep.net"];
+}
+
 - (NSArray *)itemsReturningError:(NSError **)outError {
-    if ([[[self URL] host] isCaseInsensitiveEqual:@"inspirehep.net"]) {
-        if ([[self URL] hasFirstPathComponent:@"literature"] &&
-            ([[[self URL] query] length] > 0 || [[[self URL] path] length] > 
12)) {
-            NSMutableString *downloadURLString = [[[self URL] absoluteString] 
mutableCopy];
-            NSUInteger idx = [downloadURLString 
rangeOfString:@"/literature"].location;
-            
-            if (idx != NSNotFound) {
-                [downloadURLString insertString:@"/api" atIndex:idx];
-                NSURL *downloadURL = [NSURL URLWithString:downloadURLString];
-                [downloadURLString release];
-                NSMutableURLRequest *request = [NSMutableURLRequest 
requestWithURL:downloadURL];
-                [request setValue:@"application/x-bibtex" 
forHTTPHeaderField:@"Accept"];
-                
-                [self addDownloadWithRequest:request contextInfo:nil];
-            }
-            return nil;
-        } else {
-           if (outError)
-               *outError = [NSError localErrorWithCode:kBDSKWebParserFailed 
localizedDescription:NSLocalizedString(@"You need to Reload to get the items.", 
@"Error description")];
-           NSMutableDictionary *pubFields = [NSMutableDictionary 
dictionaryWithObjectsAndKeys:NSLocalizedString(@"You need to Reload to get the 
items.", @"Error description"), BDSKTitleString, [[self URL] absoluteString], 
BDSKUrlString, nil];
-           BibItem *errorItem = [[BibItem alloc] initWithType:BDSKMiscString 
citeKey:nil pubFields:pubFields];
-           NSArray *items = [NSArray arrayWithObjects:errorItem, nil];
-           [errorItem release];
-           return items;
-        }
+    NSMutableString *downloadURLString = [[[self URL] absoluteString] 
mutableCopy];
+    NSUInteger idx = [downloadURLString rangeOfString:@"/literature"].location;
+
+    if (idx != NSNotFound) {
+        [downloadURLString insertString:@"/api" atIndex:idx];
+        NSURL *downloadURL = [NSURL URLWithString:downloadURLString];
+        [downloadURLString release];
+        NSMutableURLRequest *request = [NSMutableURLRequest 
requestWithURL:downloadURL];
+        [request setValue:@"application/x-bibtex" 
forHTTPHeaderField:@"Accept"];
+        
+        [self addDownloadWithRequest:request contextInfo:nil];
     }
     return nil;
 }
 
-- (BOOL)shouldMonitorURL { return YES; }
-
 + (NSString *)name {return @"INSPIRE"; }
 
 + (NSString *)address { return @"https://inspirehep.net/";; }

Modified: trunk/bibdesk/BDSKWebGroup.m
===================================================================
--- trunk/bibdesk/BDSKWebGroup.m        2022-10-13 06:30:20 UTC (rev 28019)
+++ trunk/bibdesk/BDSKWebGroup.m        2022-10-13 14:07:55 UTC (rev 28020)
@@ -264,11 +264,12 @@
         // the parser can finish immediately
         [parser startWithDelegate:self];
         
-        if ([parser shouldMonitorURL])
+        if ([[parser class] wantsMonitoringURL:url])
             [webView monitorURL];
         
     } else {
         
+        BOOL wantsMonitoringURL = [[[error userInfo] 
objectForKey:BDSKWantsMonitoringURLKey] boolValue];
         NSArray *newPubs = nil;
         WebDataSource *dataSource = [frame dataSource];
         NSString *MIMEType = [[dataSource mainResource] MIMEType];
@@ -311,6 +312,9 @@
             [self notifyUpdate];
         if (frame != [sender mainFrame] && [newPubs count] > 0)
             [self setObject:[[newPubs mutableCopy] autorelease] 
forKey:PUBLICATIONS_KEY forFrame:frame];
+        
+        if (wantsMonitoringURL)
+            [webView monitorURL];
     }
 }
 

Modified: trunk/bibdesk/BDSKWebParser.h
===================================================================
--- trunk/bibdesk/BDSKWebParser.h       2022-10-13 06:30:20 UTC (rev 28019)
+++ trunk/bibdesk/BDSKWebParser.h       2022-10-13 14:07:55 UTC (rev 28020)
@@ -40,6 +40,7 @@
 #import <WebKit/WebKit.h>
 #import "BDSKOwnerProtocol.h"
 
+extern NSString *BDSKWantsMonitoringURLKey;
 
 typedef NS_ENUM(NSInteger, BDSKParserFeature) {
     BDSKParserFeaturePublic = 0,
@@ -77,8 +78,6 @@
 // set at the end of -start, to know thereis not more coming, so we may finish
 @property (nonatomic) BOOL finishedStarting;
 
-@property (nonatomic) BOOL shouldMonitorURL;
-
 - (id)initWithDocument:(DOMDocument *)aDomDocument fromURL:(NSURL *)aURL;
 
 - (void)startWithDelegate:(id<BDSKWebParserDelegate>)aDelegate;
@@ -90,6 +89,9 @@
 // this must be implemented by subclasses
 + (BOOL)canParseDocument:(DOMDocument *)domDocument fromURL:(NSURL *)url;
 
+// this may be overridden by subclasses
++ (BOOL)wantsMonitoringURL:(NSURL *)url;
+
 // main method for subclasses to implement
 - (NSArray *)itemsReturningError:(NSError **)outError;
 

Modified: trunk/bibdesk/BDSKWebParser.m
===================================================================
--- trunk/bibdesk/BDSKWebParser.m       2022-10-13 06:30:20 UTC (rev 28019)
+++ trunk/bibdesk/BDSKWebParser.m       2022-10-13 14:07:55 UTC (rev 28020)
@@ -67,10 +67,11 @@
 
 #define BDSKEnableGoogleScholarWebParserKey @"BDSKEnableGoogleScholarWebParser"
 
+NSString *BDSKWantsMonitoringURLKey = @"BDSKWantsMonitoringURL";
+
 @implementation BDSKWebParser
 
 @synthesize domDocument, URL, delegate, finishedStarting;
-@dynamic shouldMonitorURL;
 
 + (NSArray *)parsers {
     static NSArray *webParsers = nil;
@@ -131,9 +132,12 @@
 + (BDSKWebParser *)parserForDocument:(DOMDocument *)domDocument fromURL:(NSURL 
*)url error:(NSError **)outError{
     
     Class parserClass = Nil;
+    BOOL wantsMonitoringURL = NO;
     for (parserClass in [self parsers]) {
         if ([parserClass canParseDocument:domDocument fromURL:url])
             break;
+        else if ([parserClass wantsMonitoringURL:url])
+            wantsMonitoringURL = YES;
     }
     
     // this may lead to some false negatives if the heuristics for 
canParseDocument::: change.
@@ -141,6 +145,8 @@
         if (outError) {
             *outError = [NSError 
mutableLocalErrorWithCode:kBDSKWebParserUnsupported 
localizedDescription:NSLocalizedString(@"Unsupported URL", @"error when parsing 
text fails")];
             [*outError setValue:NSLocalizedString(@"BibDesk was not able to 
find a parser for this web page.", @"error description") 
forKey:NSLocalizedRecoverySuggestionErrorKey];
+            if (wantsMonitoringURL)
+                [*outError setValue:[NSNumber 
numberWithBool:wantsMonitoringURL] forKey:BDSKWantsMonitoringURLKey];
         }
         return nil;
     }
@@ -150,6 +156,8 @@
     return [[[parserClass alloc] initWithDocument:domDocument fromURL:url] 
autorelease];
 }
 
++ (BOOL)wantsMonitoringURL:(NSURL *)url { return NO; }
+
 + (NSArray *)parsersForFeature:(BDSKParserFeature)feature {
     return [[self parsers] filteredArrayUsingPredicate:[NSPredicate 
predicateWithFormat:@"feature = %lu", (unsigned long)feature]];
 }
@@ -225,7 +233,7 @@
 
 - (NSArray *)itemsReturningError:(NSError **)outError { return nil; }
 
-- (BOOL)shouldMonitorURL { return NO; }
+- (BOOL)wantsMonitoringURL { return NO; }
 
 + (BOOL)canParseDocument:(DOMDocument *)domDocument fromURL:(NSURL *)url { 
return NO; }
 

Modified: trunk/bibdesk/de.lproj/Localizable.strings
===================================================================
(Binary files differ)

Modified: trunk/bibdesk/en.lproj/Localizable.strings
===================================================================
(Binary files differ)

Modified: trunk/bibdesk/fr.lproj/Localizable.strings
===================================================================
(Binary files differ)

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



_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to