KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > spi > java > classpath > support > ClassPathSupport


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.spi.java.classpath.support;
20
21 import org.netbeans.spi.java.classpath.PathResourceImplementation;
22 import org.netbeans.spi.java.classpath.ClassPathImplementation;
23 import org.netbeans.spi.java.classpath.ClassPathFactory;
24 import org.netbeans.modules.java.classpath.*;
25 import org.netbeans.api.java.classpath.ClassPath;
26 import org.openide.ErrorManager;
27 import org.openide.filesystems.FileObject;
28
29 import java.net.URL JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import org.openide.filesystems.FileStateInvalidException;
33 import org.openide.filesystems.FileUtil;
34
35 /**
36  * Convenience factory for creating classpaths of common sorts.
37  * @since org.netbeans.api.java/1 1.4
38  */

39 public class ClassPathSupport {
40
41     private ClassPathSupport () {
42     }
43
44
45     /** Creates leaf PathResourceImplementation.
46      * The created PathResourceImplementation has exactly one immutable root.
47      * @param url the root of the resource. The URL must refer to folder. In the case of archive file
48      * the jar protocol URL must be used.
49      * @return PathResourceImplementation
50      */

51     public static PathResourceImplementation createResource (URL JavaDoc url) {
52         if (url == null) {
53             throw new NullPointerException JavaDoc("Cannot pass null URL to ClassPathSupport.createResource"); // NOI18N
54
}
55         // FU.iAF is a bit slow, so don't call it except when assertions are on:
56
boolean assertions = false;
57         assert assertions = true;
58         if (assertions && FileUtil.isArchiveFile(url)) {
59             throw new IllegalArgumentException JavaDoc("File URL pointing to " + // NOI18N
60
"JAR is not valid classpath entry. Use jar: URL. Was: "+url); // NOI18N
61
}
62         if (!url.toExternalForm().endsWith("/")) { // NOI18N
63
throw new IllegalArgumentException JavaDoc("URL must be a folder URL (append '/' if necessary): " + url); // NOI18N
64
}
65         return new SimplePathResourceImplementation (url);
66     }
67
68
69     /**
70      * Create ClassPathImplementation for the given list of
71      * {@link PathResourceImplementation} entries.
72      * @param entries list of {@link PathResourceImplementation} instances;
73      * cannot be null; can be empty
74      * @return SPI classpath
75      */

76     public static ClassPathImplementation createClassPathImplementation(List JavaDoc< ? extends PathResourceImplementation> entries) {
77         if (entries == null) {
78             throw new NullPointerException JavaDoc("Cannot pass null entries"); // NOI18N
79
}
80         return new SimpleClassPathImplementation(entries);
81     }
82
83
84     /**
85      * Create ClassPath for the given list of
86      * {@link PathResourceImplementation} entries.
87      * @param entries list of {@link PathResourceImplementation} instances;
88      * cannot be null; can be empty
89      * @return API classpath
90      */

91     public static ClassPath createClassPath(List JavaDoc<? extends PathResourceImplementation> entries) {
92         if (entries == null) {
93             throw new NullPointerException JavaDoc("Cannot pass null entries"); // NOI18N
94
}
95         return ClassPathFactory.createClassPath(createClassPathImplementation(entries));
96     }
97
98
99     /**
100      * Create ClassPath for the given array of class path roots
101      * @param roots array of fileobjects which must correspond to directory.
102      * In the case of archive file, the FileObject representing the root of the
103      * archive must be used. Cannot be null; can be empty array; array can contain nulls.
104      * @return API classpath
105      */

106     public static ClassPath createClassPath (FileObject[] roots) {
107         assert roots != null;
108         List JavaDoc<PathResourceImplementation> l = new ArrayList JavaDoc<PathResourceImplementation> ();
109         for (FileObject root : roots) {
110             if (root == null) {
111                 continue;
112             }
113             try {
114                 URL JavaDoc u = root.getURL();
115                 l.add(createResource(u));
116             } catch (FileStateInvalidException e) {
117                 ErrorManager.getDefault().notify (e);
118             }
119         }
120         return createClassPath (l);
121     }
122
123
124     /**
125      * Create ClassPath for the given array of class path roots
126      * @param roots array of URLs which must correspond to directory.
127      * In the case of archive file, the jar protocol URL must be used.
128      * Cannot be null; can be empty array; array can contain nulls.
129      * @return API classpath
130      */

131     public static ClassPath createClassPath (URL JavaDoc[] roots) {
132         assert roots != null;
133         List JavaDoc<PathResourceImplementation> l = new ArrayList JavaDoc<PathResourceImplementation> ();
134         for (URL JavaDoc root : roots) {
135             if (root == null)
136                 continue;
137             l.add (createResource(root));
138         }
139         return createClassPath(l);
140     }
141
142
143     /**
144      * Creates read only proxy ClassPathImplementation for given delegates.
145      * The order of resources is given by the order of the delegates
146      * @param delegates ClassPathImplementations to delegate to.
147      * @return SPI classpath
148      */

149     public static ClassPathImplementation createProxyClassPathImplementation (ClassPathImplementation[] delegates) {
150         return new ProxyClassPathImplementation (delegates);
151     }
152
153
154     /**
155      * Creates read only proxy ClassPath for given delegates.
156      * The order of resources is given by the order of the delegates
157      * @param delegates ClassPaths to delegate to.
158      * @return API classpath
159      */

160     public static ClassPath createProxyClassPath (ClassPath[] delegates) {
161         assert delegates != null;
162         ClassPathImplementation[] impls = new ClassPathImplementation [delegates.length];
163         for (int i = 0; i < delegates.length; i++) {
164              impls[i] = ClassPathAccessor.DEFAULT.getClassPathImpl (delegates[i]);
165         }
166         return ClassPathFactory.createClassPath (createProxyClassPathImplementation(impls));
167     }
168
169 }
170
Popular Tags