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.Filer;
29 import javax.annotation.processing.Processor;
30 import javax.annotation.processing.RoundEnvironment;
31 import javax.annotation.processing.SupportedAnnotationTypes;
32 import javax.lang.model.element.Element;
33 import javax.lang.model.element.ElementKind;
34 import javax.lang.model.element.PackageElement;
35 import javax.lang.model.element.TypeElement;
36 import javax.tools.Diagnostic;
37 import javax.tools.FileObject;
38 import javax.tools.StandardLocation;
39 import org.openide.util.lookup.ServiceProvider;
43 * @author Jaroslav Tulach <jtulach@netbeans.org>
45 @ServiceProvider(service = Processor.class)
46 @SupportedAnnotationTypes("org.apidesign.bck2brwsr.vmtest.impl.GenerateZip")
47 public class GenerateZipProcessor extends AbstractProcessor {
50 public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
51 for (Element e : roundEnv.getElementsAnnotatedWith(GenerateZip.class)) {
52 GenerateZip gz = e.getAnnotation(GenerateZip.class);
56 PackageElement pe = findPackage(e);
58 generateJar(pe, gz, e);
59 } catch (IOException ex) {
60 processingEnv.getMessager().printMessage(
61 Diagnostic.Kind.ERROR,
62 "Can't generate JAR " + gz.name() + ": " + ex.getMessage()
69 private static PackageElement findPackage(Element e) {
70 while (e.getKind() != ElementKind.PACKAGE) {
71 e = e.getEnclosingElement();
73 return (PackageElement)e;
76 private void generateJar(PackageElement pe, GenerateZip gz, Element e) throws IOException {
77 final Filer filer = processingEnv.getFiler();
78 FileObject res = filer.createResource(
79 StandardLocation.CLASS_OUTPUT,
80 pe.getQualifiedName().toString(),
83 OutputStream os = res.openOutputStream();
85 if (gz.manifest().isEmpty()) {
86 jar = new JarOutputStream(os);
88 Manifest mf = new Manifest(new ByteArrayInputStream(gz.manifest().getBytes("UTF-8")));
89 jar = new JarOutputStream(os, mf);
91 String[] arr = gz.contents();
92 for (int i = 0; i < arr.length; i += 2) {
93 JarEntry je = new JarEntry(arr[i]);
95 jar.write(arr[i + 1].getBytes("UTF-8"));