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@28
|
31 |
import javax.lang.model.element.ElementKind;
|
jaroslav@28
|
32 |
import javax.lang.model.element.ExecutableElement;
|
jaroslav@28
|
33 |
import javax.lang.model.element.Modifier;
|
jaroslav@26
|
34 |
import javax.lang.model.element.PackageElement;
|
jaroslav@26
|
35 |
import javax.lang.model.element.TypeElement;
|
jaroslav@26
|
36 |
import javax.tools.Diagnostic;
|
jaroslav@26
|
37 |
import javax.tools.FileObject;
|
jaroslav@26
|
38 |
import javax.tools.StandardLocation;
|
jaroslav@28
|
39 |
import org.apidesign.bck2brwsr.htmlpage.api.OnClick;
|
jaroslav@26
|
40 |
import org.apidesign.bck2brwsr.htmlpage.api.Page;
|
jaroslav@26
|
41 |
import org.openide.util.lookup.ServiceProvider;
|
jaroslav@26
|
42 |
|
jaroslav@26
|
43 |
/** Annotation processor to process an XHTML page and generate appropriate
|
jaroslav@26
|
44 |
* "id" file.
|
jaroslav@26
|
45 |
*
|
jaroslav@26
|
46 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@26
|
47 |
*/
|
jaroslav@26
|
48 |
@ServiceProvider(service=Processor.class)
|
jaroslav@28
|
49 |
@SupportedAnnotationTypes({
|
jaroslav@28
|
50 |
"org.apidesign.bck2brwsr.htmlpage.api.Page",
|
jaroslav@28
|
51 |
"org.apidesign.bck2brwsr.htmlpage.api.OnClick"
|
jaroslav@28
|
52 |
})
|
jaroslav@26
|
53 |
public final class PageProcessor extends AbstractProcessor {
|
jaroslav@26
|
54 |
@Override
|
jaroslav@26
|
55 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
jaroslav@26
|
56 |
for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
|
jaroslav@26
|
57 |
Page p = e.getAnnotation(Page.class);
|
jaroslav@100
|
58 |
PackageElement pe = (PackageElement)e.getEnclosingElement();
|
jaroslav@26
|
59 |
String pkg = pe.getQualifiedName().toString();
|
jaroslav@26
|
60 |
|
jaroslav@26
|
61 |
ProcessPage pp;
|
jaroslav@26
|
62 |
try {
|
jaroslav@26
|
63 |
InputStream is = openStream(pkg, p.xhtml());
|
jaroslav@26
|
64 |
pp = ProcessPage.readPage(is);
|
jaroslav@26
|
65 |
is.close();
|
jaroslav@26
|
66 |
} catch (IOException iOException) {
|
jaroslav@26
|
67 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e);
|
jaroslav@26
|
68 |
return false;
|
jaroslav@26
|
69 |
}
|
jaroslav@26
|
70 |
Writer w;
|
jaroslav@26
|
71 |
try {
|
jaroslav@26
|
72 |
FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + p.name(), e);
|
jaroslav@26
|
73 |
w = new OutputStreamWriter(java.openOutputStream());
|
jaroslav@28
|
74 |
try {
|
jaroslav@28
|
75 |
w.append("package " + pkg + ";\n");
|
jaroslav@28
|
76 |
w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
|
jaroslav@28
|
77 |
w.append("class ").append(p.name()).append(" {\n");
|
jaroslav@28
|
78 |
for (String id : pp.ids()) {
|
jaroslav@28
|
79 |
String tag = pp.tagNameForId(id);
|
jaroslav@28
|
80 |
String type = type(tag);
|
jaroslav@28
|
81 |
w.append(" ").append("public static final ").
|
jaroslav@28
|
82 |
append(type).append(' ').append(cnstnt(id)).append(" = new ").
|
jaroslav@28
|
83 |
append(type).append("(\"").append(id).append("\");\n");
|
jaroslav@28
|
84 |
}
|
jaroslav@28
|
85 |
w.append(" static {\n");
|
jaroslav@28
|
86 |
if (!initializeOnClick(pe, w, pp)) {
|
jaroslav@28
|
87 |
return false;
|
jaroslav@28
|
88 |
}
|
jaroslav@28
|
89 |
w.append(" }\n");
|
jaroslav@28
|
90 |
w.append("}\n");
|
jaroslav@28
|
91 |
} finally {
|
jaroslav@28
|
92 |
w.close();
|
jaroslav@26
|
93 |
}
|
jaroslav@26
|
94 |
} catch (IOException ex) {
|
jaroslav@26
|
95 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + p.name() + ".java", e);
|
jaroslav@26
|
96 |
return false;
|
jaroslav@26
|
97 |
}
|
jaroslav@26
|
98 |
}
|
jaroslav@26
|
99 |
return true;
|
jaroslav@26
|
100 |
}
|
jaroslav@26
|
101 |
|
jaroslav@26
|
102 |
private InputStream openStream(String pkg, String name) throws IOException {
|
jaroslav@26
|
103 |
FileObject fo = processingEnv.getFiler().getResource(
|
jaroslav@26
|
104 |
StandardLocation.SOURCE_PATH, pkg, name);
|
jaroslav@26
|
105 |
try {
|
jaroslav@26
|
106 |
return fo.openInputStream();
|
jaroslav@26
|
107 |
} catch (IOException ex) {
|
jaroslav@26
|
108 |
return processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, name).openInputStream();
|
jaroslav@26
|
109 |
}
|
jaroslav@26
|
110 |
}
|
jaroslav@26
|
111 |
|
jaroslav@26
|
112 |
private static String type(String tag) {
|
jaroslav@26
|
113 |
if (tag.equals("title")) {
|
jaroslav@26
|
114 |
return "Title";
|
jaroslav@26
|
115 |
}
|
jaroslav@26
|
116 |
if (tag.equals("button")) {
|
jaroslav@26
|
117 |
return "Button";
|
jaroslav@26
|
118 |
}
|
jaroslav@26
|
119 |
if (tag.equals("input")) {
|
jaroslav@26
|
120 |
return "Input";
|
jaroslav@26
|
121 |
}
|
jaroslav@26
|
122 |
return "Element";
|
jaroslav@26
|
123 |
}
|
jaroslav@26
|
124 |
|
jaroslav@26
|
125 |
private static String cnstnt(String id) {
|
jaroslav@26
|
126 |
return id.toUpperCase(Locale.ENGLISH).replace('.', '_');
|
jaroslav@26
|
127 |
}
|
jaroslav@28
|
128 |
|
jaroslav@28
|
129 |
private boolean initializeOnClick(PackageElement pe, Writer w, ProcessPage pp) throws IOException {
|
jaroslav@28
|
130 |
for (Element clazz : pe.getEnclosedElements()) {
|
jaroslav@28
|
131 |
if (clazz.getKind() != ElementKind.CLASS) {
|
jaroslav@28
|
132 |
continue;
|
jaroslav@28
|
133 |
}
|
jaroslav@28
|
134 |
TypeElement type = (TypeElement)clazz;
|
jaroslav@28
|
135 |
for (Element method : clazz.getEnclosedElements()) {
|
jaroslav@28
|
136 |
OnClick oc = method.getAnnotation(OnClick.class);
|
jaroslav@28
|
137 |
if (oc != null) {
|
jaroslav@28
|
138 |
if (pp.tagNameForId(oc.id()) == null) {
|
jaroslav@28
|
139 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + oc.id() + " does not exist in the HTML page. Found only " + pp.ids(), method);
|
jaroslav@28
|
140 |
return false;
|
jaroslav@28
|
141 |
}
|
jaroslav@28
|
142 |
ExecutableElement ee = (ExecutableElement)method;
|
jaroslav@28
|
143 |
if (!ee.getParameters().isEmpty()) {
|
jaroslav@28
|
144 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@OnClose method can't take arguments", ee);
|
jaroslav@28
|
145 |
return false;
|
jaroslav@28
|
146 |
}
|
jaroslav@28
|
147 |
if (!ee.getModifiers().contains(Modifier.STATIC)) {
|
jaroslav@28
|
148 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@OnClose method has to be static", ee);
|
jaroslav@28
|
149 |
return false;
|
jaroslav@28
|
150 |
}
|
jaroslav@28
|
151 |
if (ee.getModifiers().contains(Modifier.PRIVATE)) {
|
jaroslav@28
|
152 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@OnClose method can't be private", ee);
|
jaroslav@28
|
153 |
return false;
|
jaroslav@28
|
154 |
}
|
jaroslav@28
|
155 |
w.append(" ").append(cnstnt(oc.id())).
|
jaroslav@28
|
156 |
append(".addOnClick(new Runnable() { public void run() {\n");
|
jaroslav@28
|
157 |
w.append(" ").append(type.getSimpleName().toString()).
|
jaroslav@28
|
158 |
append('.').append(ee.getSimpleName()).append("();\n");
|
jaroslav@28
|
159 |
w.append(" }});\n");
|
jaroslav@28
|
160 |
}
|
jaroslav@28
|
161 |
}
|
jaroslav@28
|
162 |
}
|
jaroslav@28
|
163 |
return true;
|
jaroslav@28
|
164 |
}
|
jaroslav@26
|
165 |
}
|