Revision: 22531
          http://sourceforge.net/p/bibdesk/svn/22531
Author:   hofman
Date:     2018-08-26 15:51:33 +0000 (Sun, 26 Aug 2018)
Log Message:
-----------
Web parser helper method to get items from a bibtex string

Modified Paths:
--------------
    trunk/bibdesk/BDSKAsynchronousWebParser.m
    trunk/bibdesk/BDSKBibTeXWebParser.m
    trunk/bibdesk/BDSKInspireParser.m
    trunk/bibdesk/BDSKMathSciNetParser.h
    trunk/bibdesk/BDSKMathSciNetParser.m
    trunk/bibdesk/BDSKNumdamParser.m
    trunk/bibdesk/BDSKProjectEuclidParser.m
    trunk/bibdesk/BDSKWebParser.h
    trunk/bibdesk/BDSKWebParser.m
    trunk/bibdesk/BDSKZentralblattParser.h
    trunk/bibdesk/BDSKZentralblattParser.m

Modified: trunk/bibdesk/BDSKAsynchronousWebParser.m
===================================================================
--- trunk/bibdesk/BDSKAsynchronousWebParser.m   2018-08-26 11:59:34 UTC (rev 
22530)
+++ trunk/bibdesk/BDSKAsynchronousWebParser.m   2018-08-26 15:51:33 UTC (rev 
22531)
@@ -38,8 +38,6 @@
 
 #import "BDSKAsynchronousWebParser.h"
 #import "BibItem.h"
-#import "BDSKBibTeXParser.h"
-#import "NSError_BDSKExtensions.h"
 #import "DOMNode_BDSKExtensions.h"
 #import "NSURL_BDSKExtensions.h"
 
@@ -157,23 +155,15 @@
 
 - (NSArray *)itemsFromDownload:(BDSKCitationDownload *)download error:(NSError 
**)outError {
     NSString *bibtexString = [self bibTeXStringFromDownload:download];
-    BibItem *bibtexItem = nil;
-    NSArray *bibtexItems = nil;
-    NSError *error = nil;
+    NSArray *bibtexItems = [self itemsFromBibTeXString:bibtexString 
error:outError];
+    BibItem *bibtexItem;
     
-    if (bibtexString)
-        bibtexItems = [BDSKBibTeXParser itemsFromString:bibtexString 
owner:[self delegate] error:&error];
-    if ([bibtexItems count] && [error 
isLocalErrorWithCode:kBDSKBibTeXParserFailed] == NO) {
-        bibtexItem = [bibtexItems firstObject];
-        if ([[download contextInfo] isKindOfClass:[NSString class]] &&
-            [NSString isEmptyString:[bibtexItem valueOfField:BDSKUrlString 
inherit:NO]])
-            [bibtexItem setField:BDSKUrlString toValue:[download contextInfo]];
-            
-    } else if (outError) {
-        *outError = error;
-    }
+    if ([[download contextInfo] isKindOfClass:[NSString class]] &&
+        (bibtexItem = [bibtexItems firstObject]) &&
+        [NSString isEmptyString:[bibtexItem valueOfField:BDSKUrlString 
inherit:NO]])
+        [bibtexItem setField:BDSKUrlString toValue:[download contextInfo]];
     
-    return [NSArray arrayWithObjects:bibtexItem, nil];
+    return bibtexItems;
 }
 
 - (NSString *)bibTeXStringFromDownload:(BDSKCitationDownload *)download {

Modified: trunk/bibdesk/BDSKBibTeXWebParser.m
===================================================================
--- trunk/bibdesk/BDSKBibTeXWebParser.m 2018-08-26 11:59:34 UTC (rev 22530)
+++ trunk/bibdesk/BDSKBibTeXWebParser.m 2018-08-26 15:51:33 UTC (rev 22531)
@@ -37,10 +37,8 @@
  */
 
 #import "BDSKBibTeXWebParser.h"
-#import "BDSKBibTeXParser.h"
 #import "BibItem.h"
 #import "DOMNode_BDSKExtensions.h"
-#import "NSError_BDSKExtensions.h"
 #import <AGRegex/AGRegex.h>
 
 
@@ -71,15 +69,7 @@
 
         if (closeLoc != NSNotFound) {
             range.length = closeLoc + 1 - range.location;
-            NSString *string = [text substringWithRange:range];
-            
-            if ([BDSKBibTeXParser canParseString:string]) {
-                NSError *error = nil;
-                NSArray *bibtexItems = [BDSKBibTeXParser 
itemsFromString:string owner:[self delegate] error:&error];
-                
-                if ([bibtexItems count] == 1 && [error 
isLocalErrorWithCode:kBDSKBibTeXParserFailed] == NO)
-                    [items addObjectsFromArray:bibtexItems];
-            }
+            [items addObjectsFromArray:[self itemsFromBibTeXString:[text 
substringWithRange:range] error:NULL]];
         }
        }
        

