00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 @import "CPObject.j"
00024 @import "CPDictionary.j"
00025
00032 var CPBundlesForURLStrings = { };
00033
00034 @implementation CPBundle : CPObject
00035 {
00036 CFBundle _bundle;
00037 id _delegate;
00038 }
00039
00040 + (CPBundle)bundleWithURL:(CPURL)aURL
00041 {
00042 return [[self alloc] initWithURL:aURL];
00043 }
00044
00045 + (CPBundle)bundleWithPath:(CPString)aPath
00046 {
00047 return [self bundleWithURL:aPath];
00048 }
00049
00050 + (CPBundle)bundleForClass:(Class)aClass
00051 {
00052 return [self bundleWithURL:CFBundle.bundleForClass(aClass).bundleURL()];
00053 }
00054
00055 + (CPBundle)mainBundle
00056 {
00057 return [CPBundle bundleWithPath:CFBundle.mainBundle().bundleURL()];
00058 }
00059
00060 - (id)initWithURL:(CPURL)aURL
00061 {
00062 aURL = new CFURL(aURL);
00063
00064 var URLString = aURL.absoluteString(),
00065 existingBundle = CPBundlesForURLStrings[URLString];
00066
00067 if (existingBundle)
00068 return existingBundle;
00069
00070 self = [super init];
00071
00072 if (self)
00073 {
00074 _bundle = new CFBundle(aURL);
00075 CPBundlesForURLStrings[URLString] = self;
00076 }
00077
00078 return self;
00079 }
00080
00081 - (id)initWithPath:(CPString)aPath
00082 {
00083 return [self initWithURL:aPath];
00084 }
00085
00086 - (Class)classNamed:(CPString)aString
00087 {
00088
00089 }
00090
00091 - (CPURL)bundleURL
00092 {
00093 return _bundle.bundleURL();
00094 }
00095
00096 - (CPString)bundlePath
00097 {
00098 return [[self bundleURL] path];
00099 }
00100
00101 - (CPString)resourcePath
00102 {
00103 return [[self resourceURL] path];
00104 }
00105
00106 - (CPURL)resourceURL
00107 {
00108 return _bundle.resourcesDirectoryURL();
00109 }
00110
00111 - (Class)principalClass
00112 {
00113 var className = [self objectForInfoDictionaryKey:@"CPPrincipalClass"];
00114
00115
00116
00117 return className ? CPClassFromString(className) : Nil;
00118 }
00119
00120 - (CPString)pathForResource:(CPString)aFilename
00121 {
00122 return _bundle.pathForResource(aFilename);
00123 }
00124
00125 - (CPDictionary)infoDictionary
00126 {
00127 return _bundle.infoDictionary();
00128 }
00129
00130 - (id)objectForInfoDictionaryKey:(CPString)aKey
00131 {
00132 return _bundle.valueForInfoDictionaryKey(aKey);
00133 }
00134
00135
00136
00137 - (void)loadWithDelegate:(id)aDelegate
00138 {
00139 _delegate = aDelegate;
00140
00141 _bundle.addEventListener("load", function()
00142 {
00143 [_delegate bundleDidFinishLoading:self];
00144 });
00145
00146 _bundle.addEventListener("error", function()
00147 {
00148 CPLog.error("Could not find bundle: " + self);
00149 });
00150
00151 _bundle.load(YES);
00152 }
00153
00154 - (CPArray)staticResourceURLs
00155 {
00156 var staticResourceURLs = [],
00157 staticResources = _bundle.staticResources(),
00158 index = 0,
00159 count = [staticResources count];
00160
00161 for (; index < count; ++index)
00162 [staticResourceURLs addObject:staticResources[index].URL()];
00163
00164 return staticResourceURLs;
00165 }
00166
00167 - (CPArray)environments
00168 {
00169 return _bundle.environments();
00170 }
00171
00172 - (CPString)mostEligibleEnvironment
00173 {
00174 return _bundle.mostEligibleEnvironment();
00175 }
00176
00177 - (CPString)description
00178 {
00179 return [super description] + "(" + [self bundlePath] + ")";
00180 }
00181
00182 @end