Path: blob/main/src/lwjgl/java/paulscode/sound/FilenameURL.java
8644 views
package paulscode.sound;12import java.net.URL;34/**5* The FilenameURL class is designed to associate a String filename/identifier6* with a URL. Handles either case where user supplies a String path or user7* supplies a URL instance.8*<br><br>9*<b><i> SoundSystem License:</b></i><br><b><br>10* You are free to use this library for any purpose, commercial or otherwise.11* You may modify this library or source code, and distribute it any way you12* like, provided the following conditions are met:13*<br>14* 1) You may not falsely claim to be the author of this library or any15* unmodified portion of it.16*<br>17* 2) You may not copyright this library or a modified version of it and then18* sue me for copyright infringement.19*<br>20* 3) If you modify the source code, you must clearly document the changes21* made before redistributing the modified source code, so other users know22* it is not the original code.23*<br>24* 4) You are not required to give me credit for this library in any derived25* work, but if you do, you must also mention my website:26* http://www.paulscode.com27*<br>28* 5) I the author will not be responsible for any damages (physical,29* financial, or otherwise) caused by the use if this library or any part30* of it.31*<br>32* 6) I the author do not guarantee, warrant, or make any representations,33* either expressed or implied, regarding the use of this library or any34* part of it.35* <br><br>36* Author: Paul Lamb37* <br>38* http://www.paulscode.com39* </b>40*/41public class FilenameURL42{43/**44* Processes status messages, warnings, and error messages.45*/46private SoundSystemLogger logger;4748/**49* Filename or identifier for the file.50*/51private String filename = null;5253/**54* URL interface to the file.55*/56private URL url = null;5758/**59* Constructor: Saves handles to the url and identifier. The identifier should60* look like a filename, and it must have the correct extension so SoundSystem61* knows what format to use for the file referenced by the URL instance.62* @param url URL interface to a file.63* @param identifier Identifier (filename) for the file.64*/65public FilenameURL( URL url, String identifier )66{67// grab a handle to the message logger:68logger = SoundSystemConfig.getLogger();6970filename = identifier;71this.url = url;72}7374/**75* Constructor: Saves a handle to the filename (used later to generate a URL76* instance). The file may either be located within the77* JAR or at an online location. If the file is online, filename must begin78* with "http://", since that is how SoundSystem recognizes URL names.79* @param filename Name of the file.80*/81public FilenameURL( String filename )82{83// grab a handle to the message logger:84logger = SoundSystemConfig.getLogger();8586this.filename = filename;87url = null;88}8990/**91* Returns the filename/identifier.92* @return Filename or identifier for the file.93*/94public String getFilename()95{96return filename;97}9899/**100* Returns the URL interface to the file. If a URL was not originally specified101* in the constructor, then the first time this method is called it creates a102* URL instance using the previously specified filename.103* @return URL interface to the file.104*/105public URL getURL()106{107if( url == null )108{109// Check if the file is online or inside the JAR:110if( filename.matches( SoundSystemConfig.PREFIX_URL ) )111{112// Online113try114{115url = new URL( filename );116}117catch( Exception e )118{119errorMessage( "Unable to access online URL in " +120"method 'getURL'" );121printStackTrace( e );122return null;123}124}125else126{127// Inside the JAR128url = getClass().getClassLoader().getResource(129SoundSystemConfig.getSoundFilesPackage() + filename );130}131}132return url;133}134135/**136* Prints an error message.137* @param message Message to print.138*/139private void errorMessage( String message )140{141logger.errorMessage( "MidiChannel", message, 0 );142}143144/**145* Prints an exception's error message followed by the stack trace.146* @param e Exception containing the information to print.147*/148private void printStackTrace( Exception e )149{150logger.printStackTrace( e, 1 );151}152}153154155