Revision: 5053
          http://skim-app.svn.sourceforge.net/skim-app/?rev=5053&view=rev
Author:   hofman
Date:     2008-12-12 16:22:53 +0000 (Fri, 12 Dec 2008)

Log Message:
-----------
Use a function to get the key value for templates

Modified Paths:
--------------
    trunk/SKTemplateParser.h
    trunk/SKTemplateParser.m

Modified: trunk/SKTemplateParser.h
===================================================================
--- trunk/SKTemplateParser.h    2008-12-12 13:45:13 UTC (rev 5052)
+++ trunk/SKTemplateParser.h    2008-12-12 16:22:53 UTC (rev 5053)
@@ -59,8 +59,6 @@
 
 - (BOOL)isNotEmpty;
 
-- (id)templateValueForKeyPath:(NSString *)keyPath;
-
 - (NSString *)templateStringValue;
 - (NSAttributedString 
*)templateAttributedStringValueWithAttributes:(NSDictionary *)attributes;
 

Modified: trunk/SKTemplateParser.m
===================================================================
--- trunk/SKTemplateParser.m    2008-12-12 13:45:13 UTC (rev 5052)
+++ trunk/SKTemplateParser.m    2008-12-12 16:22:53 UTC (rev 5053)
@@ -200,6 +200,37 @@
     return altTagRange;
 }
 
