#import "MyDocument.h" #import "DataItem.h" @implementation MyDocument - (id)init { self = [super init]; if (self) { theItems = [[NSMutableArray alloc] init]; measureTypes = [[NSMutableArray alloc] init]; } return self; } - (void)dealloc { [measureTypes release]; [theItems release]; [super dealloc]; } - (IBAction)createNewItem:(id)sender { DataItem *newItem = [[DataItem alloc] init]; [newItem takeValue:[dataField stringValue] forKey:@"theData"]; [newItem takeValue:[typeMenu titleOfSelectedItem] forKey:@"typeName"]; [theController addObject:newItem]; [newItem release]; [dataField setStringValue:@""]; } - (NSData *)dataRepresentationOfType:(NSString *)aType { int i; NSMutableString *output = [[NSMutableString alloc] init]; NSMutableDictionary *typeByName = [[NSMutableDictionary alloc] init]; [output appendString:@"\n"]; [output appendString:@""]; NSEnumerator *enumerator = [measureTypes objectEnumerator]; id key; while ((key = [enumerator nextObject])) { [output appendFormat:@"", [key valueForKey:@"id"], [key valueForKey:@"name"], [key valueForKey:@"type"]]; [typeByName setObject:[key valueForKey:@"id"] forKey:[key valueForKey:@"name"]]; } [output appendString: @""]; for (i=0; i < [theItems count]; i++) { [output appendFormat:@"", [[[theItems objectAtIndex:i] valueForKey:@"timeTaken"] descriptionWithCalendarFormat:@"%Y-%m-%d %H:%M:%S %z"], [typeByName valueForKey: [[theItems objectAtIndex:i] valueForKey:@"typeName"]], [[theItems objectAtIndex:i] valueForKey:@"theData"]]; } [typeByName release]; [output appendString:@""]; [output autorelease]; return [output dataUsingEncoding:NSASCIIStringEncoding]; } - (BOOL)loadDataRepresentation:(NSData *)data ofType:(NSString *)aType { BOOL success; NSXMLParser *addressParser = [[NSXMLParser alloc] initWithData:data]; [addressParser setDelegate:self]; success = [addressParser parse]; return YES; } - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict { if ( [elementName isEqualToString:@"type"]) { // this part will happen with the XML is opened----------- NSMutableDictionary *newType = [[NSMutableDictionary alloc] init]; [newType setObject:[attributeDict valueForKey:@"name"] forKey:@"name"]; [newType setObject:[attributeDict valueForKey:@"dataType"] forKey:@"type"]; [newType setObject:[attributeDict valueForKey:@"id"] forKey:@"id"]; [measureTypes addObject:newType]; [newType release]; } // only add if ID is null!!!! if ( [elementName isEqualToString:@"item"] && [[attributeDict valueForKey:@"id"] isEqualToString:@""]) { DataItem *newItem = [[DataItem alloc] init]; [newItem takeValue:[attributeDict valueForKey:@"data"] forKey:@"theData"]; NSEnumerator *enumerator = [measureTypes objectEnumerator]; id key; while ((key = [enumerator nextObject])) { if ([[attributeDict valueForKey:@"typeId"] isEqualToString:[key valueForKey:@"id"]]) { [newItem takeValue:[key valueForKey:@"name"] forKey:@"typeName"]; } } [newItem takeValue: [NSCalendarDate dateWithString:[attributeDict valueForKey:@"time"]] forKey:@"timeTaken"]; [theItems addObject:newItem]; NSLog([theItems description]); [newItem release]; return; } } - (void)windowControllerDidLoadNib:(NSWindowController *) aController { [super windowControllerDidLoadNib:aController]; [typeMenu removeAllItems]; NSEnumerator *enumerator = [measureTypes objectEnumerator]; id key; while ((key = [enumerator nextObject])) { [typeMenu addItemWithTitle:[key valueForKey:@"name"]]; } [theController setContent:theItems]; } - (NSString *)windowNibName { return @"MyDocument"; } @end