jaroslav@26
|
1 |
/**
|
jaroslav@106
|
2 |
* Back 2 Browser Bytecode Translator
|
jaroslav@106
|
3 |
* Copyright (C) 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@117
|
24 |
import java.util.ArrayList;
|
jaroslav@117
|
25 |
import java.util.Collections;
|
jaroslav@117
|
26 |
import java.util.List;
|
jaroslav@26
|
27 |
import java.util.Locale;
|
jaroslav@26
|
28 |
import java.util.Set;
|
jaroslav@26
|
29 |
import javax.annotation.processing.AbstractProcessor;
|
jaroslav@117
|
30 |
import javax.annotation.processing.Completion;
|
jaroslav@117
|
31 |
import javax.annotation.processing.Completions;
|
jaroslav@26
|
32 |
import javax.annotation.processing.Processor;
|
jaroslav@26
|
33 |
import javax.annotation.processing.RoundEnvironment;
|
jaroslav@26
|
34 |
import javax.annotation.processing.SupportedAnnotationTypes;
|
jaroslav@117
|
35 |
import javax.lang.model.element.AnnotationMirror;
|
jaroslav@26
|
36 |
import javax.lang.model.element.Element;
|
jaroslav@28
|
37 |
import javax.lang.model.element.ElementKind;
|
jaroslav@28
|
38 |
import javax.lang.model.element.ExecutableElement;
|
jaroslav@28
|
39 |
import javax.lang.model.element.Modifier;
|
jaroslav@26
|
40 |
import javax.lang.model.element.PackageElement;
|
jaroslav@26
|
41 |
import javax.lang.model.element.TypeElement;
|
jaroslav@124
|
42 |
import javax.lang.model.type.TypeMirror;
|
jaroslav@26
|
43 |
import javax.tools.Diagnostic;
|
jaroslav@26
|
44 |
import javax.tools.FileObject;
|
jaroslav@26
|
45 |
import javax.tools.StandardLocation;
|
jaroslav@435
|
46 |
import org.apidesign.bck2brwsr.htmlpage.api.On;
|
jaroslav@26
|
47 |
import org.apidesign.bck2brwsr.htmlpage.api.Page;
|
jaroslav@26
|
48 |
import org.openide.util.lookup.ServiceProvider;
|
jaroslav@26
|
49 |
|
jaroslav@26
|
50 |
/** Annotation processor to process an XHTML page and generate appropriate
|
jaroslav@26
|
51 |
* "id" file.
|
jaroslav@26
|
52 |
*
|
jaroslav@26
|
53 |
* @author Jaroslav Tulach <jtulach@netbeans.org>
|
jaroslav@26
|
54 |
*/
|
jaroslav@26
|
55 |
@ServiceProvider(service=Processor.class)
|
jaroslav@28
|
56 |
@SupportedAnnotationTypes({
|
jaroslav@28
|
57 |
"org.apidesign.bck2brwsr.htmlpage.api.Page",
|
jaroslav@435
|
58 |
"org.apidesign.bck2brwsr.htmlpage.api.On"
|
jaroslav@28
|
59 |
})
|
jaroslav@26
|
60 |
public final class PageProcessor extends AbstractProcessor {
|
jaroslav@26
|
61 |
@Override
|
jaroslav@26
|
62 |
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
jaroslav@26
|
63 |
for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
|
jaroslav@26
|
64 |
Page p = e.getAnnotation(Page.class);
|
jaroslav@463
|
65 |
if (p == null) {
|
jaroslav@463
|
66 |
continue;
|
jaroslav@463
|
67 |
}
|
jaroslav@100
|
68 |
PackageElement pe = (PackageElement)e.getEnclosingElement();
|
jaroslav@26
|
69 |
String pkg = pe.getQualifiedName().toString();
|
jaroslav@26
|
70 |
|
jaroslav@26
|
71 |
ProcessPage pp;
|
jaroslav@26
|
72 |
try {
|
jaroslav@26
|
73 |
InputStream is = openStream(pkg, p.xhtml());
|
jaroslav@26
|
74 |
pp = ProcessPage.readPage(is);
|
jaroslav@26
|
75 |
is.close();
|
jaroslav@26
|
76 |
} catch (IOException iOException) {
|
jaroslav@26
|
77 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e);
|
jaroslav@26
|
78 |
return false;
|
jaroslav@26
|
79 |
}
|
jaroslav@26
|
80 |
Writer w;
|
jaroslav@108
|
81 |
String className = p.className();
|
jaroslav@108
|
82 |
if (className.isEmpty()) {
|
jaroslav@108
|
83 |
int indx = p.xhtml().indexOf('.');
|
jaroslav@108
|
84 |
className = p.xhtml().substring(0, indx);
|
jaroslav@108
|
85 |
}
|
jaroslav@26
|
86 |
try {
|
jaroslav@108
|
87 |
FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e);
|
jaroslav@26
|
88 |
w = new OutputStreamWriter(java.openOutputStream());
|
jaroslav@28
|
89 |
try {
|
jaroslav@28
|
90 |
w.append("package " + pkg + ";\n");
|
jaroslav@28
|
91 |
w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
|
jaroslav@108
|
92 |
w.append("class ").append(className).append(" {\n");
|
jaroslav@28
|
93 |
for (String id : pp.ids()) {
|
jaroslav@28
|
94 |
String tag = pp.tagNameForId(id);
|
jaroslav@28
|
95 |
String type = type(tag);
|
jaroslav@28
|
96 |
w.append(" ").append("public static final ").
|
jaroslav@28
|
97 |
append(type).append(' ').append(cnstnt(id)).append(" = new ").
|
jaroslav@28
|
98 |
append(type).append("(\"").append(id).append("\");\n");
|
jaroslav@28
|
99 |
}
|
jaroslav@28
|
100 |
w.append(" static {\n");
|
jaroslav@28
|
101 |
if (!initializeOnClick(pe, w, pp)) {
|
jaroslav@28
|
102 |
return false;
|
jaroslav@28
|
103 |
}
|
jaroslav@28
|
104 |
w.append(" }\n");
|
jaroslav@28
|
105 |
w.append("}\n");
|
jaroslav@28
|
106 |
} finally {
|
jaroslav@28
|
107 |
w.close();
|
jaroslav@26
|
108 |
}
|
jaroslav@26
|
109 |
} catch (IOException ex) {
|
jaroslav@108
|
110 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + className + ".java", e);
|
jaroslav@26
|
111 |
return false;
|
jaroslav@26
|
112 |
}
|
jaroslav@26
|
113 |
}
|
jaroslav@26
|
114 |
return true;
|
jaroslav@26
|
115 |
}
|
jaroslav@26
|
116 |
|
jaroslav@26
|
117 |
private InputStream openStream(String pkg, String name) throws IOException {
|
jaroslav@26
|
118 |
try {
|
jaroslav@107
|
119 |
FileObject fo = processingEnv.getFiler().getResource(
|
jaroslav@107
|
120 |
StandardLocation.SOURCE_PATH, pkg, name);
|
jaroslav@26
|
121 |
return fo.openInputStream();
|
jaroslav@26
|
122 |
} catch (IOException ex) {
|
jaroslav@26
|
123 |
return processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, name).openInputStream();
|
jaroslav@26
|
124 |
}
|
jaroslav@26
|
125 |
}
|
jaroslav@26
|
126 |
|
jaroslav@26
|
127 |
private static String type(String tag) {
|
jaroslav@26
|
128 |
if (tag.equals("title")) {
|
jaroslav@26
|
129 |
return "Title";
|
jaroslav@26
|
130 |
}
|
jaroslav@26
|
131 |
if (tag.equals("button")) {
|
jaroslav@26
|
132 |
return "Button";
|
jaroslav@26
|
133 |
}
|
jaroslav@26
|
134 |
if (tag.equals("input")) {
|
jaroslav@26
|
135 |
return "Input";
|
jaroslav@26
|
136 |
}
|
jaroslav@26
|
137 |
return "Element";
|
jaroslav@26
|
138 |
}
|
jaroslav@26
|
139 |
|
jaroslav@26
|
140 |
private static String cnstnt(String id) {
|
jaroslav@26
|
141 |
return id.toUpperCase(Locale.ENGLISH).replace('.', '_');
|
jaroslav@26
|
142 |
}
|
jaroslav@28
|
143 |
|
jaroslav@28
|
144 |
private boolean initializeOnClick(PackageElement pe, Writer w, ProcessPage pp) throws IOException {
|
jaroslav@124
|
145 |
TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
|
jaroslav@28
|
146 |
for (Element clazz : pe.getEnclosedElements()) {
|
jaroslav@28
|
147 |
if (clazz.getKind() != ElementKind.CLASS) {
|
jaroslav@28
|
148 |
continue;
|
jaroslav@28
|
149 |
}
|
jaroslav@28
|
150 |
TypeElement type = (TypeElement)clazz;
|
jaroslav@28
|
151 |
for (Element method : clazz.getEnclosedElements()) {
|
jaroslav@435
|
152 |
On oc = method.getAnnotation(On.class);
|
jaroslav@28
|
153 |
if (oc != null) {
|
jaroslav@124
|
154 |
for (String id : oc.id()) {
|
jaroslav@124
|
155 |
if (pp.tagNameForId(id) == null) {
|
jaroslav@435
|
156 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + id + " does not exist in the HTML page. Found only " + pp.ids(), method);
|
jaroslav@124
|
157 |
return false;
|
jaroslav@124
|
158 |
}
|
jaroslav@124
|
159 |
ExecutableElement ee = (ExecutableElement)method;
|
jaroslav@124
|
160 |
boolean hasParam;
|
jaroslav@124
|
161 |
if (ee.getParameters().isEmpty()) {
|
jaroslav@124
|
162 |
hasParam = false;
|
jaroslav@124
|
163 |
} else {
|
jaroslav@124
|
164 |
if (ee.getParameters().size() != 1 || ee.getParameters().get(0).asType() != stringType) {
|
jaroslav@435
|
165 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method should either have no arguments or one String argument", ee);
|
jaroslav@124
|
166 |
return false;
|
jaroslav@124
|
167 |
}
|
jaroslav@124
|
168 |
hasParam = true;
|
jaroslav@124
|
169 |
}
|
jaroslav@124
|
170 |
if (!ee.getModifiers().contains(Modifier.STATIC)) {
|
jaroslav@435
|
171 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method has to be static", ee);
|
jaroslav@124
|
172 |
return false;
|
jaroslav@124
|
173 |
}
|
jaroslav@124
|
174 |
if (ee.getModifiers().contains(Modifier.PRIVATE)) {
|
jaroslav@435
|
175 |
processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method can't be private", ee);
|
jaroslav@124
|
176 |
return false;
|
jaroslav@124
|
177 |
}
|
jaroslav@435
|
178 |
w.append(" OnEvent." + oc.event()).append(".of(").append(cnstnt(id)).
|
jaroslav@435
|
179 |
append(").perform(new Runnable() { public void run() {\n");
|
jaroslav@124
|
180 |
w.append(" ").append(type.getSimpleName().toString()).
|
jaroslav@124
|
181 |
append('.').append(ee.getSimpleName()).append("(");
|
jaroslav@124
|
182 |
if (hasParam) {
|
jaroslav@124
|
183 |
w.append("\"").append(id).append("\"");
|
jaroslav@124
|
184 |
}
|
jaroslav@124
|
185 |
w.append(");\n");
|
jaroslav@124
|
186 |
w.append(" }});\n");
|
jaroslav@124
|
187 |
}
|
jaroslav@28
|
188 |
}
|
jaroslav@28
|
189 |
}
|
jaroslav@28
|
190 |
}
|
jaroslav@28
|
191 |
return true;
|
jaroslav@28
|
192 |
}
|
jaroslav@117
|
193 |
|
jaroslav@117
|
194 |
@Override
|
jaroslav@117
|
195 |
public Iterable<? extends Completion> getCompletions(
|
jaroslav@117
|
196 |
Element element, AnnotationMirror annotation,
|
jaroslav@117
|
197 |
ExecutableElement member, String userText
|
jaroslav@117
|
198 |
) {
|
jaroslav@117
|
199 |
if (!userText.startsWith("\"")) {
|
jaroslav@117
|
200 |
return Collections.emptyList();
|
jaroslav@117
|
201 |
}
|
jaroslav@117
|
202 |
|
jaroslav@117
|
203 |
Element cls = findClass(element);
|
jaroslav@117
|
204 |
Page p = cls.getAnnotation(Page.class);
|
jaroslav@117
|
205 |
PackageElement pe = (PackageElement) cls.getEnclosingElement();
|
jaroslav@117
|
206 |
String pkg = pe.getQualifiedName().toString();
|
jaroslav@117
|
207 |
ProcessPage pp;
|
jaroslav@117
|
208 |
try {
|
jaroslav@117
|
209 |
InputStream is = openStream(pkg, p.xhtml());
|
jaroslav@117
|
210 |
pp = ProcessPage.readPage(is);
|
jaroslav@117
|
211 |
is.close();
|
jaroslav@117
|
212 |
} catch (IOException iOException) {
|
jaroslav@117
|
213 |
return Collections.emptyList();
|
jaroslav@117
|
214 |
}
|
jaroslav@117
|
215 |
|
jaroslav@117
|
216 |
List<Completion> cc = new ArrayList<Completion>();
|
jaroslav@117
|
217 |
userText = userText.substring(1);
|
jaroslav@117
|
218 |
for (String id : pp.ids()) {
|
jaroslav@117
|
219 |
if (id.startsWith(userText)) {
|
jaroslav@117
|
220 |
cc.add(Completions.of("\"" + id + "\"", id));
|
jaroslav@117
|
221 |
}
|
jaroslav@117
|
222 |
}
|
jaroslav@117
|
223 |
return cc;
|
jaroslav@117
|
224 |
}
|
jaroslav@117
|
225 |
|
jaroslav@117
|
226 |
private static Element findClass(Element e) {
|
jaroslav@117
|
227 |
if (e == null) {
|
jaroslav@117
|
228 |
return null;
|
jaroslav@117
|
229 |
}
|
jaroslav@117
|
230 |
Page p = e.getAnnotation(Page.class);
|
jaroslav@117
|
231 |
if (p != null) {
|
jaroslav@117
|
232 |
return e;
|
jaroslav@117
|
233 |
}
|
jaroslav@117
|
234 |
return e.getEnclosingElement();
|
jaroslav@117
|
235 |
}
|
jaroslav@26
|
236 |
}
|