2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 * Copyright 2013-2013 Oracle and/or its affiliates. All rights reserved.
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
29 * The Original Software is NetBeans. The Initial Developer of the Original
30 * Software is Oracle. Portions Copyright 2013-2013 Oracle. All Rights Reserved.
32 * If you wish your version of this file to be governed by only the CDDL
33 * or only the GPL Version 2, indicate your decision by adding
34 * "[Contributor] elects to include this software in this distribution
35 * under the [CDDL or GPL Version 2] license." If you do not indicate a
36 * single choice of license, a recipient has the option to distribute
37 * your version of this file under either the CDDL, the GPL Version 2 or
38 * to extend the choice of license to its licensees as provided above.
39 * However, if you add GPL Version 2 code and therefore, elected the GPL
40 * Version 2 license, then the option applies only if the new code is
41 * made subject to such option by the copyright holder.
43 package net.java.html.json;
45 import java.io.ByteArrayInputStream;
46 import java.io.ByteArrayOutputStream;
47 import java.io.IOException;
48 import java.io.InputStream;
49 import java.io.OutputStream;
51 import java.net.URISyntaxException;
52 import java.util.ArrayList;
53 import java.util.Arrays;
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.Locale;
58 import java.util.regex.Matcher;
59 import java.util.regex.Pattern;
60 import javax.tools.Diagnostic;
61 import javax.tools.DiagnosticListener;
62 import javax.tools.FileObject;
63 import javax.tools.ForwardingJavaFileManager;
64 import javax.tools.JavaFileManager;
65 import javax.tools.JavaFileObject;
66 import javax.tools.JavaFileObject.Kind;
67 import javax.tools.SimpleJavaFileObject;
68 import javax.tools.StandardJavaFileManager;
69 import javax.tools.StandardLocation;
70 import javax.tools.ToolProvider;
71 import static org.testng.Assert.assertFalse;
72 import static org.testng.Assert.fail;
76 * @author Jaroslav Tulach <jtulach@netbeans.org>
78 final class Compile implements DiagnosticListener<JavaFileObject> {
79 private final List<Diagnostic<? extends JavaFileObject>> errors =
80 new ArrayList<Diagnostic<? extends JavaFileObject>>();
81 private final Map<String, byte[]> classes;
82 private final String pkg;
83 private final String cls;
84 private final String html;
85 private final String sourceLevel;
87 private Compile(String html, String code, String sl) throws IOException {
88 this.pkg = findPkg(code);
89 this.cls = findCls(code);
91 this.sourceLevel = sl;
92 classes = compile(html, code);
95 /** Performs compilation of given HTML page and associated Java code
97 public static Compile create(String html, String code) throws IOException {
98 return create(html, code, "1.7");
100 static Compile create(String html, String code, String sourceLevel) throws IOException {
101 return new Compile(html, code, sourceLevel);
104 /** Checks for given class among compiled resources */
105 public byte[] get(String res) {
106 return classes.get(res);
109 /** Obtains errors created during compilation.
111 public List<Diagnostic<? extends JavaFileObject>> getErrors() {
112 List<Diagnostic<? extends JavaFileObject>> err;
113 err = new ArrayList<Diagnostic<? extends JavaFileObject>>();
114 for (Diagnostic<? extends JavaFileObject> diagnostic : errors) {
115 if (diagnostic.getKind() == Diagnostic.Kind.ERROR) {
122 private Map<String, byte[]> compile(final String html, final String code) throws IOException {
123 StandardJavaFileManager sjfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(this, null, null);
125 final Map<String, ByteArrayOutputStream> class2BAOS;
126 class2BAOS = new HashMap<String, ByteArrayOutputStream>();
128 JavaFileObject file = new SimpleJavaFileObject(URI.create("mem://mem"), Kind.SOURCE) {
130 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
134 final JavaFileObject htmlFile = new SimpleJavaFileObject(URI.create("mem://mem2"), Kind.OTHER) {
136 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
141 public InputStream openInputStream() throws IOException {
142 return new ByteArrayInputStream(html.getBytes());
148 scratch = new URI("mem://mem3");
149 } catch (URISyntaxException ex) {
150 throw new IOException(ex);
153 JavaFileManager jfm = new ForwardingJavaFileManager<JavaFileManager>(sjfm) {
155 public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
156 if (kind == Kind.CLASS) {
157 final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
159 class2BAOS.put(className.replace('.', '/') + ".class", buffer);
160 return new SimpleJavaFileObject(sibling.toUri(), kind) {
162 public OutputStream openOutputStream() throws IOException {
168 if (kind == Kind.SOURCE) {
169 final String n = className.replace('.', '/') + ".java";
172 un = new URI("mem://" + n);
173 } catch (URISyntaxException ex) {
174 throw new IOException(ex);
176 return new VirtFO(un/*sibling.toUri()*/, kind, n);
179 throw new IllegalStateException();
183 public FileObject getFileForInput(Location location, String packageName, String relativeName) throws IOException {
184 if (location == StandardLocation.SOURCE_PATH) {
185 if (packageName.equals(pkg)) {
194 public boolean isSameFile(FileObject a, FileObject b) {
195 if (a instanceof VirtFO && b instanceof VirtFO) {
196 return ((VirtFO)a).getName().equals(((VirtFO)b).getName());
199 return super.isSameFile(a, b);
202 class VirtFO extends SimpleJavaFileObject {
204 private final String n;
206 public VirtFO(URI uri, Kind kind, String n) {
210 private final ByteArrayOutputStream data = new ByteArrayOutputStream();
213 public OutputStream openOutputStream() throws IOException {
218 public String getName() {
223 public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
225 return new String(data.toByteArray());
230 ToolProvider.getSystemJavaCompiler().getTask(null, jfm, this, /*XXX:*/Arrays.asList("-source", sourceLevel, "-target", "1.7"), null, Arrays.asList(file)).call();
232 Map<String, byte[]> result = new HashMap<String, byte[]>();
234 for (Map.Entry<String, ByteArrayOutputStream> e : class2BAOS.entrySet()) {
235 result.put(e.getKey(), e.getValue().toByteArray());
243 public void report(Diagnostic<? extends JavaFileObject> diagnostic) {
244 errors.add(diagnostic);
246 private static String findPkg(String java) throws IOException {
247 Pattern p = Pattern.compile("package\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}*;", Pattern.MULTILINE);
248 Matcher m = p.matcher(java);
250 throw new IOException("Can't find package declaration in the java file");
252 String pkg = m.group(1);
255 private static String findCls(String java) throws IOException {
256 Pattern p = Pattern.compile("class\\p{javaWhitespace}*([\\p{Alnum}\\.]+)\\p{javaWhitespace}", Pattern.MULTILINE);
257 Matcher m = p.matcher(java);
259 throw new IOException("Can't find package declaration in the java file");
261 String cls = m.group(1);
266 String fqn = "'" + pkg + '.' + cls + "'";
267 return html.replace("'${fqn}'", fqn);
269 void assertErrors() {
270 assertFalse(getErrors().isEmpty(), "There are supposed to be some errors");
273 void assertError(String expMsg) {
274 StringBuilder sb = new StringBuilder();
275 sb.append("Can't find ").append(expMsg).append(" among:");
276 for (Diagnostic<? extends JavaFileObject> e : errors) {
277 String msg = e.getMessage(Locale.US);
278 if (msg.contains(expMsg)) {