vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/GenerateZipProcessor.java
1.1 --- a/vmtest/src/main/java/org/apidesign/bck2brwsr/vmtest/impl/GenerateZipProcessor.java Thu Feb 14 12:06:16 2013 +0100
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,98 +0,0 @@
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.Processor;
1.32 -import javax.annotation.processing.RoundEnvironment;
1.33 -import javax.annotation.processing.SupportedAnnotationTypes;
1.34 -import javax.lang.model.element.Element;
1.35 -import javax.lang.model.element.ElementKind;
1.36 -import javax.lang.model.element.PackageElement;
1.37 -import javax.lang.model.element.TypeElement;
1.38 -import javax.tools.Diagnostic;
1.39 -import javax.tools.FileObject;
1.40 -import javax.tools.StandardLocation;
1.41 -import org.openide.util.lookup.ServiceProvider;
1.42 -
1.43 -/**
1.44 - *
1.45 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.46 - */
1.47 -@ServiceProvider(service = Processor.class)
1.48 -@SupportedAnnotationTypes("org.apidesign.bck2brwsr.vmtest.impl.GenerateZip")
1.49 -public class GenerateZipProcessor extends AbstractProcessor {
1.50 -
1.51 - @Override
1.52 - public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
1.53 - for (Element e : roundEnv.getElementsAnnotatedWith(GenerateZip.class)) {
1.54 - GenerateZip gz = e.getAnnotation(GenerateZip.class);
1.55 - if (gz == null) {
1.56 - continue;
1.57 - }
1.58 - PackageElement pe = findPackage(e);
1.59 - try {
1.60 - generateJar(pe, gz, e);
1.61 - } catch (IOException ex) {
1.62 - processingEnv.getMessager().printMessage(
1.63 - Diagnostic.Kind.ERROR,
1.64 - "Can't generate JAR " + gz.name() + ": " + ex.getMessage()
1.65 - );
1.66 - }
1.67 - }
1.68 - return true;
1.69 - }
1.70 -
1.71 - private static PackageElement findPackage(Element e) {
1.72 - while (e.getKind() != ElementKind.PACKAGE) {
1.73 - e = e.getEnclosingElement();
1.74 - }
1.75 - return (PackageElement)e;
1.76 - }
1.77 -
1.78 - private void generateJar(PackageElement pe, GenerateZip gz, Element e) throws IOException {
1.79 - FileObject res = processingEnv.getFiler().createResource(
1.80 - StandardLocation.CLASS_OUTPUT,
1.81 - pe.getQualifiedName().toString(),
1.82 - gz.name(), e
1.83 - );
1.84 - OutputStream os = res.openOutputStream();
1.85 - JarOutputStream jar;
1.86 - if (gz.manifest().isEmpty()) {
1.87 - jar = new JarOutputStream(os);
1.88 - } else {
1.89 - Manifest mf = new Manifest(new ByteArrayInputStream(gz.manifest().getBytes("UTF-8")));
1.90 - jar = new JarOutputStream(os, mf);
1.91 - }
1.92 - String[] arr = gz.contents();
1.93 - for (int i = 0; i < arr.length; i += 2) {
1.94 - JarEntry je = new JarEntry(arr[i]);
1.95 - jar.putNextEntry(je);
1.96 - jar.write(arr[i + 1].getBytes("UTF-8"));
1.97 - jar.closeEntry();
1.98 - }
1.99 - }
1.100 -
1.101 -}