    001    /* ========================================================================
    002     * JCommon : a free general purpose class library for the Java(tm) platform
    003     * ========================================================================
    004     *
    005     * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
    006     *
    007     * Project Info:  http://www.jfree.org/jcommon/index.html
    008     *
    009     * This library is free software; you can redistribute it and/or modify it
    010     * under the terms of the GNU Lesser General Public License as published by
    011     * the Free Software Foundation; either version 2.1 of the License, or
    012     * (at your option) any later version.
    013     *
    014     * This library is distributed in the hope that it will be useful, but
    015     * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    016     * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
    017     * License for more details.
    018     *
    019     * You should have received a copy of the GNU Lesser General Public
    020     * License along with this library; if not, write to the Free Software
    021     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
    022     * USA.
    023     *
    024     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
    025     * in the United States and other countries.]
    026     *
    027     * ------------------
    028     * FileUtilities.java
    029     * ------------------
    030     * (C) Copyright 2000-2004, by Object Refinery Limited.
    031     *
    032     * Original Author:  David Gilbert (for Object Refinery Limited);
    033     * Contributor(s):   -;
    034     *
    035     * $Id: FileUtilities.java,v 1.5 2005/10/18 13:16:02 mungady Exp $
    036     *
    037     * Changes (from 5-Nov-2001)
    038     * -------------------------
    039     * 05-Nov-2001 : Changed package to com.jrefinery.io.* (DG);
    040     * 04-Mar-2002 : Renamed Files.java --> FileUtilities.java (DG);
    041     * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
    042     *
    043     */
    044
    045    package org.jfree.io;
    046
    047    import java.io.File;
    048    import java.util.StringTokenizer;
    049
    050    /**
    051     * A class containing useful utility methods relating to files.
    052     *
    053     * @author David Gilbert
    054     */
    055    public class FileUtilities {
    056
    057        /**
    058         * To prevent unnecessary instantiation.
    059         */
    060        private FileUtilities() {
    061        }
    062
    063        /**
    064         * Returns a reference to a file with the specified name that is located
    065         * somewhere on the classpath.  The code for this method is an adaptation
    066         * of code supplied by Dave Postill.
    067         *
    068         * @param name  the filename.
    069         *
    070         * @return a reference to a file or <code>null</code> if no file could be found.
    071         */
    072        public static File findFileOnClassPath(final String name) {
    073
    074            final String classpath = System.getProperty("java.class.path");
    075            final String pathSeparator = System.getProperty("path.separator");
    076
    077            final StringTokenizer tokenizer = new StringTokenizer(classpath, pathSeparator);
    078
    079            while (tokenizer.hasMoreTokens()) {
    080                final String pathElement = tokenizer.nextToken();
    081
    082                final File directoryOrJar = new File(pathElement);
    083                final File absoluteDirectoryOrJar = directoryOrJar.getAbsoluteFile();
    084
    085                if (absoluteDirectoryOrJar.isFile()) {
    086                    final File target = new File(absoluteDirectoryOrJar.getParent(), name);
    087                    if (target.exists()) {
    088                        return target;
    089                    }
    090                }
    091                else {
    092                    final File target = new File(directoryOrJar, name);
    093                    if (target.exists()) {
    094                        return target;
    095                    }
    096                }
    097
    098            }
    099            return null;
    100
    101        }
    102
    103    }