« ABC's Archive Repository

Posted by Andy Monitzer on April 08, 2002 [Feedback (0) & TrackBack (0)]

Jiiva's Archive Repository is part of the Application Builder Collection (ABC). This tutorial will show you what it can do and how to implement it.

 

// What Does It Do?

Cocoa applications are more than just code. Even the most basic app possible (the template PB provides) contains something called a "resource" named "Main.nib". Resources can be any kind of file: Images, text files, nibs, sounds, or other applications.

Usually, you just include resources by inserting them into the project (the build system automatically copies them into the resource-directory of the application wrapper). Then you can use -[NSString stringWithContentsOfFile:], -[NSImage imageWithContentsOfFile:] or whatever you want by referencing the file with [[NSBundle bundleForClass:[self class]] pathForResource:@"filename" ofType:@"extension"]. The problem? Well, anybody can sneak into your resources just by right-clicking on your app and selecting "Show Package Contents". Additionally, hundreds of small files are difficult to manage (updating, etc).

ABC's Archive Repository is an attempt to solve some of the problems. Similar to tar-archives, its archives can hold multiple items (even in a hierarchy). The difference is that you can access the data stored in it directly via an Objective C-API which comes with ABCFoundation.

The archive itself is just another resource you can load. Jiiva promised to add some more features to it, like compression and encryption, which would add additional advantages to archives (e.g. preventing others from sneaking into your resources even when they have the right tools to do it).

 

// Preparations

The first step is to actually construct the archive. For this purpose, Jiiva has created a utility hidden inside /Applications/Utilities/, called "ABCArchiveBuilder." Open it, then create a new archive. For this demonstration app, I created two text files named "Text1.txt" and "Text2.txt" with the contents "Text1" and "Text2" respectively. Drag them into the window:

ABCArchiveBuilder

Select each entry and choose "Text" from the type-popup, then save to the disk (e.g. the desktop). Now we're done in this application.

 

// The Project

Create a new Cocoa application from the default template (I called it "ArchiveTest"), then add the Jiiva frameworks ABCApplication and ABCFoundation. Next step is to add the archive we created in the previous section (copying it into the project directory).

Ok, now we have to create an interface in IB. Since this is only a test app, I just inserted two buttons (one for each text file) and a text field (to display the contents of the chosen text file).

Window Layout
This is how our window should look like.

Create a new subclass of NSObject called "ArchiveTest" which contains two actions (-text1: and -text2:) and one outlet (NSTextField *textfield). Instantiate the class and create the connections appropiately. Then create the source files and let IB insert them into the project.

In PB, we have to define three methods, the two action methods and -awakeFromNib for loading the archive. ABC's Archive Repository provides a convenience class for easy loading of resources called "ABCArchiveGroup". There are some more sophisticated classes for handling hierarchies, but they're not necessary for our project.

#import "ArchiveTest.h"
#import <ABCFoundation/ABCArchiveRepository.h>
#import <ABCFoundation/ABCArchiveRepositoryGroup.h>

@implementation ArchiveTest

Initializing ABCArchiveGroup is pretty easy, you just have to specify the archive (reading it as another resource):

- (void)awakeFromNib {
    [[ABCArchiveRepositoryGroup defaultGroup]
     addArchiveRepositoryWithPath:[[NSBundle bundleForClass:[self class]]
     pathForResource:@"TestArchive" ofType:@"ja"]];
}

-text1: and -text2: are the nearly same, only the name of the resource differs. Since we want to get a string (and specified it in ABCArchiveBuilder), we have to use -stringWithIdentifier:

- (IBAction)text1:(id)sender {
    [textfield setStringValue:[[ABCArchiveRepositoryGroup defaultGroup]
     stringWithIdentifier:@"Text1.txt"]];
}

- (IBAction)text2:(id)sender {
    [textfield setStringValue:[[ABCArchiveRepositoryGroup defaultGroup]
     stringWithIdentifier:@"Text2.txt"]];
}

@end

Well, that's it! You can now compile and run your application.

 

// Conclusion

ABC's Archive Repository looks pretty interesting at first sight, but there are currently few reasons to choose it over regular resources. When encryption is addded, this view changes dramatically, since it's another step to provide security for your intellectual property.


Comments
Post a comment