rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java
branchclosure
changeset 874 2bcbe348dbec
parent 860 35507d1a5069
child 1020 a6bacea2518f
child 1359 5d93ca1561c3
     1.1 --- a/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Tue Mar 19 13:08:44 2013 +0100
     1.2 +++ b/rt/vm/src/main/java/org/apidesign/vm4brwsr/Bck2Brwsr.java	Fri Mar 22 14:46:10 2013 +0100
     1.3 @@ -54,10 +54,17 @@
     1.4   * @author Jaroslav Tulach <jtulach@netbeans.org>
     1.5   */
     1.6  public final class Bck2Brwsr {
     1.7 -    private Bck2Brwsr() {
     1.8 +    private final ObfuscationLevel level;
     1.9 +    private final StringArray classes;
    1.10 +    private final Resources res;
    1.11 +
    1.12 +    private Bck2Brwsr(ObfuscationLevel level, StringArray classes, Resources resources) {
    1.13 +        this.level = level;
    1.14 +        this.classes = classes;
    1.15 +        this.res = resources;
    1.16      }
    1.17 -
    1.18 -    /** Generates virtual machine from bytes served by a <code>resources</code>
    1.19 +    
    1.20 +    /** Helper method to generate virtual machine from bytes served by a <code>resources</code>
    1.21       * provider.
    1.22       *
    1.23       * @param out the output to write the generated JavaScript to
    1.24 @@ -66,10 +73,10 @@
    1.25       * @throws IOException I/O exception can be thrown when something goes wrong
    1.26       */
    1.27      public static void generate(Appendable out, Resources resources, String... classes) throws IOException {
    1.28 -        generate(out, ObfuscationLevel.NONE, resources, classes);
    1.29 +        newCompiler().resources(resources).addRootClasses(classes).generate(out);
    1.30      }
    1.31  
    1.32 -    /** Generates virtual machine from bytes served by a class loader.
    1.33 +    /** Helper method to generate virtual machine from bytes served by a class loader.
    1.34       *
    1.35       * @param out the output to write the generated JavaScript to
    1.36       * @param loader class loader to load needed classes from
    1.37 @@ -77,27 +84,87 @@
    1.38       * @throws IOException I/O exception can be thrown when something goes wrong
    1.39       */
    1.40      public static void generate(Appendable out, ClassLoader loader, String... classes) throws IOException {
    1.41 -        generate(out, ObfuscationLevel.NONE, loader, classes);
    1.42 +        newCompiler().resources(loader).addRootClasses(classes).generate(out);
    1.43 +    }
    1.44 +    
    1.45 +    /** Creates new instance of Bck2Brwsr compiler which is ready to generate
    1.46 +     * empty Bck2Brwsr virtual machine. The instance can be further
    1.47 +     * configured by calling chain of methods. For example: 
    1.48 +     * <pre>
    1.49 +     * {@link #createCompiler()}.{@link #resources(org.apidesign.vm4brwsr.Bck2Brwsr.Resources) resources(loader)}.{@link #addRootClasses(java.lang.String[]) addRootClasses("your/Clazz")}.{@link #generate(java.lang.Appendable) generate(out)};
    1.50 +     * </pre>
    1.51 +     * 
    1.52 +     * @return new instance of the Bck2Brwsr compiler
    1.53 +     * @since 0.5
    1.54 +     */
    1.55 +    public static Bck2Brwsr newCompiler() {
    1.56 +        StringArray arr = StringArray.asList(VM.class.getName().replace('.', '/'));
    1.57 +        return new Bck2Brwsr(ObfuscationLevel.NONE, arr, null);
    1.58      }
    1.59  
    1.60 -    /** Generates virtual machine from bytes served by a <code>resources</code>
    1.61 -     * provider.
    1.62 +    /** Creates new instance of the Bck2Brwsr compiler which inherits
    1.63 +     * all values from <code>this</code> instance and adds additional classes 
    1.64 +     * to the list of those that should be compiled by the {@link #generate(java.lang.Appendable)} 
    1.65 +     * method.
    1.66 +     * 
    1.67 +     * @param classes the classes to add to the compilation
    1.68 +     * @return new instance of the compiler
    1.69 +     */
    1.70 +    public Bck2Brwsr addRootClasses(String... classes) {
    1.71 +        if (classes.length == 0) {
    1.72 +            return this;
    1.73 +        } else {
    1.74 +            return new Bck2Brwsr(level, this.classes.addAndNew(classes), res);
    1.75 +        }
    1.76 +    }
    1.77 +    
    1.78 +    /** Changes the obfuscation level for the compiler by creating new instance
    1.79 +     * which inherits all values from <code>this</code> and adjust the level
    1.80 +     * of obfuscation.
    1.81 +     * 
    1.82 +     * @param level the new level of obfuscation
    1.83 +     * @return new instance of the compiler with changed level of obfuscation
    1.84 +     * @since 0.5
    1.85 +     */
    1.86 +    public Bck2Brwsr obfuscation(ObfuscationLevel level) {
    1.87 +        return new Bck2Brwsr(level, classes, res);
    1.88 +    }
    1.89 +    
    1.90 +    /** A way to change the provider of additional resources (classes) for the 
    1.91 +     * compiler. 
    1.92 +     * 
    1.93 +     * @param res the implementation of resources provider
    1.94 +     * @return new instance of the compiler with all values remaining the same, just 
    1.95 +     *   with different resources provider
    1.96 +     * @since 0.5
    1.97 +     */
    1.98 +    public Bck2Brwsr resources(Resources res) {
    1.99 +        return new Bck2Brwsr(level, classes, res);
   1.100 +    }
   1.101 +
   1.102 +    /** A way to change the provider of additional resources (classes) for the 
   1.103 +     * compiler by specifying classloader to use for loading them.
   1.104 +     * 
   1.105 +     * @param loader class loader to load the resources from
   1.106 +     * @return new instance of the compiler with all values being the same, just 
   1.107 +     *   different resources provider
   1.108 +     * @since 0.5
   1.109 +     */
   1.110 +    public Bck2Brwsr resources(final ClassLoader loader) {
   1.111 +        return resources(new LdrRsrcs(loader));
   1.112 +    }
   1.113 +    
   1.114 +    /** Generates virtual machine based on previous configuration of the 
   1.115 +     * compiler.
   1.116       * 
   1.117       * @param out the output to write the generated JavaScript to
   1.118 -     * @param obfuscationLevel the obfuscation level for the generated
   1.119 -     *                         JavaScript
   1.120 -     * @param resources provider of class files to use
   1.121 -     * @param classes additional classes to include in the generated script
   1.122 -     * @throws IOException I/O exception can be thrown when something goes wrong
   1.123       * @since 0.5
   1.124       */
   1.125 -    public static void generate(Appendable out, ObfuscationLevel obfuscationLevel, Resources resources, String... classes) throws IOException {
   1.126 -        StringArray arr = StringArray.asList(classes);
   1.127 -        arr.add(VM.class.getName().replace('.', '/'));
   1.128 -
   1.129 -        if (obfuscationLevel != ObfuscationLevel.NONE) {
   1.130 +    public void generate(Appendable out) throws IOException {
   1.131 +        Resources r = res != null ? res : new LdrRsrcs(Bck2Brwsr.class.getClassLoader());
   1.132 +        if (level != ObfuscationLevel.NONE) {
   1.133              try {
   1.134 -                ClosureWrapper.produceTo(out, obfuscationLevel, resources, arr);
   1.135 +                ClosureWrapper.produceTo(out, level, r, classes);
   1.136                  return;
   1.137              } catch (IOException ex) {
   1.138                  throw ex;
   1.139 @@ -107,35 +174,7 @@
   1.140              }
   1.141          }
   1.142  
   1.143 -        VM.compile(resources, out, arr);
   1.144 -    }
   1.145 -    
   1.146 -    /** Generates virtual machine from bytes served by a class loader.
   1.147 -     * 
   1.148 -     * @param out the output to write the generated JavaScript to
   1.149 -     * @param obfuscationLevel the obfuscation level for the generated
   1.150 -     *                         JavaScript
   1.151 -     * @param loader class loader to load needed classes from
   1.152 -     * @param classes additional classes to include in the generated script
   1.153 -     * @throws IOException I/O exception can be thrown when something goes wrong
   1.154 -     * @since 0.5
   1.155 -     */
   1.156 -    public static void generate(Appendable out, ObfuscationLevel obfuscationLevel, final ClassLoader loader, String... classes) throws IOException {
   1.157 -        class R implements Resources {
   1.158 -            @Override
   1.159 -            public InputStream get(String name) throws IOException {
   1.160 -                Enumeration<URL> en = loader.getResources(name);
   1.161 -                URL u = null;
   1.162 -                while (en.hasMoreElements()) {
   1.163 -                    u = en.nextElement();
   1.164 -                }
   1.165 -                if (u == null) {
   1.166 -                    throw new IOException("Can't find " + name);
   1.167 -                }
   1.168 -                return u.openStream();
   1.169 -            }
   1.170 -        }
   1.171 -        generate(out, obfuscationLevel, new R(), classes);
   1.172 +        VM.compile(r, out, classes);
   1.173      }
   1.174      
   1.175      /** Provider of resources (classes and other files). The