Skip to content

Commit

Permalink
Improved support for resources in nib2cib.
Browse files Browse the repository at this point in the history
Reviewed by me.
  • Loading branch information
Francisco Ryan Tolmasky I committed Feb 1, 2009
1 parent 064a554 commit 9ec781f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 4 deletions.
44 changes: 44 additions & 0 deletions Tools/nib2cib/NSCustomResource.j
Expand Up @@ -25,6 +25,8 @@

@import <AppKit/_CPCibCustomResource.j>

importClass(javax.imageio.ImageIO);


@implementation _CPCibCustomResource (NSCoding)

Expand All @@ -36,6 +38,48 @@
{
_className = CP_NSMapClassName([aCoder decodeObjectForKey:@"NSClassName"]);
_resourceName = [aCoder decodeObjectForKey:@"NSResourceName"];

var size = CGSizeMakeZero();

if (![aCoder resourcesFile])
{
CPLog.warn("***WARNING: Resources found in nib, but no resources path specified with -R option.");

_properties = [CPDictionary dictionaryWithObject:CGSizeMakeZero() forKey:@"size"];
}
else
{
var resourceFile = [aCoder resourceFileForName:_resourceName];

if (!resourceFile)
CPLog.warn("***WARNING: Resource named " + _resourceName + " not found in supplied resources path.");
else
{
var imageStream = ImageIO.createImageInputStream(resourceFile),
readers = ImageIO.getImageReaders(imageStream),
reader = null;

if(readers.hasNext())
reader = readers.next();

else
{
imageStream.close();
//can't read image format... what do you want to do about it,
//throw an exception, return ?
}

reader.setInput(imageStream, true, true);

// Now we know the size (yay!)
size = CGSizeMake(reader.getWidth(0), reader.getHeight(0));
print(size.width + " " + size.height);
reader.dispose();
imageStream.close();
}
}

_properties = [CPDictionary dictionaryWithObject:size forKey:@"size"];
}

return self;
Expand Down
52 changes: 52 additions & 0 deletions Tools/nib2cib/Nib2CibKeyedUnarchiver.j
@@ -0,0 +1,52 @@

@import <Foundation/CPKeyedUnarchiver.j>


@implementation Nib2CibKeyedUnarchiver : CPKeyedUnarchiver
{
File resourcesFile;
}

- (id)initForReadingWithData:(CPData)data resourcesFile:(File)aResourcesFile
{
self = [super initForReadingWithData:data];

if (self)
resourcesFile = aResourcesFile;

return self;
}

- (File)resourcesFile
{
return resourcesFile;
}

- (File)resourceFileForName:(CPString)aName
{
var moreFiles = [resourcesFile.listFiles()];

do
{
var files = moreFiles.shift(),
index = 0,
count = files.length;

for (; index < count; ++index)
{
var file = files[index].getCanonicalFile(),
name = String(file.getName());

if (name === aName)
return file;

if (file.isDirectory())
moreFiles.push(file.listFiles());
}
}
while (moreFiles.length > 0)

return NULL;
}

@end
27 changes: 23 additions & 4 deletions Tools/nib2cib/main.j
Expand Up @@ -27,8 +27,10 @@

@import "NSFoundation.j"
@import "NSAppKit.j"
@import "Nib2CibKeyedUnarchiver.j"

importPackage(java.io);
importClass(java.io.File);

CPLogRegister(CPLogPrint);

Expand Down Expand Up @@ -76,8 +78,21 @@ function cibExtension(aPath)
return aPath.substr(0, dotIndex) + ".cib";
}

function convert(inputFileName, outputFileName)
function convert(inputFileName, outputFileName, resourcesPath)
{
var resourcesFile = nil;

if (resourcesPath)
{
resourcesFile = new File(resourcesPath).getCanonicalFile();

if (!resourcesFile.canRead())
{
print("Could not find Resources at " + resourcesFile);
return;
}
}

// Make sure we can read the file
if (!(new Packages.java.io.File(inputFileName)).canRead())
{
Expand Down Expand Up @@ -115,7 +130,7 @@ function convert(inputFileName, outputFileName)
[data setString:[data string].replace(/\<key\>\s*CF\$UID\s*\<\/key\>/g, "<key>CP$UID</key>")];

// Unarchive the NS data
var unarchiver = [[CPKeyedUnarchiver alloc] initForReadingWithData:data],
var unarchiver = [[Nib2CibKeyedUnarchiver alloc] initForReadingWithData:data resourcesFile:resourcesFile],
objectData = [unarchiver decodeObjectForKey:@"IB.objectdata"],

data = [CPData data],
Expand Down Expand Up @@ -201,6 +216,7 @@ function main()

inputFileName = nil,
outputFileName = nil,
resourcesPath = nil,
frameworkPaths = [];

for (; index < count; ++index)
Expand All @@ -212,6 +228,9 @@ function main()

case "-F": frameworkPaths.push(arguments[++index]);
break;

case "-R": resourcesPath = arguments[++index];
break;

default: if (inputFileName && inputFileName.length > 0)
outputFileName = arguments[index];
Expand All @@ -224,10 +243,10 @@ function main()
outputFileName = cibExtension(inputFileName);

if (frameworkPaths.length)
loadFrameworks(frameworkPaths, function() { convert(inputFileName, outputFileName); });
loadFrameworks(frameworkPaths, function() { convert(inputFileName, outputFileName, resourcesPath); });

else
convert(inputFileName, outputFileName);
convert(inputFileName, outputFileName, resourcesPath);
}

main.apply(main, args);

0 comments on commit 9ec781f

Please sign in to comment.