Revision: 22502
          http://sourceforge.net/p/bibdesk/svn/22502
Author:   hofman
Date:     2018-08-18 18:11:13 +0000 (Sat, 18 Aug 2018)
Log Message:
-----------
Add DOMNode category reflecting NSXMLNode category for eventual replacement

Modified Paths:
--------------
    trunk/bibdesk/BDSKWebParser.m
    trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj

Added Paths:
-----------
    trunk/bibdesk/DOMNode_BDSKExtensions.h
    trunk/bibdesk/DOMNode_BDSKExtensions.m

Modified: trunk/bibdesk/BDSKWebParser.m
===================================================================
--- trunk/bibdesk/BDSKWebParser.m       2018-08-18 17:20:48 UTC (rev 22501)
+++ trunk/bibdesk/BDSKWebParser.m       2018-08-18 18:11:13 UTC (rev 22502)
@@ -61,6 +61,7 @@
 #import "BibItem.h"
 #import "NSError_BDSKExtensions.h"
 #import "BDSKRuntime.h"
+#import "DOMNode_BDSKExtensions.h"
 
 @implementation BDSKWebParser
 

Modified: trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj
===================================================================
--- trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj     2018-08-18 17:20:48 UTC 
(rev 22501)
+++ trunk/bibdesk/Bibdesk.xcodeproj/project.pbxproj     2018-08-18 18:11:13 UTC 
(rev 22502)
@@ -155,6 +155,7 @@
                CE424A450D0F123500F824E7 /* BDSKCompletionServerProtocol.h in 
Headers */ = {isa = PBXBuildFile; fileRef = CE3A0BD50B1634D500233208 /* 
BDSKCompletionServerProtocol.h */; settings = {ATTRIBUTES = (Public, ); }; };
                CE4376F70D24341E00C993CF /* NSViewAnimation_BDSKExtensions.m in 
Sources */ = {isa = PBXBuildFile; fileRef = CE4376F50D24341E00C993CF /* 
NSViewAnimation_BDSKExtensions.m */; };
                CE4385E90BB81D0500A56987 /* BDSKSearchBookmarkController.m in 
Sources */ = {isa = PBXBuildFile; fileRef = CE4385E70BB81D0500A56987 /* 
BDSKSearchBookmarkController.m */; };
+               CE4476DC2128907100DF38E1 /* DOMNode_BDSKExtensions.m in Sources 
*/ = {isa = PBXBuildFile; fileRef = CE4476DA2128907100DF38E1 /* 
DOMNode_BDSKExtensions.m */; };
                CE458971191B91D100D1790F /* WokServiceSoapBinding.m in Sources 
*/ = {isa = PBXBuildFile; fileRef = CE45896F191B91D100D1790F /* 
WokServiceSoapBinding.m */; };
                CE4A0E141115ABEF000A95C5 /* BDSKServiceProvider.m in Sources */ 
= {isa = PBXBuildFile; fileRef = CE4A0E121115ABEF000A95C5 /* 
BDSKServiceProvider.m */; };
                CE4D85290C3A8C8F002C20CB /* BDSKAutofileCommand.m in Sources */ 
= {isa = PBXBuildFile; fileRef = CE4D85270C3A8C8E002C20CB /* 
BDSKAutofileCommand.m */; };
@@ -1034,6 +1035,8 @@
                CE4376F50D24341E00C993CF /* NSViewAnimation_BDSKExtensions.m */ 
= {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = 
sourcecode.c.objc; path = NSViewAnimation_BDSKExtensions.m; sourceTree = 
"<group>"; };
                CE4385E60BB81D0500A56987 /* BDSKSearchBookmarkController.h */ = 
{isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; 
path = BDSKSearchBookmarkController.h; sourceTree = "<group>"; };
                CE4385E70BB81D0500A56987 /* BDSKSearchBookmarkController.m */ = 
{isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = 
sourcecode.c.objc; path = BDSKSearchBookmarkController.m; sourceTree = 
"<group>"; };
+               CE4476D92128907100DF38E1 /* DOMNode_BDSKExtensions.h */ = {isa 
= PBXFileReference; lastKnownFileType = sourcecode.c.h; path = 
DOMNode_BDSKExtensions.h; sourceTree = "<group>"; };
+               CE4476DA2128907100DF38E1 /* DOMNode_BDSKExtensions.m */ = {isa 
= PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = 
DOMNode_BDSKExtensions.m; sourceTree = "<group>"; };
                CE452AC00F1EBBD500DA1A5A /* TestBDSKRISParser.h */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = 
TestBDSKRISParser.h; sourceTree = "<group>"; };
                CE45896E191B91D100D1790F /* WokServiceSoapBinding.h */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = 
