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 113
    if( [s isEqualToString:@"-"])
114 114
       data = [[NSFileHandle fileHandleWithStandardInput] readDataToEndOfFile];
115 115
    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 116
    {
125
-      NSLog( @"property list failure: %@", error);
126
-      return( nil);
117
+      if( [[s pathExtension] isEqualToString:@"plist"])
118
+      {
119
+         data = [NSData dataWithContentsOfFile:s];
120
+   
121
+         error = nil;
122
+         plist = [NSPropertyListSerialization propertyListFromData:data
123
+                                                  mutabilityOption:NSPropertyListImmutable
124
+                                                            format:NULL
125
+                                                  errorDescription:&error];
126
+         if( ! plist)
127
+         {
128
+            NSLog( @"property list failure: %@", error);
129
+            return( nil);
130
+         
131
+         }
132
+      }
133
+      else
134
+      {
135
+         bundle = [NSBundle bundleWithPath:s];
136
+         cls    = [bundle principalClass];
137
+         if( ! cls)
138
+         {
139
+            NSLog( @"bundle \"%@\" load failure", s);
140
+            return( nil);
141
+         }
142
+         
143
+         if( ! [cls respondsToSelector:@selector( mulleScionDataSource)])
144
+         {
145
+            NSLog( @"bundle's principal class \"%@\" does not respond to +mulleScionDataSource", cls);
146
+            return( nil);
147
+         }
148
+         
149
+         plist = [cls performSelector:@selector( mulleScionDataSource)];
150
+         if( ! plist)
151
+         {
152
+            NSLog( @"bundle's principal class \"%@\" returned nil for +mulleScionDataSource", cls);
153
+            return( nil);
154
+         }
155
+
156
+         if( ! [plist respondsToSelector:@selector( valueForKeyPath:)])
157
+         {
158
+            NSLog( @"bundle's dataSource\"%@\" does not respond to -valueForKeyPath:", [plist class]);
159
+            return( nil);
160
+         }
161
+      }
127 162
    }
128 163
    return( plist);
129 164
 }
... ...
@@ -155,11 +192,11 @@ static NSDictionary  *getInfoFromArguments( void)
155 155
    if( ! [outputName length])
156 156
       outputName = @"-";
157 157
    
158
-   plist = acquirePropertyList( plistName);
158
+   plist = acquirePropertyListOrDataSourceFromBundle( plistName);
159 159
    if( ! plist)
160 160
       goto usage;
161 161
    [info setObject:plist
162
-            forKey:@"plist"];
162
+            forKey:@"dataSource"];
163 163
    
164 164
    [info setObject:templateName
165 165
             forKey:@"MulleScionRootTemplate"];
... ...
@@ -171,7 +208,7 @@ static NSDictionary  *getInfoFromArguments( void)
171 171
    return( info);
172 172
    
173 173
 usage:
174
-   fprintf( stderr, "%s [-w] <template> [propertylist|-|none] [output]\n", [processName cString]);
174
+   fprintf( stderr, "%s [-w] <template> [bundle|propertylist|-|none] [output]\n", [processName cString]);
175 175
    return( nil);
176 176
 }
177 177
 
... ...
@@ -229,7 +266,7 @@ static int _main(int argc, const char * argv[])
229 229
       return( -2);
230 230
    
231 231
    return( run( [info objectForKey:@"MulleScionRootTemplate"],
232
-               [info objectForKey:@"plist"],
232
+               [info objectForKey:@"dataSource"],
233 233
                stream,
234 234
                localVariablesFromInfo( info)));
235 235
 }