rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/GenerateZipProcessor.java
changeset 772 d382dacfd73f
parent 672 add357fd6c5c
child 1787 ea12a3bb4b33
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/rt/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/GenerateZipProcessor.java	Tue Feb 26 16:54:16 2013 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/**
     1.5 + * Back 2 Browser Bytecode Translator
     1.6 + * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     1.7 + *
     1.8 + * This program is free software: you can redistribute it and/or modify
     1.9 + * it under the terms of the GNU General Public License as published by
    1.10 + * the Free Software Foundation, version 2 of the License.
    1.11 + *
    1.12 + * This program is distributed in the hope that it will be useful,
    1.13 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.14 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.15 + * GNU General Public License for more details.
    1.16 + *
    1.17 + * You should have received a copy of the GNU General Public License
    1.18 + * along with this program. Look for COPYING file in the top folder.
    1.19 + * If not, see http://opensource.org/licenses/GPL-2.0.
    1.20 + */
    1.21 +package org.apidesign.bck2brwsr.vmtest.impl;
    1.22 +
    1.23 +import java.io.ByteArrayInputStream;
    1.24 +import java.io.IOException;
    1.25 +import java.io.OutputStream;
    1.26 +import java.util.Set;
    1.27 +import java.util.jar.JarEntry;
    1.28 +import java.util.jar.JarOutputStream;
    1.29 +import java.util.jar.Manifest;
    1.30 +import javax.annotation.processing.AbstractProcessor;
    1.31 +import javax.annotation.processing.Filer;
    1.32 +import javax.annotation.processing.Processor;
    1.33 +import javax.annotation.processing.RoundEnvironment;
    1.34 +import javax.annotation.processing.SupportedAnnotationTypes;
    1.35 +import javax.lang.model.element.Element;
    1.36 +import javax.lang.model.element.ElementKind;
    1.37 +import javax.lang.model.element.PackageElement;
    1.38 +import javax.lang.model.element.TypeElement;
    1.39 +import javax.tools.Diagnostic;
    1.40 +import javax.tools.FileObject;
    1.41 +import javax.tools.StandardLocation;
    1.42 +import org.openide.util.lookup.ServiceProvider;
    1.43 +
    1.44 +/**
    1.45 + *
    1.46 + * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.47 + */
    1.48 +@ServiceProvider(service = Processor.class)
    1.49 +@SupportedAnnotationTypes("org.apidesign.bck2brwsr.vmtest.impl.GenerateZip")
    1.50 +public class GenerateZipProcessor extends AbstractProcessor {
    1.51 +
    1.52 +    @Override
    1.53 +    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.54 +        for (Element e : roundEnv.getElementsAnnotatedWith(GenerateZip.class)) {
    1.55 +            GenerateZip gz = e.getAnnotation(GenerateZip.class);
    1.56 +            if (gz == null) {
    1.57 +                continue;
    1.58 +            }
    1.59 +            PackageElement pe = findPackage(e);
    1.60 +            try {
    1.61 +                generateJar(pe, gz, e);
    1.62 +            } catch (IOException ex) {
    1.63 +                processingEnv.getMessager().printMessage(
    1.64 +                    Diagnostic.Kind.ERROR, 
    1.65 +                    "Can't generate JAR " + gz.name() + ": " + ex.getMessage()
    1.66 +                );
    1.67 +            }
    1.68 +        }
    1.69 +        return true;
    1.70 +    }
    1.71 +
    1.72 +    private static PackageElement findPackage(Element e) {
    1.73 +        while (e.getKind() != ElementKind.PACKAGE) {
    1.74 +            e = e.getEnclosingElement();
    1.75 +        }
    1.76 +        return (PackageElement)e;
    1.77 +    }
    1.78 +
    1.79 +    private void generateJar(PackageElement pe, GenerateZip gz, Element e) throws IOException {
    1.80 +        final Filer filer = processingEnv.getFiler();
    1.81 +        FileObject res = filer.createResource(
    1.82 +            StandardLocation.CLASS_OUTPUT, 
    1.83 +            pe.getQualifiedName().toString(), 
    1.84 +            gz.name(), e
    1.85 +        );
    1.86 +        OutputStream os = res.openOutputStream();
    1.87 +        JarOutputStream jar;
    1.88 +        if (gz.manifest().isEmpty()) {
    1.89 +            jar = new JarOutputStream(os);
    1.90 +        } else {
    1.91 +            Manifest mf = new Manifest(new ByteArrayInputStream(gz.manifest().getBytes("UTF-8")));
    1.92 +            jar = new JarOutputStream(os, mf);
    1.93 +        }
    1.94 +        String[] arr = gz.contents();
    1.95 +        for (int i = 0; i < arr.length; i += 2) {
    1.96 +            JarEntry je = new JarEntry(arr[i]);
    1.97 +            jar.putNextEntry(je);
    1.98 +            jar.write(arr[i + 1].getBytes("UTF-8"));
    1.99 +            jar.closeEntry();
   1.100 +        }
   1.101 +        jar.close();
   1.102 +    }
   1.103 +
   1.104 +}