2 * Back 2 Browser Bytecode Translator
3 * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 2 of the License.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. Look for COPYING file in the top folder.
16 * If not, see http://opensource.org/licenses/GPL-2.0.
18 package org.apidesign.bck2brwsr.htmlpage;
20 import java.io.ByteArrayInputStream;
21 import java.io.ByteArrayOutputStream;
22 import java.io.IOException;
23 import java.io.InputStream;
24 import java.io.OutputStream;
26 import java.net.URISyntaxException;
27 import java.util.ArrayList;
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.List;
32 import java.util.regex.Matcher;
33 import java.util.regex.Pattern;
34 import javax.tools.Diagnostic;
35 import javax.tools.DiagnosticListener;
36 import javax.tools.FileObject;
37 import javax.tools.ForwardingJavaFileManager;
38 import javax.tools.JavaFileManager;
39 import javax.tools.JavaFileObject;
40 import javax.tools.JavaFileObject.Kind;
41 import javax.tools.SimpleJavaFileObject;
42 import javax.tools.StandardJavaFileManager;
43 import javax.tools.StandardLocation;
44 import javax.tools.ToolProvider;
45 import org.apidesign.bck2brwsr.core.ExtraJavaScript;
49 * @author Jaroslav Tulach <jtulach@netbeans.org>
51 @ExtraJavaScript(processByteCode = false, resource = "")
52 final class Compile implements DiagnosticListener<JavaFileObject> {
53 private final List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<>();
54 private final Map<String, byte[]> classes;
55 private final String pkg;
56 private final String cls;
57 private final String html;
59 private Compile(String html, String code) throws IOException {
60 this.pkg = findPkg(code);
61 this.cls = findCls(code);
63 classes = compile(html, code);
66 /** Performs compilation of given HTML page and associated Java code
68 public static Compile create(String html, String code) throws IOException {
69 return new Compile(html, code);
72 /** Checks for given class among compiled resources */
73 public byte[] get(String res) {
74 return classes.get(res);
77 /** Obtains errors created during compilation.
79 public List<Diagnostic<? extends JavaFileObject>> getErrors() {
80 List<Diagnostic<? extends JavaFileObject>> err = new ArrayList<>();
81 for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
82 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
89 private Map<String, byte[]> compile(final String html, final String code) throws IOException {
90 StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
92 final Map<String, ByteArrayOutputStream> class2BAOS = new HashMap<>();
94 JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
96 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
100 final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) {
102 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
107 public InputStream openInputStream() throws IOException {
108 return new ByteArrayInputStream(html.getBytes());
114 scratch = new URI("mem://mem3");
115 } catch (URISyntaxException ex) {
116 throw new IOException(ex);
119 JavaFileManager jfm = new ForwardingJavaFileManagerImpl(sjfm, class2BAOS, scratch, htmlFile);
121 ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", "1.7", "-target", "1.7"), null, Arrays.asList(file)).call();
123 Map<String, byte[]> result = new HashMap<>();
125 for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
126 result.put(e.getKey(), e.getValue().toByteArray());
134 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
135 errors.add(diagnostic);
137 private static String findPkg(String java) throws IOException {
138 Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
139 Matcher m = p.matcher(java);
141 throw new IOException("Can't find package declaration in the java file");
143 String pkg = m.group(1);
146 private static String findCls(String java) throws IOException {
147 Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
148 Matcher m = p.matcher(java);
150 throw new IOException("Can't find package declaration in the java file");
152 String cls = m.group(1);
157 String fqn = "'" + pkg + '.' + cls + "'";
158 return html.replace("'${fqn}'", fqn);
161 @ExtraJavaScript(processByteCode = false, resource = "")
162 private class ForwardingJavaFileManagerImpl extends ForwardingJavaFileManager<JavaFileManager> {
164 private final Map<String, ByteArrayOutputStream> class2BAOS;
165 private final URI scratch;
166 private final JavaFileObject htmlFile;
168 public ForwardingJavaFileManagerImpl(JavaFileManager fileManager, Map<String, ByteArrayOutputStream> class2BAOS, URI scratch, JavaFileObject htmlFile) {
170 this.class2BAOS = class2BAOS;
171 this.scratch = scratch;
172 this.htmlFile = htmlFile;
176 public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
177 if (kind == Kind.CLASS) {
178 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
180 class2BAOS.put(className.replace('.', '/') + ".class", buffer);
181 return new SimpleJavaFileObject(sibling.toUri(), kind) {
183 public OutputStream openOutputStream() throws IOException {
189 if (kind == Kind.SOURCE) {
190 return new SimpleJavaFileObject(scratch/*sibling.toUri()*/, kind) {
191 private final ByteArrayOutputStream data = new ByteArrayOutputStream();
193 public OutputStream openOutputStream() throws IOException {
198 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
200 return new String(data.toByteArray());
205 throw new IllegalStateException();
209 public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
210 if (location == StandardLocation.SOURCE_PATH) {
211 if (packageName.equals(pkg)) {