+static inline id templateValueForKeyPath(id object, NSString *keyPath, int 
anIndex) {
+    if ([keyPath hasPrefix:@"#"] && anIndex > 0) {
+        object = [NSNumber numberWithInt:anIndex];
+        if ([keyPath length] == 1)
+            return object;
+        if ([keyPath hasPrefix:@"#."] == NO || [keyPath length] < 3)
+            return nil;
+        keyPath = [keyPath substringFromIndex:2];
+    }
+    if (object == nil)
+        return nil;
+    id value = nil;
+    NSString *trailingKeyPath = nil;
+    unsigned int atIndex = [keyPath rangeOfString:@"@"].location;
+    if (atIndex != NSNotFound) {
+        unsigned int dotIndex = [keyPath rangeOfString:@"." options:0 
range:NSMakeRange(atIndex + 1, [keyPath length] - atIndex - 1)].location;
+        if (dotIndex != NSNotFound) {
+            static NSSet *arrayOperators = nil;
+            if (arrayOperators == nil)
+                arrayOperators = [[NSSet alloc] initWithObjects:@"@avg", 
@"@max", @"@min", @"@sum", @"@distinctUnionOfArrays", 
@"@distinctUnionOfObjects", @"@distinctUnionOfSets", @"@unionOfArrays", 
@"@unionOfObjects", @"@unionOfSets", nil];
+            if ([arrayOperators containsObject:[keyPath 
substringWithRange:NSMakeRange(atIndex, dotIndex - atIndex)]] == NO) {
+                trailingKeyPath = [keyPath substringFromIndex:dotIndex + 1];
+                keyPath = [keyPath substringToIndex:dotIndex];
+            }
+        }
+    }
+    @try{ value = [object valueForKeyPath:keyPath]; }
+    @catch(id exception) { value = nil; }
+    return trailingKeyPath ? templateValueForKeyPath(value, trailingKeyPath, 
0) : value;
+}
+
 static inline BOOL matchesCondition(NSString *keyValue, NSString *matchString, 
SKTemplateTagMatchType matchType) {
     switch (matchType) {
         case SKTemplateTagMatchEqual:
@@ -253,7 +284,7 @@
 #pragma mark Parsing string templates
 
 + (NSString *)stringByParsingTemplateString:(NSString *)template 
usingObject:(id)object {
-    return [self stringFromTemplateArray:[self 
arrayByParsingTemplateString:template] usingObject:object atIndex:1];
+    return [self stringFromTemplateArray:[self 
arrayByParsingTemplateString:template] usingObject:object atIndex:0];
 }
 
 + (NSArray *)arrayByParsingTemplateString:(NSString *)template {
@@ -430,16 +461,8 @@
         } else {
             
             NSString *keyPath = [tag keyPath];
-            id keyValue = nil;
+            id keyValue = templateValueForKeyPath(object, keyPath, anIndex);
             
-            if ([keyPath hasPrefix:@"#"]) {
-                keyValue = [NSNumber numberWithInt:anIndex];
-                if ([keyPath hasPrefix:@"#."] && [keyPath length] > 2)
-                    keyValue = [keyValue templateValueForKeyPath:[keyPath 
substringFromIndex:2]];
-            } else {
-                keyValue = [object templateValueForKeyPath:keyPath];
-            }
-            
             if (type == SKValueTemplateTagType) {
                 
                 if (keyValue)
@@ -473,7 +496,7 @@
                 for (i = 0; i < count; i++) {
                     matchString = [matchStrings objectAtIndex:i];
                     if ([matchString hasPrefix:@"$"])
-                        matchString = [[object 
templateValueForKeyPath:[matchString substringFromIndex:1]] 
templateStringValue] ?: @"";
+                        matchString = [templateValueForKeyPath(object, 
[matchString substringFromIndex:1], anIndex) templateStringValue] ?: @"";
                     if (matchesCondition(keyValue, matchString, [tag 
matchType])) {
                         subtemplate = [tag subtemplateAtIndex:i];
                         break;
@@ -497,7 +520,7 @@
 #pragma mark Parsing attributed string templates
 
 + (NSAttributedString 
*)attributedStringByParsingTemplateAttributedString:(NSAttributedString 
*)template usingObject:(id)object {
-    return [self attributedStringFromTemplateArray:[self 
arrayByParsingTemplateAttributedString:template] usingObject:object atIndex:1];
+    return [self attributedStringFromTemplateArray:[self 
arrayByParsingTemplateAttributedString:template] usingObject:object atIndex:0];
 }
 
 + (NSArray *)arrayByParsingTemplateAttributedString:(NSAttributedString 
*)template {
@@ -692,16 +715,8 @@
         } else {
             
             NSString *keyPath = [tag keyPath];
-            id keyValue = nil;
+            id keyValue = templateValueForKeyPath(object, keyPath, anIndex);
             
-            if ([keyPath hasPrefix:@"#"]) {
-                keyValue = [NSNumber numberWithInt:anIndex];
-                if ([keyPath hasPrefix:@"#."] && [keyPath length] > 2)
-                    keyValue = [keyValue templateValueForKeyPath:[keyPath 
substringFromIndex:2]];
-            } else {
-                keyValue = [object templateValueForKeyPath:keyPath];
-            }
-            
             if (type == SKValueTemplateTagType) {
                 
                 if (keyValue)
@@ -737,7 +752,7 @@
                 for (i = 0; i < count; i++) {
                     matchString = [matchStrings objectAtIndex:i];
                     if ([matchString hasPrefix:@"$"])
-                        matchString = [[object 
templateValueForKeyPath:[matchString substringFromIndex:1]] 
templateStringValue] ?: @"";
+                        matchString = [templateValueForKeyPath(object, 
[matchString substringFromIndex:1], anIndex) templateStringValue] ?: @"";
                     if (matchesCondition(keyValue, matchString, [tag 
matchType])) {
                         subtemplate = [tag subtemplateAtIndex:i];
                         break;
@@ -774,27 +789,6 @@
     return YES;
 }
 
-- (id)templateValueForKeyPath:(NSString *)keyPath {
-    id value = nil;
-    NSString *trailingKeyPath = nil;
-    unsigned int atIndex = [keyPath rangeOfString:@"@"].location;
-    if (atIndex != NSNotFound) {
-        unsigned int dotIndex = [keyPath rangeOfString:@"." options:0 
range:NSMakeRange(atIndex + 1, [keyPath length] - atIndex - 1)].location;
-        if (dotIndex != NSNotFound) {
-            static NSSet *arrayOperators = nil;
-            if (arrayOperators == nil)
-                arrayOperators = [[NSSet alloc] initWithObjects:@"@avg", 
@"@max", @"@min", @"@sum", @"@distinctUnionOfArrays", 
@"@distinctUnionOfObjects", @"@distinctUnionOfSets", @"@unionOfArrays", 
@"@unionOfObjects", @"@unionOfSets", nil];
-            if ([arrayOperators containsObject:[keyPath 
substringWithRange:NSMakeRange(atIndex, dotIndex - atIndex)]] == NO) {
-                trailingKeyPath = [keyPath substringFromIndex:dotIndex + 1];
-                keyPath = [keyPath substringToIndex:dotIndex];
-            }
-        }
-    }
-    @try{ value = [self valueForKeyPath:keyPath]; }
-    @catch(id exception) { value = nil; }
-    return trailingKeyPath ? [value templateValueForKeyPath:trailingKeyPath] : 
value;
-}
-
 - (NSString *)templateStringValue {
     if ([self respondsToSelector:@selector(stringValue)])
         return [(id)self stringValue] ?: @"";


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

------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you.  Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Skim-app-commit mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/skim-app-commit

Reply via email to