vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/GenerateZipProcessor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Wed, 30 Jan 2013 14:03:49 +0100
branchemul
changeset 611 9839e9a75bcf
child 672 add357fd6c5c
permissions -rw-r--r--
Implementation of ZipInputStream
     1 /**
     2  * Back 2 Browser Bytecode Translator
     3  * Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     4  *
     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.
     8  *
     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.
    13  *
    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.
    17  */
    18 package org.apidesign.bck2brwsr.vmtest.impl;
    19 
    20 import java.io.ByteArrayInputStream;
    21 import java.io.IOException;
    22 import java.io.OutputStream;
    23 import java.util.Set;
    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;
    39 
    40 /**
    41  *
    42  * @author Jaroslav Tulach <jtulach@netbeans.org>
    43  */
    44 @ServiceProvider(service = Processor.class)
    45 @SupportedAnnotationTypes("org.apidesign.bck2brwsr.vmtest.impl.GenerateZip")
    46 public class GenerateZipProcessor extends AbstractProcessor {
    47 
    48     @Override
    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);
    52             if (gz == null) {
    53                 continue;
    54             }
    55             PackageElement pe = findPackage(e);
    56             try {
    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()
    62                 );
    63             }
    64         }
    65         return true;
    66     }
    67 
    68     private static PackageElement findPackage(Element e) {
    69         while (e.getKind() != ElementKind.PACKAGE) {
    70             e = e.getEnclosingElement();
    71         }
    72         return (PackageElement)e;
    73     }
    74 
    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(), 
    79             gz.name(), e
    80         );
    81         OutputStream os = res.openOutputStream();
    82         JarOutputStream jar;
    83         if (gz.manifest().isEmpty()) {
    84             jar = new JarOutputStream(os);
    85         } else {
    86             Manifest mf = new Manifest(new ByteArrayInputStream(gz.manifest().getBytes("UTF-8")));
    87             jar = new JarOutputStream(os, mf);
    88         }
    89         String[] arr = gz.contents();
    90         for (int i = 0; i < arr.length; i += 2) {
    91             JarEntry je = new JarEntry(arr[i]);
    92             jar.putNextEntry(je);
    93             jar.write(arr[i + 1].getBytes("UTF-8"));
    94             jar.closeEntry();
    95         }
    96     }
    97     
    98 }