Modified: trunk/bibdesk/BDSKInspireParser.m
===================================================================
--- trunk/bibdesk/BDSKInspireParser.m   2018-08-26 11:59:34 UTC (rev 22530)
+++ trunk/bibdesk/BDSKInspireParser.m   2018-08-26 15:51:33 UTC (rev 22531)
@@ -37,8 +37,6 @@
  */
 
 #import "BDSKInspireParser.h"
-#import "BDSKBibTeXParser.h"
-#import "NSError_BDSKExtensions.h"
 #import "DOMNode_BDSKExtensions.h"
 #import "NSURL_BDSKExtensions.h"
 
@@ -72,25 +70,9 @@
 
 - (NSArray *)itemsReturningError:(NSError **)outError {
     if ([[self URL] hasFirstPathComponent:@"record"]) {
-        NSError *error = nil;
-        NSMutableArray *items = [NSMutableArray array];
-        NSString *bibtexString = nil;
-        
         DOMNode *preNode = [[[self domDocument] documentElement] 
singleNodeForXPath:@"./body/div/div/pre[contains(text(),'@')]"];
-        bibtexString = [[[preNode textContent] retain] autorelease];
         
-        NSArray *bibtexItems = nil;
-        if (bibtexString)
-            bibtexItems = [BDSKBibTeXParser itemsFromString:bibtexString 
owner:[self delegate] error:&error];
-        
-        if ([bibtexItems count] && [error 
isLocalErrorWithCode:kBDSKBibTeXParserFailed] == NO) {
-            BibItem *bibtexItem = [bibtexItems objectAtIndex:0];
-            [items addObject:bibtexItem];
-        } else if (outError) {
-            *outError = error;
-        }
-        
-        return items;
+        return [self itemsFromBibTeXString:[preNode textContent] 
error:outError];
     } else {
         return [super itemsReturningError:outError];
     }

Modified: trunk/bibdesk/BDSKMathSciNetParser.h
===================================================================
--- trunk/bibdesk/BDSKMathSciNetParser.h        2018-08-26 11:59:34 UTC (rev 
22530)
+++ trunk/bibdesk/BDSKMathSciNetParser.h        2018-08-26 15:51:33 UTC (rev 
22531)
@@ -43,5 +43,5 @@
 
 @interface BDSKMathSciNetParser : BDSKAsynchronousWebParser
 + (NSArray *)bibTeXRequestsForMRIDs:(NSArray *)IDs referrer:(NSURL *)URL 
error:(NSError **)outError;
-+ (NSArray *)itemsFromDownload:(BDSKCitationDownload *)download 
owner:(id<BDSKOwner>)owner error:(NSError **)outError;
++ (NSArray *)itemsFromDownload:(BDSKCitationDownload *)download 
parser:(BDSKWebParser *)parser error:(NSError **)outError;
 @end

Modified: trunk/bibdesk/BDSKMathSciNetParser.m
===================================================================
--- trunk/bibdesk/BDSKMathSciNetParser.m        2018-08-26 11:59:34 UTC (rev 
22530)
+++ trunk/bibdesk/BDSKMathSciNetParser.m        2018-08-26 15:51:33 UTC (rev 
22531)
@@ -37,9 +37,7 @@
  */
 
 #import "BDSKMathSciNetParser.h"
-#import "BDSKBibTeXParser.h"
 #import "BibItem.h"
-#import "NSError_BDSKExtensions.h"
 #import "NSURL_BDSKExtensions.h"
 #import <AGRegex/AGRegex.h>
 
@@ -95,7 +93,7 @@
     NSString *bibTeXString = [download string];
     
     if (bibTeXString)
-        results = [BDSKMathSciNetParser itemsFromDownload:download owner:[self 
delegate] error:outError];
+        results = [BDSKMathSciNetParser itemsFromDownload:download parser:self 
error:outError];
     
     return results;
 }
@@ -135,7 +133,7 @@
 }
 
 // Parses the bibTeX string returned from a request to get an array of 
