/** EOKeyValueArchiver.m EOKeyValueArchiver Class Copyright (C) 2000-2002,2003,2004,2005 Free Software Foundation, Inc. Author: Manuel Guesdon Date: September 2000 $Revision$ $Date$ EOKeyValueArchiver object is used to archive a tree of objects into a key/value propertyList. EOKeyValueUnarchiver object is used to unarchive from a propertyList a tree of objects archived with a EOKeyValueArchiver. Example: // Archiving: EOKeyValueArchiver* archive=AUTORELEASE([EOKeyValueArchiver new]); [archive setDelegate:MyArchivingDelegate]; [archiver encodeObject:anObject forKey:@"anObjectKey"]; [archiver encodeInt:125 forKey:@"aKey"]; ... NSDictionary* archivePropertyList=[archiver dictionary]; // Now unarchive archivePropertyList EOKeyValueUnarchiver* unarchiver=AUTORELEASE([[EOKeyValueUnarchiver alloc]initWith:archivePropertyList]); [archive setDelegate:MyUnarchivingDelegate]; id anObject=[unarchiver decodeObjectForKey:@"anObjectKey"]; int anInt=[unarchiver decodeIntForKey:@"anKey"]; [unarchiver finishInitializationOfObjects]; [unarchiver awakeObjects] This file is part of the GNUstep Database Library. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License for more details. You should have received a copy of the GNU Library General Public License along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. **/ #include "config.h" RCS_ID("$Id$") #ifdef GNUSTEP #include #include #include #include #include #include #include #else #include #endif #ifndef GNUSTEP #include #include #endif #include #include #include @interface EOKeyValueArchivingContainer : NSObject { id _object; id _parent; NSDictionary * _propertyList; } + (EOKeyValueArchivingContainer*)keyValueArchivingContainer; - (void) setPropertyList: (id)propList; - (id) propertyList; - (void) setParent: (id)parent; - (id) parent; - (void) setObject: (id)object; - (id) object; @end @implementation EOKeyValueArchivingContainer + (void)initialize { static BOOL initialized=NO; if (!initialized) { initialized=YES; GDL2_PrivateInit(); } } + (EOKeyValueArchivingContainer *)keyValueArchivingContainer { return [[[self alloc] init] autorelease]; } - (void) setPropertyList: (id)propList { ASSIGN(_propertyList, propList); } - (id) propertyList { return _propertyList; } - (void) setParent: (id)parent { _parent = parent; } - (id) parent { return _parent; } - (void) setObject: (id)object { ASSIGN(_object, object); } - (id) object { return _object; } - (void) dealloc { DESTROY(_object); _parent = nil; DESTROY(_propertyList); [super dealloc]; } @end @implementation EOKeyValueArchiver + (void)initialize { static BOOL initialized=NO; if (!initialized) { initialized=YES; GDL2_PrivateInit(); } } /** Init method **/ - (id) init { if ((self=[super init])) { _propertyList=[NSMutableDictionary new]; }; return self; } - (void) dealloc { DESTROY(_propertyList); [super dealloc]; } /** Returns archived object/tree as propertList **/ - (NSDictionary*) dictionary { return _propertyList; } /** Archives integer 'intValue' as 'key' **/ - (void) encodeInt: (int)intValue forKey: (NSString*)key { NSAssert(key,@"No key"); [_propertyList setObject: [NSNumber numberWithInt: intValue] forKey: key]; } /** Archives boolean 'yn' as 'key' **/ - (void) encodeBool: (BOOL)yn forKey: (NSString*)key { NSAssert(key,@"No key"); [_propertyList setObject: [NSNumber numberWithBool: yn] forKey: key]; } /** Archives a dictionary for 'key' **/ - (void) _encodeDictionary: (NSDictionary*)dictionary forKey: (NSString*)key { NSAssert(key,@"No key"); if ([dictionary count]>0) { NSEnumerator* keyEnumerator=nil; NSString* tmpKey=nil; NSMutableDictionary* currentPropertyList=nil; // Save current propertyList currentPropertyList=AUTORELEASE(_propertyList); // Set new empty propertyList to encode each object _propertyList=[NSMutableDictionary new]; keyEnumerator=[dictionary keyEnumerator]; while((tmpKey=[keyEnumerator nextObject])) { id object=[dictionary valueForKey:tmpKey]; [self encodeObject:object forKey:tmpKey]; }; // add _propertyList into current propertyList // for the key [currentPropertyList setObject:_propertyList forKey:key]; // put back current propertyList ASSIGN(_propertyList,currentPropertyList); } else { [_propertyList setObject:[NSDictionary dictionary] forKey:key]; }; } /** Archives an array objects for 'key' **/ - (void) _encodeObjects: (NSArray*)objects forKey: (NSString*)key { unsigned int count=0; NSAssert(key,@"No key"); count=[objects count]; if (count>0) { unsigned int i=0; NSMutableDictionary* currentPropertyList=nil; NSMutableArray* archiveArray=(NSMutableArray*)[NSMutableArray array]; // Save current propertyList currentPropertyList=AUTORELEASE(_propertyList); // Set new empty propertyList to encode each object _propertyList=[NSMutableDictionary new]; for(i=0;i