jaroslav@26
|
1 |
/**
|
jaroslav@26
|
2 |
* Java 4 Browser Bytecode Translator
|
jaroslav@26
|
3 |
* Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
|
jaroslav@26
|
4 |
*
|
jaroslav@26
|
5 |
* This program is free software: you can redistribute it and/or modify
|
jaroslav@26
|
6 |
* it under the terms of the GNU General Public License as published by
|
jaroslav@26
|
7 |
* the Free Software Foundation, version 2 of the License.
|
jaroslav@26
|
8 |
*
|
jaroslav@26
|
9 |
* This program is distributed in the hope that it will be useful,
|
jaroslav@26
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
jaroslav@26
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
jaroslav@26
|
12 |
* GNU General Public License for more details.
|
jaroslav@26
|
13 |
*
|
jaroslav@26
|
14 |
* You should have received a copy of the GNU General Public License
|
jaroslav@26
|
15 |
* along with this program. Look for COPYING file in the top folder.
|
jaroslav@26
|
16 |
* If not, see http://opensource.org/licenses/GPL-2.0.
|
jaroslav@26
|
17 |
*/
|
jaroslav@26
|
18 |
package org.apidesign.bck2brwsr.htmlpage;
|
jaroslav@26
|
19 |
|
jaroslav@26
|
20 |
import java.io.IOException;
|
jaroslav@26
|
21 |
import java.io.InputStream;
|
jaroslav@26
|
22 |
import java.io.OutputStreamWriter;
|
jaroslav@26
|
23 |
import java.io.Writer;
|
jaroslav@26
|
24 |
import java.util.Locale;
|
jaroslav@26
|
25 |
import java.util.Set;
|
jaroslav@26
|
26 |
import javax.annotation.processing.AbstractProcessor;
|
jaroslav@26
|
27 |
import javax.annotation.processing.Processor;
|
jaroslav@26
|
28 |
import javax.annotation.processing.RoundEnvironment;
|
jaroslav@26
|
29 |
import javax.annotation.processing.SupportedAnnotationTypes;
|
jaroslav@26
|
30 |
import javax.lang.model.element.Element;
|
jaroslav@26
|
31 |
import javax.lang.model.element.PackageElement;
|
jaroslav@26
|
32 |
import javax.lang.model.element.TypeElement;
|
jaroslav@26
|
33 |
import javax.tools.Diagnostic;
|
jaroslav@26
|
34 |
import javax.tools.FileObject;
|
jaroslav@26
|
35 |
import javax.tools.StandardLocation;
|
jaroslav@26
|
36 |
import org.apidesign.bck2brwsr.htmlpage.api.Page;
|
jaroslav@26
|
37 |
import org.openide.util.lookup.ServiceProvider;
|
jaroslav@26
|
38 |
|
jaroslav@26
|
39 |
/** Annotation processor to process an XHTML page and generate appropriate
|
jaroslav@26
|
40 |
* "id" file.
|
jaroslav@26
|
41 |
*
|
jaroslav@26
|
42 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@26
|
43 |
*/
|
jaroslav@26
|
44 |
@ServiceProvider(service=Processor.class)
|
jaroslav@26
|
45 |
@SupportedAnnotationTypes("org.apidesign.bck2brwsr.htmlpage.api.Page")
|
jaroslav@26
|
46 |
public final class PageProcessor extends AbstractProcessor {
|
jaroslav@26
|
47 |
@Override
|
jaroslav@26
|
48 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
jaroslav@26
|
49 |
for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
|
jaroslav@26
|
50 |
Page p = e.getAnnotation(Page.class);
|
jaroslav@26
|
51 |
PackageElement pe = (PackageElement)e;
|
jaroslav@26
|
52 |
String pkg = pe.getQualifiedName().toString();
|
jaroslav@26
|
53 |
|
jaroslav@26
|
54 |
ProcessPage pp;
|
jaroslav@26
|
55 |
try {
|
jaroslav@26
|
56 |
InputStream is = openStream(pkg, p.xhtml());
|
jaroslav@26
|
57 |
pp = ProcessPage.readPage(is);
|
jaroslav@26
|
58 |
is.close();
|
jaroslav@26
|
59 |
} catch (IOException iOException) {
|
jaroslav@26
|
60 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e);
|
jaroslav@26
|
61 |
return false;
|
jaroslav@26
|
62 |
}
|
jaroslav@26
|
63 |
Writer w;
|
jaroslav@26
|
64 |
try {
|
jaroslav@26
|
65 |
FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + p.name(), e);
|
jaroslav@26
|
66 |
w = new OutputStreamWriter(java.openOutputStream());
|
jaroslav@26
|
67 |
w.append("package " + pkg + ";\n");
|
jaroslav@26
|
68 |
w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
|
jaroslav@26
|
69 |
w.append("class ").append(p.name()).append(" {\n");
|
jaroslav@26
|
70 |
for (String id : pp.ids()) {
|
jaroslav@26
|
71 |
String tag = pp.tagNameForId(id);
|
jaroslav@26
|
72 |
String type = type(tag);
|
jaroslav@26
|
73 |
w.append(" ").append("public static final ").
|
jaroslav@26
|
74 |
append(type).append(' ').append(cnstnt(id)).append(" = new ").
|
jaroslav@26
|
75 |
append(type).append("(\"").append(id).append("\");\n");
|
jaroslav@26
|
76 |
}
|
jaroslav@26
|
77 |
w.append("}");
|
jaroslav@26
|
78 |
w.close();
|
jaroslav@26
|
79 |
} catch (IOException ex) {
|
jaroslav@26
|
80 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + p.name() + ".java", e);
|
jaroslav@26
|
81 |
return false;
|
jaroslav@26
|
82 |
}
|
jaroslav@26
|
83 |
}
|
jaroslav@26
|
84 |
return true;
|
jaroslav@26
|
85 |
}
|
jaroslav@26
|
86 |
|
jaroslav@26
|
87 |
private InputStream openStream(String pkg, String name) throws IOException {
|
jaroslav@26
|
88 |
FileObject fo = processingEnv.getFiler().getResource(
|
jaroslav@26
|
89 |
StandardLocation.SOURCE_PATH, pkg, name);
|
jaroslav@26
|
90 |
try {
|
jaroslav@26
|
91 |
return fo.openInputStream();
|
jaroslav@26
|
92 |
} catch (IOException ex) {
|
jaroslav@26
|
93 |
return processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, name).openInputStream();
|
jaroslav@26
|
94 |
}
|
jaroslav@26
|
95 |
}
|
jaroslav@26
|
96 |
|
jaroslav@26
|
97 |
private static String type(String tag) {
|
jaroslav@26
|
98 |
if (tag.equals("title")) {
|
jaroslav@26
|
99 |
return "Title";
|
jaroslav@26
|
100 |
}
|
jaroslav@26
|
101 |
if (tag.equals("button")) {
|
jaroslav@26
|
102 |
return "Button";
|
jaroslav@26
|
103 |
}
|
jaroslav@26
|
104 |
if (tag.equals("input")) {
|
jaroslav@26
|
105 |
return "Input";
|
jaroslav@26
|
106 |
}
|
jaroslav@26
|
107 |
return "Element";
|
jaroslav@26
|
108 |
}
|
jaroslav@26
|
109 |
|
jaroslav@26
|
110 |
private static String cnstnt(String id) {
|
jaroslav@26
|
111 |
return id.toUpperCase(Locale.ENGLISH).replace('.', '_');
|
jaroslav@26
|
112 |
}
|
jaroslav@26
|
113 |
}
|