rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java
branchjdk8
changeset 1679 93f4fbc4d1b7
parent 1678 35daab73e225
child 1681 2082d4c4bf11
     1.1 --- a/rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java	Sat Sep 13 13:44:01 2014 +0200
     1.2 +++ b/rt/aot/src/main/java/org/apidesign/bck2brwsr/aot/Bck2BrwsrJars.java	Sat Sep 13 14:19:43 2014 +0200
     1.3 @@ -19,6 +19,7 @@
     1.4  
     1.5  import java.io.BufferedReader;
     1.6  import java.io.File;
     1.7 +import java.io.FileInputStream;
     1.8  import java.io.IOException;
     1.9  import java.io.InputStream;
    1.10  import java.io.InputStreamReader;
    1.11 @@ -45,7 +46,7 @@
    1.12   */
    1.13  public final class Bck2BrwsrJars {
    1.14      private static final Logger LOG = Logger.getLogger(Bck2BrwsrJars.class.getName());
    1.15 -    
    1.16 +
    1.17      private Bck2BrwsrJars() {
    1.18      }
    1.19      
    1.20 @@ -66,6 +67,9 @@
    1.21       * @throws IOException if something goes wrong
    1.22       */
    1.23      public static Bck2Brwsr configureFrom(Bck2Brwsr c, File jar) throws IOException {
    1.24 +        if (jar.isDirectory()) {
    1.25 +            return configureDir(c, jar);
    1.26 +        }
    1.27          final JarFile jf = new JarFile(jar);
    1.28          final List<String> classes = new ArrayList<>();
    1.29          List<String> resources = new ArrayList<>();
    1.30 @@ -228,5 +232,55 @@
    1.31          }
    1.32      }
    1.33      
    1.34 +    private static Bck2Brwsr configureDir(Bck2Brwsr c, final File dir) throws IOException {
    1.35 +        List<String> arr = new ArrayList<>();
    1.36 +        List<String> classes = new ArrayList<>();
    1.37 +        class DirRes extends EmulationResources {
    1.38 +            public DirRes(List<String> classes) {
    1.39 +                super(classes);
    1.40 +            }
    1.41 +
    1.42 +            @Override
    1.43 +            public InputStream get(String name) throws IOException {
    1.44 +                InputStream is = super.get(name);
    1.45 +                if (is != null) {
    1.46 +                    return is;
    1.47 +                }
    1.48 +                File r = new File(dir, name.replace('/', File.separatorChar));
    1.49 +                if (r.exists()) {
    1.50 +                    return new FileInputStream(r);
    1.51 +                }
    1.52 +                return null;
    1.53 +            }
    1.54 +        }
    1.55 +        DirRes dirRes = new DirRes(classes);
    1.56 +        listDir(dir, null, dirRes, arr);
    1.57 +        if (c == null) {
    1.58 +            c = Bck2Brwsr.newCompiler();
    1.59 +        }
    1.60 +        return c
    1.61 +        .addRootClasses(classes.toArray(new String[0]))
    1.62 +        .addResources(arr.toArray(new String[0]))
    1.63 +        .library()
    1.64 +        //.obfuscation(ObfuscationLevel.FULL)
    1.65 +        .resources(dirRes);
    1.66 +    }
    1.67 +
    1.68 +    private static void listDir(
    1.69 +        File f, String pref, EmulationResources res, List<String> resources
    1.70 +    ) throws IOException {
    1.71 +        File[] arr = f.listFiles();
    1.72 +        if (arr == null) {
    1.73 +            if (f.getName().endsWith(".class")) {
    1.74 +                res.addClassResource(pref + f.getName());
    1.75 +            } else {
    1.76 +                resources.add(pref + f.getName());
    1.77 +            }
    1.78 +        } else {
    1.79 +            for (File ch : arr) {
    1.80 +                listDir(ch, pref == null ? "" : pref + f.getName() + "/", res, resources);
    1.81 +            }
    1.82 +        }
    1.83 +    }
    1.84      
    1.85  }