wyvern.lib
Class Resource

java.lang.Object
  extended bywyvern.lib.Resource

public final class Resource
extends java.lang.Object

A Resource is an object that encapsulates a path to a Wyvern file resource, such as a map, archetype, code, artwork, or music file. Using this class can save you a lot of code dedicated to manipulating file paths and permissions.

A Resource object keeps track of many attributes of a resource, including its absolute path, relative path, file extension, version number, and (for Wizard resources) the name of the owning Wizard.

Resources are obtained using the Factory methods getRelative() and getAbsolute(). Any two Resources referring to the same file can be compared for equality using the "==" operator, even if one was obtained using getRelative() and the other using getAbsolute().

All the methods on this class are thread-safe.

Version:
1.0, Jul 19, 2003
Author:
Steve Yegge

Field Summary
static Resource NULL
           
 
Method Summary
 boolean exists()
          Returns true if the file actually exists on disk.
static Resource getAbsolute(java.lang.String abspath)
          Returns a Resource encapsulating the requested file.
 java.lang.String getAbsoluteDir()
          Returns the directory containing this Resource.
 java.lang.String getAbsolutePath()
          Returns the absolute path to this Resource.
 java.lang.String getDotExtension()
          Returns the file extension for the file encapsulated by this resource.
 java.lang.String getExtension()
          Returns the file extension for the file encapsulated by this Resource.
 java.io.File getFile()
          Returns a java.io.File object for this resource.
 java.lang.String getFilename()
          Returns the filename, without the path.
 java.lang.String getFilenameNoExt()
          Returns the filename, without its path or extension.
static Resource getRelative(java.lang.String relpath)
          Returns a Resource encapsulating the requested file.
 java.lang.String getRelativeDir()
          Returns the directory containing this Resource.
 java.lang.String getRelativePath()
          Returns the relative path to this Resource.
 java.lang.String getRelativePathNoExt()
          Returns the relative path to this resource, sans extension.
static java.util.List getSupportedExtensions()
          Returns a List of the supported extensions, e.g. ".map", ".arch", etc.
 java.lang.String getWizard()
          Returns the Wizard's name (lowercase) who created this resource.
 boolean isBuiltin()
          Returns true if this Resource is a built-in game resource.
 boolean isWizResource()
          Returns true if this Resource is in a Wizard directory.
static void main(java.lang.String[] args)
          Tests out the initialization functions.
 java.lang.String toString()
          Returns a debugging representation of this Resource.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

NULL

public static final Resource NULL
Method Detail

getRelative

public static Resource getRelative(java.lang.String relpath)
Returns a Resource encapsulating the requested file.

Parameters:
relpath - the relative path to the file, such as "wiz/foobar/arch/mything.arch", or, as an example of a built-in game resource, "monsters/goblin/goblin.S.gif". The relative path needs to include the file extension.
Returns:
a Resource object encapsulating the file. You can query it for the file's absolute path, relative wyvern path, whether it's a wiz path, who the owning Wizard is, and other attributes.
Throws:
java.lang.IllegalArgumentException - if the Resource can't be determined from the path. This only happens for built-in game resources where the extension isn't in the list returned from getSupportedExtensions().

getAbsolute

public static Resource getAbsolute(java.lang.String abspath)
Returns a Resource encapsulating the requested file.

Parameters:
abspath - the absolute path to the file, such as "/games/wyvern/wiz/foobar/arch/mything.arch", or, as an example of a built-in game resource, "/games/wyvern/art/game/monsters/goblin/goblin.S.gif". The path needs to include the file extension.
Returns:
a Resource object encapsulating the file. You can query it for the file's absolute path, relative wyvern path, whether it's a wiz path, who the owning Wizard is, and other attributes.

getAbsolutePath

public java.lang.String getAbsolutePath()
Returns the absolute path to this Resource.

Returns:
the absolute path, including file extension

getRelativePath

public java.lang.String getRelativePath()
Returns the relative path to this Resource.

Returns:
the relative path, including file extension

getRelativePathNoExt

public java.lang.String getRelativePathNoExt()
Returns the relative path to this resource, sans extension.

Returns:
the relative path, without the file extension

isWizResource

public boolean isWizResource()
Returns true if this Resource is in a Wizard directory.

Returns:
true if the relative path starts with "wiz/".

isBuiltin

public boolean isBuiltin()
Returns true if this Resource is a built-in game resource.

Returns:
true if it's not a wiz resource (same as !isWizResource())

getWizard

public java.lang.String getWizard()
Returns the Wizard's name (lowercase) who created this resource.

Returns:
the name of the wizard who created the resource, or null if isWizResource() is false for this Resource.

getExtension

public java.lang.String getExtension()
Returns the file extension for the file encapsulated by this Resource.

Returns:
the Resource file's extension, e.g. "arch"

getDotExtension

public java.lang.String getDotExtension()
Returns the file extension for the file encapsulated by this resource.

Returns:
the dotted extension, e.g. ".arch"

toString

public java.lang.String toString()
Returns a debugging representation of this Resource.


getFile

public java.io.File getFile()
Returns a java.io.File object for this resource.


exists

public boolean exists()
Returns true if the file actually exists on disk.

Returns:
true if the file exists for this Resource.

getFilename

public java.lang.String getFilename()
Returns the filename, without the path.

Returns:
everything after the last "/" in the path for this Resource, including the extension.

getFilenameNoExt

public java.lang.String getFilenameNoExt()
Returns the filename, without its path or extension.

Returns:
everything after the last "/" in the path for this Resource, up to (but not including) the last "."

getAbsoluteDir

public java.lang.String getAbsoluteDir()
Returns the directory containing this Resource.

Returns:
the absolute path of the directory containing this file; i.e., everything up to the last "/". Includes the trailing slash.

getRelativeDir

public java.lang.String getRelativeDir()
Returns the directory containing this Resource.

Returns:
the relative path of the directory containing this file; i.e., everything up to the last "/". Includes the trailing slash.

getSupportedExtensions

public static java.util.List getSupportedExtensions()
Returns a List of the supported extensions, e.g. ".map", ".arch", etc.

Returns:
a List of the extensions for which you can ask for Resource objects. If the extension you want isn't in this list, then you'll have to do the path manipulation the old-fashioned way, with String operations.

main

public static void main(java.lang.String[] args)
Tests out the initialization functions.