home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / What's New? / Sample Code / Java / JavaRadioStation / Source / com / apple / jens / radio / RadioProperties.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  5.5 KB  |  155 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        RadioProperties.java
  3.     
  4.     Copyright:     © Copyright 1999-2000 Apple Computer, Inc. All rights reserved.
  5.     
  6.     Disclaimer:    IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc.
  7.                 ("Apple") in consideration of your agreement to the following terms, and your
  8.                 use, installation, modification or redistribution of this Apple software
  9.                 constitutes acceptance of these terms.  If you do not agree with these terms,
  10.                 please do not use, install, modify or redistribute this Apple software.
  11.  
  12.                 In consideration of your agreement to abide by the following terms, and subject
  13.                 to these terms, Apple grants you a personal, non-exclusive license, under Apple’s
  14.                 copyrights in this original Apple software (the "Apple Software"), to use,
  15.                 reproduce, modify and redistribute the Apple Software, with or without
  16.                 modifications, in source and/or binary forms; provided that if you redistribute
  17.                 the Apple Software in its entirety and without modifications, you must retain
  18.                 this notice and the following text and disclaimers in all such redistributions of
  19.                 the Apple Software.  Neither the name, trademarks, service marks or logos of
  20.                 Apple Computer, Inc. may be used to endorse or promote products derived from the
  21.                 Apple Software without specific prior written permission from Apple.  Except as
  22.                 expressly stated in this notice, no other rights or licenses, express or implied,
  23.                 are granted by Apple herein, including but not limited to any patent rights that
  24.                 may be infringed by your derivative works or by other works in which the Apple
  25.                 Software may be incorporated.
  26.  
  27.                 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO
  28.                 WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
  29.                 WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30.                 PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
  31.                 COMBINATION WITH YOUR PRODUCTS.
  32.  
  33.                 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
  34.                 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  35.                 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36.                 ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
  37.                 OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
  38.                 (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
  39.                 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  40.                 
  41.     Change History (most recent first):
  42.  
  43. */
  44.  
  45.  
  46. package com.apple.jens.radio;
  47.  
  48. import java.io.*;
  49. import java.util.Properties;
  50.  
  51.  
  52. /** A convenience subclass of Properties that makes it easy to read properties
  53.     from a file, and to get property values as integers or classnames.
  54.     
  55. */
  56.     
  57. public class RadioProperties extends Properties {
  58.  
  59.     /** Default properties. Set when the app initializes. */
  60.     public static RadioProperties sDefaults;
  61.     
  62.  
  63.     /** Reads properties from a file. */
  64.     public RadioProperties( File f, Properties parent ) throws IOException {
  65.         super(parent);
  66.         if( f.exists() ) {
  67.             FileInputStream in = new FileInputStream(f);
  68.             try{
  69.                 load(in);
  70.             }finally{
  71.                 in.close();
  72.             }
  73.         }
  74.     }
  75.     
  76.     
  77.     /** My version of getProperty trims leading/trailing whitespace from the value. */
  78.     public String getProperty( String name ) {
  79.         String value = super.getProperty(name);
  80.         if( value != null )
  81.             value = value.trim();
  82.         return value;
  83.     }
  84.     
  85.     
  86.     /** Looks up an integer-valued property.
  87.         If the property is not defined, the default value is returned.
  88.         If the property value is not a valid number, a warning is generated
  89.         and the default value returned. */
  90.     public int getIntProperty( String name, int deflt ) {
  91.         String str = getProperty(name);
  92.         if( str == null )
  93.             return deflt;
  94.         else {
  95.             try{
  96.                 return Integer.parseInt(str);
  97.             }catch( NumberFormatException x ) {
  98.                 WARN_VALUE(name,str,"is not a valid number -- will use default value "+deflt);
  99.                 return deflt;
  100.             }
  101.         }
  102.     }
  103.     
  104.     
  105.     /** Looks up a classname-valued property, returning a new instance of that class.
  106.         If the property is not defined, null is returned.
  107.         If the class does not exist or cannot be instantiated, a warning is generated
  108.         and null returned. */
  109.     public Object getObjectProperty( String name ) {
  110.         return propertyValueToObject( name, getProperty(name) );
  111.     }
  112.     
  113.     
  114.     /** Instantiates an object, given the classname read from a property.
  115.         If the instantiation fails, a warning is printed and null is returned. */
  116.     public Object propertyValueToObject( String propName, String className ) {
  117.         if( className != null && className.indexOf('.') < 0 )
  118.             className = "com.apple.jens.radio." + className;        // Default package name
  119.             
  120.         try{
  121.             return Class.forName(className).newInstance();
  122.         }catch( Throwable t ) {
  123.             WARN_VALUE(propName,className,"which could not be instantiated:");
  124.             if(Radio.sLogLevel>=1 )
  125.                 t.printStackTrace(System.err);
  126.             return null;
  127.         }
  128.     }
  129.     
  130.     
  131.     /** Looks up a filename-valued property.
  132.         If the property is not defined, null is returned.
  133.         If the file does not exist, a warning is generated
  134.         and null returned. */
  135.     public File getFileProperty( String name ) {
  136.         String value = getProperty(name);
  137.         if( value == null )
  138.             return null;
  139.         File f = new File(value);
  140.         if( ! f.exists() ) {
  141.             WARN_VALUE(name,value,"is not an existing file/directory");
  142.             return null;
  143.         }
  144.         return f;
  145.     }
  146.     
  147.     
  148.     private void WARN_VALUE( String name, String value, String message ) {
  149.         Radio.WARN(1,"RadioProperties","Property '"+name
  150.                                     +"'s value is '"+value
  151.                                     +"' which "+message);
  152.     }
  153.     
  154. }
  155.