Xcode 2.1 cleanup, universal binary experiments
[TestXSLT.git] / src / Workset.m
1 //
2 //  Workset.m
3 //  TestXSLT
4 //
5 //  Created by Marc Liyanage on Sun Mar 03 2002.
6 //  Copyright (c) 2001 __MyCompanyName__. All rights reserved.
7 //
8
9 #import "Workset.h"
10
11
12 @implementation Workset
13
14 + (void)initialize {
15         [Workset setKeys:[NSArray arrayWithObject:@"xmlCode"] triggerChangeNotificationsForDependentKey:@"hasXmlCode"];
16         [Workset setKeys:[NSArray arrayWithObject:@"xsltCode"] triggerChangeNotificationsForDependentKey:@"hasXsltCode"];
17         [Workset setKeys:[NSArray arrayWithObject:@"result"] triggerChangeNotificationsForDependentKey:@"hasResult"];
18         [Workset setKeys:[NSArray arrayWithObject:@"resultFilename"] triggerChangeNotificationsForDependentKey:@"hasResultFilename"];
19         [Workset setKeys:[NSArray arrayWithObject:@"xmlFilename"] triggerChangeNotificationsForDependentKey:@"hasXmlFilename"];
20         [Workset setKeys:[NSArray arrayWithObject:@"xsltFilename"] triggerChangeNotificationsForDependentKey:@"hasXsltFilename"];
21
22         [Workset setKeys:[NSArray arrayWithObject:@"result"] triggerChangeNotificationsForDependentKey:@"stringResult"];
23
24 }
25
26
27
28 - (id)init {
29         
30         self = [super init];
31         if (!self) return nil;
32         
33         id defaults = [NSUserDefaults standardUserDefaults];
34         
35         [self setValue:[defaults stringForKey:@"templateXML"] forKey:@"xmlCode"];
36         [self setValue:[defaults stringForKey:@"templateXSLT"] forKey:@"xsltCode"];
37
38         [self setValue:nil forKey:@"resultFilename"];
39         
40         result = [[NSData alloc] init];
41         parameterSet = [[ParameterSet alloc] init];
42
43         return self;
44         
45 }
46
47
48 - (NSArray *)coderKeys {
49         return [NSArray arrayWithObjects:
50                 @"xmlCode",
51                 @"xsltCode",
52                 @"result",
53                 @"parameterSet",
54                 @"xmlFilename",
55                 @"xsltFilename",
56                 @"resultFilename",
57                 nil];
58 }
59
60
61 - (id)initWithCoder:(NSCoder *)coder {
62
63         if (self = [super init]) {
64                 NSEnumerator *keys = [[self coderKeys] objectEnumerator];
65                 id key;
66                 while (key = [keys nextObject]) {
67                         [self setValue:[coder decodeObjectForKey:key] forKey:key];
68                 }
69         }
70         return self;
71
72 }
73
74
75 - (void)encodeWithCoder:(NSCoder *)coder {
76
77         NSEnumerator *keys = [[self coderKeys] objectEnumerator];
78         id key;
79         while (key = [keys nextObject]) {
80                 [coder encodeObject:[self valueForKey:key] forKey:key];
81         }
82
83 }
84
85
86 - (void)dealloc {
87
88         [xmlCode release];
89         [xsltCode release];
90         [result release];
91         [parameterSet release];
92
93
94         [super dealloc];
95
96 //      NSLog(@"Workset dealloc");
97
98 }
99
100
101 - (BOOL)hasXmlFilename {
102         return xmlFilename != nil;
103 }
104
105 - (BOOL)saveXml {
106
107         if (![self hasXmlFilename]) {
108                 return FALSE;
109         }
110         
111         [[XMLUtils getDataWithEncodingFromString:xmlCode] writeToFile:xmlFilename atomically:NO];
112         [self updateXmlFileModificationDate];
113
114         return YES;
115         
116 }
117
118 - (BOOL)xmlModifiedExternally {
119
120         NSDictionary *fileAttr;
121
122         if (![self hasXmlFilename]) {
123                 return FALSE;
124         }
125
126         fileAttr = [[NSFileManager defaultManager] fileAttributesAtPath:xmlFilename traverseLink:YES];
127
128         if (fileAttr == nil || xmlFileModificationDate == nil) {
129                 return FALSE;
130         }
131
132         if (![xmlFileModificationDate isEqualToDate:[fileAttr objectForKey:NSFileModificationDate]]) {
133                 return TRUE;
134         }
135
136         return FALSE;
137         
138 }
139
140 - (void)updateXmlFileModificationDate {
141
142         NSDictionary *fileAttr;
143
144         if ([self hasXmlFilename]) {
145                 fileAttr = [[NSFileManager defaultManager] fileAttributesAtPath:xmlFilename traverseLink:YES];
146                 [xmlFileModificationDate release];
147                 xmlFileModificationDate = [fileAttr objectForKey:NSFileModificationDate];
148                 [xmlFileModificationDate retain];
149 //              NSLog(@"xml file mod date: %@", xmlFileModificationDate);
150         }
151
152 }
153
154 - (void)reloadXmlFromFile {
155         [self setValue:[XMLUtils getStringWithEncodingFromFile:xmlFilename] forKey:@"xmlCode"];
156         [self updateXmlFileModificationDate];
157 }
158
159
160
161
162
163 - (int)resultEncoding {
164         return resultEncoding;
165 }
166
167 - (void)setResultEncoding:(NSStringEncoding)newencoding {
168         resultEncoding = newencoding;
169 }
170
171
172
173 - (BOOL)hasXsltFilename {
174         return xsltFilename != nil;
175 }
176
177 - (BOOL)saveXslt {
178
179         if (![self hasXsltFilename]) {
180                 return FALSE;
181         }
182
183         [[XMLUtils getDataWithEncodingFromString:xsltCode] writeToFile:xsltFilename atomically:NO];
184         [self updateXsltFileModificationDate];
185
186         return YES;
187
188 }
189
190 - (BOOL)xsltModifiedExternally {
191
192         NSDictionary *fileAttr;
193
194         if (![self hasXsltFilename]) {
195                 return FALSE;
196         }
197
198         fileAttr = [[NSFileManager defaultManager] fileAttributesAtPath:xsltFilename traverseLink:YES];
199
200         if (fileAttr == nil || xsltFileModificationDate == nil) {
201                 return FALSE;
202         }
203
204         if (![xsltFileModificationDate isEqualToDate:[fileAttr objectForKey:NSFileModificationDate]]) {
205                 return TRUE;
206         }
207
208         return FALSE;
209
210 }
211
212 - (void)updateXsltFileModificationDate {
213
214         NSDictionary *fileAttr;
215
216         if ([self hasXsltFilename]) {
217                 fileAttr = [[NSFileManager defaultManager] fileAttributesAtPath:xsltFilename traverseLink:YES];
218                 [xsltFileModificationDate release];
219                 xsltFileModificationDate = [fileAttr objectForKey:NSFileModificationDate];
220                 [xsltFileModificationDate retain];
221 //              NSLog(@"xslt file mod date: %@", xsltFileModificationDate);
222         }
223 }
224
225 - (void)reloadXsltFromFile {
226         [self setValue:[XMLUtils getStringWithEncodingFromFile:xsltFilename] forKey:@"xsltCode"];
227         [self updateXsltFileModificationDate];
228 }
229
230
231 - (BOOL)hasResultFilename {
232         return resultFilename != nil;
233 }
234
235
236 - (NSString *)stringResult {
237         return [[[NSString alloc] initWithData:result encoding:[self resultEncoding]] autorelease];
238 }
239
240 - (void)saveResult {
241         
242         if (![self hasResultFilename]) {
243                 NSLog(@"saveResult called but no filename is available");
244                 return;
245         }
246         [result writeToFile:resultFilename atomically:NO];
247 }
248
249
250 - (BOOL)hasXmlCode {
251         return ([xmlCode length] > 0);
252 }
253
254 - (BOOL)hasXsltCode {
255         return ([xsltCode length] > 0);
256 }
257
258 - (BOOL)hasResult {
259         return ([result length] > 0);
260 }
261
262 - (BOOL)hasParameters {
263         return ([parameterSet count] > 0);
264 }
265
266
267
268
269
270 @end