WokServiceSoapBinding.h; sourceTree = "<group>"; };
                CE45896F191B91D100D1790F /* WokServiceSoapBinding.m */ = {isa = 
PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path 
= WokServiceSoapBinding.m; sourceTree = "<group>"; };
@@ -2045,6 +2048,7 @@
                                CE624DA52030B35A004B6E0B /* 
BDSKScienceDirectParser.m */,
                                CD6295371140DE1C002E4751 /* 
BDSKSpringerParser.m */,
                                6C567DB80F8189F500DE285D /* 
BDSKZentralblattParser.m */,
+                               CE4476DA2128907100DF38E1 /* 
DOMNode_BDSKExtensions.m */,
                        );
                        name = "Web Parsers";
                        sourceTree = "<group>";
@@ -2765,6 +2769,7 @@
                                F95D3FAC09DDF6A900D793A6 /* BibPref_Sharing.h 
*/,
                                F9022CB8075803AC00C3F701 /* BibPref_TeX.h */,
                                F9025F2E0969D66F008A551C /* 
CFString_BDSKExtensions.h */,
+                               CE4476D92128907100DF38E1 /* 
DOMNode_BDSKExtensions.h */,
                                F9022C9B0758038000C3F701 /* html2tex.h */,
                                CE3011B70D5CC4D100C0B7FA /* 
NSAppleEventDescriptor_BDSKExtensions.h */,
                                F99BC25C095A5C7000E018C3 /* 
NSArray_BDSKExtensions.h */,
@@ -3498,6 +3503,7 @@
                                CE6FB32309DFFCB5005E3E14 /* 
BDSKSharingBrowser.m in Sources */,
                                F92ED7CD09E0A93400A244D0 /* BDSKSharingServer.m 
in Sources */,
                                CE51922109E5755600E97C3A /* 
BDSKFindFieldEditor.m in Sources */,
+                               CE4476DC2128907100DF38E1 /* 
DOMNode_BDSKExtensions.m in Sources */,
                                F92EF32309E6242200A244D0 /* BDSKSharedGroup.m 
in Sources */,
                                F946DCE909FDC4B600D471DF /* 
BDSKAsynchronousDOServer.m in Sources */,
                                CE34E97A0A03C16B00222942 /* 
BDSKConditionsView.m in Sources */,

