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 / FolderMusicLibrary.java < prev    next >
Encoding:
Java Source  |  2000-09-28  |  6.2 KB  |  174 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        FolderMusicLibrary.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 com.apple.jens.mp3.MP3File;
  49. import java.io.*;
  50. import java.util.Vector;
  51.  
  52.  
  53. /** A MusicLibrary that makes available all the MP3 files in a directory tree.
  54.     Directories whose names begin with "(" and "." are not scanned, however,
  55.     which provides an easy way to "comment out" portions of the tree. (This
  56.     behavior can be customized by overriding <code>filterDir</code>, and
  57.     control over whether individual files are added can be achieved by
  58.     overriding <code>filterFile</code>.)
  59.     
  60.     <p> The modification dates of all directories in the tree are cached,
  61.     and checked when the <code>syncTrackList</code> method is called; if any
  62.     directories have changed, the whole tree is re-read from scratch.
  63.     
  64. */
  65.     
  66. public class FolderMusicLibrary implements MusicLibrary {
  67.  
  68.     public synchronized void initialize( Station station ) {
  69.         RadioProperties props = station.getProperties();
  70.         File dir = props.getFileProperty(kPropLibraryHome);
  71.         if( dir==null )
  72.             throw new IllegalArgumentException("No "+kPropLibraryHome+" property is set!");
  73.         if( ! dir.exists() || ! dir.isDirectory() )
  74.             throw new IllegalArgumentException(dir+" is not a directory");
  75.         fRootDir = dir;
  76.         Radio.LOG(1,this,"initialized on "+dir);
  77.         buildTrackList();
  78.     }
  79.     
  80.  
  81.     public synchronized boolean syncTrackList( ) {
  82.         // Rebuild the file list if the directory's been modified:
  83.         if( hasBeenModified() ) {
  84.             buildTrackList();
  85.             return true;
  86.         } else
  87.             return false;
  88.     }
  89.     
  90.     
  91.     private boolean hasBeenModified( ) {
  92.         int n = fDirs.size();
  93.         for( int i=0; i<n; i++ ) {
  94.             File f = (File)fDirs.elementAt(i);
  95.             Long mod = (Long)fModDates.elementAt(i);
  96.             if( f.lastModified() != mod.longValue() )
  97.                 return true;
  98.         }
  99.         return false;
  100.     }
  101.     
  102.     
  103.     private void buildTrackList( ) {
  104.         fDirs.setSize(0);
  105.         fModDates.setSize(0);
  106.         Vector files = new Vector(100);
  107.         fDirs.addElement(fRootDir);
  108.         for( int i=0; i<fDirs.size(); i++ ) {
  109.             addDir( (File)fDirs.elementAt(i), files );
  110.         }
  111.         int nFiles = files.size();
  112.         fFiles = new MP3File[nFiles];
  113.         files.copyInto(fFiles);
  114.     }
  115.     
  116.     
  117.     private void addDir( File dir, Vector files ) {
  118.         fModDates.addElement( new Long( dir.lastModified() ) );
  119.         String[] list = dir.list();
  120.         for( int i=0; i<list.length; i++ ) {
  121.             File f = new File(dir,list[i]);
  122.             if( f.isDirectory() ) {
  123.                 if( filterDir(f) )
  124.                     fDirs.addElement(f);                // Push onto directory list
  125.             } else {
  126.                 MP3File f3 = new MP3File(f);
  127.                 if( filterFile(f3) )
  128.                     files.addElement(f3);
  129.                 //System.out.println("FolderMusicDirector: added track "+f.getPath());
  130.             }
  131.         }
  132.     }
  133.     
  134.     
  135.     /** Determines whether a directory should be searched.
  136.         By default it accepts all directories whose names don't start with "." or "(".
  137.         You can override this to do fancier filtering.
  138.         @return  True to accept the directory, false to skip it. */
  139.     protected boolean filterDir( File dir ) {
  140.         char first = dir.getName().charAt(0);
  141.         return first!='.' && first!='(';
  142.         //! In Java2 should check isHidden, too
  143.     }
  144.     
  145.     
  146.     /** Determines whether a file should be added to the library.
  147.         This implementation just checks whether the filename ends with ".mp3",
  148.         which is crude but fast.
  149.         You can override this to filter files based on other criteria,
  150.         possibly including the contents of ID3 tags -- but call the inherited
  151.         method first so you don't try to parse non-MP3 files.
  152.         @return  True to accept the file, false to skip it. */
  153.     protected boolean filterFile( MP3File f ) {
  154.         return f.isMP3FileType();
  155.     }
  156.     
  157.     
  158.     public synchronized MP3File[] getTrackList( ) {
  159.         return fFiles;
  160.     }
  161.     
  162.     
  163.     public String toString( ) {
  164.         return getClass().getName()+" on "+fRootDir;
  165.     }
  166.     
  167.     
  168.     protected File fRootDir;
  169.     protected Vector /*[File]*/ fDirs = new Vector();
  170.     protected Vector /*[Long]*/ fModDates = new Vector();
  171.     protected MP3File[] fFiles;
  172.     
  173. }
  174.