corba/src/share/classes/com/sun/tools/corba/se/idl/som/cff/FileLocator.java
author ohair
Tue, 25 May 2010 15:52:11 -0700
changeset 5555 b2b5ed3f0d0d
parent 4 02bb8761fcce
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     1
/*
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     2
 * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
02bb8761fcce Initial load
duke
parents:
diff changeset
     4
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
02bb8761fcce Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
4
02bb8761fcce Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    10
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
02bb8761fcce Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
02bb8761fcce Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
02bb8761fcce Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
02bb8761fcce Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
02bb8761fcce Initial load
duke
parents:
diff changeset
    16
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
02bb8761fcce Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
02bb8761fcce Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
02bb8761fcce Initial load
duke
parents:
diff changeset
    20
 *
5555
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b2b5ed3f0d0d 6943119: Rebrand source copyright notices
ohair
parents: 4
diff changeset
    23
 * questions.
4
02bb8761fcce Initial load
duke
parents:
diff changeset
    24
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    25
/*
02bb8761fcce Initial load
duke
parents:
diff changeset
    26
 * Licensed Materials - Property of IBM
02bb8761fcce Initial load
duke
parents:
diff changeset
    27
 * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997,1998
02bb8761fcce Initial load
duke
parents:
diff changeset
    28
 * RMI-IIOP v1.0
02bb8761fcce Initial load
duke
parents:
diff changeset
    29
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    30
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    31
02bb8761fcce Initial load
duke
parents:
diff changeset
    32
package com.sun.tools.corba.se.idl.som.cff;
02bb8761fcce Initial load
duke
parents:
diff changeset
    33
02bb8761fcce Initial load
duke
parents:
diff changeset
    34
import java.lang.Exception;
02bb8761fcce Initial load
duke
parents:
diff changeset
    35
import java.lang.String;
02bb8761fcce Initial load
duke
parents:
diff changeset
    36
import java.lang.System;
02bb8761fcce Initial load
duke
parents:
diff changeset
    37
import java.io.BufferedInputStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    38
import java.io.DataInputStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    39
import java.io.File;
02bb8761fcce Initial load
duke
parents:
diff changeset
    40
import java.io.FileInputStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    41
import java.io.FileNotFoundException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    42
import java.io.InputStream;
02bb8761fcce Initial load
duke
parents:
diff changeset
    43
import java.io.IOException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    44
import java.util.Locale;
02bb8761fcce Initial load
duke
parents:
diff changeset
    45
import java.util.NoSuchElementException;
02bb8761fcce Initial load
duke
parents:
diff changeset
    46
import java.util.Properties;
02bb8761fcce Initial load
duke
parents:
diff changeset
    47
import java.util.StringTokenizer;
02bb8761fcce Initial load
duke
parents:
diff changeset
    48
import java.util.zip.*;
02bb8761fcce Initial load
duke
parents:
diff changeset
    49
02bb8761fcce Initial load
duke
parents:
diff changeset
    50
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
    51
 * FileLocator is an abstract class (one that cannot be instantiated) that
02bb8761fcce Initial load
duke
parents:
diff changeset
    52
 * provides class methods for finding files in the directories or zip
02bb8761fcce Initial load
duke
parents:
diff changeset
    53
 * archives that make up the CLASSPATH.
02bb8761fcce Initial load
duke
parents:
diff changeset
    54
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
    55
 * @author      Larry K. Raper
02bb8761fcce Initial load
duke
parents:
diff changeset
    56
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
    57
public abstract class FileLocator extends Object {
02bb8761fcce Initial load
duke
parents:
diff changeset
    58
02bb8761fcce Initial load
duke
parents:
diff changeset
    59
    /* Class variables */
02bb8761fcce Initial load
duke
parents:
diff changeset
    60
02bb8761fcce Initial load
duke
parents:
diff changeset
    61
02bb8761fcce Initial load
duke
parents:
diff changeset
    62
    static final Properties pp = System.getProperties ();
02bb8761fcce Initial load
duke
parents:
diff changeset
    63
    static final String classPath = pp.getProperty ("java.class.path", ".");
02bb8761fcce Initial load
duke
parents:
diff changeset
    64
    static final String pathSeparator = pp.getProperty ("path.separator", ";");
