|
WebObjects 5.2.3 | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.webobjects.foundation.NSBundle
An NSBundle represents a location in the file system that groups code and resources that can be used in a program. NSBundles locate program resources and assist in localization. You build a bundle in Xcode using a Java WebObjects Application or Java WebObjects Framework project.
NSBundles can be stored on disk in two ways.
The first way is a directory where related resources--including executable code--are stored. The directory, in essence, "bundles" a set of resources used by an application into convenient chunks, and the NSBundle object makes those resources available to the application. NSBundle can find requested resources in the directory. The term bundle refers both to the object and to the directory it represents.
The second way is a java JAR file (Java ARchive). Again, all of the classes and resources are encapsulated into the JAR file. In this case, the term bundle refers to the JAR file itself.
At this point, JAR files are the preferred storage mechanism for NSBundles. However using JAR files requires that java.net.URLs be used to reference resources inside bundles. Path APIs (which typically operate on Strings) cannot be used. All Path APIs in the base WebObjects frameworks should be deprecated.
Bundles are useful in a variety of contexts. Since bundles combine executable code with the resources used by that code, they facilitate installation and localization. NSBundles are also used to locate specific resources and to determine which classes are loaded.
Each resource in a bundle usually resides in its own file. Bundled resources include such things as:
".woa"
. To build an
application bundle with Xcode, use the Java WebObjects Application
project type.
Every application has a single application bundle called the "main bundle".
You obtain an NSBundle object corresponding to the main bundle with the
mainBundle
static method. This is typically the running application itself.
".framework"
extension. To build a framework bundle with Xcode,
use the Java WebObjects Framework project type.
You can get an NSBundle object associated with a framework by invoking the
static method bundleForName
specifying, as the argument, the name of the
framework without the ".framework"
extension. Alternatively you
can invoke the bundleForClass
method specifying a class that's defined in the
framework. To get all the framework bundles available to your
application, you can invoke the frameworkBundles
static method.
Resources specific to a particular language are grouped together in a
resource directory. This directory has the name of the language (in English)
followed by a ".lproj"
extension
(for "language project"). The application mentioned above,
for example, would have Japanese.lproj
, English.lproj
,
French.lproj
, German.lproj
, and Spanish.lproj
directories. The application also has a Nonlocalized.lproj
directory, which contains resources shared by all locales.
It is good programming practice to ensure that if a resource appears in
one language directory it also appears in all the others. Thus, Icon.gif
in French.lproj
should be the French counterpart to the
Spanish Icon.gif
in Spanish.lproj
, and so on.
However this discipline is not completely necessary. If German.lproj
does not contain an Icon.gif
resource, the Icon.gif
resource in
Nonlocalized.lproj
will be used instead.
The server's locale determines which set of localized resources will
actually be used by the application. NSBundle objects invoke the
java.util.Locale.getDefault
method to determine the locale and chooses
the localized resources accordingly.
Resources
within the bundle directory on the file system. Within the
Resources
directory are all of the language
directories except Nonlocalized.lproj
. The non-localized resources in the
Nonlocalized.lproj
directory are mapped into the top level of the
Resources
directory on the file system.
For example, suppose the NSBundle resources are organized as shown below in Xcode:
English.lproj Edit.wo Edit.html Edit.wod Edit.woo Nonlocalized.lproj Edit.wo Edit.html Edit.wod Edit.woo Images Icon.gif Background.jpeg Main.wo Main.html Main.wod Main.wooThen, these resources will appear on the file system as:
Resources Edit.wo Edit.html Edit.wod Edit.woo Images Icon.gif Background.jpeg Main.wo Main.html Main.wod Main.woo English.lproj Edit.wo Edit.html Edit.wod Edit.woo
NSBundle provides two methods to determine the resources it contains:
resourcePathsForResources
and resourcePathsForLocalizedResources
.
These methods return resource paths, or paths specified according
to NSBundle's resource organization, not the physical resource organization as it
appears on the file system. For example, the resource path to the Background.jpeg
resource in above example is Nonlocalized.lproj/Images/Background.jpeg
.
NSBundle provides two methods to access resources: bytesForResourcePath
and inputStreamForResourcePath
. Both methods require a single argument:
a full resource path as returned by the resourcePathsForResources
and
resourcePathsForLocalizedResources
methods.
The bytesForResourcePath
method returns a byte array containing data for the
resource specified by the path. The inputStreamForResourcePath
returns an
java.io.InputStream
for the resource specified by the path.
Sometimes you want to access a localized resource without specifying the
full resource path. For example, if you might want to get the Icon.gif
resource
appropriate for the current locale. To do this, you invoke
resourcePathForLocalizedResourceNamed
to determine the full resource path
for the localized resource and, in turn, invoke bytesForResourcePath
or
inputStreamForResourcePath
with the full path.
mainBundle()
,
bundleForName(String)
,
bundleForClass(Class)
,
frameworkBundles()
,
resourcePathsForResources(String, String)
,
resourcePathsForLocalizedResources(String, String)
,
resourcePathsForDirectories(String, String)
,
bytesForResourcePath(String)
,
inputStreamForResourcePath(String)
,
resourcePathForLocalizedResourceNamed(String, String)
Field Summary | |
static String |
BundleDidLoadNotification
Notification that is sent when bundle has been loaded. |
static String |
LoadedClassesNotification
Notification that is sent when classes of this NSBundle have been loaded. |
Constructor Summary | |
NSBundle()
|
Method Summary | |
static NSArray |
allBundles()
Deprecated. The only non-framework bundle that an application can access without using deprecated APIs is the main bundle. Use mainBundle instead. |
static NSArray |
allFrameworks()
Deprecated. Use frameworkBundles instead |
NSArray |
bundleClassNames()
Gets array containing the names of all the receiver's classes |
NSArray |
bundleClassPackageNames()
Gets an NSArray of the names of all the packages containing the receiver's classes. |
static NSBundle |
bundleForClass(Class aClass)
Gets the NSBundle that contains the class aClass . |
static NSBundle |
bundleForName(String aName)
Gets the NSBundle with the specified name aName . |
String |
bundlePath()
Deprecated. You should not need to know the file system path to the bundle directory. |
URL |
bundlePathURL()
Returns the URL to the NSBundle. |
static NSBundle |
bundleWithPath(String aPath)
Deprecated. To access a bundle that was loaded when the application started, use bundleForName or bundleForClass instead |
byte[] |
bytesForResourcePath(String aResourcePath)
Gets the byte array containing the data for the resource specified by aResourcePath . |
static NSArray |
frameworkBundles()
Gets an NSArray containing the bundles for all the frameworks included in the application. |
NSDictionary |
infoDictionary()
Deprecated. Do not use this method. |
InputStream |
inputStreamForResourcePath(String aResourcePath)
Gets an input stream containing the resource specified by aResourcePath . |
boolean |
isFramework()
Determines if this NSBundle object is a framework. |
boolean |
isJar()
Determines if this NSBundle object represents a JAR file. |
boolean |
load()
Deprecated. Don't use this method since "Dynamic loading" is no longer supported. The default ClassLoader does not allow true dynamic class loading, it loads only those classes in the classpath at launch time. |
static NSBundle |
mainBundle()
Gets the application's main bundle. |
String |
name()
Gets the name of this NSBundle object. |
String |
pathForResource(String aName,
String anExtension)
Deprecated. Use resourcePathForLocalizedResourceNamed instead. |
String |
pathForResource(String aName,
String anExtension,
String aSubDirPath)
Deprecated. Don't use this method. Use resourcePathForLocalizedResourceNamed instead. |
NSArray |
pathsForResources(String anExtension,
String aSubDirPath)
Deprecated. Don't use this method. Use resourcePathsForResources instead. |
URL |
pathURLForResourcePath(String aResourcePath)
Returns an URL to the resource specified by aResourcePath . |
Class |
principalClass()
Gets the NSBundle's principal class, which is responsible for ensuring that all classes in the framework are properly initialized. |
Properties |
properties()
Get the properties that are located in the Properties file in the Resources subdirectory
of the directory corresponding to this NSBundle. |
String |
resourcePath()
Deprecated. Don't use this method. Resources are now accessed using the bytesForResourcePath and inputStreamForResourcePath methods. |
String |
resourcePathForLocalizedResourceNamed(String aName,
String aSubDirPath)
Determines the resource path of the resource named aName based on the
current locale. |
NSArray |
resourcePathsForDirectories(String anExtension,
String aSubDirPath)
Gets an array containing the resource paths of all the directories with the specified extension anExtension beneath the specified subdirectory
aSubDirPath . |
NSArray |
resourcePathsForLocalizedResources(String anExtension,
String aSubDirPath)
Gets an array containing the resource paths for all of the receiver's resources that are appropriate for the current locale, have the specified file extension anExtension , and lie within the specified subdirectory
aSubDirPath . |
NSArray |
resourcePathsForResources(String anExtension,
String aSubDirPath)
Gets an array containing the resource paths of all of the receiver's resources that have the specified file extension anExtension and lie within the specified
subdirectory aSubDirPath . |
String |
toString()
The string returned includes the receiver's class name (NSBundle or a subclass), its name, its path, the names of its packages (as returned by bundleClassPackageNames ), and the number
of classes it contains. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final String BundleDidLoadNotification
public static final String LoadedClassesNotification
Constructor Detail |
public NSBundle()
Method Detail |
public static NSArray allBundles()
mainBundle
instead.
mainBundle()
public static NSArray allFrameworks()
frameworkBundles
instead
frameworkBundles()
public NSArray bundleClassNames()
public NSArray bundleClassPackageNames()
public static NSBundle bundleForClass(Class aClass)
aClass
.
aClass
- the class which belongs to the returned bundle
aClass
public static NSBundle bundleForName(String aName)
aName
.
See name()
for more information about how the name relates to the NSBundle's
file system organization. This should only be used to look up frameworks; the main bundle
should be resolved using NSBundle.mainBundle()
aName
- the name of the NSBundle
null
otherwisename()
public String bundlePath()
public URL bundlePathURL()
file:/path/to/bundle
public static NSBundle bundleWithPath(String aPath)
bundleForName
or bundleForClass
instead
aPath
- the input path identifying an accessible bundle
null
otherwisebundleForName(String)
,
bundleForClass(Class)
public byte[] bytesForResourcePath(String aResourcePath)
aResourcePath
.
The resource path must be specified relative to the top level of
the resources hierarchy, that is, the directory containing the language
subdirectories. It is usually returned from resourcePathsForResources
or resourcePathsForLocalizedResources
.
Note that the resource path for a resource is not the same as its file system path. See "Determining Available Resources" in the class description for more information about resource paths.
aResourcePath
- the path of the resource, according to the resources hierarchy
resourcePathsForResources(String, String)
,
resourcePathsForLocalizedResources(String, String)
public static NSArray frameworkBundles()
public NSDictionary infoDictionary()
public InputStream inputStreamForResourcePath(String aResourcePath)
aResourcePath
.
The resource path must be specified relative to the top level of
the resources hierarchy, that is, the directory containing the language
subdirectories. It is usually returned from resourcePathsForResources
or resourcePathsForLocalizedResources
.
Note that the resource path for a resource is not the same as its file system path. See "Determining Available Resources" in the class description for more information about resource paths.
aResourcePath
- the path of the resource, according to the resources hierarchy
resourcePathsForResources(String, String)
,
resourcePathsForLocalizedResources(String, String)
public boolean isFramework()
true
if this NSBundle object is a framework else false
public boolean isJar()
true
if this NSBundle is a JAR file else false
public boolean load()
true
if bundle loaded at application startup, false
otherwisepublic static NSBundle mainBundle()
".woa"
extension.
public String name()
".woa"
extension. If the bundle is a WebObjects
Framework, this method returns the name of the directory containing the
framework without the ".framework"
extension.
public String pathForResource(String aName, String anExtension)
resourcePathForLocalizedResourceNamed
instead.
aName
- anExtension
- resourcePathForLocalizedResourceNamed(String, String)
public String pathForResource(String aName, String anExtension, String aSubDirPath)
resourcePathForLocalizedResourceNamed
instead.
aName
- anExtension
- aSubDirPath
- resourcePathForLocalizedResourceNamed(String, String)
public URL pathURLForResourcePath(String aResourcePath)
aResourcePath
.
The resource path must be specified relative to the top level of
the resources hierarchy, that is, the directory containing the language
subdirectories. It is usually returned from resourcePathsForResources
or resourcePathsForLocalizedResources
.
Note that the resource path for a resource is not the same as its file system path. See "Determining Available Resources" in the class description for more information about resource paths.
aResourcePath
- the path of the resource, according to the resources hierarchy
resourcePathsForResources(String, String)
,
resourcePathsForLocalizedResources(String, String)
public NSArray pathsForResources(String anExtension, String aSubDirPath)
resourcePathsForResources
instead.
anExtension
- aSubDirPath
- resourcePathsForResources(String, String)
public Class principalClass()
NSPrincipalClass
. If the
principal class is not specified in the property list, the method returns
null
. If you create a framework that needs to be initialized
using a principal class, you must specify the class name in the
CustomInfo.plist
file, a source file for the bundle's property
list. For example, if your principal class is
myPackage.myPrincipalClass
, your CustomInfo.plist
file should look like:
{ NSPrincipalClass = myPackage.myPrincipalClass; }
public Properties properties()
Properties
file in the Resources
subdirectory
of the directory corresponding to this NSBundle.
Properties
filepublic String resourcePath()
bytesForResourcePath
and inputStreamForResourcePath
methods.
bytesForResourcePath(String)
,
inputStreamForResourcePath(String)
public String resourcePathForLocalizedResourceNamed(String aName, String aSubDirPath)
aName
based on the
current locale. This method first searches
the current locale's language directory for the resource then the
Nonlocalized.lproj
directory. If it finds the resource, it returns
the resource's path. Otherwise it returns null
. You can specify
a subdirectory aSubDirPath
for the method to search in.
For example, if the current locale
is English, and the resources are organized as shown in the class description
and you invoke "Edit.html"
resource in the "Edit.wo"
subdirectory, the method
returns "English.lproj/Edit.wo/Edit.html"
. If the current locale is German,
the method returns "Nonlocalized.lproj/Edit.wo/Edit.html"
.
aName
- the name of the resourceaSubDirPath
- the subdirectory where it is searched; if it is null
,
the method returns a resource path for a localized resource at the language directory
level.
resourcePathsForLocalizedResources(
String extension, String subdirectory)
,
resourcePathsForResources(String extension,
String subdirectory)
,
resourcePathsForDirectories(String, String)
public NSArray resourcePathsForDirectories(String anExtension, String aSubDirPath)
anExtension
beneath the specified subdirectory
aSubDirPath
.
If anExtension
is null
, the method includes directories
regardless of extension. If aSubDirPath
is null
,
the method returns directories beneath the top level directory (the one
containing the language directories).
Examples of invoking resourcePathsForDirectories
with various parameters
for the bundle depicted in the class description.
extension | subdirectory | Result |
---|---|---|
null |
null |
{"English.lproj/Edit.wo", "English.lproj/Images", "Nonlocalized.lproj/Edit.wo",
"Nonlocalized.lproj/Images", "Nonlocalized.lproj/Main.wo"} |
"wo" |
null |
{"Nonlocalized.lproj/Main.wo", "Nonlocalized.lproj/Edit.wo",
"English.lproj/Edit.wo"} |
null |
"English.lproj" |
{"English.lproj/Edit.wo", "English.lproj/Images"} |
"wo" |
"English.lproj" |
{"English.lproj/Edit.wo"} |
anExtension
- the extension of the resourceaSubDirPath
- the directory where it is searched
resourcePathsForResources(String, String)
,
resourcePathsForLocalizedResources(String, String)
public NSArray resourcePathsForLocalizedResources(String anExtension, String aSubDirPath)
anExtension
, and lie within the specified subdirectory
aSubDirPath
.
If a resource appears in more than one language directory, this method chooses whether to include it in the array based on the following criteria:
Nonlocalized.lproj
or the
current locale's language directory the method does not include its path in
the results array.
This method also takes the extension
and subdirectory
arguments that allow you to filter the result array based on the extension or
subdirectory. Examples of invoking this method with various parameters for the bundle
depicted in the class description.
extension | subdirectory | Result |
---|---|---|
null |
null |
{ "English.lproj/Edit.wo/Edit.html",
"English.lproj/Edit.wo/Edit.wod",
"English.lproj/Edit.wo/Edit.woo",
"Nonlocalized.lproj/Images/Icon.gif",
"Nonlocalized.lproj/Images/Background.jpeg",
"Nonlocalized.lproj/Main.wo/Main.html",
"Nonlocalized.lproj/Main.wo/Main.wod",
"Nonlocalized.lproj/Main.wo/Main.woo" } |
"html" |
null |
{ "English.lproj/Edit.wo/Edit.html",
"Nonlocalized.lproj/Main.wo/Main.html" } |
null |
"Edit.wo" |
{ "English.lproj/Edit.wo/Edit.html",
"English.lproj/Edit.wo/Edit.wod",
"English.lproj/Edit.wo/Edit.woo" } |
"html" |
"Edit.wo" |
{ "English.lproj/Edit.wo/Edit.html" } |
anExtension
- extension of the resourceaSubDirPath
- the directory where it is searched
resourcePathsForResources(String, String)
,
resourcePathsForDirectories(String, String)
public NSArray resourcePathsForResources(String anExtension, String aSubDirPath)
anExtension
and lie within the specified
subdirectory aSubDirPath
.
If anExtension
is null
, the method includes
resources regardless of extension. If aSubDirPath
is null
, the method returns resources
beneath the top level directory (the one containing the language
directories).
The following shows examples of invoking this method with various parameters for the bundle depicted in the class description.
extension | subdirectory | Result |
---|---|---|
null |
null |
{ "English.lproj/Edit.wo/Edit.html",
"English.lproj/Edit.wo/Edit.wod",
"English.lproj/Edit.wo/Edit.woo",
"English.lproj/Images/Icon.gif",
"Nonlocalized.lproj/Edit.wo/Edit.html",
"Nonlocalized.lproj/Edit.wo/Edit.wod",
"Nonlocalized.lproj/Edit.wo/Edit.woo",
Nonlocalized.lproj/Images/Icon.gif",
"Nonlocalized.lproj/Images/Background.jpeg",
"Nonlocalized.lproj/Main.wo/Main.html",
"Nonlocalized.lproj/Main.wo/Main.wod",
"Nonlocalized.lproj/Main.wo/Main.woo" } |
"gif" |
null |
{ "English.lproj/Images/Icon.gif",
"Nonlocalized.lproj/Images/Icon.gif" } |
null |
"English.lproj" |
{ "English.lproj/Edit.wo/Edit.html",
"English.lproj/Edit.wo/Edit.wod",
"English.lproj/Edit.wo/Edit.woo",
"English.lproj/Images/Icon.gif" } |
"gif" |
"English.lproj" |
{ "English.lproj/Images/Icon.gif" } |
anExtension
- extension of the resourceaSubDirPath
- the directory where it is searched
resourcePathsForDirectories(String, String)
,
resourcePathsForLocalizedResources(String, String)
public String toString()
bundleClassPackageNames
), and the number
of classes it contains.
bundleClassPackageNames()
,
name()
|
Last updated Thu Oct 21 15:04:16 PDT 2004. | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |