summaryrefslogtreecommitdiff
path: root/src/compiler/scala/tools/util/ClassPath.java
blob: 8df26355c8e77507810c35f37ae8c25e42182f2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*     ____ ____  ____ ____  ______                                     *\
**    / __// __ \/ __// __ \/ ____/    SOcos COmpiles Scala             **
**  __\_ \/ /_/ / /__/ /_/ /\_ \       (c) 2002, LAMP/EPFL              **
** /_____/\____/\___/\____/____/                                        **
\*                                                                      */

// $Id$

package scala.tools.util;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Set;

/** This class represents a Java/Scala class path. */
public class ClassPath {

    //########################################################################
    // Public Constants

    /** The system-dependent path-separator character */
    public static final String PATH_SEPARATOR =
        System.getProperty("path.separator", ":");

    /** The location of the scala library classes */
    public static final String SCALA_LIBRARY_CLASSPATH =
        System.getProperty("scala.library.class.path", "");

    /** The location of the scala library sources */
    public static final String SCALA_LIBRARY_SOURCEPATH =
        System.getProperty("scala.library.source.path", "");

    /** The current VM's boot class path */
    public static final String RUNTIME_BOOTCLASSPATH =
        System.getProperty("sun.boot.class.path", "");

    /** The current VM's extension directory path */
    public static final String RUNTIME_EXTDIRS =
        System.getProperty("java.ext.dirs", "");

    /** The implicit boot class path */
    public static final String IMPLICIT_BOOTCLASSPATH =
        concat(new String[]{
            SCALA_LIBRARY_CLASSPATH,
            SCALA_LIBRARY_SOURCEPATH,
            RUNTIME_BOOTCLASSPATH});

    /** The default class path */
    public static final String DEFAULT_CLASSPATH =
        System.getProperty("scala.class.path", ".");

    /** The default source path */
    public static final String DEFAULT_SOURCEPATH =
        System.getProperty("scala.source.path", "");

    /** The default boot class path */
    public static final String DEFAULT_BOOTCLASSPATH =
        System.getProperty("scala.boot.class.path", IMPLICIT_BOOTCLASSPATH);

    /** The default extension directory path */
    public static final String DEFAULT_EXTDIRS =
        System.getProperty("scala.ext.dirs", RUNTIME_EXTDIRS);

    //########################################################################
    // Public Functions

    /**
     * Adds all zip and jar archives found in the specified extension
     * directory path to the specified file set. See also remark about
     * file order in method "addFilesFromPath".
     */
    public static void addArchivesInExtDirPath(Set/*<File>*/files,String path){
        Set extdirs = new LinkedHashSet();
        addFilesInPath(extdirs, path);
        for (Iterator i = extdirs.iterator(); i.hasNext(); )
            addArchivesInExtDir(files, (File)i.next());
    }

    /**
     * Adds all zip and jar archives found in the specified extension
     * directory to the specified file set. See also remark about file
     * order in method "addFilesFromPath".
     */
    public static void addArchivesInExtDir(Set/*<File>*/ files, File extdir) {
        String[] names = extdir.list();
        if (names == null) return;
        for (int i = 0; i < names.length; i++) {
            if (names[i].endsWith(".jar") || names[i].endsWith(".zip")) {
                File archive = new File(extdir, names[i]);
                if (archive.isFile()) files.add(archive);
            }
        }
    }

    /**
     * Parses the specified path and adds all files that exist to the
     * specified file set. If order needs to be preserved, one should
     * pass in an order preserving implementation of Set.
     */
    public static void addFilesInPath(Set/*<File>*/ files, String path) {
        path += PATH_SEPARATOR;
        for (int i = 0; i < path.length(); ) {
            int j = path.indexOf(PATH_SEPARATOR, i);
            File file = new File(path.substring(i, j));
            if (file.exists()) files.add(file);
            i = j + 1;
        }
    }

    //########################################################################
    // Private Functions

    /** Returns the concatenation of the two paths. */
    private static String concat(String path1, String path2) {
        if (path1.length() == 0) return path2;
        if (path2.length() == 0) return path1;
        return path1 + PATH_SEPARATOR + path2;
    }

    /** Returns the concatenation of the array of paths. */
    private static String concat(String[] paths) {
        String path = "";
        for (int i = 0; i < paths.length; i++) path = concat(path, paths[i]);
        return path;
    }

    //########################################################################
    // Private Fields

    /** The abstract directory represented by this class path */
    private final AbstractFile root;

    //########################################################################
    // Public Constructors

    /** Initializes this instance with default paths. */
    public ClassPath() {
        this(DEFAULT_CLASSPATH);
    }

    /**
     * Initializes this instance with the specified class path and
     * default source, boot class and extension directory paths.
     */
    public ClassPath(String classpath) {
        this(classpath, DEFAULT_SOURCEPATH, DEFAULT_BOOTCLASSPATH,
            DEFAULT_EXTDIRS);
    }

    /** Initializes this instance with the specified paths. */
    public ClassPath(String classpath, String sourcepath, String bootclasspath,
        String extdirs)
    {
        // replace first empty path in bootclasspath by IMPLICIT_BOOTCLASSPATH
        if (!bootclasspath.equals(IMPLICIT_BOOTCLASSPATH)) {
            String path = PATH_SEPARATOR + bootclasspath + PATH_SEPARATOR;
            int index = path.indexOf(PATH_SEPARATOR + PATH_SEPARATOR);
            if (index >= 0)
                bootclasspath =
                    path.substring(1, index + 1) + IMPLICIT_BOOTCLASSPATH +
                    path.substring(index + 1, path.length() - 1);
        }
        Set files = new LinkedHashSet();
        addFilesInPath(files, bootclasspath);
        addArchivesInExtDirPath(files, extdirs);
        addFilesInPath(files, classpath);
        addFilesInPath(files, sourcepath);
        ArrayList dirs = new ArrayList(files.size());
        for (Iterator i = files.iterator(); i.hasNext(); ) {
            AbstractFile dir = AbstractFile.getDirectory((File)i.next());
            if (dir != null) dirs.add(dir);
        }
        Object[] array = dirs.toArray(new AbstractFile[dirs.size()]);
        this.root = DirectoryPath.fromArray("<root>", (AbstractFile[])array);
    }

    //########################################################################
    // Public Methods

    /** Returns the root of this class path. */
    public AbstractFile getRoot() {
        return root;
    }

    /** Returns a string representation of this class path. */
    public String toString() {
        return root.toString();
    }

    //########################################################################
}