API  1.0.0
CPBundle.j
Go to the documentation of this file.
1 /*
2  * CPBundle.j
3  * Foundation
4  *
5  * Created by Francisco Tolmasky.
6  * Copyright 2008, 280 North, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 
24 @global CFBundleCopyBundleLocalizations
25 @global CFBundleCopyLocalizedString
26 
27 CPBundleDidLoadNotification = @"CPBundleDidLoadNotification";
28 
29 @protocol CPBundleDelegate <CPObject>
30 
31 @required
32 - (void)bundleDidFinishLoading:(CPBundle)aBundle;
33 
34 @end
35 
42 
43 @implementation CPBundle : CPObject
44 {
45  CFBundle _bundle;
46  id <CPBundleDelegate> _delegate;
47 }
48 
49 + (CPBundle)bundleWithURL:(CPURL)aURL
50 {
51  return [[self alloc] initWithURL:aURL];
52 }
53 
54 + (CPBundle)bundleWithPath:(CPString)aPath
55 {
56  return [self bundleWithURL:aPath];
57 }
58 
59 + (CPBundle)bundleWithIdentifier:(CPString)anIdentifier
60 {
61  var bundle = CFBundle.bundleWithIdentifier(anIdentifier);
62 
63  if (bundle)
64  {
65  var url = bundle.bundleURL(),
66  cpBundle = CPBundlesForURLStrings[url.absoluteString()];
67 
68  if (!cpBundle)
69  cpBundle = [self bundleWithURL:url];
70 
71  return cpBundle;
72  }
73 
74  return nil;
75 }
76 
77 + (CPBundle)bundleForClass:(Class)aClass
78 {
79  return [self bundleWithURL:CFBundle.bundleForClass(aClass).bundleURL()];
80 }
81 
82 + (CPBundle)mainBundle
83 {
84  return [CPBundle bundleWithPath:CFBundle.mainBundle().bundleURL()];
85 }
86 
87 - (id)initWithURL:(CPURL)aURL
88 {
89  aURL = new CFURL(aURL);
90 
91  var URLString = aURL.absoluteString(),
92  existingBundle = CPBundlesForURLStrings[URLString];
93 
94  if (existingBundle)
95  return existingBundle;
96 
97  self = [super init];
98 
99  if (self)
100  {
101  _bundle = new CFBundle(aURL);
102 
103  CPBundlesForURLStrings[URLString] = self;
104  }
105 
106  return self;
107 }
108 
109 - (id)initWithPath:(CPString)aPath
110 {
111  return [self initWithURL:aPath];
112 }
113 
114 - (Class)classNamed:(CPString)aString
115 {
116  // ???
117 }
118 
119 - (CPURL)bundleURL
120 {
121  return _bundle.bundleURL();
122 }
123 
124 - (CPString)bundlePath
125 {
126  return [[self bundleURL] path];
127 }
128 
129 - (CPString)resourcePath
130 {
131  return [[self resourceURL] path];
132 }
133 
134 - (CPURL)resourceURL
135 {
136  return _bundle.resourcesDirectoryURL();
137 }
138 
139 - (Class)principalClass
140 {
141  var className = [self objectForInfoDictionaryKey:@"CPPrincipalClass"];
142 
143  //[self load];
144 
145  return className ? CPClassFromString(className) : nil;
146 }
147 
148 - (CPString)bundleIdentifier
149 {
150  return _bundle.identifier();
151 }
152 
153 - (BOOL)isLoaded
154 {
155  return _bundle.isLoaded();
156 }
157 
158 - (CPString)pathForResource:(CPString)aFilename
159 {
160  return _bundle.pathForResource(aFilename);
161 }
162 
163 - (CPString)pathForResource:(CPString)aFilename ofType:(CPString)extension
164 {
165  return _bundle.pathForResource(aFilename, extension);
166 }
167 
168 - (CPString)pathForResource:(CPString)aFilename ofType:(CPString)extension inDirectory:(CPString)subpath
169 {
170  return _bundle.pathForResource(aFilename, extension, subpath);
171 }
172 
173 - (CPString)pathForResource:(CPString)aFilename ofType:(CPString)extension inDirectory:(CPString)subpath forLocalization:(CPString)localizationName
174 {
175  return _bundle.pathForResource(aFilename, extension, subpath, localizationName);
176 }
177 
178 - (CPDictionary)infoDictionary
179 {
180  return _bundle.infoDictionary();
181 }
182 
183 - (id)objectForInfoDictionaryKey:(CPString)aKey
184 {
185  return _bundle.valueForInfoDictionaryKey(aKey);
186 }
187 
188 - (void)loadWithDelegate:(id <CPBundleDelegate>)aDelegate
189 {
190  _delegate = aDelegate;
191 
192  _bundle.addEventListener("load", function()
193  {
194  [_delegate bundleDidFinishLoading:self];
195  // userInfo should contain a list of all classes loaded from this bundle. When writing this there
196  // seems to be no efficient way to get it though.
197  [[CPNotificationCenter defaultCenter] postNotificationName:CPBundleDidLoadNotification object:self userInfo:nil];
198  });
199 
200  _bundle.addEventListener("error", function()
201  {
202  CPLog.error("Could not find bundle: " + self);
203  });
204 
205  _bundle.load(YES);
206 }
207 
208 - (CPArray)staticResourceURLs
209 {
210  var staticResources = _bundle.staticResources();
211 
212  return [staticResources arrayByApplyingBlock:function(resource)
213  {
214  return resource.URL();
215  }];
216 }
217 
218 - (CPArray)environments
219 {
220  return _bundle.environments();
221 }
222 
223 - (CPString)mostEligibleEnvironment
224 {
225  return _bundle.mostEligibleEnvironment();
226 }
227 
229 {
230  return [super description] + "(" + [self bundlePath] + ")";
231 }
232 
233 
234 #pragma mark -
235 #pragma mark Localization
236 
237 - (CPArray)localizations
238 {
239  return CFBundleCopyBundleLocalizations(_bundle);
240 }
241 
242 - (CPString)localizedStringForKey:(CPString)aKey value:(CPString)aValue table:(CPString)aTable
243 {
244  return CFBundleCopyLocalizedString(_bundle, aKey, aValue, aTable);
245 }
246 
247 @end
248 
249 function CPLocalizedString(key, comment)
250 {
251  return CFCopyLocalizedString(key, comment);
252 }
253 
254 function CPLocalizedStringFromTable(key, table, comment)
255 {
256  return CFCopyLocalizedStringFromTable(key, table, comment);
257 }
258 
259 function CPCopyLocalizedStringFromTableInBundle(key, table, bundle, comment)
260 {
261  return CFCopyLocalizedStringFromTableInBundle(key, table, bundle._bundle, comment);
262 }
global CFBundleCopyBundleLocalizations global CFBundleCopyLocalizedString CPBundleDidLoadNotification
Definition: CPBundle.j:27
BOOL isLoaded()
Definition: CPBundle.j:153
function CPLocalizedStringFromTable(key, table, comment)
Definition: CPBundle.j:254
void postNotificationName:object:userInfo:(CPString aNotificationName, [object] id anObject, [userInfo] CPDictionary aUserInfo)
function CPLocalizedString(key, comment)
Definition: CPBundle.j:249
var CPBundlesForURLStrings
Groups information about an application&#39;s code & resources.
Definition: CPBundle.j:41
CPString bundlePath()
Definition: CPBundle.j:124
void load()
Definition: CPObject.j:109
CPURL resourceURL()
Definition: CPBundle.j:134
CPString description()
Definition: CPObject.j:358
CPNotificationCenter defaultCenter()
A mutable key-value pair collection.
Definition: CPDictionary.h:2
An immutable string (collection of characters).
Definition: CPString.h:2
CPString absoluteString()
Definition: CPURL.j:105
CPString path()
Definition: CPURL.j:116
CPBundle bundleWithURL:(CPURL aURL)
Definition: CPBundle.j:49
CPURL bundleURL()
Definition: CPBundle.j:119
CPArray environments()
Definition: CPBundle.j:218
CPString mostEligibleEnvironment()
Definition: CPBundle.j:223
id objectForInfoDictionaryKey:(CPString aKey)
Definition: CPBundle.j:183
id init()
Definition: CPObject.j:145
Sends messages (CPNotification) between objects.
CPBundle bundleWithPath:(CPString aPath)
Definition: CPBundle.j:54
id initWithURL:(CPURL aURL)
Definition: CPBundle.j:87
CPDictionary infoDictionary()
Definition: CPBundle.j:178
Definition: CPURL.h:2
function CPCopyLocalizedStringFromTableInBundle(key, table, bundle, comment)
Definition: CPBundle.j:259
function CPClassFromString(aClassName)
Definition: CPObjJRuntime.j:33
id alloc()
Definition: CPObject.j:130
FrameUpdater prototype description