02bb8761fcce Initial load
duke
parents:
diff changeset
    65
02bb8761fcce Initial load
duke
parents:
diff changeset
    66
    /* Instance variables */
02bb8761fcce Initial load
duke
parents:
diff changeset
    67
02bb8761fcce Initial load
duke
parents:
diff changeset
    68
    /* [None, no instances of this class are ever instantiated.] */
02bb8761fcce Initial load
duke
parents:
diff changeset
    69
02bb8761fcce Initial load
duke
parents:
diff changeset
    70
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
    71
     * locateClassFile returns a DataInputStream with mark/reset
02bb8761fcce Initial load
duke
parents:
diff changeset
    72
     * capability that can be used to read the requested class file.  The
02bb8761fcce Initial load
duke
parents:
diff changeset
    73
     * CLASSPATH is used to locate the class.
02bb8761fcce Initial load
duke
parents:
diff changeset
    74
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
    75
     * @param classFileName The name of the class to locate.  The class name
02bb8761fcce Initial load
duke
parents:
diff changeset
    76
     * should be given in fully-qualified form, for example:
02bb8761fcce Initial load
duke
parents:
diff changeset
    77
     * <pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
    78
     *     java.lang.Object
02bb8761fcce Initial load
duke
parents:
diff changeset
    79
     *     java.io.DataInputStream
02bb8761fcce Initial load
duke
parents:
diff changeset
    80
     * </pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
    81
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
    82
     * @exception java.io.FileNotFoundException The requested class file
02bb8761fcce Initial load
duke
parents:
diff changeset
    83
     * could not be found.
02bb8761fcce Initial load
duke
parents:
diff changeset
    84
     * @exception java.io.IOException The requested class file
02bb8761fcce Initial load
duke
parents:
diff changeset
    85
     * could not be opened.
02bb8761fcce Initial load
duke
parents:
diff changeset
    86
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
    87
    public static DataInputStream locateClassFile (String classFileName)
