1 package org.simantics.scl.compiler.source.repository;
4 import java.io.IOException;
5 import java.net.JarURLConnection;
7 import java.net.URLConnection;
8 import java.util.ArrayList;
9 import java.util.Collection;
10 import java.util.Enumeration;
11 import java.util.jar.JarEntry;
12 import java.util.jar.JarFile;
14 import org.junit.Test;
15 import org.simantics.scl.runtime.SCLContext;
17 public class ModuleBrowsingUtil {
19 public static final String SCL_EXTENSION = ".scl";
20 public static final int SCL_EXTENSION_LENGTH = SCL_EXTENSION.length();
22 public static void collectModuleSourcesAt(Collection<String> result, URL url) throws IOException {
23 String protocol = url.getProtocol();
24 if("jar".equals(protocol)) {
25 URLConnection connection = url.openConnection();
26 if(!(connection instanceof JarURLConnection))
27 throw new UnsupportedOperationException("Expected JarURLConnection when opening connection to " + url + ".");
28 JarURLConnection jarConnection = (JarURLConnection)connection;
29 collectModuleSourcesAt(result, jarConnection.getEntryName(), jarConnection.getJarFile());
31 else if("file".equals(protocol)) {
32 File file = new File(url.getPath());
34 throw new UnsupportedOperationException("File " + url + " not found.");
35 collectModuleSourcesAt(result, file);
37 else if("bundleresource".equals(protocol)) {
38 URLConnection connection = url.openConnection();
39 Class<?> connectionClass = connection.getClass();
40 // We don't want an explicit dependency to osgi, therefore using reflection
41 if(!connectionClass.getSimpleName().equals("BundleURLConnection")) {
42 throw new UnsupportedOperationException("Expected BundleURLConnection when opening connection to " + url + ".");
46 localURL = (URL) connectionClass.getMethod("getLocalURL").invoke(connection);
47 } catch (Exception e) {
48 throw new UnsupportedOperationException("Couldn't browse " + url + ".", e);
50 collectModuleSourcesAt(result, localURL);
53 throw new UnsupportedOperationException("Protocol of " + url + " does not support directory browsing.");
56 public static void collectModuleSourcesAt(Collection<String> result, File file) {
57 for(File child : file.listFiles()) {
58 String name = child.getName();
59 if(child.isDirectory())
60 collectModuleSourcesAt(result, name + "/", child);
62 if(name.endsWith(SCL_EXTENSION))
63 result.add(name.substring(0, name.length()-SCL_EXTENSION_LENGTH));
67 private static void collectModuleSourcesAt(Collection<String> result, String prefix, File file) {
68 for(File child : file.listFiles()) {
69 String name = child.getName();
70 if(child.isDirectory())
71 collectModuleSourcesAt(result, prefix + name + "/", child);
73 if(name.endsWith(SCL_EXTENSION))
74 result.add(prefix + name.substring(0, name.length()-SCL_EXTENSION_LENGTH));
78 private static void collectModuleSourcesAt(Collection<String> result, String entryName, JarFile jarFile) throws IOException {
79 int entryNameLength = entryName.length();
81 for(Enumeration<JarEntry> e = jarFile.entries();e.hasMoreElements()
82 && (entry = e.nextElement()) != null;) {
83 String name = entry.getName();
84 if(name.startsWith(entryName) && name.endsWith(SCL_EXTENSION))
85 result.add(name.substring(entryNameLength+1, name.length()-SCL_EXTENSION_LENGTH));
90 public void osgiTest() throws IOException {
91 ArrayList<String> l = new ArrayList<String>();
92 collectModuleSourcesAt(l, SCLContext.class.getResource("prelude"));
93 System.out.println(l);
96 public static File toFile(URL url) {
97 String protocol = url.getProtocol();
98 if("file".equals(protocol)) {
99 File file = new File(url.getPath());