1.1 --- a/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Thu Oct 11 06:15:22 2012 -0700
1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000
1.3 @@ -1,165 +0,0 @@
1.4 -/**
1.5 - * Java 4 Browser Bytecode Translator
1.6 - * Copyright (C) 2012-2012 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
1.7 - *
1.8 - * This program is free software: you can redistribute it and/or modify
1.9 - * it under the terms of the GNU General Public License as published by
1.10 - * the Free Software Foundation, version 2 of the License.
1.11 - *
1.12 - * This program is distributed in the hope that it will be useful,
1.13 - * but WITHOUT ANY WARRANTY; without even the implied warranty of
1.14 - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1.15 - * GNU General Public License for more details.
1.16 - *
1.17 - * You should have received a copy of the GNU General Public License
1.18 - * along with this program. Look for COPYING file in the top folder.
1.19 - * If not, see http://opensource.org/licenses/GPL-2.0.
1.20 - */
1.21 -package org.apidesign.bck2brwsr.htmlpage;
1.22 -
1.23 -import java.io.IOException;
1.24 -import java.io.InputStream;
1.25 -import java.io.OutputStreamWriter;
1.26 -import java.io.Writer;
1.27 -import java.util.Locale;
1.28 -import java.util.Set;
1.29 -import javax.annotation.processing.AbstractProcessor;
1.30 -import javax.annotation.processing.Processor;
1.31 -import javax.annotation.processing.RoundEnvironment;
1.32 -import javax.annotation.processing.SupportedAnnotationTypes;
1.33 -import javax.lang.model.element.Element;
1.34 -import javax.lang.model.element.ElementKind;
1.35 -import javax.lang.model.element.ExecutableElement;
1.36 -import javax.lang.model.element.Modifier;
1.37 -import javax.lang.model.element.PackageElement;
1.38 -import javax.lang.model.element.TypeElement;
1.39 -import javax.tools.Diagnostic;
1.40 -import javax.tools.FileObject;
1.41 -import javax.tools.StandardLocation;
1.42 -import org.apidesign.bck2brwsr.htmlpage.api.OnClick;
1.43 -import org.apidesign.bck2brwsr.htmlpage.api.Page;
1.44 -import org.openide.util.lookup.ServiceProvider;
1.45 -
1.46 -/** Annotation processor to process an XHTML page and generate appropriate
1.47 - * "id" file.
1.48 - *
1.49 - * @author Jaroslav Tulach <jtulach@netbeans.org>
1.50 - */
1.51 -@ServiceProvider(service=Processor.class)
1.52 -@SupportedAnnotationTypes({
1.53 - "org.apidesign.bck2brwsr.htmlpage.api.Page",
1.54 - "org.apidesign.bck2brwsr.htmlpage.api.OnClick"
1.55 -})
1.56 -public final class PageProcessor extends AbstractProcessor {
1.57 - @Override
1.58 - public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
1.59 - for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
1.60 - Page p = e.getAnnotation(Page.class);
1.61 - PackageElement pe = (PackageElement)e.getEnclosingElement();
1.62 - String pkg = pe.getQualifiedName().toString();
1.63 -
1.64 - ProcessPage pp;
1.65 - try {
1.66 - InputStream is = openStream(pkg, p.xhtml());
1.67 - pp = ProcessPage.readPage(is);
1.68 - is.close();
1.69 - } catch (IOException iOException) {
1.70 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e);
1.71 - return false;
1.72 - }
1.73 - Writer w;
1.74 - try {
1.75 - FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + p.name(), e);
1.76 - w = new OutputStreamWriter(java.openOutputStream());
1.77 - try {
1.78 - w.append("package " + pkg + ";\n");
1.79 - w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
1.80 - w.append("class ").append(p.name()).append(" {\n");
1.81 - for (String id : pp.ids()) {
1.82 - String tag = pp.tagNameForId(id);
1.83 - String type = type(tag);
1.84 - w.append(" ").append("public static final ").
1.85 - append(type).append(' ').append(cnstnt(id)).append(" = new ").
1.86 - append(type).append("(\"").append(id).append("\");\n");
1.87 - }
1.88 - w.append(" static {\n");
1.89 - if (!initializeOnClick(pe, w, pp)) {
1.90 - return false;
1.91 - }
1.92 - w.append(" }\n");
1.93 - w.append("}\n");
1.94 - } finally {
1.95 - w.close();
1.96 - }
1.97 - } catch (IOException ex) {
1.98 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + p.name() + ".java", e);
1.99 - return false;
1.100 - }
1.101 - }
1.102 - return true;
1.103 - }
1.104 -
1.105 - private InputStream openStream(String pkg, String name) throws IOException {
1.106 - FileObject fo = processingEnv.getFiler().getResource(
1.107 - StandardLocation.SOURCE_PATH, pkg, name);
1.108 - try {
1.109 - return fo.openInputStream();
1.110 - } catch (IOException ex) {
1.111 - return processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, name).openInputStream();
1.112 - }
1.113 - }
1.114 -
1.115 - private static String type(String tag) {
1.116 - if (tag.equals("title")) {
1.117 - return "Title";
1.118 - }
1.119 - if (tag.equals("button")) {
1.120 - return "Button";
1.121 - }
1.122 - if (tag.equals("input")) {
1.123 - return "Input";
1.124 - }
1.125 - return "Element";
1.126 - }
1.127 -
1.128 - private static String cnstnt(String id) {
1.129 - return id.toUpperCase(Locale.ENGLISH).replace('.', '_');
1.130 - }
1.131 -
1.132 - private boolean initializeOnClick(PackageElement pe, Writer w, ProcessPage pp) throws IOException {
1.133 - for (Element clazz : pe.getEnclosedElements()) {
1.134 - if (clazz.getKind() != ElementKind.CLASS) {
1.135 - continue;
1.136 - }
1.137 - TypeElement type = (TypeElement)clazz;
1.138 - for (Element method : clazz.getEnclosedElements()) {
1.139 - OnClick oc = method.getAnnotation(OnClick.class);
1.140 - if (oc != null) {
1.141 - if (pp.tagNameForId(oc.id()) == null) {
1.142 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + oc.id() + " does not exist in the HTML page. Found only " + pp.ids(), method);
1.143 - return false;
1.144 - }
1.145 - ExecutableElement ee = (ExecutableElement)method;
1.146 - if (!ee.getParameters().isEmpty()) {
1.147 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@OnClose method can't take arguments", ee);
1.148 - return false;
1.149 - }
1.150 - if (!ee.getModifiers().contains(Modifier.STATIC)) {
1.151 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@OnClose method has to be static", ee);
1.152 - return false;
1.153 - }
1.154 - if (ee.getModifiers().contains(Modifier.PRIVATE)) {
1.155 - processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@OnClose method can't be private", ee);
1.156 - return false;
1.157 - }
1.158 - w.append(" ").append(cnstnt(oc.id())).
1.159 - append(".addOnClick(new Runnable() { public void run() {\n");
1.160 - w.append(" ").append(type.getSimpleName().toString()).
1.161 - append('.').append(ee.getSimpleName()).append("();\n");
1.162 - w.append(" }});\n");
1.163 - }
1.164 - }
1.165 - }
1.166 - return true;
1.167 - }
1.168 -}