02bb8761fcce Initial load
duke
parents:
diff changeset
    88
        throws FileNotFoundException, IOException {
02bb8761fcce Initial load
duke
parents:
diff changeset
    89
02bb8761fcce Initial load
duke
parents:
diff changeset
    90
        boolean notFound = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
    91
        StringTokenizer st;
02bb8761fcce Initial load
duke
parents:
diff changeset
    92
        String path = "";
02bb8761fcce Initial load
duke
parents:
diff changeset
    93
        String pathNameForm;
02bb8761fcce Initial load
duke
parents:
diff changeset
    94
        File cf = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
    95
        NamedDataInputStream result;
02bb8761fcce Initial load
duke
parents:
diff changeset
    96
02bb8761fcce Initial load
duke
parents:
diff changeset
    97
        st = new StringTokenizer (classPath, pathSeparator, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
    98
        pathNameForm = classFileName.replace ('.', File.separatorChar) +
02bb8761fcce Initial load
duke
parents:
diff changeset
    99
            ".class";
02bb8761fcce Initial load
duke
parents:
diff changeset
   100
02bb8761fcce Initial load
duke
parents:
diff changeset
   101
        while (st.hasMoreTokens () && notFound) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   102
02bb8761fcce Initial load
duke
parents:
diff changeset
   103
            try {path = st.nextToken ();}
02bb8761fcce Initial load
duke
parents:
diff changeset
   104
                catch (NoSuchElementException nse) {break;}
02bb8761fcce Initial load
duke
parents:
diff changeset
   105
            int pLen = path.length ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   106
            String pathLast4 = pLen > 3 ? path.substring (pLen - 4) : "";
02bb8761fcce Initial load
duke
parents:
diff changeset
   107
            if (pathLast4.equalsIgnoreCase (".zip") ||
02bb8761fcce Initial load
duke
parents:
diff changeset
   108
                pathLast4.equalsIgnoreCase (".jar")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   109
02bb8761fcce Initial load
duke
parents:
diff changeset
   110
                try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   111
02bb8761fcce Initial load
duke
parents:
diff changeset
   112
                    result = locateInZipFile (path, classFileName, true, true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   113
                    if (result == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   114
                        continue;
02bb8761fcce Initial load
duke
parents:
diff changeset
   115
                    return (DataInputStream) result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   116
02bb8761fcce Initial load
duke
parents:
diff changeset
   117
                } catch (ZipException zfe) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   118
                    continue;
02bb8761fcce Initial load
duke
parents:
diff changeset
   119
                } catch (IOException ioe) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   120
                    continue;
02bb8761fcce Initial load
duke
parents:
diff changeset
   121
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   122
02bb8761fcce Initial load
duke
parents:
diff changeset
   123
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   124
                try {cf = new File (path + File.separator + pathNameForm);
02bb8761fcce Initial load
duke
parents:
diff changeset
   125
                } catch (NullPointerException npe) { continue; }
02bb8761fcce Initial load
duke
parents:
diff changeset
   126
                if ((cf != null) && cf.exists ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   127
                    notFound = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   128
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   129
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   130
02bb8761fcce Initial load
duke
parents:
diff changeset
   131
        if (notFound) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   132
02bb8761fcce Initial load
duke
parents:
diff changeset
   133
            /* Make one last attempt to find the file in the current
02bb8761fcce Initial load
duke
parents:
diff changeset
   134
             * directory
02bb8761fcce Initial load
duke
parents:
diff changeset
   135
             */
02bb8761fcce Initial load
duke
parents:
diff changeset
   136
02bb8761fcce Initial load
duke
parents:
diff changeset
   137
            int lastdot = classFileName.lastIndexOf ('.');
02bb8761fcce Initial load
duke
parents:
diff changeset
   138
            String simpleName =
02bb8761fcce Initial load
duke
parents:
diff changeset
   139
                (lastdot >= 0) ? classFileName.substring (lastdot+1) :
02bb8761fcce Initial load
duke
parents:
diff changeset
   140
                classFileName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   141
02bb8761fcce Initial load
duke
parents:
diff changeset
   142
            result = new NamedDataInputStream (new BufferedInputStream (
02bb8761fcce Initial load
duke
parents:
diff changeset
   143
               new FileInputStream (simpleName + ".class")),
02bb8761fcce Initial load
duke
parents:
diff changeset
   144
                   simpleName + ".class", false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   145
            return (DataInputStream) result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   146
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   147
02bb8761fcce Initial load
duke
parents:
diff changeset
   148
        result = new NamedDataInputStream (new BufferedInputStream (
02bb8761fcce Initial load
duke
parents:
diff changeset
   149
            new FileInputStream (cf)), path + File.separator + pathNameForm,
02bb8761fcce Initial load
duke
parents:
diff changeset
   150
                false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   151
        return (DataInputStream) result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   152
02bb8761fcce Initial load
duke
parents:
diff changeset
   153
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   154
02bb8761fcce Initial load
duke
parents:
diff changeset
   155
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   156
     * locateLocaleSpecificFileInClassPath returns a DataInputStream that
02bb8761fcce Initial load
duke
parents:
diff changeset
   157
     * can be used to read the requested file, but the name of the file is
02bb8761fcce Initial load
duke
parents:
diff changeset
   158
     * determined using information from the current locale and the supplied
02bb8761fcce Initial load
duke
parents:
diff changeset
   159
     * file name (which is treated as a "base" name, and is supplemented with
02bb8761fcce Initial load
duke
parents:
diff changeset
   160
     * country and language related suffixes, obtained from the current
02bb8761fcce Initial load
duke
parents:
diff changeset
   161
     * locale).  The CLASSPATH is used to locate the file.
02bb8761fcce Initial load
duke
parents:
diff changeset
   162
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   163
     * @param fileName The name of the file to locate.  The file name
02bb8761fcce Initial load
duke
parents:
diff changeset
   164
     * may be qualified with a partial path name, using '/' as the separator
02bb8761fcce Initial load
duke
parents:
diff changeset
   165
     * character or using separator characters appropriate for the host file
02bb8761fcce Initial load
duke
parents:
diff changeset
   166
     * system, in which case each directory or zip file in the CLASSPATH will
02bb8761fcce Initial load
duke
parents:
diff changeset
   167
     * be used as a base for finding the fully-qualified file.
02bb8761fcce Initial load
duke
parents:
diff changeset
   168
     * Here is an example of how the supplied fileName is used as a base
02bb8761fcce Initial load
duke
parents:
diff changeset
   169
     * for locating a locale-specific file:
02bb8761fcce Initial load
duke
parents:
diff changeset
   170
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   171
     * <pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   172
     *     Supplied fileName: a/b/c/x.y,  current locale: US English
02bb8761fcce Initial load
duke
parents:
diff changeset
   173
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   174
     *                     Look first for: a/b/c/x_en_US.y
02bb8761fcce Initial load
duke
parents:
diff changeset
   175
     *     (if that fails) Look next for:  a/b/c/x_en.y
02bb8761fcce Initial load
duke
parents:
diff changeset
   176
     *     (if that fails) Look last for:  a/b/c/x.y
02bb8761fcce Initial load
duke
parents:
diff changeset
   177
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   178
     *     All elements of the class path are searched for each name,
02bb8761fcce Initial load
duke
parents:
diff changeset
   179
     *     before the next possible name is tried.
02bb8761fcce Initial load
duke
parents:
diff changeset
   180
     * </pre>
02bb8761fcce Initial load
duke
parents:
diff changeset
   181
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   182
     * @exception java.io.FileNotFoundException The requested class file
02bb8761fcce Initial load
duke
parents:
diff changeset
   183
     * could not be found.
02bb8761fcce Initial load
duke
parents:
diff changeset
   184
     * @exception java.io.IOException The requested class file
02bb8761fcce Initial load
duke
parents:
diff changeset
   185
     * could not be opened.
02bb8761fcce Initial load
duke
parents:
diff changeset
   186
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   187
    public static DataInputStream locateLocaleSpecificFileInClassPath (
02bb8761fcce Initial load
duke
parents:
diff changeset
   188
        String fileName) throws FileNotFoundException, IOException {
02bb8761fcce Initial load
duke
parents:
diff changeset
   189
02bb8761fcce Initial load
duke
parents:
diff changeset
   190
        String localeSuffix = "_" + Locale.getDefault ().toString ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   191
        int lastSlash = fileName.lastIndexOf ('/');
02bb8761fcce Initial load
duke
parents:
diff changeset
   192
        int lastDot   = fileName.lastIndexOf ('.');
02bb8761fcce Initial load
duke
parents:
diff changeset
   193
        String fnFront, fnEnd;
02bb8761fcce Initial load
duke
parents:
diff changeset
   194
        DataInputStream result = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   195
        boolean lastAttempt = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   196
02bb8761fcce Initial load
duke
parents:
diff changeset
   197
        if ((lastDot > 0) && (lastDot > lastSlash)) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   198
            fnFront = fileName.substring (0, lastDot);
02bb8761fcce Initial load
duke
parents:
diff changeset
   199
            fnEnd   = fileName.substring (lastDot);
02bb8761fcce Initial load
duke
parents:
diff changeset
   200
        } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   201
            fnFront = fileName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   202
            fnEnd   = "";
02bb8761fcce Initial load
duke
parents:
diff changeset
   203
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   204
02bb8761fcce Initial load
duke
parents:
diff changeset
   205
        while (true) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   206
            if (lastAttempt)
02bb8761fcce Initial load
duke
parents:
diff changeset
   207
                result = locateFileInClassPath (fileName);
02bb8761fcce Initial load
duke
parents:
diff changeset
   208
            else try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   209
                result = locateFileInClassPath (fnFront + localeSuffix + fnEnd);
02bb8761fcce Initial load
duke
parents:
diff changeset
   210
            } catch (Exception e) { /* ignore */ }
02bb8761fcce Initial load
duke
parents:
diff changeset
   211
            if ((result != null) || lastAttempt)
02bb8761fcce Initial load
duke
parents:
diff changeset
   212
                break;
02bb8761fcce Initial load
duke
parents:
diff changeset
   213
            int lastUnderbar = localeSuffix.lastIndexOf ('_');
02bb8761fcce Initial load
duke
parents:
diff changeset
   214
            if (lastUnderbar > 0)
02bb8761fcce Initial load
duke
parents:
diff changeset
   215
                localeSuffix = localeSuffix.substring (0, lastUnderbar);
02bb8761fcce Initial load
duke
parents:
diff changeset
   216
            else
02bb8761fcce Initial load
duke
parents:
diff changeset
   217
                lastAttempt = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   218
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   219
        return result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   220
02bb8761fcce Initial load
duke
parents:
diff changeset
   221
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   222
02bb8761fcce Initial load
duke
parents:
diff changeset
   223
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   224
     * locateFileInClassPath returns a DataInputStream that can be used
02bb8761fcce Initial load
duke
parents:
diff changeset
   225
     * to read the requested file.  The CLASSPATH is used to locate the file.
02bb8761fcce Initial load
duke
parents:
diff changeset
   226
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   227
     * @param fileName The name of the file to locate.  The file name
02bb8761fcce Initial load
duke
parents:
diff changeset
   228
     * may be qualified with a partial path name, using '/' as the separator
02bb8761fcce Initial load
duke
parents:
diff changeset
   229
     * character or using separator characters appropriate for the host file
02bb8761fcce Initial load
duke
parents:
diff changeset
   230
     * system, in which case each directory or zip file in the CLASSPATH will
02bb8761fcce Initial load
duke
parents:
diff changeset
   231
     * be used as a base for finding the fully-qualified file.
02bb8761fcce Initial load
duke
parents:
diff changeset
   232
     *
02bb8761fcce Initial load
duke
parents:
diff changeset
   233
     * @exception java.io.FileNotFoundException The requested class file
02bb8761fcce Initial load
duke
parents:
diff changeset
   234
     * could not be found.
02bb8761fcce Initial load
duke
parents:
diff changeset
   235
     * @exception java.io.IOException The requested class file
02bb8761fcce Initial load
duke
parents:
diff changeset
   236
     * could not be opened.
02bb8761fcce Initial load
duke
parents:
diff changeset
   237
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   238
    public static DataInputStream locateFileInClassPath (String fileName)
02bb8761fcce Initial load
duke
parents:
diff changeset
   239
        throws FileNotFoundException, IOException {
02bb8761fcce Initial load
duke
parents:
diff changeset
   240
02bb8761fcce Initial load
duke
parents:
diff changeset
   241
        boolean notFound = true;
02bb8761fcce Initial load
duke
parents:
diff changeset
   242
        StringTokenizer st;
02bb8761fcce Initial load
duke
parents:
diff changeset
   243
        String path = "";
02bb8761fcce Initial load
duke
parents:
diff changeset
   244
        File cf = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   245
        NamedDataInputStream result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   246
02bb8761fcce Initial load
duke
parents:
diff changeset
   247
        String zipEntryName = File.separatorChar == '/' ? fileName :
02bb8761fcce Initial load
duke
parents:
diff changeset
   248
            fileName.replace (File.separatorChar, '/');
02bb8761fcce Initial load
duke
parents:
diff changeset
   249
02bb8761fcce Initial load
duke
parents:
diff changeset
   250
        String localFileName = File.separatorChar == '/' ? fileName :
02bb8761fcce Initial load
duke
parents:
diff changeset
   251
            fileName.replace ('/', File.separatorChar);
02bb8761fcce Initial load
duke
parents:
diff changeset
   252
02bb8761fcce Initial load
duke
parents:
diff changeset
   253
        st = new StringTokenizer (classPath, pathSeparator, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   254
02bb8761fcce Initial load
duke
parents:
diff changeset
   255
        while (st.hasMoreTokens () && notFound) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   256
02bb8761fcce Initial load
duke
parents:
diff changeset
   257
            try {path = st.nextToken ();}
02bb8761fcce Initial load
duke
parents:
diff changeset
   258
                catch (NoSuchElementException nse) {break;}
02bb8761fcce Initial load
duke
parents:
diff changeset
   259
            int pLen = path.length ();
02bb8761fcce Initial load
duke
parents:
diff changeset
   260
            String pathLast4 = pLen > 3 ? path.substring (pLen - 4) : "";
02bb8761fcce Initial load
duke
parents:
diff changeset
   261
            if (pathLast4.equalsIgnoreCase (".zip") ||
02bb8761fcce Initial load
duke
parents:
diff changeset
   262
                pathLast4.equalsIgnoreCase (".jar")) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   263
02bb8761fcce Initial load
duke
parents:
diff changeset
   264
                try {
02bb8761fcce Initial load
duke
parents:
diff changeset
   265
02bb8761fcce Initial load
duke
parents:
diff changeset
   266
                    result = locateInZipFile (path, zipEntryName, false, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   267
                    if (result == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   268
                        continue;
02bb8761fcce Initial load
duke
parents:
diff changeset
   269
                    return (DataInputStream) result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   270
02bb8761fcce Initial load
duke
parents:
diff changeset
   271
                } catch (ZipException zfe) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   272
                    continue;
02bb8761fcce Initial load
duke
parents:
diff changeset
   273
                } catch (IOException ioe) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   274
                    continue;
02bb8761fcce Initial load
duke
parents:
diff changeset
   275
                }
02bb8761fcce Initial load
duke
parents:
diff changeset
   276
02bb8761fcce Initial load
duke
parents:
diff changeset
   277
            } else {
02bb8761fcce Initial load
duke
parents:
diff changeset
   278
                try {cf = new File (path + File.separator + localFileName);
02bb8761fcce Initial load
duke
parents:
diff changeset
   279
                } catch (NullPointerException npe) { continue; }
02bb8761fcce Initial load
duke
parents:
diff changeset
   280
                if ((cf != null) && cf.exists ())
02bb8761fcce Initial load
duke
parents:
diff changeset
   281
                    notFound = false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   282
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   283
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   284
02bb8761fcce Initial load
duke
parents:
diff changeset
   285
        if (notFound) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   286
02bb8761fcce Initial load
duke
parents:
diff changeset
   287
            /* Make one last attempt to find the file in the current
02bb8761fcce Initial load
duke
parents:
diff changeset
   288
             * directory
02bb8761fcce Initial load
duke
parents:
diff changeset
   289
             */
02bb8761fcce Initial load
duke
parents:
diff changeset
   290
02bb8761fcce Initial load
duke
parents:
diff changeset
   291
            int lastpart = localFileName.lastIndexOf (File.separator);
02bb8761fcce Initial load
duke
parents:
diff changeset
   292
            String simpleName =
02bb8761fcce Initial load
duke
parents:
diff changeset
   293
                (lastpart >= 0) ? localFileName.substring (lastpart+1) :
02bb8761fcce Initial load
duke
parents:
diff changeset
   294
                localFileName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   295
02bb8761fcce Initial load
duke
parents:
diff changeset
   296
            result = new NamedDataInputStream (new BufferedInputStream (
02bb8761fcce Initial load
duke
parents:
diff changeset
   297
               new FileInputStream (simpleName)), simpleName, false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   298
            return (DataInputStream) result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   299
        }
02bb8761fcce Initial load
duke
parents:
diff changeset
   300
02bb8761fcce Initial load
duke
parents:
diff changeset
   301
        result = new NamedDataInputStream (new BufferedInputStream (
02bb8761fcce Initial load
duke
parents:
diff changeset
   302
            new FileInputStream (cf)), path + File.separator + localFileName,
02bb8761fcce Initial load
duke
parents:
diff changeset
   303
                false);
02bb8761fcce Initial load
duke
parents:
diff changeset
   304
        return (DataInputStream) result;
02bb8761fcce Initial load
duke
parents:
diff changeset
   305
02bb8761fcce Initial load
duke
parents:
diff changeset
   306
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   307
02bb8761fcce Initial load
duke
parents:
diff changeset
   308
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   309
     * Returns the fully qualified file name associated with the passed
02bb8761fcce Initial load
duke
parents:
diff changeset
   310
     * DataInputStream <i>if the DataInputStream was created using one
02bb8761fcce Initial load
duke
parents:
diff changeset
   311
     * of the static locate methods supplied with this class</i>, otherwise
02bb8761fcce Initial load
duke
parents:
diff changeset
   312
     * returns a zero length string.
02bb8761fcce Initial load
duke
parents:
diff changeset
   313
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   314
    public static String getFileNameFromStream (DataInputStream ds) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   315
02bb8761fcce Initial load
duke
parents:
diff changeset
   316
        if (ds instanceof NamedDataInputStream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   317
            return ((NamedDataInputStream) ds).fullyQualifiedFileName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   318
        return "";
02bb8761fcce Initial load
duke
parents:
diff changeset
   319
02bb8761fcce Initial load
duke
parents:
diff changeset
   320
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   321
02bb8761fcce Initial load
duke
parents:
diff changeset
   322
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   323
     * Returns an indication of whether the passed DataInputStream is
02bb8761fcce Initial load
duke
parents:
diff changeset
   324
     * associated with a member of a zip file <i>if the DataInputStream was
02bb8761fcce Initial load
duke
parents:
diff changeset
   325
     * created using one of the static locate methods supplied with this
02bb8761fcce Initial load
duke
parents:
diff changeset
   326
     * class</i>, otherwise returns false.
02bb8761fcce Initial load
duke
parents:
diff changeset
   327
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   328
    public static boolean isZipFileAssociatedWithStream (DataInputStream ds) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   329
02bb8761fcce Initial load
duke
parents:
diff changeset
   330
        if (ds instanceof NamedDataInputStream)
02bb8761fcce Initial load
duke
parents:
diff changeset
   331
            return ((NamedDataInputStream) ds).inZipFile;
02bb8761fcce Initial load
duke
parents:
diff changeset
   332
        return false;
02bb8761fcce Initial load
duke
parents:
diff changeset
   333
02bb8761fcce Initial load
duke
parents:
diff changeset
   334
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   335
02bb8761fcce Initial load
duke
parents:
diff changeset
   336
    private static NamedDataInputStream locateInZipFile (String zipFileName,
02bb8761fcce Initial load
duke
parents:
diff changeset
   337
        String fileName, boolean wantClass, boolean buffered)
02bb8761fcce Initial load
duke
parents:
diff changeset
   338
        throws ZipException, IOException {
02bb8761fcce Initial load
duke
parents:
diff changeset
   339
02bb8761fcce Initial load
duke
parents:
diff changeset
   340
        ZipFile zf;
02bb8761fcce Initial load
duke
parents:
diff changeset
   341
        ZipEntry ze;
02bb8761fcce Initial load
duke
parents:
diff changeset
   342
        zf = new ZipFile (zipFileName);
02bb8761fcce Initial load
duke
parents:
diff changeset
   343
02bb8761fcce Initial load
duke
parents:
diff changeset
   344
        if (zf == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   345
            return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   346
        String zeName = wantClass ?
02bb8761fcce Initial load
duke
parents:
diff changeset
   347
            fileName.replace ('.', '/') + ".class" :
02bb8761fcce Initial load
duke
parents:
diff changeset
   348
            fileName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   349
02bb8761fcce Initial load
duke
parents:
diff changeset
   350
        //  This code works with JDK 1.0 level SUN zip classes
02bb8761fcce Initial load
duke
parents:
diff changeset
   351
        //
02bb8761fcce Initial load
duke
parents:
diff changeset
   352
02bb8761fcce Initial load
duke
parents:
diff changeset
   353
        //  ze = zf.get (zeName);
02bb8761fcce Initial load
duke
parents:
diff changeset
   354
        //  if (ze == null)
02bb8761fcce Initial load
duke
parents:
diff changeset
   355
        //      return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   356
        //  return new NamedDataInputStream (
02bb8761fcce Initial load
duke
parents:
diff changeset
   357
        //      new BufferedInputStream (new ZipInputStream (ze)),
02bb8761fcce Initial load
duke
parents:
diff changeset
   358
        //          zipFileName + '(' +zeName + ')', true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   359
02bb8761fcce Initial load
duke
parents:
diff changeset
   360
        //  This code works with JDK 1.0.2 and JDK 1.1 level SUN zip classes
02bb8761fcce Initial load
duke
parents:
diff changeset
   361
        //
02bb8761fcce Initial load
duke
parents:
diff changeset
   362
02bb8761fcce Initial load
duke
parents:
diff changeset
   363
            ze = zf.getEntry (zeName);
02bb8761fcce Initial load
duke
parents:
diff changeset
   364
            if (ze == null) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   365
                zf.close(); // D55355, D56419
02bb8761fcce Initial load
duke
parents:
diff changeset
   366
                zf = null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   367
                return null;
02bb8761fcce Initial load
duke
parents:
diff changeset
   368
            }
02bb8761fcce Initial load
duke
parents:
diff changeset
   369
            InputStream istream = zf.getInputStream(ze);
02bb8761fcce Initial load
duke
parents:
diff changeset
   370
            if (buffered)
02bb8761fcce Initial load
duke
parents:
diff changeset
   371
                istream = new BufferedInputStream(istream);
02bb8761fcce Initial load
duke
parents:
diff changeset
   372
            return new NamedDataInputStream (istream,
02bb8761fcce Initial load
duke
parents:
diff changeset
   373
                    zipFileName + '(' + zeName + ')', true);
02bb8761fcce Initial load
duke
parents:
diff changeset
   374
02bb8761fcce Initial load
duke
parents:
diff changeset
   375
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   376
02bb8761fcce Initial load
duke
parents:
diff changeset
   377
}
02bb8761fcce Initial load
duke
parents:
diff changeset
   378
02bb8761fcce Initial load
duke
parents:
diff changeset
   379
/**
02bb8761fcce Initial load
duke
parents:
diff changeset
   380
 * This class is used to associate a filename with a DataInputStream
02bb8761fcce Initial load
duke
parents:
diff changeset
   381
 * The host platform's file naming conventions are assumed for the filename.
02bb8761fcce Initial load
duke
parents:
diff changeset
   382
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
   383
 * @author      Larry K. Raper
02bb8761fcce Initial load
duke
parents:
diff changeset
   384
 *
02bb8761fcce Initial load
duke
parents:
diff changeset
   385
 */
02bb8761fcce Initial load
duke
parents:
diff changeset
   386
/* default access */ class NamedDataInputStream extends DataInputStream {
02bb8761fcce Initial load
duke
parents:
diff changeset
   387
02bb8761fcce Initial load
duke
parents:
diff changeset
   388
    /* Instance variables */
02bb8761fcce Initial load
duke
parents:
diff changeset
   389
02bb8761fcce Initial load
duke
parents:
diff changeset
   390
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   391
     * The name of the file associated with the DataInputStream.
02bb8761fcce Initial load
duke
parents:
diff changeset
   392
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   393
    public String fullyQualifiedFileName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   394
02bb8761fcce Initial load
duke
parents:
diff changeset
   395
    /**
02bb8761fcce Initial load
duke
parents:
diff changeset
   396
     * Indicates whether or not the file is contained in a .zip file.
02bb8761fcce Initial load
duke
parents:
diff changeset
   397
     */
02bb8761fcce Initial load
duke
parents:
diff changeset
   398
    public boolean inZipFile;
02bb8761fcce Initial load
duke
parents:
diff changeset
   399
02bb8761fcce Initial load
duke
parents:
diff changeset
   400
    /* Constructors */
02bb8761fcce Initial load
duke
parents:
diff changeset
   401
02bb8761fcce Initial load
duke
parents:
diff changeset
   402
    protected NamedDataInputStream (InputStream in, String fullyQualifiedName,
02bb8761fcce Initial load
duke
parents:
diff changeset
   403
        boolean inZipFile) {
02bb8761fcce Initial load
duke
parents:
diff changeset
   404
02bb8761fcce Initial load
duke
parents:
diff changeset
   405
        super (in);
02bb8761fcce Initial load
duke
parents:
diff changeset
   406
        this.fullyQualifiedFileName = fullyQualifiedName;
02bb8761fcce Initial load
duke
parents:
diff changeset
   407
        this.inZipFile = inZipFile;
02bb8761fcce Initial load
duke
parents:
diff changeset
   408
02bb8761fcce Initial load
duke
parents:
diff changeset
   409
    }
02bb8761fcce Initial load
duke
parents:
diff changeset
   410
02bb8761fcce Initial load
duke
parents:
diff changeset
   411
}