#import "UnarchiverAppDelegate.h" #import "Presentity.h" @implementation UnarchiverAppDelegate @synthesize window; @synthesize label; - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { NSArray *unarchive; NSString *outputPath = @"/Users/USERNAME/Documents/iChats XML/"; NSString *path = @"/Users/USERNAME/Documents/iChats/"; NSFileManager *fileMan = [[NSFileManager alloc] init]; NSArray *files = [fileMan contentsOfDirectoryAtPath:path error:nil]; for(int i = 0; i < [files count]; i++) { if([[files objectAtIndex:i] hasSuffix:@".chat"]) { [self print:[files objectAtIndex:i]]; unarchive = [NSUnarchiver unarchiveObjectWithFile:[path stringByAppendingString:[files objectAtIndex:i]]]; xml = [[NSXMLDocument alloc] initWithRootElement:[[NSXMLElement alloc] initWithName:[unarchive className]]]; [self printArray:unarchive withPrefix:@"" andContext:[xml rootElement]]; [[[xml rootElement] XMLStringWithOptions:NSXMLNodePrettyPrint] writeToFile:[[outputPath stringByAppendingString:[files objectAtIndex:i]] stringByAppendingString:@".xml"] atomically:TRUE encoding:NSUTF8StringEncoding error:nil ]; } } [self print:@"done!"]; } - (void)print:(NSString*)message { printf("%s", [message UTF8String]); printf("\n"); } - (void)printArray:(NSArray*)array withPrefix:(NSString*)prefix andContext:(NSXMLElement*)node { for(int i = 0; i < [array count]; i++) { id elem = [array objectAtIndex:i]; NSXMLElement *newNode = [[NSXMLElement alloc] initWithName:[elem className]]; [node addChild:newNode]; //[self print:[prefix stringByAppendingString:[elem className]]]; if([[elem className] isEqualToString:@"NSCFArray"]) { // for arrays, recurse into them [self printArray:elem withPrefix:[prefix stringByAppendingString:@" "] andContext:node]; } else if([[elem className] isEqualToString:@"NSCFString"]) { // just print out strings directly //[self print:[@"--" stringByAppendingString:elem]]; [newNode addChild:[NSXMLNode textWithStringValue:elem]]; } else if([[elem className] isEqualToString:@"NSCFNumber"]) { // Just print out numbers directly too //[self print:[@"++" stringByAppendingString:[elem stringValue]]]; [newNode addChild:[NSXMLNode textWithStringValue:[elem stringValue]]]; } else if([[elem className] isEqualToString:@"Presentity"]) { // Sender/service information [newNode addAttribute:[NSXMLNode attributeWithName:@"service" stringValue:[elem service]]]; [newNode addAttribute:[NSXMLNode attributeWithName:@"senderID" stringValue:[elem senderID]]]; } else if([[elem className] isEqualToString:@"InstantMessage"]) { // Now, we come to the truth of it Presentity *sender = [elem sender]; NSDate *date = [elem date]; NSAttributedString *text = [elem text]; NSXMLNode *senderNode = [[NSXMLElement alloc] initWithName:@"sender"]; [senderNode addAttribute:[NSXMLNode attributeWithName:@"service" stringValue:[sender service]]]; [senderNode addAttribute:[NSXMLNode attributeWithName:@"senderID" stringValue:[sender senderID]]]; [newNode addChild:senderNode]; [newNode addAttribute:[NSXMLNode attributeWithName:@"date" stringValue:[date description]]]; [newNode addChild:[NSXMLNode textWithStringValue:[text string]]]; } } } @end