BibItems.
-+ (NSArray *)itemsFromDownload:(BDSKCitationDownload *)download 
owner:(id<BDSKOwner>)owner error:(NSError **)outError {
++ (NSArray *)itemsFromDownload:(BDSKCitationDownload *)download 
parser:(BDSKWebParser *)parser error:(NSError **)outError {
     NSError * error = nil;
     
     NSXMLDocument * resultsPage = [[[NSXMLDocument alloc] 
initWithData:[download data] options: NSXMLDocumentTidyHTML error:&error] 
autorelease];
@@ -168,11 +166,7 @@
         NSString * cleanedRecord =     [preContent 
stringByCollapsingAndTrimmingCharactersInSet:[NSCharacterSet 
whitespaceAndNewlineCharacterSet]];
         cleanedRecord = [umlautFixer replaceWithString:@"{$1}" 
inString:cleanedRecord];
         
-        NSError * parseError = nil;
-        NSArray * newPubs = [BDSKBibTeXParser itemsFromString:cleanedRecord 
owner:owner error: &parseError];
-        
-        if ([newPubs count] && [parseError 
isLocalErrorWithCode:kBDSKBibTeXParserFailed] == NO)
-            [results addObjectsFromArray:newPubs];
+        [results addObjectsFromArray:[parser 
itemsFromBibTeXString:cleanedRecord error:NULL]];
     }
     
     // Add a URL reference pointing to the review's web page to each record.

Modified: trunk/bibdesk/BDSKNumdamParser.m
===================================================================
--- trunk/bibdesk/BDSKNumdamParser.m    2018-08-26 11:59:34 UTC (rev 22530)
+++ trunk/bibdesk/BDSKNumdamParser.m    2018-08-26 15:51:33 UTC (rev 22531)
@@ -144,7 +144,7 @@
     NSString *content = [contextInfo objectForKey:@"content"];
     BOOL useUrl = [[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKUseLocalUrlAndUrlKey];
     
-    results = [parserClass itemsFromDownload:download owner:[self delegate] 
error:outError];
+    results = [parserClass itemsFromDownload:download parser:self 
error:outError];
     
     if ([results count] > 0 && content != nil) {
         // add Numdam URL to item's own record

Modified: trunk/bibdesk/BDSKProjectEuclidParser.m
===================================================================
--- trunk/bibdesk/BDSKProjectEuclidParser.m     2018-08-26 11:59:34 UTC (rev 
22530)
+++ trunk/bibdesk/BDSKProjectEuclidParser.m     2018-08-26 15:51:33 UTC (rev 
22531)
@@ -136,7 +136,7 @@
     NSString *myIdentifier = [contextInfo objectForKey:@"myIdentifier"];
     BOOL useUrl = [[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKUseLocalUrlAndUrlKey];
     
-    results = [parserClass itemsFromDownload:download owner:[self delegate] 
error:&error];
+    results = [parserClass itemsFromDownload:download parser:self 
error:&error];
     
     if ([results count] > 0 && myIdentifier != nil) {
         // add Project Euclid URL to item's own record

Modified: trunk/bibdesk/BDSKWebParser.h
===================================================================
--- trunk/bibdesk/BDSKWebParser.h       2018-08-26 11:59:34 UTC (rev 22530)
+++ trunk/bibdesk/BDSKWebParser.h       2018-08-26 15:51:33 UTC (rev 22531)
@@ -97,6 +97,9 @@
 + (NSString *)info;
 + (BDSKParserFeature)feature;
 
+// convenience method to return items from a bibtex string
+- (NSArray *)itemsFromBibTeXString :(NSString *)bibtexString error:(NSError 
**)outError;
+
 @end
 
 @protocol BDSKWebParserDelegate <BDSKOwner>

Modified: trunk/bibdesk/BDSKWebParser.m
===================================================================
--- trunk/bibdesk/BDSKWebParser.m       2018-08-26 11:59:34 UTC (rev 22530)
+++ trunk/bibdesk/BDSKWebParser.m       2018-08-26 15:51:33 UTC (rev 22531)
@@ -59,6 +59,7 @@
 #import "BDSKMASParser.h"
 #import "BDSKDOIWebParser.h"
 #import "BibItem.h"
+#import "BDSKBibTeXParser.h"
 #import "NSError_BDSKExtensions.h"
 #import "BDSKRuntime.h"
 #import "DOMNode_BDSKExtensions.h"
@@ -212,5 +213,19 @@
 
 + (BDSKParserFeature)feature { return BDSKParserFeaturePublic; }
 
+- (NSArray *)itemsFromBibTeXString :(NSString *)bibtexString error:(NSError 
**)outError {
+    NSArray *bibtexItems = nil;
+    NSError *error = nil;
+    
+    if (bibtexString) {
+        bibtexItems = [BDSKBibTeXParser itemsFromString:bibtexString 
owner:[self delegate] error:&error];
+        if ([error isLocalErrorWithCode:kBDSKBibTeXParserFailed])
+            bibtexItems = nil;
+    }
+    if ([bibtexItems count] == 0 && outError)
+        *outError = error;
+    return bibtexItems;
+}
+
 @end
 

Modified: trunk/bibdesk/BDSKZentralblattParser.h
===================================================================
--- trunk/bibdesk/BDSKZentralblattParser.h      2018-08-26 11:59:34 UTC (rev 
22530)
+++ trunk/bibdesk/BDSKZentralblattParser.h      2018-08-26 15:51:33 UTC (rev 
22531)
@@ -39,9 +39,8 @@
 #import <Cocoa/Cocoa.h>
 #import "BDSKAsynchronousWebParser.h"
 
-@protocol BDSKOwner;
 
 @interface BDSKZentralblattParser : BDSKAsynchronousWebParser
 + (NSArray *)bibTeXRequestsForZMathIDs:(NSArray *)IDs referrer:(NSURL 
*)referrer error:(NSError **) outError;
-+ (NSArray *)itemsFromDownload:(BDSKCitationDownload *)download 
owner:(id<BDSKOwner>)owner error:(NSError **)outError;
++ (NSArray *)itemsFromDownload:(BDSKCitationDownload *)download 
parser:(BDSKWebParser *)parser error:(NSError **)outError;
 @end

Modified: trunk/bibdesk/BDSKZentralblattParser.m
===================================================================
--- trunk/bibdesk/BDSKZentralblattParser.m      2018-08-26 11:59:34 UTC (rev 
22530)
+++ trunk/bibdesk/BDSKZentralblattParser.m      2018-08-26 15:51:33 UTC (rev 
22531)
@@ -37,7 +37,6 @@
  */
 
 #import "BDSKZentralblattParser.h"
-#import "BDSKBibTeXParser.h"
 #import "BibItem.h"
 #import "NSError_BDSKExtensions.h"
 #import "NSURL_BDSKExtensions.h"
@@ -95,7 +94,7 @@
     NSError *error = nil;
     
     if (bibTeXString) {
-        results = [BDSKZentralblattParser itemsFromDownload:download 
owner:[self delegate] error:&error];
+        results = [BDSKZentralblattParser itemsFromDownload:download 
parser:self error:&error];
         if ([results count] && [error 
isLocalErrorWithCode:kBDSKBibTeXParserFailed])
             results = nil;
     }
@@ -148,7 +147,7 @@
 }
 
 // Parses a returned bibTeX string to get an array of BibItems.
-+ (NSArray *)itemsFromDownload:(BDSKCitationDownload *)download 
owner:(id<BDSKOwner>)owner error:(NSError **)outError {
++ (NSArray *)itemsFromDownload:(BDSKCitationDownload *)download 
parser:(BDSKWebParser *)parser error:(NSError **)outError {
     NSString * bibTeXString = [download string];
     
     /*    ZMath sometimes uses \"o for umlauts which is incorrect, fix that by 
adding brackets around it for the parser to work. Also add brackets to acute 
and grave accents, so BibDesk translates them to Unicode properly for display */
@@ -157,7 +156,7 @@
     bibTeXString = [bibTeXString 
stringByCollapsingAndTrimmingCharactersInSet:[NSCharacterSet 
whitespaceAndNewlineCharacterSet]];
     bibTeXString = [umlautFixer replaceWithString:@"{$1}" 
inString:bibTeXString];
     
-    NSArray * results = [BDSKBibTeXParser itemsFromString:bibTeXString 
owner:owner error: outError];
+    NSArray * results = [parser itemsFromBibTeXString:bibTeXString 
error:outError];
     BOOL useUrl = [[NSUserDefaults standardUserDefaults] 
boolForKey:BDSKUseLocalUrlAndUrlKey];
     
     // Add a URL reference to the review's web page to each record.

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


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Bibdesk-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to