launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java
branchdew
changeset 464 9823859d253a
parent 463 3641fd0663d3
child 468 a7ff47e054f3
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Wed Jan 16 10:52:06 2013 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Wed Jan 16 11:07:28 2013 +0100
     1.3 @@ -1,7 +1,3 @@
     1.4 -/*
     1.5 - * To change this template, choose Tools | Templates
     1.6 - * and open the template in the editor.
     1.7 - */
     1.8  package org.apidesign.bck2brwsr.dew;
     1.9  
    1.10  import java.io.ByteArrayInputStream;
    1.11 @@ -11,26 +7,17 @@
    1.12  import java.io.OutputStream;
    1.13  import java.net.URI;
    1.14  import java.net.URISyntaxException;
    1.15 -import java.nio.charset.Charset;
    1.16  import java.util.ArrayList;
    1.17  import java.util.Arrays;
    1.18 -import java.util.Collections;
    1.19  import java.util.HashMap;
    1.20 -import java.util.Iterator;
    1.21  import java.util.List;
    1.22 -import java.util.Locale;
    1.23  import java.util.Map;
    1.24 -import java.util.Set;
    1.25 -import java.util.logging.Level;
    1.26 -import java.util.logging.Logger;
    1.27  import java.util.regex.Matcher;
    1.28  import java.util.regex.Pattern;
    1.29  import javax.tools.Diagnostic;
    1.30  import javax.tools.DiagnosticListener;
    1.31  import javax.tools.FileObject;
    1.32  import javax.tools.ForwardingJavaFileManager;
    1.33 -import javax.tools.JavaCompiler;
    1.34 -import javax.tools.JavaCompiler.CompilationTask;
    1.35  import javax.tools.JavaFileManager;
    1.36  import javax.tools.JavaFileObject;
    1.37  import javax.tools.JavaFileObject.Kind;
    1.38 @@ -43,51 +30,45 @@
    1.39   *
    1.40   * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.41   */
    1.42 -final class Compile implements JavaFileManager, DiagnosticListener<JavaFileObject> {
    1.43 -    private final JFO jfo;
    1.44 +final class Compile implements DiagnosticListener<JavaFileObject> {
    1.45 +    private final List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
    1.46 +    private final Map<String, byte[]> classes;
    1.47      private final String pkg;
    1.48 -    private StandardJavaFileManager delegate;
    1.49 -    private List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
    1.50 +    private final String cls;
    1.51  
    1.52 -    public Compile(String pkg, JFO jfo) {
    1.53 -        this.pkg = pkg;
    1.54 -        this.jfo = jfo;
    1.55 +    private Compile(String html, String code) throws IOException {
    1.56 +        this.pkg = findPkg(code);
    1.57 +        this.cls = findCls(code);
    1.58 +        classes = compile(html, code);
    1.59 +    }
    1.60 +
    1.61 +    /** Performs compilation of given HTML page and associated Java code
    1.62 +     */
    1.63 +    public static Compile create(String html, String code) throws IOException {
    1.64 +        return new Compile(html, code);
    1.65      }
    1.66      
    1.67 -    /*
    1.68 -    public static Map<String,byte[]> compile(String html, String java) throws IOException {
    1.69 -        JavaCompiler jc = javax.tools.ToolProvider.getSystemJavaCompiler();
    1.70 -        String pkg = findPkg(java);
    1.71 -        String cls = findCls(java);
    1.72 -        
    1.73 -        JFO jfo = new JFO(java, pkg.replace('.', '/') + '/' + cls + ".java");
    1.74 -        final Compile cmp = new Compile(pkg, jfo);
    1.75 -        cmp.delegate = jc.getStandardFileManager(cmp, Locale.ENGLISH, Charset.forName("UTF-8"));
    1.76 -        
    1.77 -        Set<String> toCmp = Collections.singleton(pkg + '.' + cls);
    1.78 -        Set<JFO> unit = Collections.singleton(jfo);
    1.79 -        CompilationTask task = jc.getTask(null, cmp, cmp, null, null, unit);
    1.80 -        if (task.call() != true) {
    1.81 -            throw new IOException("Compilation failed: " + cmp.errors);
    1.82 +    /** Checks for given class among compiled resources */
    1.83 +    public byte[] get(String res) {
    1.84 +        return classes.get(res);
    1.85 +    }
    1.86 +    
    1.87 +    /** Obtains errors created during compilation.
    1.88 +     */
    1.89 +    public List<Diagnostic<? extends JavaFileObject>> getErrors() {
    1.90 +        List<Diagnostic<? extends JavaFileObject>> err = new ArrayList<>();
    1.91 +        for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
    1.92 +            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
    1.93 +                err.add(diagnostic);
    1.94 +            }
    1.95          }
    1.96 -        return Collections.emptyMap();
    1.97 +        return err;
    1.98      }
    1.99 -    */
   1.100 -    public static Map<String, byte[]> compile(final String html, final String code) throws IOException {
   1.101 -        final String pkg = findPkg(code);
   1.102 -//        String cls = findCls(code);
   1.103 -        
   1.104 -        DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() {
   1.105 -            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.106 -                System.err.println("diagnostic=" + diagnostic);
   1.107 -            }
   1.108 -        };
   1.109 -        StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(devNull, null, null);
   1.110 +    
   1.111 +    private Map<String, byte[]> compile(final String html, final String code) throws IOException {
   1.112 +        StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
   1.113  
   1.114 -//        sjfm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, toFiles(boot));
   1.115 -//        sjfm.setLocation(StandardLocation.CLASS_PATH, toFiles(compile));
   1.116 -
   1.117 -        final Map<String, ByteArrayOutputStream> class2BAOS = new HashMap<String, ByteArrayOutputStream>();
   1.118 +        final Map<String, ByteArrayOutputStream> class2BAOS = new HashMap<>();
   1.119  
   1.120          JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
   1.121              @Override
   1.122 @@ -147,28 +128,10 @@
   1.123                  
   1.124                  throw new IllegalStateException();
   1.125              }
   1.126 -//            @Override
   1.127 -//            public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException {
   1.128 -//                if (location == StandardLocation.PLATFORM_CLASS_PATH) {
   1.129 -//                    return super.list(location, packageName, kinds, recurse);
   1.130 -//                }
   1.131 -//                if (location == StandardLocation.CLASS_PATH) {
   1.132 -//                    return super.list(location, packageName, kinds, recurse);
   1.133 -//                }
   1.134 -//                if (location == StandardLocation.SOURCE_PATH) {
   1.135 -//                    System.out.println("src path for " + packageName + " kinds: " + kinds);
   1.136 -//                    if (packageName.equals(pkg) && kinds.contains(Kind.OTHER)) {
   1.137 -//                        return Collections.<JavaFileObject>singleton(htmlFile);
   1.138 -//                    }
   1.139 -//                    return Collections.emptyList();
   1.140 -//                }
   1.141 -//                throw new UnsupportedOperationException("Loc: " + location + " pkg: " + packageName + " kinds: " + kinds + " rec: " + recurse);
   1.142 -//            }
   1.143  
   1.144              @Override
   1.145              public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   1.146                  if (location == StandardLocation.SOURCE_PATH) {
   1.147 -//                    System.out.println("src path for " + packageName + " kinds: " + kinds);
   1.148                      if (packageName.equals(pkg)) {
   1.149                          return htmlFile;
   1.150                      }
   1.151 @@ -179,9 +142,9 @@
   1.152              
   1.153          };
   1.154  
   1.155 -        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, devNull, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
   1.156 +        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
   1.157  
   1.158 -        Map<String, byte[]> result = new HashMap<String, byte[]>();
   1.159 +        Map<String, byte[]> result = new HashMap<>();
   1.160  
   1.161          for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
   1.162              result.put(e.getKey(), e.getValue().toByteArray());
   1.163 @@ -190,84 +153,6 @@
   1.164          return result;
   1.165      }
   1.166  
   1.167 -    @Override
   1.168 -    public ClassLoader getClassLoader(Location location) {
   1.169 -        return null;//Compile.class.getClassLoader();
   1.170 -    }
   1.171 -
   1.172 -    @Override
   1.173 -    public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException {
   1.174 -        if (location == StandardLocation.PLATFORM_CLASS_PATH) {
   1.175 -            return delegate.list(location, packageName, kinds, recurse);
   1.176 -        }
   1.177 -        if (location == StandardLocation.CLASS_PATH) {
   1.178 -            return delegate.list(location, packageName, kinds, recurse);
   1.179 -        }
   1.180 -        if (location == StandardLocation.SOURCE_PATH) {
   1.181 -            if (packageName.equals(pkg)) {
   1.182 -                return Collections.<JavaFileObject>singleton(jfo);
   1.183 -            }
   1.184 -            return Collections.emptyList();
   1.185 -        }
   1.186 -        throw new UnsupportedOperationException("Loc: " + location + " pkg: " + packageName + " kinds: " + kinds + " rec: " + recurse);
   1.187 -    }
   1.188 -
   1.189 -    @Override
   1.190 -    public String inferBinaryName(Location location, JavaFileObject file) {
   1.191 -        if (file == jfo) {
   1.192 -            return pkg + "." + file.getName();
   1.193 -        }
   1.194 -        return delegate.inferBinaryName(location, file);
   1.195 -    }
   1.196 -
   1.197 -    @Override
   1.198 -    public boolean isSameFile(FileObject a, FileObject b) {
   1.199 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.200 -    }
   1.201 -
   1.202 -    @Override
   1.203 -    public boolean handleOption(String current, Iterator<String> remaining) {
   1.204 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.205 -    }
   1.206 -
   1.207 -    @Override
   1.208 -    public boolean hasLocation(Location location) {
   1.209 -        return true;
   1.210 -    }
   1.211 -
   1.212 -    @Override
   1.213 -    public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException {
   1.214 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.215 -    }
   1.216 -
   1.217 -    @Override
   1.218 -    public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
   1.219 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.220 -    }
   1.221 -
   1.222 -    @Override
   1.223 -    public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   1.224 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.225 -    }
   1.226 -
   1.227 -    @Override
   1.228 -    public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException {
   1.229 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.230 -    }
   1.231 -
   1.232 -    @Override
   1.233 -    public void flush() throws IOException {
   1.234 -    }
   1.235 -
   1.236 -    @Override
   1.237 -    public void close() throws IOException {
   1.238 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.239 -    }
   1.240 -
   1.241 -    @Override
   1.242 -    public int isSupportedOption(String option) {
   1.243 -        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
   1.244 -    }
   1.245  
   1.246      @Override
   1.247      public void report(Diagnostic<? extends JavaFileObject> diagnostic) {