vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/GenerateZipProcessor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Thu, 07 Feb 2013 12:58:12 +0100
branchemul
changeset 694 0d277415ed02
parent 611 9839e9a75bcf
permissions -rw-r--r--
Rebasing the Inflater support on jzlib which, unlike GNU ClassPath, has correct implementation of Huffman code. Making the implementation more easily testable by turning Inflater and ZipInputStream into pure delegates. Current implementation is going to need proper long support.
     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.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;
    40 
    41 /**
    42  *
    43  * @author Jaroslav Tulach <jtulach@netbeans.org>
    44  */
    45 @ServiceProvider(service = Processor.class)
    46 @SupportedAnnotationTypes("org.apidesign.bck2brwsr.vmtest.impl.GenerateZip")
    47 public class GenerateZipProcessor extends AbstractProcessor {
    48 
    49     @Override
    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);
    53             if (gz == null) {
    54                 continue;
    55             }
    56             PackageElement pe = findPackage(e);
    57             try {
    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()
    63                 );
    64             }
    65         }
    66         return true;
    67     }
    68 
    69     private static PackageElement findPackage(Element e) {
    70         while (e.getKind() != ElementKind.PACKAGE) {
    71             e = e.getEnclosingElement();
    72         }
    73         return (PackageElement)e;
    74     }
    75 
    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(), 
    81             gz.name(), e
    82         );
    83         OutputStream os = res.openOutputStream();
    84         JarOutputStream jar;
    85         if (gz.manifest().isEmpty()) {
    86             jar = new JarOutputStream(os);
    87         } else {
    88             Manifest mf = new Manifest(new ByteArrayInputStream(gz.manifest().getBytes("UTF-8")));
    89             jar = new JarOutputStream(os, mf);
    90         }
    91         String[] arr = gz.contents();
    92         for (int i = 0; i < arr.length; i += 2) {
    93             JarEntry je = new JarEntry(arr[i]);
    94             jar.putNextEntry(je);
    95             jar.write(arr[i + 1].getBytes("UTF-8"));
    96             jar.closeEntry();
    97         }
    98         jar.close();
    99     }
   100 
   101 }