Added: trunk/bibdesk/DOMNode_BDSKExtensions.h
===================================================================
--- trunk/bibdesk/DOMNode_BDSKExtensions.h                              (rev 0)
+++ trunk/bibdesk/DOMNode_BDSKExtensions.h      2018-08-18 18:11:13 UTC (rev 
22502)
@@ -0,0 +1,55 @@
+//
+//  DOMNode_BDSKExtensions.h
+//  BibDesk
+//
+//  Created by Christiaan on 18/08/2018.
+/*
+ This software is Copyright (c) 2018
+ Christiaan Hofman. All rights reserved.
+ 
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ 
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ 
+ - Neither the name of Christiaan Hofman nor the names of any
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+ 
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import <Cocoa/Cocoa.h>
+#import <WebKit/WebKit.h>
+
+@interface DOMNode (BDSKExtensions)
+
+- (NSArray *)nodesForXPath:(NSString *)xpath;
+- (NSString *)stringValue;
+- (NSString *)stringValueOfAttribute:(NSString *)attrName;
+- (NSArray *)descendantOrSelfNodesWithClassName:(NSString *)className;
+- (BOOL)hasParentWithClassName:(NSString *)class;
+- (NSArray *)classNames;
+- (NSString *)fullStringValueIfABBR;
+- (NSString *)searchXPath:(NSString *)searchPath addTo:(NSMutableDictionary 
*)dict forKey:(NSString *)key;
+- (NSString *)searchXPath:(NSString *)searchPath addTo:(NSMutableDictionary 
*)dict forKey:(NSString *)key last:(BOOL)last;
+- (NSString *)textStringValue;
+
+@end

Added: trunk/bibdesk/DOMNode_BDSKExtensions.m
===================================================================
--- trunk/bibdesk/DOMNode_BDSKExtensions.m                              (rev 0)
+++ trunk/bibdesk/DOMNode_BDSKExtensions.m      2018-08-18 18:11:13 UTC (rev 
22502)
@@ -0,0 +1,177 @@
+//
+//  DOMNode_BDSKExtensions.m
+//  BibDesk
+//
+//  Created by Christiaan on 18/08/2018.
+/*
+ This software is Copyright (c) 2018
+ Christiaan Hofman. All rights reserved.
+ 
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions
+ are met:
+ 
+ - Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+ 
+ - Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in
+ the documentation and/or other materials provided with the
+ distribution.
+ 
+ - Neither the name of Christiaan Hofman nor the names of any
+ contributors may be used to endorse or promote products derived
+ from this software without specific prior written permission.
+ 
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#import "DOMNode_BDSKExtensions.h"
+#import "NSString_BDSKExtensions.h"
+
+@implementation DOMNode (BDSKExtensions)
+
+- (NSArray *)nodesForXPath:(NSString *)xpath {
+    DOMXPathResult *result = [[self ownerDocument] evaluate:xpath 
contextNode:self resolver:nil type:DOM_ANY_TYPE inResult:nil];
+    DOMNode *node;
+    NSMutableArray *nodes = nil;
+    
+    while ((node = [result iterateNext])) {
+        if (nodes == nil)
+            nodes = [NSMutableArray array];
+        [nodes addObject:node];
+    }
+    
+    return nodes;
+}
+
+- (NSString *)stringValue {
+    return [self textContent];
+}
+
+- (NSString *)stringValueOfAttribute:(NSString *)attrName {
+    NSString *path = [NSString stringWithFormat:@"./@%@", attrName];
+    NSArray *atts = [self nodesForXPath:path];
+    if ([atts count] == 0) return nil;
+    return [[atts objectAtIndex:0] textContent];
+}
+
+- (NSArray *)descendantOrSelfNodesWithClassName:(NSString *)className {
+    NSString *path = [NSString stringWithFormat:@".//*[contains(concat(' ', 
normalize-space(@class), ' '), ' %@ ')]", className];
+    NSArray *ar = [self nodesForXPath:path];
+    return ar;
+}
+
+- (BOOL)hasParentWithClassName:(NSString *)className {
+    
+    DOMNode *parent = [self parentNode];
+    
+    do {
+        if ([parent nodeType] != DOM_ELEMENT_NODE) return NO; // handles root 
node
+        
+        if ([[parent classNames] containsObject:className])
+            return YES;
+        
+    } while ((parent = [parent parentNode]));
+    
+    return NO;
+}
+
+- (NSArray *)classNames {
+    
+    if([self nodeType] != DOM_ELEMENT_NODE) [NSException 
raise:NSInvalidArgumentException format:@"wrong node kind"];
+    
+    NSMutableArray *array = [NSMutableArray arrayWithCapacity:0];
+    NSError *err = nil;
+    NSArray *classNodes = [self nodesForXPath:@"@class"];
+    
+    if ([classNodes count] == 0)
+        return array;
+    
+    NSAssert ([classNodes count] == 1, @"too many nodes in classNodes");
+    
+    NSXMLNode *classNode = [classNodes objectAtIndex:0];
+    
+    [array addObjectsFromArray:[[classNode stringValue] 
componentsSeparatedByString:@" "]];
+    
+    return array;
+}
+
+- (NSString *)fullStringValueIfABBR {
+    NSError *err;
+    if([self nodeType] != DOM_ELEMENT_NODE) [NSException 
raise:NSInvalidArgumentException format:@"wrong node kind"];
+    
+    if ([[[self nodeName] lowercaseString] isEqualToString:@"abbr"]){
+        //todo: will need more robust comparison for namespaced node titles.
+        
+        // return value of title attribute instead
+        NSArray *titleNodes = [self nodesForXPath:@"@title"];
+        if ([titleNodes count] > 0)
+            return [[titleNodes firstObject] stringValue];
+    }
+    
+    return [self stringValue];
+}
+
+- (NSString *)searchXPath:(NSString *)searchPath addTo:(NSMutableDictionary 
*)dict forKey:(NSString *)key {
+    return [self searchXPath:searchPath addTo:dict forKey:key last:NO];
+}
+
+- (NSString *)searchXPath:(NSString *)searchPath addTo:(NSMutableDictionary 
*)dict forKey:(NSString *)key last:(BOOL)last {
+    NSArray *nodes = [self nodesForXPath:searchPath];
+    NSString *string = nil;
+    
+    if (nil != nodes && 0 < [nodes count]) {
+        string = [[nodes objectAtIndex:last ? ([nodes count] - 1) : 0] 
stringValue];
+        if (string) {
+            string = [string stringByRemovingSurroundingWhitespaceAndNewlines];
+            [dict setValue:string forKey:key];
+        }
+    }
+    return string;
+}
+
+static void appendTextNodes(NSMutableString *string, DOMNode *node) {
+    if ([node nodeType] == DOM_TEXT_NODE)
+        [string appendString:[node stringValue]];
+    else if ([[[node nodeName] lowercaseString] isEqualToString:@"script"] == 
NO) {
+        DOMNodeList *children = [node childNodes];
+        unsigned i, iMax = [children length];
+        for (i = 0; i < iMax; i++)
+            appendTextNodes(string, [children item:i]);
+    }
+}
+
+- (NSString *)textStringValue {
+    NSMutableString *text = [NSMutableString string];
+    appendTextNodes(text, self);
+    return text;
+}
+
+@end
+
+
+@interface DOMDocument (BDSKExtensions)
+@end
+
+@implementation DOMDocument (BDSKExtensions)
+
+- (NSString *)textStringValue {
+    NSError *error;
+    NSArray *body = [self nodesForXPath:@"./html/body"];
+    if ([body count] == 1)
+        return [[body firstObject] textStringValue];
+    return [[self documentElement] textStringValue];
+}
+
+@end

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