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