dew/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java
changeset 1372 cc58b30499e5
parent 1371 fd2d4ca28bd3
child 1373 c4e57ec5f0df
     1.1 --- a/dew/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java	Sat Oct 12 09:05:08 2013 +0200
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,146 +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 -    private final String html;
    1.59 -
    1.60 -    private Compile(String html, String code) throws IOException {
    1.61 -        this.pkg = findPkg(code);
    1.62 -        this.cls = findCls(code);
    1.63 -        this.html = html;
    1.64 -        classes = compile(html, code);
    1.65 -    }
    1.66 -
    1.67 -    /** Performs compilation of given HTML page and associated Java code
    1.68 -     */
    1.69 -    public static Compile create(String html, String code) throws IOException {
    1.70 -        return new Compile(html, code);
    1.71 -    }
    1.72 -    
    1.73 -    /** Checks for given class among compiled resources */
    1.74 -    public byte[] get(String res) {
    1.75 -        return classes.get(res);
    1.76 -    }
    1.77 -    
    1.78 -    /** Obtains errors created during compilation.
    1.79 -     */
    1.80 -    public List<Diagnostic<? extends JavaFileObject>> getErrors() {
    1.81 -        List<Diagnostic<? extends JavaFileObject>> err = new ArrayList<>();
    1.82 -        for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
    1.83 -            if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
    1.84 -                err.add(diagnostic);
    1.85 -            }
    1.86 -        }
    1.87 -        return err;
    1.88 -    }
    1.89 -    
    1.90 -    private Map<String, byte[]> compile(final String html, final String code) throws IOException {
    1.91 -        final ClassLoaderFileManager clfm = new ClassLoaderFileManager();
    1.92 -        final JavaFileObject file = clfm.createMemoryFileObject(
    1.93 -                ClassLoaderFileManager.convertFQNToResource(pkg.isEmpty() ? cls : pkg + "." + cls) + Kind.SOURCE.extension,
    1.94 -                Kind.SOURCE,
    1.95 -                code.getBytes());
    1.96 -        final JavaFileObject htmlFile = clfm.createMemoryFileObject(
    1.97 -            ClassLoaderFileManager.convertFQNToResource(pkg),
    1.98 -            Kind.OTHER,
    1.99 -            html.getBytes());
   1.100 -
   1.101 -        JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(clfm) {            
   1.102 -            @Override
   1.103 -            public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
   1.104 -                if (location == StandardLocation.SOURCE_PATH) {
   1.105 -                    if (packageName.equals(pkg)) {
   1.106 -                        return htmlFile;
   1.107 -                    }
   1.108 -                }                
   1.109 -                return null;
   1.110 -            }
   1.111 -        };
   1.112 -
   1.113 -        final Boolean res = ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
   1.114 -        Map<String, byte[]> result = new HashMap<>();
   1.115 -        for (MemoryFileObject generated : clfm.getGeneratedFiles(Kind.CLASS)) {
   1.116 -            result.put(generated.getName().substring(1), generated.getContent());
   1.117 -        }
   1.118 -        return result;
   1.119 -    }
   1.120 -
   1.121 -
   1.122 -    @Override
   1.123 -    public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
   1.124 -        errors.add(diagnostic);
   1.125 -    }
   1.126 -    private static String findPkg(String java) throws IOException {
   1.127 -        Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
   1.128 -        Matcher m = p.matcher(java);
   1.129 -        if (!m.find()) {
   1.130 -            throw new IOException("Can't find package declaration in the java file");
   1.131 -        }
   1.132 -        String pkg = m.group(1);
   1.133 -        return pkg;
   1.134 -    }
   1.135 -    private static String findCls(String java) throws IOException {
   1.136 -        Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
   1.137 -        Matcher m = p.matcher(java);
   1.138 -        if (!m.find()) {
   1.139 -            throw new IOException("Can't find package declaration in the java file");
   1.140 -        }
   1.141 -        String cls = m.group(1);
   1.142 -        return cls;
   1.143 -    }
   1.144 -
   1.145 -    String getHtml() {
   1.146 -        String fqn = "'" + pkg + '.' + cls + "'";
   1.147 -        return html.replace("'${fqn}'", fqn);
   1.148 -    }
   1.149 -}