Browse code

enable a bundle as a dataSource not just plists. Will be useful sometime in MulleScionDrake.

Nat! authored on 17-11-2013 23:34:51
Showing 1 changed files
... ...
@@ -42,7 +42,7 @@
42 42
 
43 43
 static NSFileHandle  *outputStreamWithInfo( NSDictionary *info);
44 44
 static NSDictionary  *getInfoFromArguments( void);
45
-static id            acquirePropertyList( NSString *s);
45
+static id            acquirePropertyListOrDataSourceFromBundle( NSString *s);
46 46
 
47 47
 
48 48
 @interface NSFileHandle ( MulleScionOutput) < MulleScionOutput >
... ...
@@ -101,11 +101,13 @@ static int   run( NSString *template,
101 101
    ##### #####
102 102
          ##### */
103 103
 
104
-static id   acquirePropertyList( NSString *s)
104
+static id   acquirePropertyListOrDataSourceFromBundle( NSString *s)
105 105
 {
106 106
    NSData    *data;
107 107
    NSString  *error;
108
+   NSBundle  *bundle;
108 109
    id        plist;
110
+   Class     cls;
109 111
    
110 112
    if( [s isEqualToString:@"none"])
111 113
       return( [NSDictionary dictionary]);
... ...
@@ -113,17 +115,52 @@ static id   acquirePropertyList( NSString *s)
113 115
    if( [s isEqualToString:@"-"])
114 116
       data = [[NSFileHandle fileHandleWithStandardInput] readDataToEndOfFile];
115 117
    else
116
-      data = [NSData dataWithContentsOfFile:s];
117
-   
118
-   error = nil;
119
-   plist = [NSPropertyListSerialization propertyListFromData:data
120
-                                            mutabilityOption:NSPropertyListImmutable
121
-                                                      format:NULL
122
-                                            errorDescription:&error];
123
-   if( ! plist)
124 118
    {
125
-      NSLog( @"property list failure: %@", error);
126
-      return( nil);
119
+      if( [[s pathExtension] isEqualToString:@"plist"])
120
+      {
121
+         data = [NSData dataWithContentsOfFile:s];
122
+   
123
+         error = nil;
124
+         plist = [NSPropertyListSerialization propertyListFromData:data
125
+                                                  mutabilityOption:NSPropertyListImmutable
126
+                                                            format:NULL
127
+                                                  errorDescription:&error];
128
+         if( ! plist)
129
+         {
130
+            NSLog( @"property list failure: %@", error);
131
+            return( nil);
132
+         
133
+         }
134
+      }
135
+      else
136
+      {
137
+         bundle = [NSBundle bundleWithPath:s];
138
+         cls    = [bundle principalClass];
139
+         if( ! cls)
140
+         {
141
+            NSLog( @"bundle \"%@\" load failure", s);
142
+            return( nil);
143
+         }
144
+         
145
+         if( ! [cls respondsToSelector:@selector( mulleScionDataSource)])
146
+         {
147
+            NSLog( @"bundle's principal class \"%@\" does not respond to +mulleScionDataSource", cls);
148
+            return( nil);
149
+         }
150
+         
151
+         plist = [cls performSelector:@selector( mulleScionDataSource)];
152
+         if( ! plist)
153
+         {
154
+            NSLog( @"bundle's principal class \"%@\" returned nil for +mulleScionDataSource", cls);
155
+            return( nil);
156
+         }
157
+
158
+         if( ! [plist respondsToSelector:@selector( valueForKeyPath:)])
159
+         {
160
+            NSLog( @"bundle's dataSource\"%@\" does not respond to -valueForKeyPath:", [plist class]);
161
+            return( nil);
162
+         }
163
+      }
127 164
    }
128 165
    return( plist);
129 166
 }
... ...
@@ -155,11 +192,11 @@ static NSDictionary  *getInfoFromArguments( void)
155 192
    if( ! [outputName length])
156 193
       outputName = @"-";
157 194
    
158
-   plist = acquirePropertyList( plistName);
195
+   plist = acquirePropertyListOrDataSourceFromBundle( plistName);
159 196
    if( ! plist)
160 197
       goto usage;
161 198
    [info setObject:plist
162
-            forKey:@"plist"];
199
+            forKey:@"dataSource"];
163 200
    
164 201
    [info setObject:templateName
165 202
             forKey:@"MulleScionRootTemplate"];
... ...
@@ -171,7 +208,7 @@ static NSDictionary  *getInfoFromArguments( void)
171 208
    return( info);
172 209
    
173 210
 usage:
174
-   fprintf( stderr, "%s [-w] <template> [propertylist|-|none] [output]\n", [processName cString]);
211
+   fprintf( stderr, "%s [-w] <template> [bundle|propertylist|-|none] [output]\n", [processName cString]);
175 212
    return( nil);
176 213
 }
177 214
 
... ...
@@ -229,7 +266,7 @@ static int _main(int argc, const char * argv[])
229 266
       return( -2);
230 267
    
231 268
    return( run( [info objectForKey:@"MulleScionRootTemplate"],
232
-               [info objectForKey:@"plist"],
269
+               [info objectForKey:@"dataSource"],
233 270
                stream,
234 271
                localVariablesFromInfo( info)));
235 272
 }