launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java
branchdew
changeset 544 08ffdc3938e7
parent 543 1adce93fea0f
child 545 29b8e1b87fad
     1.1 --- a/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Wed Jan 23 12:53:23 2013 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,196 +0,0 @@
     1.4 -/**
     1.5 - * Back 2 Browser Bytecode Translator
     1.6 - * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 - *
     1.8 - * This program is free software: you can redistribute it and/or modify
     1.9 - * it under the terms of the GNU General Public License as published by
    1.10 - * the Free Software Foundation, version 2 of the License.
    1.11 - *
    1.12 - * This program is distributed in the hope that it will be useful,
    1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 - * GNU General Public License for more details.
    1.16 - *
    1.17 - * You should have received a copy of the GNU General Public License
    1.18 - * along with this program. Look for COPYING file in the top folder.
    1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 - */
    1.21 -package org.apidesign.bck2brwsr.dew;
    1.22 -
    1.23 -import java.io.ByteArrayInputStream;
    1.24 -import java.io.ByteArrayOutputStream;
    1.25 -import java.io.IOException;
    1.26 -import java.io.InputStream;
    1.27 -import java.io.OutputStream;
    1.28 -import java.net.URI;
    1.29 -import java.net.URISyntaxException;
    1.30 -import java.util.ArrayList;
    1.31 -import java.util.Arrays;
    1.32 -import java.util.HashMap;
    1.33 -import java.util.List;
    1.34 -import java.util.Map;
    1.35 -import java.util.regex.Matcher;
    1.36 -import java.util.regex.Pattern;
    1.37 -import javax.tools.Diagnostic;
    1.38 -import javax.tools.DiagnosticListener;
    1.39 -import javax.tools.FileObject;
    1.40 -import javax.tools.ForwardingJavaFileManager;
    1.41 -import javax.tools.JavaFileManager;
    1.42 -import javax.tools.JavaFileObject;
    1.43 -import javax.tools.JavaFileObject.Kind;
    1.44 -import javax.tools.SimpleJavaFileObject;
    1.45 -import javax.tools.StandardJavaFileManager;
    1.46 -import javax.tools.StandardLocation;
    1.47 -import javax.tools.ToolProvider;
    1.48 -
    1.49 -/**
    1.50 - *
    1.51 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.52 - */
    1.53 -final class Compile implements DiagnosticListener<JavaFileObject> {
    1.54 -    private final List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
    1.55 -    private final Map<String, byte[]> classes;
    1.56 -    private final String pkg;
    1.57 -    private final String cls;
    1.58 -
    1.59 -    private Compile(String html, String code) throws IOException {
    1.60 -        this.pkg = findPkg(code);
    1.61 -        this.cls = findCls(code);
    1.62 -        classes = compile(html, code);
    1.63 -    }
    1.64 -
    1.65 -    /** Performs compilation of given HTML page and associated Java code
    1.66 -     */
    1.67 -    public static Compile create(String html, String code) throws IOException {
    1.68 -        return new Compile(html, code);
    1.69 -    }
    1.70 -    
    1.71 -    /** Checks for given class among compiled resources */
    1.72 -    public byte[] get(String res) {
    1.73 -        return classes.get(res);
    1.74 -    }
    1.75 -    
    1.76 -    /** Obtains errors created during compilation.
    1.77 -     */
    1.78 -    public List<Diagnostic<? extends JavaFileObject>> getErrors() {
    1.79 -        List<Diagnostic<? extends JavaFileObject>> err = new ArrayList<>();
    1.80 -        for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
    1.81 -            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
    1.82 -                err.add(diagnostic);
    1.83 -            }
    1.84 -        }
    1.85 -        return err;
    1.86 -    }
    1.87 -    
    1.88 -    private Map<String, byte[]> compile(final String html, final String code) throws IOException {
    1.89 -        StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
    1.90 -
    1.91 -        final Map<String, ByteArrayOutputStream> class2BAOS = new HashMap<>();
    1.92 -
    1.93 -        JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
    1.94 -            @Override
    1.95 -            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
    1.96 -                return code;
    1.97 -            }
    1.98 -        };
    1.99 -        final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) {
   1.100 -            @Override
   1.101 -            public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.102 -                return html;
   1.103 -            }
   1.104 -
   1.105 -            @Override
   1.106 -            public InputStream openInputStream() throws IOException {
   1.107 -                return new ByteArrayInputStream(html.getBytes());
   1.108 -            }
   1.109 -        };
   1.110 -        
   1.111 -        final URI scratch;
   1.112 -        try {
   1.113 -            scratch = new URI("mem://mem3");
   1.114 -        } catch (URISyntaxException ex) {
   1.115 -            throw new IOException(ex);
   1.116 -        }
   1.117 -        
   1.118 -        JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) {
   1.119 -            @Override
   1.120 -            public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
   1.121 -                if (kind  == Kind.CLASS) {
   1.122 -                    final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
   1.123 -
   1.124 -                    class2BAOS.put(className.replace('.', '/') + ".class", buffer);
   1.125 -                    return new SimpleJavaFileObject(sibling.toUri(), kind) {
   1.126 -                        @Override
   1.127 -                        public OutputStream openOutputStream() throws IOException {
   1.128 -                            return buffer;
   1.129 -                        }
   1.130 -                    };
   1.131 -                }
   1.132 -                
   1.133 -                if (kind == Kind.SOURCE) {
   1.134 -                    return new SimpleJavaFileObject(scratch/*sibling.toUri()*/, kind) {
   1.135 -                        private final ByteArrayOutputStream data = new ByteArrayOutputStream();
   1.136 -                        @Override
   1.137 -                        public OutputStream openOutputStream() throws IOException {
   1.138 -                            return data;
   1.139 -                        }
   1.140 -
   1.141 -                        @Override
   1.142 -                        public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
   1.143 -                            data.close();
   1.144 -                            return new String(data.toByteArray());
   1.145 -                        }
   1.146 -                    };
   1.147 -                }
   1.148 -                
   1.149 -                throw new IllegalStateException();
   1.150 -            }
   1.151 -
   1.152 -            @Override
   1.153 -            public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   1.154 -                if (location == StandardLocation.SOURCE_PATH) {
   1.155 -                    if (packageName.equals(pkg)) {
   1.156 -                        return htmlFile;
   1.157 -                    }
   1.158 -                }
   1.159 -                
   1.160 -                return null;
   1.161 -            }
   1.162 -            
   1.163 -        };
   1.164 -
   1.165 -        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
   1.166 -
   1.167 -        Map<String, byte[]> result = new HashMap<>();
   1.168 -
   1.169 -        for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
   1.170 -            result.put(e.getKey(), e.getValue().toByteArray());
   1.171 -        }
   1.172 -
   1.173 -        return result;
   1.174 -    }
   1.175 -
   1.176 -
   1.177 -    @Override
   1.178 -    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.179 -        errors.add(diagnostic);
   1.180 -    }
   1.181 -    private static String findPkg(String java) throws IOException {
   1.182 -        Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
   1.183 -        Matcher m = p.matcher(java);
   1.184 -        if (!m.find()) {
   1.185 -            throw new IOException("Can't find package declaration in the java file");
   1.186 -        }
   1.187 -        String pkg = m.group(1);
   1.188 -        return pkg;
   1.189 -    }
   1.190 -    private static String findCls(String java) throws IOException {
   1.191 -        Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
   1.192 -        Matcher m = p.matcher(java);
   1.193 -        if (!m.find()) {
   1.194 -            throw new IOException("Can't find package declaration in the java file");
   1.195 -        }
   1.196 -        String cls = m.group(1);
   1.197 -        return cls;
   1.198 -    }
   1.199 -}