vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/GenerateZipProcessor.java
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.vmtest.impl;
20 import java.io.ByteArrayInputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
24 import java.util.jar.JarEntry;
25 import java.util.jar.JarOutputStream;
26 import java.util.jar.Manifest;
27 import javax.annotation.processing.AbstractProcessor;
28 import javax.annotation.processing.Processor;
29 import javax.annotation.processing.RoundEnvironment;
30 import javax.annotation.processing.SupportedAnnotationTypes;
31 import javax.lang.model.element.Element;
32 import javax.lang.model.element.ElementKind;
33 import javax.lang.model.element.PackageElement;
34 import javax.lang.model.element.TypeElement;
35 import javax.tools.Diagnostic;
36 import javax.tools.FileObject;
37 import javax.tools.StandardLocation;
38 import org.openide.util.lookup.ServiceProvider;
42 * @author Jaroslav Tulach <jtulach@netbeans.org>
44 @ServiceProvider(service = Processor.class)
45 @SupportedAnnotationTypes("org.apidesign.bck2brwsr.vmtest.impl.GenerateZip")
46 public class GenerateZipProcessor extends AbstractProcessor {
49 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
50 for (Element e : roundEnv.getElementsAnnotatedWith(GenerateZip.class)) {
51 GenerateZip gz = e.getAnnotation(GenerateZip.class);
55 PackageElement pe = findPackage(e);
57 generateJar(pe, gz, e);
58 } catch (IOException ex) {
59 processingEnv.getMessager().printMessage(
60 Diagnostic.Kind.ERROR,
61 "Can't generate JAR " + gz.name() + ": " + ex.getMessage()
68 private static PackageElement findPackage(Element e) {
69 while (e.getKind() != ElementKind.PACKAGE) {
70 e = e.getEnclosingElement();
72 return (PackageElement)e;
75 private void generateJar(PackageElement pe, GenerateZip gz, Element e) throws IOException {
76 FileObject res = processingEnv.getFiler().createResource(
77 StandardLocation.CLASS_OUTPUT,
78 pe.getQualifiedName().toString(),
81 OutputStream os = res.openOutputStream();
83 if (gz.manifest().isEmpty()) {
84 jar = new JarOutputStream(os);
86 Manifest mf = new Manifest(new ByteArrayInputStream(gz.manifest().getBytes("UTF-8")));
87 jar = new JarOutputStream(os, mf);
89 String[] arr = gz.contents();
90 for (int i = 0; i < arr.length; i += 2) {
91 JarEntry je = new JarEntry(arr[i]);
93 jar.write(arr[i + 1].getBytes("UTF-8"));