jaroslav@611
|
1 |
/**
|
jaroslav@611
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@611
|
3 |
* Copyright (C) 2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@611
|
4 |
*
|
jaroslav@611
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@611
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@611
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@611
|
8 |
*
|
jaroslav@611
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@611
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@611
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@611
|
12 |
* GNU General Public License for more details.
|
jaroslav@611
|
13 |
*
|
jaroslav@611
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@611
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@611
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@611
|
17 |
*/
|
jaroslav@611
|
18 |
package org.apidesign.bck2brwsr.vmtest.impl;
|
jaroslav@611
|
19 |
|
jaroslav@611
|
20 |
import java.io.ByteArrayInputStream;
|
jaroslav@611
|
21 |
import java.io.IOException;
|
jaroslav@611
|
22 |
import java.io.OutputStream;
|
jaroslav@611
|
23 |
import java.util.Set;
|
jaroslav@611
|
24 |
import java.util.jar.JarEntry;
|
jaroslav@611
|
25 |
import java.util.jar.JarOutputStream;
|
jaroslav@611
|
26 |
import java.util.jar.Manifest;
|
jaroslav@611
|
27 |
import javax.annotation.processing.AbstractProcessor;
|
jaroslav@672
|
28 |
import javax.annotation.processing.Filer;
|
jaroslav@611
|
29 |
import javax.annotation.processing.Processor;
|
jaroslav@611
|
30 |
import javax.annotation.processing.RoundEnvironment;
|
jaroslav@611
|
31 |
import javax.annotation.processing.SupportedAnnotationTypes;
|
jaroslav@611
|
32 |
import javax.lang.model.element.Element;
|
jaroslav@611
|
33 |
import javax.lang.model.element.ElementKind;
|
jaroslav@611
|
34 |
import javax.lang.model.element.PackageElement;
|
jaroslav@611
|
35 |
import javax.lang.model.element.TypeElement;
|
jaroslav@611
|
36 |
import javax.tools.Diagnostic;
|
jaroslav@611
|
37 |
import javax.tools.FileObject;
|
jaroslav@611
|
38 |
import javax.tools.StandardLocation;
|
jaroslav@611
|
39 |
import org.openide.util.lookup.ServiceProvider;
|
jaroslav@611
|
40 |
|
jaroslav@611
|
41 |
/**
|
jaroslav@611
|
42 |
*
|
jaroslav@611
|
43 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@611
|
44 |
*/
|
jaroslav@611
|
45 |
@ServiceProvider(service = Processor.class)
|
jaroslav@611
|
46 |
@SupportedAnnotationTypes("org.apidesign.bck2brwsr.vmtest.impl.GenerateZip")
|
jaroslav@611
|
47 |
public class GenerateZipProcessor extends AbstractProcessor {
|
jaroslav@611
|
48 |
|
jaroslav@611
|
49 |
@Override
|
jaroslav@611
|
50 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
jaroslav@611
|
51 |
for (Element e : roundEnv.getElementsAnnotatedWith(GenerateZip.class)) {
|
jaroslav@611
|
52 |
GenerateZip gz = e.getAnnotation(GenerateZip.class);
|
jaroslav@611
|
53 |
if (gz == null) {
|
jaroslav@611
|
54 |
continue;
|
jaroslav@611
|
55 |
}
|
jaroslav@611
|
56 |
PackageElement pe = findPackage(e);
|
jaroslav@611
|
57 |
try {
|
jaroslav@611
|
58 |
generateJar(pe, gz, e);
|
jaroslav@611
|
59 |
} catch (IOException ex) {
|
jaroslav@611
|
60 |
processingEnv.getMessager().printMessage(
|
jaroslav@611
|
61 |
Diagnostic.Kind.ERROR,
|
jaroslav@611
|
62 |
"Can't generate JAR " + gz.name() + ": " + ex.getMessage()
|
jaroslav@611
|
63 |
);
|
jaroslav@611
|
64 |
}
|
jaroslav@611
|
65 |
}
|
jaroslav@611
|
66 |
return true;
|
jaroslav@611
|
67 |
}
|
jaroslav@611
|
68 |
|
jaroslav@611
|
69 |
private static PackageElement findPackage(Element e) {
|
jaroslav@611
|
70 |
while (e.getKind() != ElementKind.PACKAGE) {
|
jaroslav@611
|
71 |
e = e.getEnclosingElement();
|
jaroslav@611
|
72 |
}
|
jaroslav@611
|
73 |
return (PackageElement)e;
|
jaroslav@611
|
74 |
}
|
jaroslav@611
|
75 |
|
jaroslav@611
|
76 |
private void generateJar(PackageElement pe, GenerateZip gz, Element e) throws IOException {
|
jaroslav@672
|
77 |
final Filer filer = processingEnv.getFiler();
|
jaroslav@672
|
78 |
FileObject res = filer.createResource(
|
jaroslav@611
|
79 |
StandardLocation.CLASS_OUTPUT,
|
jaroslav@611
|
80 |
pe.getQualifiedName().toString(),
|
jaroslav@611
|
81 |
gz.name(), e
|
jaroslav@611
|
82 |
);
|
jaroslav@611
|
83 |
OutputStream os = res.openOutputStream();
|
jaroslav@611
|
84 |
JarOutputStream jar;
|
jaroslav@611
|
85 |
if (gz.manifest().isEmpty()) {
|
jaroslav@611
|
86 |
jar = new JarOutputStream(os);
|
jaroslav@611
|
87 |
} else {
|
jaroslav@611
|
88 |
Manifest mf = new Manifest(new ByteArrayInputStream(gz.manifest().getBytes("UTF-8")));
|
jaroslav@611
|
89 |
jar = new JarOutputStream(os, mf);
|
jaroslav@611
|
90 |
}
|
jaroslav@611
|
91 |
String[] arr = gz.contents();
|
jaroslav@611
|
92 |
for (int i = 0; i < arr.length; i += 2) {
|
jaroslav@611
|
93 |
JarEntry je = new JarEntry(arr[i]);
|
jaroslav@611
|
94 |
jar.putNextEntry(je);
|
jaroslav@611
|
95 |
jar.write(arr[i + 1].getBytes("UTF-8"));
|
jaroslav@611
|
96 |
jar.closeEntry();
|
jaroslav@611
|
97 |
}
|
jaroslav@672
|
98 |
jar.close();
|
jaroslav@611
|
99 |
}
|
jaroslav@672
|
100 |
|
jaroslav@611
|
101 |
}
|