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
00026 @import "CPURLRequest.j"
00027
00032 @implementation CPBundle : CPObject
00033 {
00034 }
00035
00036 + (id)alloc
00037 {
00038 return new objj_bundle;
00039 }
00040
00041 + (CPBundle)bundleWithPath:(CPString)aPath
00042 {
00043 return objj_getBundleWithPath(aPath);
00044 }
00045
00046 + (CPBundle)bundleForClass:(Class)aClass
00047 {
00048 return objj_bundleForClass(aClass);
00049 }
00050
00051 + (CPBundle)mainBundle
00052 {
00053 return [CPBundle bundleWithPath:"Info.plist"];
00054 }
00055
00056 - (id)initWithPath:(CPString)aPath
00057 {
00058 self = [super init];
00059
00060 if (self)
00061 {
00062 path = aPath;
00063
00064 objj_setBundleForPath(path, self);
00065 }
00066
00067 return self;
00068 }
00069
00070 - (Class)classNamed:(CPString)aString
00071 {
00072
00073 }
00074
00075 - (CPString)bundlePath
00076 {
00077 return [path stringByDeletingLastPathComponent];
00078 }
00079
00080 - (CPString)resourcePath
00081 {
00082 var resourcePath = [self bundlePath];
00083
00084 if (resourcePath.length)
00085 resourcePath += '/';
00086
00087 return resourcePath + "Resources";
00088 }
00089
00090 - (Class)principalClass
00091 {
00092 var className = [self objectForInfoDictionaryKey:@"CPPrincipalClass"];
00093
00094
00095
00096 return className ? CPClassFromString(className) : Nil;
00097 }
00098
00099 - (CPString)pathForResource:(CPString)aFilename
00100 {
00101 return [self resourcePath] + '/' + aFilename;
00102 }
00103
00104 - (CPDictionary)infoDictionary
00105 {
00106 return info;
00107 }
00108
00109 - (id)objectForInfoDictionaryKey:(CPString)aKey
00110 {
00111 return [info objectForKey:aKey];
00112 }
00113
00114
00115
00116 - (void)loadWithDelegate:(id)aDelegate
00117 {
00118 self._delegate = aDelegate;
00119 self._infoConnection = [CPURLConnection connectionWithRequest:[CPURLRequest requestWithURL:[self bundlePath] + "/Info.plist"] delegate:self];
00120 }
00121
00122 - (void)connection:(CPURLConnection)aConnection didReceiveData:(CPString)data
00123 {
00124 if (aConnection === self._infoConnection)
00125 {
00126 info = CPPropertyListCreateFromData([CPData dataWithString:data]);
00127
00128 var platform = '/',
00129 platforms = [self objectForInfoDictionaryKey:"CPBundlePlatforms"];
00130
00131 if (platforms)
00132 {
00133 platform = [platforms firstObjectCommonWithArray:OBJJ_PLATFORMS];
00134 platform = platform ? '/' + platform + ".platform/" : '/';
00135 }
00136
00137 [CPURLConnection connectionWithRequest:[CPURLRequest requestWithURL:[self bundlePath] + platform + [self objectForInfoDictionaryKey:"CPBundleExecutable"]] delegate:self];
00138 }
00139 else
00140 {
00141 objj_decompile([data string], self);
00142
00143 var context = new objj_context();
00144
00145 if ([_delegate respondsToSelector:@selector(bundleDidFinishLoading:)])
00146 context.didCompleteCallback = function() { [_delegate bundleDidFinishLoading:self]; };
00147
00148 var files = [self objectForInfoDictionaryKey:@"CPBundleReplacedFiles"],
00149 count = files.length,
00150 bundlePath = [self bundlePath];
00151
00152 while (count--)
00153 {
00154 var fileName = files[count];
00155
00156 if (fileName.indexOf(".j") === fileName.length - 2)
00157 context.pushFragment(fragment_create_file(bundlePath + '/' + fileName, new objj_bundle(""), YES, NULL));
00158 }
00159
00160 if (context.fragments.length)
00161 context.evaluate();
00162 else
00163 [_delegate bundleDidFinishLoading:self];
00164 }
00165 }
00166
00167 - (void)connection:(CPURLConnection)aConnection didFailWithError:(CPError)anError
00168 {
00169 alert("Couldnot find bundle:" + anError)
00170 }
00171
00172 - (void)connectionDidFinishLoading:(CPURLConnection)aConnection
00173 {
00174 }
00175
00176 @end
00177
00178 objj_bundle.prototype.isa = CPBundle;