Debian Codegraph @

Tools/pcpg/PLCPG.m

raw
1 /*
2 Project: pcpg
3
4 Copyright (C) 2011 Free Software Foundation
5
6 Author: Gregory John Casamento
7
8 Created: 2011-08-16 14:15:42 -0400 by heron
9
10 This application is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
14
15 This application is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
19
20 You should have received a copy of the GNU General Public
21 License along with this library; if not, write to the Free
22 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
23 */
24
25 #import "PLCPG.h"
26
27 @interface NSString (PLCPG)
28 - (NSString *) stringByUpperCasingFirstCharacter;
29 @end
30
31 @implementation NSString (PLCPG)
32 - (NSString *) stringByUpperCasingFirstCharacter
33 {
34 unichar c = [self characterAtIndex: 0];
35 NSString *oneChar = [[NSString stringWithFormat: @"%C",c] uppercaseString];
36 NSString *newString = [self stringByReplacingCharactersInRange: NSMakeRange(0,1) withString: oneChar];
37 return newString;
38 }
39 @end
40
41 @implementation PLCPG
42
43 - (id) initWithPlist: (NSString *)plistName
44 {
45 if((self = [super init]) != nil)
46 {
47 plist = [[NSDictionary alloc] initWithContentsOfFile: plistName];
48 dictionary = [plist objectForKey: @"objects"];
49 classes = [[NSMutableDictionary alloc] initWithCapacity: 10];
50 classNameMap = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
51 @"NSString", @"NSConstantString",
52 @"NSString", @"GSMutableString",
53 @"NSMutableArray", @"GSMutableArray",
54 @"NSMutableDictionary", @"GSMutableDictionary",
55 nil];
56 }
57 return self;
58 }
59
60 /*
61 - (void) dealloc
62 {
63 [plist release];
64 [dictionary release];
65 [classes release];
66 [classNameMap release];
67 [super dealloc];
68 }
69 */
70
71 - (NSString *) remapClassName: (NSString *)className
72 {
73 NSString *newClassName = [classNameMap objectForKey: className];
74 return (newClassName == nil) ? className : newClassName;
75 }
76
77 - (void) collectClassDetails
78 {
79 NSEnumerator *en = [dictionary keyEnumerator];
80 NSString *key = nil;
81 while((key = [en nextObject]) != nil)
82 {
83 id obj = [dictionary objectForKey: key];
84 if([obj isKindOfClass: [NSDictionary class]])
85 {
86 NSString *className = [obj objectForKey: @"isa"];
87 if(className == nil)
88 {
89 continue;
90 }
91
92 NSMutableDictionary *classInfo = [classes objectForKey: className];
93 if(classInfo == nil)
94 {
95 NSEnumerator *cen = [obj keyEnumerator];
96 id ckey = nil;
97
98 classInfo = [NSMutableDictionary dictionaryWithCapacity:10];
99 [classes setObject: classInfo forKey: className];
100 while((ckey = [cen nextObject]) != nil)
101 {
102 if([ckey isEqualToString: @"isa"] == NO)
103 {
104 id cobj = [obj objectForKey: ckey];
105 if([cobj isKindOfClass: [NSString class]])
106 {
107 id ref = [dictionary objectForKey: cobj];
108
109 // if cobj is a string, it might be a key.. so check.
110 if([ref isKindOfClass: [NSDictionary class]])
111 {
112 NSString *refClassName = [ref objectForKey: @"isa"];
113 [classInfo setObject: refClassName forKey: ckey];
114 }
115 else
116 {
117 NSString *className = NSStringFromClass([cobj class]);
118 className = [self remapClassName: className];
119 [classInfo setObject: className forKey: ckey];
120 }
121 }
122 else
123 {
124 NSString *className = NSStringFromClass([cobj class]);
125 className = [self remapClassName: className];
126 [classInfo setObject: className forKey: ckey];
127 }
128 }
129 }
130 }
131 }
132 }
133 }
134
135 - (void) createHeaderForClassNamed: (NSString *)className
136 {
137 NSString *classFileFormat =
138 @"#import <Foundation/Foundation.h>\n\n"
139 @"// Local includes\n"
140 @"#import \"PBXCoder.h\"\n"
141 @"%@\n\n@interface %@ : NSObject\n"
142 @"{\n%@}\n\n// Methods....\n%@\n"
143 @"@end";
144 NSDictionary *classInfo = [classes objectForKey: className];
145 NSEnumerator *en = [classInfo keyEnumerator];
146 NSString *ivarString = @"";
147 NSString *includeString = @"";
148 NSString *methodString = @"";
149
150 NSString *ivarName = nil;
151 while((ivarName = [en nextObject]) != nil)
152 {
153 NSString *type = [classInfo objectForKey: ivarName];
154
155 // Map certain types...
156 if ([type isEqualToString: @"GSCInlineString"])
157 {
158 type = @"NSString";
159 }
160
161 ivarString = [ivarString stringByAppendingString: [NSString stringWithFormat: @"\t%@ *_%@;\n",type, ivarName]];
162 if(![type isEqualToString: @"NSString"] &&
163 ![type isEqualToString: @"NSMutableArray"] &&
164 ![type isEqualToString: @"NSArray"] &&
165 ![type isEqualToString: @"NSDictionary"] &&
166 ![type isEqualToString: @"NSMutableDictionary"])
167 {
168 includeString = [includeString stringByAppendingString: [NSString stringWithFormat: @"#import \"%@.h\"\n", type]];
169 }
170
171 methodString = [methodString stringByAppendingString: [NSString stringWithFormat: @"- (%@ *) %@; // getter\n",type,ivarName]];
172 NSString *name = [ivarName stringByUpperCasingFirstCharacter];
173 methodString = [methodString stringByAppendingString: [NSString stringWithFormat: @"- (void) set%@: (%@ *)object; // setter\n",name,type]];
174 }
175
176 NSString *classFileName = [className stringByAppendingString: @".h"];
177 NSString *classFile = [NSString stringWithFormat: classFileFormat, includeString, className, ivarString, methodString];
178 NSError *error = nil;
179 [classFile writeToFile: classFileName
180 atomically: NO
181 encoding: NSASCIIStringEncoding
182 error: &error];
183 }
184
185 - (void) createSourceForClassNamed: (NSString *)className
186 {
187 NSString *classFileFormat = @"#import \"PBXCommon.h\"\n#import \"%@.h\"\n\n@implementation %@\n\n// Methods....\n%@\n@end";
188 NSString *methodString = @"";
189 NSDictionary *classInfo = [classes objectForKey: className];
190 NSEnumerator *en = [classInfo keyEnumerator];
191
192 NSString *ivarName = nil;
193 while((ivarName = [en nextObject]) != nil)
194 {
195 NSString *type = [classInfo objectForKey: ivarName];
196
197 // Map certain types...
198 if ([type isEqualToString: @"GSCInlineString"])
199 {
200 type = @"NSString";
201 }
202
203 methodString = [methodString stringByAppendingString: [NSString stringWithFormat: @"- (%@ *) %@ // getter\n{\n\treturn _%@;\n}\n\n",
204 type,ivarName,ivarName]];
205 NSString *name = [ivarName stringByUpperCasingFirstCharacter];
206 methodString = [methodString stringByAppendingString: [NSString stringWithFormat:
207 @"- (void) set%@: (%@ *)object; // setter\n{\n\tASSIGN(_%@, object);\n}\n\n",
208 name,type,ivarName]];
209 }
210
211 NSString *classFileName = [className stringByAppendingString: @".m"];
212 NSString *classFile = [NSString stringWithFormat: classFileFormat, className, className, methodString];
213 NSError *error = nil;
214
215 [classFile writeToFile: classFileName atomically: NO encoding: NSASCIIStringEncoding error: &error];
216 }
217
218 - (void) createHeaderAndSourceFileForClassNamed: (NSString *)className
219 {
220 [self createHeaderForClassNamed: className];
221 [self createSourceForClassNamed: className];
222 }
223
224 - (void) generateClassCode
225 {
226 NSLog(@"%@",classes);
227 NSEnumerator *en = [classes keyEnumerator];
228 id cls = nil;
229
230 while((cls = [en nextObject]) != nil)
231 {
232 [self createHeaderAndSourceFileForClassNamed: cls];
233 }
234 }
235
236 - (void) generate
237 {
238 [self collectClassDetails];
239 [self generateClassCode];
240 }
241
242 @end