Source for org.jfree.io.FileUtilities

   1: /* ========================================================================
   2:  * JCommon : a free general purpose class library for the Java(tm) platform
   3:  * ========================================================================
   4:  *
   5:  * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
   6:  * 
   7:  * Project Info:  http://www.jfree.org/jcommon/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  * 
  27:  * ------------------
  28:  * FileUtilities.java
  29:  * ------------------
  30:  * (C) Copyright 2000-2004, by Object Refinery Limited.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: FileUtilities.java,v 1.5 2005/10/18 13:16:02 mungady Exp $
  36:  *
  37:  * Changes (from 5-Nov-2001)
  38:  * -------------------------
  39:  * 05-Nov-2001 : Changed package to com.jrefinery.io.* (DG);
  40:  * 04-Mar-2002 : Renamed Files.java --> FileUtilities.java (DG);
  41:  * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  42:  *
  43:  */
  44: 
  45: package org.jfree.io;
  46: 
  47: import java.io.File;
  48: import java.util.StringTokenizer;
  49: 
  50: /**
  51:  * A class containing useful utility methods relating to files.
  52:  *
  53:  * @author David Gilbert
  54:  */
  55: public class FileUtilities {
  56: 
  57:     /**
  58:      * To prevent unnecessary instantiation.
  59:      */
  60:     private FileUtilities() {
  61:     }
  62: 
  63:     /**
  64:      * Returns a reference to a file with the specified name that is located
  65:      * somewhere on the classpath.  The code for this method is an adaptation
  66:      * of code supplied by Dave Postill.
  67:      *
  68:      * @param name  the filename.
  69:      *
  70:      * @return a reference to a file or <code>null</code> if no file could be found.
  71:      */
  72:     public static File findFileOnClassPath(final String name) {
  73: 
  74:         final String classpath = System.getProperty("java.class.path");
  75:         final String pathSeparator = System.getProperty("path.separator");
  76: 
  77:         final StringTokenizer tokenizer = new StringTokenizer(classpath, pathSeparator);
  78: 
  79:         while (tokenizer.hasMoreTokens()) {
  80:             final String pathElement = tokenizer.nextToken();
  81: 
  82:             final File directoryOrJar = new File(pathElement);
  83:             final File absoluteDirectoryOrJar = directoryOrJar.getAbsoluteFile();
  84: 
  85:             if (absoluteDirectoryOrJar.isFile()) {
  86:                 final File target = new File(absoluteDirectoryOrJar.getParent(), name);
  87:                 if (target.exists()) {
  88:                     return target;
  89:                 }
  90:             }
  91:             else {
  92:                 final File target = new File(directoryOrJar, name);
  93:                 if (target.exists()) {
  94:                     return target;
  95:                 }
  96:             }
  97: 
  98:         }
  99:         return null;
 100: 
 101:     }
 102: 
 103: }