diff -r e2d1dc505c24 -r 9321b4016d5c javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java --- a/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Thu Apr 18 17:34:28 2013 +0200 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Thu May 02 10:08:35 2013 +0200 @@ -299,109 +299,85 @@ } String pkg = findPkgName(e); - ProcessPage pp; - try (InputStream is = openStream(pkg, p.xhtml())) { - pp = ProcessPage.readPage(is); - is.close(); - } catch (IOException iOException) { - error("Can't read " + p.xhtml() + " as " + iOException.getMessage(), e); - ok = false; - pp = null; - } - Writer w; - String className = p.className(); - if (className.isEmpty()) { - int indx = p.xhtml().indexOf('.'); - className = p.xhtml().substring(0, indx); - } - try { - StringWriter body = new StringWriter(); - List propsGetSet = new ArrayList<>(); - List functions = new ArrayList<>(); - Map> propsDeps = new HashMap<>(); - Map> functionDeps = new HashMap<>(); - - Prprt[] props = createProps(e, p.properties()); - if (!generateComputedProperties(body, props, e.getEnclosedElements(), propsGetSet, propsDeps)) { - ok = false; + ProcessPage pp; + ElementGenerator eGen = new ElementGenerator(processingEnv); + try { + InputStream is = openStream(pkg, p.xhtml()); + pp = ProcessPage.readPage(is); + is.close(); + } catch (IOException iOException) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e); + return false; } - if (!generateOnChange(e, propsDeps, props, className, functionDeps)) { - ok = false; + Writer w; + String className = p.className(); + if (className.isEmpty()) { + int indx = p.xhtml().indexOf('.'); + className = p.xhtml().substring(0, indx); } - if (!generateProperties(e, body, props, propsGetSet, propsDeps, functionDeps)) { - ok = false; - } - if (!generateFunctions(e, body, className, e.getEnclosedElements(), functions)) { - ok = false; - } - if (!generateReceive(e, body, className, e.getEnclosedElements(), functions)) { - ok = false; - } - - FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e); - w = new OutputStreamWriter(java.openOutputStream()); try { - w.append("package " + pkg + ";\n"); - w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n"); - w.append("import org.apidesign.bck2brwsr.htmlpage.KOList;\n"); - w.append("final class ").append(className).append(" {\n"); - w.append(" private boolean locked;\n"); - if (!initializeOnClick(className, (TypeElement) e, w, pp)) { - ok = false; - } else { - if (pp != null) for (String id : pp.ids()) { + FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e); + w = new OutputStreamWriter(java.openOutputStream()); + try { + w.append("package " + pkg + ";\n"); + w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n"); + w.append("final class ").append(className).append(" {\n"); + w.append(" private boolean locked;\n"); + if (!initializeOnClick(className, (TypeElement) e, w, pp)) { + return false; + } + for (String id : pp.ids()) { String tag = pp.tagNameForId(id); - String type = type(tag); + String type = eGen.getType(pkg, tag, e); w.append(" ").append("public final "). append(type).append(' ').append(cnstnt(id)).append(" = new "). append(type).append("(\"").append(id).append("\");\n"); } + List propsGetSet = new ArrayList(); + Map> functionDeps = new HashMap<>(); + Map> propsDeps = new HashMap>(); + List functions = new ArrayList<>(); + Prprt[] props = createProps(e, p.properties()); + if (!generateComputedProperties(w, props, e.getEnclosedElements(), propsGetSet, propsDeps)) { + ok = false; + } + if (!generateOnChange(e, propsDeps, props, className, functionDeps)) { + ok = false; + } + if (!generateProperties(e, w, props, propsGetSet, propsDeps, functionDeps)) { + ok = false; + } + if (!generateFunctions(e, w, className, e.getEnclosedElements(), functions)) { + ok = false; + } + if (!generateReceive(e, w, className, e.getEnclosedElements(), functions)) { + ok = false; + } + w.append(" private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n"); + if (!propsGetSet.isEmpty()) { + w.write("public " + className + " applyBindings() {\n"); + w.write(" ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings("); + w.write(className + ".class, this, "); + writeStringArray(propsGetSet, w); + w.append(", "); + writeStringArray(functions, w); + w.write(");\n return this;\n}\n"); + + w.write("public void triggerEvent(Element e, OnEvent ev) {\n"); + w.write(" org.apidesign.bck2brwsr.htmlpage.Knockout.triggerEvent(e.getId(), ev.getElementPropertyName());\n"); + w.write("}\n"); + } + w.append("}\n"); + } finally { + w.close(); } - w.append(" private org.apidesign.bck2brwsr.htmlpage.Knockout ko;\n"); - w.append(body.toString()); - if (!propsGetSet.isEmpty()) { - w.write("public " + className + " applyBindings() {\n"); - w.write(" ko = org.apidesign.bck2brwsr.htmlpage.Knockout.applyBindings("); - w.write(className + ".class, this, "); - writeStringArray(propsGetSet, w); - w.append(", "); - writeStringArray(functions, w); - w.write(");\n return this;\n}\n"); - - w.write("public void triggerEvent(Element e, OnEvent ev) {\n"); - w.write(" org.apidesign.bck2brwsr.htmlpage.Knockout.triggerEvent(e.getId(), ev.getElementPropertyName());\n"); - w.write("}\n"); - } - w.append("}\n"); - } finally { - w.close(); + } catch (IOException ex) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + className + ".java", e); + return false; } - } catch (IOException ex) { - error("Can't create " + className + ".java", e); - return false; - } return ok; } - private static String type(String tag) { - if (tag.equals("title")) { - return "Title"; - } - if (tag.equals("button")) { - return "Button"; - } - if (tag.equals("input")) { - return "Input"; - } - if (tag.equals("canvas")) { - return "Canvas"; - } - if (tag.equals("img")) { - return "Image"; - } - return "Element"; - } - private static String cnstnt(String id) { return id.replace('.', '_').replace('-', '_'); } @@ -409,7 +385,6 @@ private boolean initializeOnClick( String className, TypeElement type, Writer w, ProcessPage pp ) throws IOException { - boolean ok = true; TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType(); { //for (Element clazz : pe.getEnclosedElements()) { // if (clazz.getKind() != ElementKind.CLASS) { @@ -422,27 +397,58 @@ On oc = method.getAnnotation(On.class); if (oc != null) { for (String id : oc.id()) { - if (pp == null) { - error("id = " + id + " not found in HTML page.", method); - ok = false; - continue; - } if (pp.tagNameForId(id) == null) { - error("id = " + id + " does not exist in the HTML page. Found only " + pp.ids(), method); - ok = false; - continue; + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + id + " does not exist in the HTML page. Found only " + pp.ids(), method); + return false; } ExecutableElement ee = (ExecutableElement)method; - CharSequence params = wrapParams(ee, id, className, "ev", null); + StringBuilder params = new StringBuilder(); + { + boolean first = true; + for (VariableElement ve : ee.getParameters()) { + if (!first) { + params.append(", "); + } + first = false; + if (ve.asType() == stringType) { + if (ve.getSimpleName().contentEquals("id")) { + params.append('"').append(id).append('"'); + continue; + } + params.append("org.apidesign.bck2brwsr.htmlpage.ConvertTypes.toString(ev, \""); + params.append(ve.getSimpleName().toString()); + params.append("\")"); + continue; + } + if (processingEnv.getTypeUtils().getPrimitiveType(TypeKind.DOUBLE) == ve.asType()) { + params.append("org.apidesign.bck2brwsr.htmlpage.ConvertTypes.toDouble(ev, \""); + params.append(ve.getSimpleName().toString()); + params.append("\")"); + continue; + } + String rn = ve.asType().toString(); + int last = rn.lastIndexOf('.'); + if (last >= 0) { + rn = rn.substring(last + 1); + } + if (rn.equals(className)) { + params.append(className).append(".this"); + continue; + } + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, + "@On method can only accept String named 'id' or " + className + " arguments", + ee + ); + return false; + } + } if (!ee.getModifiers().contains(Modifier.STATIC)) { - error("@On method has to be static", ee); - ok = false; - continue; + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method has to be static", ee); + return false; } if (ee.getModifiers().contains(Modifier.PRIVATE)) { - error("@On method can't be private", ee); - ok = false; - continue; + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@On method can't be private", ee); + return false; } w.append(" OnEvent." + oc.event()).append(".of(").append(cnstnt(id)). append(").perform(new OnDispatch(" + dispatchCnt + "));\n"); @@ -473,7 +479,7 @@ } - return ok; + return true; } @Override @@ -487,7 +493,8 @@ Element cls = findClass(element); Page p = cls.getAnnotation(Page.class); - String pkg = findPkgName(cls); + PackageElement pe = (PackageElement) cls.getEnclosingElement(); + String pkg = pe.getQualifiedName().toString(); ProcessPage pp; try { InputStream is = openStream(pkg, p.xhtml()); @@ -497,7 +504,7 @@ return Collections.emptyList(); } - List cc = new ArrayList<>(); + List cc = new ArrayList(); userText = userText.substring(1); for (String id : pp.ids()) { if (id.startsWith(userText)) { @@ -771,9 +778,9 @@ } private boolean generateFunctions( - Element clazz, StringWriter body, String className, + Element clazz, Writer body, String className, List enclosedElements, List functions - ) { + ) throws IOException { for (Element m : enclosedElements) { if (m.getKind() != ElementKind.METHOD) { continue; @@ -868,9 +875,9 @@ } private boolean generateReceive( - Element clazz, StringWriter body, String className, + Element clazz, Writer body, String className, List enclosedElements, List functions - ) { + ) throws IOException { for (Element m : enclosedElements) { if (m.getKind() != ElementKind.METHOD) { continue;