From d9ee68315e52f309cd65acc0d4b16fa4d7f35476 Mon Sep 17 00:00:00 2001 From: Jaroslav Tulach Date: Thu, 24 Dec 2015 07:33:52 +0100 Subject: [PATCH] Can include resources in the generated file --- .../java/org/teavm/html4j/HTML4JPlugin.java | 1 + .../teavm/html4j/ResourcesInterceptor.java | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 html4j/src/main/java/org/teavm/html4j/ResourcesInterceptor.java diff --git a/html4j/src/main/java/org/teavm/html4j/HTML4JPlugin.java b/html4j/src/main/java/org/teavm/html4j/HTML4JPlugin.java index 1530d308d..118a3ab80 100644 --- a/html4j/src/main/java/org/teavm/html4j/HTML4JPlugin.java +++ b/html4j/src/main/java/org/teavm/html4j/HTML4JPlugin.java @@ -29,5 +29,6 @@ public class HTML4JPlugin implements TeaVMPlugin { host.add(new JavaScriptBodyTransformer()); host.add(new JCLHacks()); host.add(new JavaScriptResourceInterceptor()); + host.add(new ResourcesInterceptor()); } } diff --git a/html4j/src/main/java/org/teavm/html4j/ResourcesInterceptor.java b/html4j/src/main/java/org/teavm/html4j/ResourcesInterceptor.java new file mode 100644 index 000000000..6c7cc0715 --- /dev/null +++ b/html4j/src/main/java/org/teavm/html4j/ResourcesInterceptor.java @@ -0,0 +1,66 @@ +/* + * Copyright 2014 Alexey Andreev. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.teavm.html4j; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.util.Base64; +import java.util.HashSet; +import java.util.Set; +import org.apache.commons.io.IOUtils; +import org.teavm.codegen.SourceWriter; +import org.teavm.javascript.RenderingContext; +import org.teavm.vm.BuildTarget; +import org.teavm.vm.spi.AbstractRendererListener; + +/** + * + * @author Jaroslav Tulach + */ +public class ResourcesInterceptor extends AbstractRendererListener { + private final Set processed = new HashSet<>(); + @Override + public void begin(RenderingContext context, BuildTarget buildTarget) throws IOException { + boolean hasOneResource = false; + for (String className : context.getClassSource().getClassNames()) { + final int lastDot = className.lastIndexOf('.'); + if (lastDot == -1) { + continue; + } + String packageName = className.substring(0, lastDot); + String resourceName = packageName.replace('.', '/') + "/" + "jvm.txt"; + try (InputStream input = context.getClassLoader().getResourceAsStream(resourceName)) { + if (input == null || !processed.add(resourceName)) { + continue; + } + ByteArrayOutputStream arr = new ByteArrayOutputStream(); + IOUtils.copy(input, arr); + String base64 = Base64.getEncoder().encodeToString(arr.toByteArray()); + input.close(); + final SourceWriter w = context.getWriter(); + w.append("// Resource " + resourceName + " included by " + className).newLine(); + w.append("if (!window.teaVMResources) window.teaVMResources = {};").newLine(); + w.append("window.teaVMResources['" + resourceName + "'] = '"); + w.append(base64).append("';").newLine().newLine(); + } + hasOneResource = true; + } + if (hasOneResource) { + context.getWriter().append("// TeaVM generated classes").newLine(); + } + } +}