Revision: 22513
          http://sourceforge.net/p/bibdesk/svn/22513
Author:   hofman
Date:     2018-08-23 13:01:36 +0000 (Thu, 23 Aug 2018)
Log Message:
-----------
reuse xpath expressions

Modified Paths:
--------------
    trunk/bibdesk/BDSKHCiteParser.m

Modified: trunk/bibdesk/BDSKHCiteParser.m
===================================================================
--- trunk/bibdesk/BDSKHCiteParser.m     2018-08-23 09:55:22 UTC (rev 22512)
+++ trunk/bibdesk/BDSKHCiteParser.m     2018-08-23 13:01:36 UTC (rev 22513)
@@ -44,8 +44,8 @@
 
 @interface BDSKHCiteParser (Private)
 - (NSDate *)dateFromNode:(DOMNode *)node;
-- (NSString *)BTAuthorStringFromVCardNode:(DOMNode *)node;
-- (NSMutableDictionary *)dictionaryFromCitationNode:(DOMNode *)citationNode 
isContainer:(BOOL)isContainer;
+- (NSString *)BTAuthorStringFromVCardNode:(DOMNode *)node 
xpath:(DOMXPathExpression *)xpath;
+- (NSMutableDictionary *)dictionaryFromCitationNode:(DOMNode *)citationNode 
isContainer:(BOOL)isContainer  xpaths:(NSDictionary *)xpaths;
 
 @end
 
@@ -67,12 +67,28 @@
     NSString *hciteXpath = @".//*[contains(concat(' ',normalize-space(@class), 
