1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/launcher/src/main/java/org/apidesign/bck2brwsr/dew/Compile.java Tue Jan 15 22:48:17 2013 +0100
1.3 @@ -0,0 +1,163 @@
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.IOException;
1.11 +import java.nio.charset.Charset;
1.12 +import java.util.ArrayList;
1.13 +import java.util.Collections;
1.14 +import java.util.Iterator;
1.15 +import java.util.List;
1.16 +import java.util.Locale;
1.17 +import java.util.Map;
1.18 +import java.util.Set;
1.19 +import java.util.regex.Matcher;
1.20 +import java.util.regex.Pattern;
1.21 +import javax.tools.Diagnostic;
1.22 +import javax.tools.DiagnosticListener;
1.23 +import javax.tools.FileObject;
1.24 +import javax.tools.JavaCompiler;
1.25 +import javax.tools.JavaCompiler.CompilationTask;
1.26 +import javax.tools.JavaFileManager;
1.27 +import javax.tools.JavaFileObject;
1.28 +import javax.tools.JavaFileObject.Kind;
1.29 +import javax.tools.StandardJavaFileManager;
1.30 +import javax.tools.StandardLocation;
1.31 +
1.32 +/**
1.33 + *
1.34 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.35 + */
1.36 +final class Compile implements JavaFileManager, DiagnosticListener<JavaFileObject> {
1.37 + private final JFO jfo;
1.38 + private final String pkg;
1.39 + private StandardJavaFileManager delegate;
1.40 + private List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
1.41 +
1.42 + public Compile(String pkg, JFO jfo) {
1.43 + this.pkg = pkg;
1.44 + this.jfo = jfo;
1.45 + }
1.46 +
1.47 + public static Map<String,byte[]> compile(String html, String java) throws IOException {
1.48 + JavaCompiler jc = javax.tools.ToolProvider.getSystemJavaCompiler();
1.49 + String pkg = findPkg(java);
1.50 + String cls = findCls(java);
1.51 +
1.52 + JFO jfo = new JFO(java, pkg.replace('.', '/') + '/' + cls + ".java");
1.53 + final Compile cmp = new Compile(pkg, jfo);
1.54 + cmp.delegate = jc.getStandardFileManager(cmp, Locale.ENGLISH, Charset.forName("UTF-8"));
1.55 +
1.56 + Set<String> toCmp = Collections.singleton(pkg + '.' + cls);
1.57 + Set<JFO> unit = Collections.singleton(jfo);
1.58 + CompilationTask task = jc.getTask(null, cmp, cmp, null, null, unit);
1.59 + if (task.call() != true) {
1.60 + throw new IOException("Compilation failed: " + cmp.errors);
1.61 + }
1.62 + return Collections.emptyMap();
1.63 + }
1.64 +
1.65 + @Override
1.66 + public ClassLoader getClassLoader(Location location) {
1.67 + return null;//Compile.class.getClassLoader();
1.68 + }
1.69 +
1.70 + @Override
1.71 + public Iterable<JavaFileObject> list(Location location, String packageName, Set<Kind> kinds, boolean recurse) throws IOException {
1.72 + if (location == StandardLocation.PLATFORM_CLASS_PATH) {
1.73 + return delegate.list(location, packageName, kinds, recurse);
1.74 + }
1.75 + if (location == StandardLocation.CLASS_PATH) {
1.76 + return delegate.list(location, packageName, kinds, recurse);
1.77 + }
1.78 + if (location == StandardLocation.SOURCE_PATH) {
1.79 + if (packageName.equals(pkg)) {
1.80 + return Collections.<JavaFileObject>singleton(jfo);
1.81 + }
1.82 + return Collections.emptyList();
1.83 + }
1.84 + throw new UnsupportedOperationException("Loc: " + location + " pkg: " + packageName + " kinds: " + kinds + " rec: " + recurse);
1.85 + }
1.86 +
1.87 + @Override
1.88 + public String inferBinaryName(Location location, JavaFileObject file) {
1.89 + if (file == jfo) {
1.90 + return pkg + "." + file.getName();
1.91 + }
1.92 + return delegate.inferBinaryName(location, file);
1.93 + }
1.94 +
1.95 + @Override
1.96 + public boolean isSameFile(FileObject a, FileObject b) {
1.97 + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1.98 + }
1.99 +
1.100 + @Override
1.101 + public boolean handleOption(String current, Iterator<String> remaining) {
1.102 + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1.103 + }
1.104 +
1.105 + @Override
1.106 + public boolean hasLocation(Location location) {
1.107 + return true;
1.108 + }
1.109 +
1.110 + @Override
1.111 + public JavaFileObject getJavaFileForInput(Location location, String className, Kind kind) throws IOException {
1.112 + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1.113 + }
1.114 +
1.115 + @Override
1.116 + public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
1.117 + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1.118 + }
1.119 +
1.120 + @Override
1.121 + public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
1.122 + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1.123 + }
1.124 +
1.125 + @Override
1.126 + public FileObject getFileForOutput(Location location, String packageName, String relativeName, FileObject sibling) throws IOException {
1.127 + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1.128 + }
1.129 +
1.130 + @Override
1.131 + public void flush() throws IOException {
1.132 + }
1.133 +
1.134 + @Override
1.135 + public void close() throws IOException {
1.136 + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1.137 + }
1.138 +
1.139 + @Override
1.140 + public int isSupportedOption(String option) {
1.141 + throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
1.142 + }
1.143 +
1.144 + @Override
1.145 + public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
1.146 + errors.add(diagnostic);
1.147 + }
1.148 + private static String findPkg(String java) throws IOException {
1.149 + Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
1.150 + Matcher m = p.matcher(java);
1.151 + if (!m.find()) {
1.152 + throw new IOException("Can't find package declaration in the java file");
1.153 + }
1.154 + String pkg = m.group(1);
1.155 + return pkg;
1.156 + }
1.157 + private static String findCls(String java) throws IOException {
1.158 + Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
1.159 + Matcher m = p.matcher(java);
1.160 + if (!m.find()) {
1.161 + throw new IOException("Can't find package declaration in the java file");
1.162 + }
1.163 + String cls = m.group(1);
1.164 + return cls;
1.165 + }
1.166 +}