Saturday, August 20, 2005

Making a filter - Part 2

AppleTrans plugins are all written in Objective-C. For a filter plugin, you simply implements the following 4 methods as defined in the protocol:

@protocol FilterPlugin
- (NSMutableData *)unarchiveFilter:(NSMutableData *)data context:(NSDictionary **)context;
- (NSMutableData *)archiveFilter:(NSMutableData *)data context:(NSDictionary *)context;
- (NSMutableAttributedString *)importFilter:(NSMutableAttributedString *)content context:(NSDictionary **)context;
- (NSMutableAttributedString *)exportFilter:(NSMutableAttributedString *)content context:(NSDictionary *)context;

The first two methods are used to perform binary-text conversion. A unarchiveFilter:context: message is sent right after reading a file, and an archiveFilter:context: message is sent right before writing a file. If you do not process the data passed in data, just return it unmodified.

The text encoding conversion is done by AppleTrans. If you want to ensure a safe conversion, use Unicode (NSUnicodeStringEncoding) when you convert binary data to text format.

The next two methods are used to reformat the text content. An importFilter:context: message is sent after unarchiveFilter:context:, and an exportFilter:context: message is sent before archiveFilter:context:. If you do not process the data passed in content, just return it unmodified.

Both unarchiveFilter:context: and importFilter:context: pass a pointer to an NSDictionary object in context, where you can store persistent data that lives with AppleTrans document. At the time you get these messages, context might point to nil. You may then need to create an NSMutableDictionary and set context to points to the new object.

Well, that's pretty much about it. In Part 3, we will design a simple filter which converts PDF to plain text.


Post a Comment

<< Home