[Drawkit] File format extensions

Brad Larson larson at sonoplot.com
Thu May 15 16:43:02 PDT 2008


Alright, so I was able to implement the changes I suggested for adding  
hooks for other file formats.  The other file formats can be added as  
categories on DKDrawing.  These categories need to provide  
drawingWith[Filetype]: and dataFor[Filetype] methods, with [Filetype]  
standing for the document type name, as specified in the Info.plist  
document with your application.  That way, you can add or remove file  
types easily at the application level, and set what extensions they'll  
correspond to at that same level.

For example, the header to the current Drawing filetype category is

@interface DKDrawing (FiletypeDrawing)

+ (DKDrawing*)				drawingWithDrawing:(NSData*) drawingData;
- (NSData*)					dataForDrawing;

@end

This works by using selectors in the DKDrawingDocument  
dataOfType:error: and readFromData:ofType:error: methods as follows:

- (NSData *)			dataOfType:(NSString*) typeName error:(NSError**)  
outError
{
	NSString *nameOfFiletypeSavingMethod = [NSString  
stringWithFormat:@"dataFor%@", typeName];
	
	// If the selector is not there for this file format, throw an error
	if (![[self drawing]  
respondsToSelector:NSSelectorFromString(nameOfFiletypeSavingMethod)])
	{
		*outError = [NSError errorWithDomain:NSCocoaErrorDomain  
code:NSFileWriteUnsupportedSchemeError userInfo:nil];
		return nil;
	}
	
	return [[self drawing]  
performSelector:NSSelectorFromString(nameOfFiletypeSavingMethod)];
}

with similar code being used for the loading end of things.  This does  
a check first to make sure that the file format is supported via the  
framework's categories or the application's categories, throwing an  
NSError and aborting if not.  You can try this out by adding a  
different file type to the Properties (accessible through the Target  
tab in XCode) or Info.plist of DrawDemo and see how it handles the  
loading and saving of supported and unsupported formats.

A modified version of Beta 3 with this framework in place is currently  
being hosted at http://www.sonoplot.com/downloads/drawkit.tar.bz2 ,  
but that might not last long.  I'll find a better place to post  
additions.

Let me know if you feel this is not the right way to implement new  
file formats and I'll rethink the interface.  Otherwise, I'll proceed  
with the SVG import / export capabilities and our own application- 
specific XML format.

Also, there are a bunch of classes with the "GC" prefix still left in  
the framework (including the compiled framework itself).  I can clean  
all those up, if you'd like.

______________________
Brad Larson
SonoPlot, Inc.
3030 Laura Lane, Suite 120
Middleton, WI 53562





More information about the Drawkit mailing list