launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java
branchdew
changeset 463 3641fd0663d3
parent 462 aa69b1387624
child 464 9823859d253a
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Tue Jan 15 22:48:17 2013 +0100
     1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Wed Jan 16 10:52:06 2013 +0100
     1.3 @@ -4,27 +4,40 @@
     1.4   */
     1.5  package org.apidesign.bck2brwsr.dew;
     1.6  
     1.7 +import java.io.ByteArrayInputStream;
     1.8 +import java.io.ByteArrayOutputStream;
     1.9  import java.io.IOException;
    1.10 +import java.io.InputStream;
    1.11 +import java.io.OutputStream;
    1.12 +import java.net.URI;
    1.13 +import java.net.URISyntaxException;
    1.14  import java.nio.charset.Charset;
    1.15  import java.util.ArrayList;
    1.16 +import java.util.Arrays;
    1.17  import java.util.Collections;
    1.18 +import java.util.HashMap;
    1.19  import java.util.Iterator;
    1.20  import java.util.List;
    1.21  import java.util.Locale;
    1.22  import java.util.Map;
    1.23  import java.util.Set;
    1.24 +import java.util.logging.Level;
    1.25 +import java.util.logging.Logger;
    1.26  import java.util.regex.Matcher;
    1.27  import java.util.regex.Pattern;
    1.28  import javax.tools.Diagnostic;
    1.29  import javax.tools.DiagnosticListener;
    1.30  import javax.tools.FileObject;
    1.31 +import javax.tools.ForwardingJavaFileManager;
    1.32  import javax.tools.JavaCompiler;
    1.33  import javax.tools.JavaCompiler.CompilationTask;
    1.34  import javax.tools.JavaFileManager;
    1.35  import javax.tools.JavaFileObject;
    1.36  import javax.tools.JavaFileObject.Kind;
    1.37 +import javax.tools.SimpleJavaFileObject;
    1.38  import javax.tools.StandardJavaFileManager;
    1.39  import javax.tools.StandardLocation;
    1.40 +import javax.tools.ToolProvider;
    1.41  
    1.42  /**
    1.43   *
    1.44 @@ -41,6 +54,7 @@
    1.45          this.jfo = jfo;
    1.46      }
    1.47      
    1.48 +    /*
    1.49      public static Map<String,byte[]> compile(String html, String java) throws IOException {
    1.50          JavaCompiler jc = javax.tools.ToolProvider.getSystemJavaCompiler();
    1.51          String pkg = findPkg(java);
    1.52 @@ -58,6 +72,123 @@
    1.53          }
    1.54          return Collections.emptyMap();
    1.55      }
    1.56 +    */
    1.57 +    public static Map<String, byte[]> compile(final String html, final String code) throws IOException {
    1.58 +        final String pkg = findPkg(code);
    1.59 +//        String cls = findCls(code);
    1.60 +        
    1.61 +        DiagnosticListener<JavaFileObject> devNull = new DiagnosticListener<JavaFileObject>() {
    1.62 +            public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
    1.63 +                System.err.println("diagnostic=" + diagnostic);
    1.64 +            }
    1.65 +        };
    1.66 +        StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(devNull, null, null);
    1.67 +
    1.68 +//        sjfm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, toFiles(boot));
    1.69 +//        sjfm.setLocation(StandardLocation.CLASS_PATH, toFiles(compile));
    1.70 +
    1.71 +        final Map<String, ByteArrayOutputStream> class2BAOS = new HashMap<String, ByteArrayOutputStream>();
    1.72 +
    1.73 +        JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
    1.74 +            @Override
    1.75 +            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    1.76 +                return code;
    1.77 +            }
    1.78 +        };
    1.79 +        final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) {
    1.80 +            @Override
    1.81 +            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    1.82 +                return html;
    1.83 +            }
    1.84 +
    1.85 +            @Override
    1.86 +            public InputStream openInputStream() throws IOException {
    1.87 +                return new ByteArrayInputStream(html.getBytes());
    1.88 +            }
    1.89 +        };
    1.90 +        
    1.91 +        final URI scratch;
    1.92 +        try {
    1.93 +            scratch = new URI("mem://mem3");
    1.94 +        } catch (URISyntaxException ex) {
    1.95 +            throw new IOException(ex);
    1.96 +        }
    1.97 +        
    1.98 +        JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) {
    1.99 +            @Override
   1.100 +            public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
   1.101 +                if (kind  == Kind.CLASS) {
   1.102 +                    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
   1.103 +
   1.104 +                    class2BAOS.put(className.replace('.', '/') + ".class", buffer);
   1.105 +                    return new SimpleJavaFileObject(sibling.toUri(), kind) {
   1.106 +                        @Override
   1.107 +                        public OutputStream openOutputStream() throws IOException {
   1.108 +                            return buffer;
   1.109 +                        }
   1.110 +                    };
   1.111 +                }
   1.112 +                
   1.113 +                if (kind == Kind.SOURCE) {
   1.114 +                    return new SimpleJavaFileObject(scratch/*sibling.toUri()*/, kind) {
   1.115 +                        private final ByteArrayOutputStream data = new ByteArrayOutputStream();
   1.116 +                        @Override
   1.117 +                        public OutputStream openOutputStream() throws IOException {
   1.118 +                            return data;
   1.119 +                        }
   1.120 +
   1.121 +                        @Override
   1.122 +                        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.123 +                            data.close();
   1.124 +                            return new String(data.toByteArray());
   1.125 +                        }
   1.126 +                    };
   1.127 +                }
   1.128 +                
   1.129 +                throw new IllegalStateException();
   1.130 +            }
   1.131 +//            @Override
   1.132 +//            public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException {
   1.133 +//                if (location == StandardLocation.PLATFORM_CLASS_PATH) {
   1.134 +//                    return super.list(location, packageName, kinds, recurse);
   1.135 +//                }
   1.136 +//                if (location == StandardLocation.CLASS_PATH) {
   1.137 +//                    return super.list(location, packageName, kinds, recurse);
   1.138 +//                }
   1.139 +//                if (location == StandardLocation.SOURCE_PATH) {
   1.140 +//                    System.out.println("src path for " + packageName + " kinds: " + kinds);
   1.141 +//                    if (packageName.equals(pkg) && kinds.contains(Kind.OTHER)) {
   1.142 +//                        return Collections.<JavaFileObject>singleton(htmlFile);
   1.143 +//                    }
   1.144 +//                    return Collections.emptyList();
   1.145 +//                }
   1.146 +//                throw new UnsupportedOperationException("Loc: " + location + " pkg: " + packageName + " kinds: " + kinds + " rec: " + recurse);
   1.147 +//            }
   1.148 +
   1.149 +            @Override
   1.150 +            public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   1.151 +                if (location == StandardLocation.SOURCE_PATH) {
   1.152 +//                    System.out.println("src path for " + packageName + " kinds: " + kinds);
   1.153 +                    if (packageName.equals(pkg)) {
   1.154 +                        return htmlFile;
   1.155 +                    }
   1.156 +                }
   1.157 +                
   1.158 +                return null;
   1.159 +            }
   1.160 +            
   1.161 +        };
   1.162 +
   1.163 +        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, devNull, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
   1.164 +
   1.165 +        Map<String, byte[]> result = new HashMap<String, byte[]>();
   1.166 +
   1.167 +        for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
   1.168 +            result.put(e.getKey(), e.getValue().toByteArray());
   1.169 +        }
   1.170 +
   1.171 +        return result;
   1.172 +    }
   1.173  
   1.174      @Override
   1.175      public ClassLoader getClassLoader(Location location) {