' '),' hcite ') and not(contains(concat(' ', normalize-space(@class), ' '),' 
container '))]";
     NSArray *mainNodes = [[[self domDocument] documentElement] 
nodesForXPath:hciteXpath];
     
+    if ([mainNodes count] == 0)
+        return items;
     
+    NSMutableDictionary *xpaths = [NSMutableDictionary dictionary];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class), ' '),' 
creator ') and contains(concat(' ',normalize-space(@class), ' '),' vcard ')]" 
resolver:nil] forKey:@"author"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ', normalize-space(@rel), ' '), ' tag 
')]" resolver:nil] forKey:@"tag"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class), ' '),' 
hcite ') and contains(concat(' ', normalize-space(@class), ' '),' container 
')]" resolver:nil] forKey:@"container"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class),' '),' type 
')]" resolver:nil] forKey:@"type"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class),' '),' 
title ')]" resolver:nil] forKey:@"title"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class),' '),' 
description ')]" resolver:nil] forKey:@"description"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class),' '),' 
abstract ')]" resolver:nil] forKey:@"abstract"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class),' '),' 
date-published ')]" resolver:nil] forKey:@"date-published"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class),' '),' 
issue ')]" resolver:nil] forKey:@"issue"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class),' '),' 
pages ')]" resolver:nil] forKey:@"pages"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class),' '),' uri 
')]" resolver:nil] forKey:@"uri"];
+    [xpaths setValue:[[self domDocument] 
createExpression:@".//*[contains(concat(' ',normalize-space(@class),' '),' fn 
')]" resolver:nil] forKey:@"fn"];
+
     for (DOMNode *obj in mainNodes) {
         NSMutableDictionary *rd = nil;
         NSString *type = nil;
         
-        rd = [self dictionaryFromCitationNode:obj isContainer:NO];
+        rd = [self dictionaryFromCitationNode:obj isContainer:NO 
xpaths:xpaths];
         type = [rd objectForKey:BDSKTypeString];
         [rd removeObjectForKey:BDSKTypeString];
         
@@ -84,12 +100,12 @@
     return items;
 }
 
-- (NSMutableDictionary *)dictionaryFromCitationNode:(DOMNode *)citationNode 
isContainer:(BOOL)isContainer{
+- (NSMutableDictionary *)dictionaryFromCitationNode:(DOMNode *)citationNode 
isContainer:(BOOL)isContainer  xpaths:(NSDictionary *)xpaths{
     BDSKTypeManager *typeMan = [BDSKTypeManager sharedManager];
     NSMutableDictionary *rd = [NSMutableDictionary dictionaryWithCapacity:0];
     
     // find type but not type that's a descendant of 'container'.
-    NSArray *typeNodes = [citationNode 
descendantOrSelfNodesWithClassName:@"type"];
+    NSArray *typeNodes = [citationNode nodesForXPathExpression:[xpaths 
objectForKey:@"type"]];
     
     NSString *typeString = nil;
     for (DOMNode *node in typeNodes) {
@@ -106,7 +122,7 @@
     
     // find title node
     
-    NSArray *titleNodes = [citationNode 
descendantOrSelfNodesWithClassName:@"title"];
+    NSArray *titleNodes = [citationNode nodesForXPathExpression:[xpaths 
objectForKey:@"title"]];
     
     for(DOMNode *node in titleNodes){
         // deal with this citation's container later
@@ -119,18 +135,18 @@
     // find authors
     
     NSString *authorXpath = @".//*[contains(concat(' 
',normalize-space(@class), ' '),' creator ') and contains(concat(' 
',normalize-space(@class), ' '),' vcard ')]";
-    NSArray *authorNodes = [citationNode nodesForXPath:authorXpath];
+    NSArray *authorNodes = [citationNode nodesForXPathExpression:[xpaths 
objectForKey:@"author"]];
     NSMutableString *BTAuthString = [NSMutableString stringWithCapacity:0];
     
     for(DOMNode *node in authorNodes){
         if ([BTAuthString length]) [BTAuthString appendFormat:@" and "];
-        [BTAuthString appendString:[self BTAuthorStringFromVCardNode:node]];
+        [BTAuthString appendString:[self BTAuthorStringFromVCardNode:node 
xpath:[xpaths objectForKey:@"fn"]]];
     }
     [rd setObject:BTAuthString forKey:BDSKAuthorString];
     
     // find keywords
     
-    NSArray *tagNodes = [citationNode nodesForXPath:@".//*[contains(concat(' 
', normalize-space(@rel), ' '), ' tag ')]"];
+    NSArray *tagNodes = [citationNode nodesForXPathExpression:[xpaths 
objectForKey:@"tag"]];
     
     [rd setObject:[[tagNodes valueForKey:@"stringValue"] 
componentsJoinedByString:@"; "] forKey:BDSKKeywordsString];
     
@@ -137,14 +153,14 @@
     // find description (append multiple descriptions to avoid data loss)
     
     NSMutableArray *descNodes = [NSMutableArray arrayWithCapacity:0];
-    [descNodes addObjectsFromArray:[citationNode 
descendantOrSelfNodesWithClassName:@"description"]];
-    [descNodes addObjectsFromArray:[citationNode 
descendantOrSelfNodesWithClassName:@"abstract"]];
+    [descNodes addObjectsFromArray:[citationNode 
nodesForXPathExpression:[xpaths objectForKey:@"description"]]];
+    [descNodes addObjectsFromArray:[citationNode 
nodesForXPathExpression:[xpaths objectForKey:@"abstract"]]];
     
     [rd setObject:[[descNodes valueForKey:@"stringValue"] 
componentsJoinedByString:@"\n"] forKey:BDSKAbstractString];
     
     // find date published
     
-    NSArray *datePublishedNodes = [citationNode 
descendantOrSelfNodesWithClassName:@"date-published"];
+    NSArray *datePublishedNodes = [citationNode 
nodesForXPathExpression:[xpaths objectForKey:@"date-published"]];
     
     if([datePublishedNodes count] > 0) {
         DOMNode *datePublishedNode = [datePublishedNodes objectAtIndex:0]; // 
Only use the first such node.
@@ -160,7 +176,7 @@
     
     // find issue
     
-    NSArray *issueNodes = [citationNode 
descendantOrSelfNodesWithClassName:@"issue"];
+    NSArray *issueNodes = [citationNode nodesForXPathExpression:[xpaths 
objectForKey:@"issue"]];
     
     if([issueNodes count] > 0) {
         // Only use the first such node.
@@ -169,7 +185,7 @@
     
     // find pages
     
-    NSArray *pagesNodes = [citationNode 
descendantOrSelfNodesWithClassName:@"pages"];
+    NSArray *pagesNodes = [citationNode nodesForXPathExpression:[xpaths 
objectForKey:@"pages"]];
     
     if([pagesNodes count] > 0) {
         // Only use the first such node.
@@ -178,7 +194,7 @@
     
     // find URI
     
-    NSArray *URINodes = [citationNode 
descendantOrSelfNodesWithClassName:@"uri"];
+    NSArray *URINodes = [citationNode nodesForXPathExpression:[xpaths 
objectForKey:@"uri"]];
     
     if([URINodes count] > 0) {
         DOMElement *URINode = [URINodes objectAtIndex:0]; // Only use the 
first such node.
@@ -201,8 +217,7 @@
     // get container info:
     // *** NOTE: should do this last, to avoid overwriting data
     
-    NSString *containerXpath = @".//*[contains(concat(' 
',normalize-space(@class), ' '),' hcite ') and contains(concat(' ', 
normalize-space(@class), ' '),' container ')]";
-    NSArray *containerNodes = [citationNode nodesForXPath:containerXpath];
+    NSArray *containerNodes = [citationNode nodesForXPathExpression:[xpaths 
objectForKey:@"container"]];
     
     if([containerNodes count] > 0) {
         DOMNode *containerNode = [containerNodes objectAtIndex:0];
@@ -209,7 +224,7 @@
 
         NSString *citationType = [rd objectForKey:BDSKTypeString];
 
-        NSMutableDictionary *containerDict = [NSMutableDictionary 
dictionaryWithDictionary:[self dictionaryFromCitationNode:containerNode 
isContainer:YES]];
+        NSMutableDictionary *containerDict = [NSMutableDictionary 
dictionaryWithDictionary:[self dictionaryFromCitationNode:containerNode 
isContainer:YES xpaths:xpaths]];
         NSString *containerTitle = [containerDict 
objectForKey:BDSKTitleString];
         NSString *containerType = [containerDict objectForKey:BDSKTypeString];
 
@@ -248,9 +263,9 @@
      return rd;
 }
 
-- (NSString *)BTAuthorStringFromVCardNode:(DOMNode *)node{
+- (NSString *)BTAuthorStringFromVCardNode:(DOMNode *)node 
xpath:(DOMXPathExpression *)xpath{
     // note: may eventually need to do more than just look at fn and abbr.
-    NSArray *fnNodes = [node descendantOrSelfNodesWithClassName:@"fn"];
+    NSArray *fnNodes = [node nodesForXPathExpression:xpath];
     
     if([fnNodes count] < 1) return @"";
     

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