jaroslav@468
|
1 |
/**
|
jaroslav@468
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@468
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@468
|
4 |
*
|
jaroslav@468
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@468
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@468
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@468
|
8 |
*
|
jaroslav@468
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@468
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@468
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@468
|
12 |
* GNU General Public License for more details.
|
jaroslav@468
|
13 |
*
|
jaroslav@468
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@468
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@468
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@468
|
17 |
*/
|
jaroslav@462
|
18 |
package org.apidesign.bck2brwsr.dew;
|
jaroslav@462
|
19 |
|
jaroslav@463
|
20 |
import java.io.ByteArrayInputStream;
|
jaroslav@463
|
21 |
import java.io.ByteArrayOutputStream;
|
jaroslav@462
|
22 |
import java.io.IOException;
|
jaroslav@463
|
23 |
import java.io.InputStream;
|
jaroslav@463
|
24 |
import java.io.OutputStream;
|
jaroslav@463
|
25 |
import java.net.URI;
|
jaroslav@463
|
26 |
import java.net.URISyntaxException;
|
jaroslav@462
|
27 |
import java.util.ArrayList;
|
jaroslav@463
|
28 |
import java.util.Arrays;
|
jaroslav@463
|
29 |
import java.util.HashMap;
|
jaroslav@462
|
30 |
import java.util.List;
|
jaroslav@462
|
31 |
import java.util.Map;
|
jaroslav@462
|
32 |
import java.util.regex.Matcher;
|
jaroslav@462
|
33 |
import java.util.regex.Pattern;
|
jaroslav@462
|
34 |
import javax.tools.Diagnostic;
|
jaroslav@462
|
35 |
import javax.tools.DiagnosticListener;
|
jaroslav@462
|
36 |
import javax.tools.FileObject;
|
jaroslav@463
|
37 |
import javax.tools.ForwardingJavaFileManager;
|
jaroslav@462
|
38 |
import javax.tools.JavaFileManager;
|
jaroslav@462
|
39 |
import javax.tools.JavaFileObject;
|
jaroslav@462
|
40 |
import javax.tools.JavaFileObject.Kind;
|
jaroslav@463
|
41 |
import javax.tools.SimpleJavaFileObject;
|
jaroslav@462
|
42 |
import javax.tools.StandardJavaFileManager;
|
jaroslav@462
|
43 |
import javax.tools.StandardLocation;
|
jaroslav@463
|
44 |
import javax.tools.ToolProvider;
|
jaroslav@462
|
45 |
|
jaroslav@462
|
46 |
/**
|
jaroslav@462
|
47 |
*
|
jaroslav@462
|
48 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@462
|
49 |
*/
|
jaroslav@464
|
50 |
final class Compile implements DiagnosticListener<JavaFileObject> {
|
jaroslav@464
|
51 |
private final List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
|
jaroslav@464
|
52 |
private final Map<String, byte[]> classes;
|
jaroslav@462
|
53 |
private final String pkg;
|
jaroslav@464
|
54 |
private final String cls;
|
jaroslav@579
|
55 |
private final String html;
|
jaroslav@462
|
56 |
|
jaroslav@464
|
57 |
private Compile(String html, String code) throws IOException {
|
jaroslav@464
|
58 |
this.pkg = findPkg(code);
|
jaroslav@464
|
59 |
this.cls = findCls(code);
|
jaroslav@579
|
60 |
this.html = html;
|
jaroslav@464
|
61 |
classes = compile(html, code);
|
jaroslav@464
|
62 |
}
|
jaroslav@464
|
63 |
|
jaroslav@464
|
64 |
/** Performs compilation of given HTML page and associated Java code
|
jaroslav@464
|
65 |
*/
|
jaroslav@464
|
66 |
public static Compile create(String html, String code) throws IOException {
|
jaroslav@464
|
67 |
return new Compile(html, code);
|
jaroslav@462
|
68 |
}
|
jaroslav@462
|
69 |
|
jaroslav@464
|
70 |
/** Checks for given class among compiled resources */
|
jaroslav@464
|
71 |
public byte[] get(String res) {
|
jaroslav@464
|
72 |
return classes.get(res);
|
jaroslav@464
|
73 |
}
|
jaroslav@464
|
74 |
|
jaroslav@464
|
75 |
/** Obtains errors created during compilation.
|
jaroslav@464
|
76 |
*/
|
jaroslav@464
|
77 |
public List<Diagnostic<? extends JavaFileObject>> getErrors() {
|
jaroslav@464
|
78 |
List<Diagnostic<? extends JavaFileObject>> err = new ArrayList<>();
|
jaroslav@464
|
79 |
for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
|
jaroslav@464
|
80 |
if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
|
jaroslav@464
|
81 |
err.add(diagnostic);
|
jaroslav@464
|
82 |
}
|
jaroslav@462
|
83 |
}
|
jaroslav@464
|
84 |
return err;
|
jaroslav@462
|
85 |
}
|
jaroslav@464
|
86 |
|
jaroslav@464
|
87 |
private Map<String, byte[]> compile(final String html, final String code) throws IOException {
|
jaroslav@464
|
88 |
StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
|
jaroslav@463
|
89 |
|
jaroslav@464
|
90 |
final Map<String, ByteArrayOutputStream> class2BAOS = new HashMap<>();
|
jaroslav@463
|
91 |
|
jaroslav@463
|
92 |
JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
|
jaroslav@463
|
93 |
@Override
|
jaroslav@463
|
94 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
|
jaroslav@463
|
95 |
return code;
|
jaroslav@463
|
96 |
}
|
jaroslav@463
|
97 |
};
|
jaroslav@463
|
98 |
final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) {
|
jaroslav@463
|
99 |
@Override
|
jaroslav@463
|
100 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
|
jaroslav@463
|
101 |
return html;
|
jaroslav@463
|
102 |
}
|
jaroslav@463
|
103 |
|
jaroslav@463
|
104 |
@Override
|
jaroslav@463
|
105 |
public InputStream openInputStream() throws IOException {
|
jaroslav@463
|
106 |
return new ByteArrayInputStream(html.getBytes());
|
jaroslav@463
|
107 |
}
|
jaroslav@463
|
108 |
};
|
jaroslav@463
|
109 |
|
jaroslav@463
|
110 |
final URI scratch;
|
jaroslav@463
|
111 |
try {
|
jaroslav@463
|
112 |
scratch = new URI("mem://mem3");
|
jaroslav@463
|
113 |
} catch (URISyntaxException ex) {
|
jaroslav@463
|
114 |
throw new IOException(ex);
|
jaroslav@463
|
115 |
}
|
jaroslav@463
|
116 |
|
jaroslav@463
|
117 |
JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) {
|
jaroslav@463
|
118 |
@Override
|
jaroslav@463
|
119 |
public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
|
jaroslav@463
|
120 |
if (kind == Kind.CLASS) {
|
jaroslav@463
|
121 |
final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
|
jaroslav@463
|
122 |
|
jaroslav@463
|
123 |
class2BAOS.put(className.replace('.', '/') + ".class", buffer);
|
jaroslav@463
|
124 |
return new SimpleJavaFileObject(sibling.toUri(), kind) {
|
jaroslav@463
|
125 |
@Override
|
jaroslav@463
|
126 |
public OutputStream openOutputStream() throws IOException {
|
jaroslav@463
|
127 |
return buffer;
|
jaroslav@463
|
128 |
}
|
jaroslav@463
|
129 |
};
|
jaroslav@463
|
130 |
}
|
jaroslav@463
|
131 |
|
jaroslav@463
|
132 |
if (kind == Kind.SOURCE) {
|
jaroslav@463
|
133 |
return new SimpleJavaFileObject(scratch/*sibling.toUri()*/, kind) {
|
jaroslav@463
|
134 |
private final ByteArrayOutputStream data = new ByteArrayOutputStream();
|
jaroslav@463
|
135 |
@Override
|
jaroslav@463
|
136 |
public OutputStream openOutputStream() throws IOException {
|
jaroslav@463
|
137 |
return data;
|
jaroslav@463
|
138 |
}
|
jaroslav@463
|
139 |
|
jaroslav@463
|
140 |
@Override
|
jaroslav@463
|
141 |
public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
|
jaroslav@463
|
142 |
data.close();
|
jaroslav@463
|
143 |
return new String(data.toByteArray());
|
jaroslav@463
|
144 |
}
|
jaroslav@463
|
145 |
};
|
jaroslav@463
|
146 |
}
|
jaroslav@463
|
147 |
|
jaroslav@463
|
148 |
throw new IllegalStateException();
|
jaroslav@463
|
149 |
}
|
jaroslav@463
|
150 |
|
jaroslav@463
|
151 |
@Override
|
jaroslav@463
|
152 |
public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
|
jaroslav@463
|
153 |
if (location == StandardLocation.SOURCE_PATH) {
|
jaroslav@463
|
154 |
if (packageName.equals(pkg)) {
|
jaroslav@463
|
155 |
return htmlFile;
|
jaroslav@463
|
156 |
}
|
jaroslav@463
|
157 |
}
|
jaroslav@463
|
158 |
|
jaroslav@463
|
159 |
return null;
|
jaroslav@463
|
160 |
}
|
jaroslav@463
|
161 |
|
jaroslav@463
|
162 |
};
|
jaroslav@463
|
163 |
|
jaroslav@464
|
164 |
ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
|
jaroslav@463
|
165 |
|
jaroslav@464
|
166 |
Map<String, byte[]> result = new HashMap<>();
|
jaroslav@463
|
167 |
|
jaroslav@463
|
168 |
for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
|
jaroslav@463
|
169 |
result.put(e.getKey(), e.getValue().toByteArray());
|
jaroslav@463
|
170 |
}
|
jaroslav@463
|
171 |
|
jaroslav@463
|
172 |
return result;
|
jaroslav@463
|
173 |
}
|
jaroslav@462
|
174 |
|
jaroslav@462
|
175 |
|
jaroslav@462
|
176 |
@Override
|
jaroslav@462
|
177 |
public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
|
jaroslav@462
|
178 |
errors.add(diagnostic);
|
jaroslav@462
|
179 |
}
|
jaroslav@462
|
180 |
private static String findPkg(String java) throws IOException {
|
jaroslav@462
|
181 |
Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
|
jaroslav@462
|
182 |
Matcher m = p.matcher(java);
|
jaroslav@462
|
183 |
if (!m.find()) {
|
jaroslav@462
|
184 |
throw new IOException("Can't find package declaration in the java file");
|
jaroslav@462
|
185 |
}
|
jaroslav@462
|
186 |
String pkg = m.group(1);
|
jaroslav@462
|
187 |
return pkg;
|
jaroslav@462
|
188 |
}
|
jaroslav@462
|
189 |
private static String findCls(String java) throws IOException {
|
jaroslav@462
|
190 |
Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
|
jaroslav@462
|
191 |
Matcher m = p.matcher(java);
|
jaroslav@462
|
192 |
if (!m.find()) {
|
jaroslav@462
|
193 |
throw new IOException("Can't find package declaration in the java file");
|
jaroslav@462
|
194 |
}
|
jaroslav@462
|
195 |
String cls = m.group(1);
|
jaroslav@462
|
196 |
return cls;
|
jaroslav@462
|
197 |
}
|
jaroslav@579
|
198 |
|
jaroslav@579
|
199 |
String getHtml() {
|
jaroslav@579
|
200 |
String fqn = "'" + pkg + '.' + cls + "'";
|
jaroslav@579
|
201 |
return html.replace("'${fqn}'", fqn);
|
jaroslav@579
|
202 |
}
|
jaroslav@462
|
203 |
}
|