Source code: javax/ide/extension/spi/JARExtensionSource.java
1 package javax.ide.extension.spi;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.net.URI;
7 import javax.ide.extension.Extension;
8 import javax.ide.net.URIFactory;
9 import javax.ide.net.VirtualFileSystem;
10
11 /**
12 * A default implementation of ExtensionSource which loads an extension
13 * manifest from the META-INF/extension.xml entry of a JAR file.
14 */
15 public class JARExtensionSource implements ExtensionSource
16 {
17 private final static String MANIFEST_ENTRY = "META-INF/extension.xml";
18 private final static String MANIFEST_ALT_ENTRY = "meta-inf/extension.xml";
19 private final URI _jarURI;
20 private URI _manifestURI;
21
22 public JARExtensionSource( URI jarURI )
23 {
24 _jarURI = jarURI;
25 }
26
27
28
29 /**
30 * Get the URI of this JAR file.
31 *
32 * @return the URI of this jar file.
33 */
34 public final URI getURI()
35 {
36 return _jarURI;
37 }
38
39 public final URI getClasspathEntry()
40 {
41 return _jarURI;
42 }
43
44 /**
45 * Get the URI of the manifest file within the extension source.
46 *
47 * @return the URI of the manifest file.
48 */
49 public URI getManifestURI()
50 {
51 if ( _manifestURI == null )
52 {
53 _manifestURI = URIFactory.newJarURI( _jarURI, MANIFEST_ENTRY );
54 if ( !VirtualFileSystem.getVirtualFileSystem().exists( _manifestURI ) )
55 {
56 _manifestURI = URIFactory.newJarURI( _jarURI, MANIFEST_ALT_ENTRY );
57 }
58 }
59 return _manifestURI;
60 }
61
62 /**
63 * Resolve a relative path from the manifest file. For JAR sources, the
64 * path may either be within the JAR (if the path starts with a /),
65 * or relative to the location of the jar file otherwise.
66 *
67 * @param extension the extension being processed.
68 * @param path a relative path within the JAR.
69 * @return the absolute URI of the referenced resource.
70 */
71 public URI resolvePath( Extension extension, String path )
72 {
73 // First check if it's already an absolute file name.
74 File f = new File( path );
75 if ( f.exists() )
76 {
77 return URIFactory.newFileURI( f );
78 }
79
80 // First see whether it's resolvable within the jar file.
81 if ( path.length() >= 2 && path.charAt( 0 ) == '/' )
82 {
83 return URIFactory.newJarURI( _jarURI, path.substring( 1 ) );
84 }
85
86 URI parentOfJar =
87 VirtualFileSystem.getVirtualFileSystem().getParent( _jarURI );
88
89 return URIFactory.newURI( parentOfJar, path );
90 }
91
92 /**
93 * Get the name of this source. This will be used to present the source
94 * in human readable messages.<p>
95 *
96 * This implementation returns the result of calling
97 * VirtualFileSytem.toDisplayString() on the jar URI.
98 *
99 * @return the name of this source.
100 */
101 public String getName()
102 {
103 return VirtualFileSystem.getVirtualFileSystem().toDisplayString(
104 _jarURI
105 );
106 }
107
108
109 public InputStream getInputStream()
110 throws IOException
111 {
112 return VirtualFileSystem.getVirtualFileSystem().openInputStream(
113 getManifestURI() );
114 }
115
116 }