htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java
changeset 140 590958fcb7d7
parent 139 070fc5844295
child 141 63be794c1eeb
     1.1 --- a/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java	Fri Nov 09 09:00:46 2012 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,233 +0,0 @@
     1.4 -/**
     1.5 - * Back 2 Browser Bytecode Translator
     1.6 - * Copyright (C) 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.ArrayList;
    1.28 -import java.util.Collections;
    1.29 -import java.util.List;
    1.30 -import java.util.Locale;
    1.31 -import java.util.Set;
    1.32 -import javax.annotation.processing.AbstractProcessor;
    1.33 -import javax.annotation.processing.Completion;
    1.34 -import javax.annotation.processing.Completions;
    1.35 -import javax.annotation.processing.Processor;
    1.36 -import javax.annotation.processing.RoundEnvironment;
    1.37 -import javax.annotation.processing.SupportedAnnotationTypes;
    1.38 -import javax.lang.model.element.AnnotationMirror;
    1.39 -import javax.lang.model.element.Element;
    1.40 -import javax.lang.model.element.ElementKind;
    1.41 -import javax.lang.model.element.ExecutableElement;
    1.42 -import javax.lang.model.element.Modifier;
    1.43 -import javax.lang.model.element.PackageElement;
    1.44 -import javax.lang.model.element.TypeElement;
    1.45 -import javax.lang.model.type.TypeMirror;
    1.46 -import javax.tools.Diagnostic;
    1.47 -import javax.tools.FileObject;
    1.48 -import javax.tools.StandardLocation;
    1.49 -import org.apidesign.bck2brwsr.htmlpage.api.OnClick;
    1.50 -import org.apidesign.bck2brwsr.htmlpage.api.Page;
    1.51 -import org.openide.util.lookup.ServiceProvider;
    1.52 -
    1.53 -/** Annotation processor to process an XHTML page and generate appropriate 
    1.54 - * "id" file.
    1.55 - *
    1.56 - * @author Jaroslav Tulach <jtulach@netbeans.org>
    1.57 - */
    1.58 -@ServiceProvider(service=Processor.class)
    1.59 -@SupportedAnnotationTypes({
    1.60 -    "org.apidesign.bck2brwsr.htmlpage.api.Page",
    1.61 -    "org.apidesign.bck2brwsr.htmlpage.api.OnClick"
    1.62 -})
    1.63 -public final class PageProcessor extends AbstractProcessor {
    1.64 -    @Override
    1.65 -    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    1.66 -        for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
    1.67 -            Page p = e.getAnnotation(Page.class);
    1.68 -            PackageElement pe = (PackageElement)e.getEnclosingElement();
    1.69 -            String pkg = pe.getQualifiedName().toString();
    1.70 -            
    1.71 -            ProcessPage pp;
    1.72 -            try {
    1.73 -                InputStream is = openStream(pkg, p.xhtml());
    1.74 -                pp = ProcessPage.readPage(is);
    1.75 -                is.close();
    1.76 -            } catch (IOException iOException) {
    1.77 -                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e);
    1.78 -                return false;
    1.79 -            }
    1.80 -            Writer w;
    1.81 -            String className = p.className();
    1.82 -            if (className.isEmpty()) {
    1.83 -                int indx = p.xhtml().indexOf('.');
    1.84 -                className = p.xhtml().substring(0, indx);
    1.85 -            }
    1.86 -            try {
    1.87 -                FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e);
    1.88 -                w = new OutputStreamWriter(java.openOutputStream());
    1.89 -                try {
    1.90 -                    w.append("package " + pkg + ";\n");
    1.91 -                    w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
    1.92 -                    w.append("class ").append(className).append(" {\n");
    1.93 -                    for (String id : pp.ids()) {
    1.94 -                        String tag = pp.tagNameForId(id);
    1.95 -                        String type = type(tag);
    1.96 -                        w.append("  ").append("public static final ").
    1.97 -                            append(type).append(' ').append(cnstnt(id)).append(" = new ").
    1.98 -                            append(type).append("(\"").append(id).append("\");\n");
    1.99 -                    }
   1.100 -                    w.append("  static {\n");
   1.101 -                    if (!initializeOnClick(pe, w, pp)) {
   1.102 -                        return false;
   1.103 -                    }
   1.104 -                    w.append("  }\n");
   1.105 -                    w.append("}\n");
   1.106 -                } finally {
   1.107 -                    w.close();
   1.108 -                }
   1.109 -            } catch (IOException ex) {
   1.110 -                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + className + ".java", e);
   1.111 -                return false;
   1.112 -            }
   1.113 -        }
   1.114 -        return true;
   1.115 -    }
   1.116 -
   1.117 -    private InputStream openStream(String pkg, String name) throws IOException {
   1.118 -        try {
   1.119 -            FileObject fo = processingEnv.getFiler().getResource(
   1.120 -                StandardLocation.SOURCE_PATH, pkg, name);
   1.121 -            return fo.openInputStream();
   1.122 -        } catch (IOException ex) {
   1.123 -            return processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, name).openInputStream();
   1.124 -        }
   1.125 -    }
   1.126 -
   1.127 -    private static String type(String tag) {
   1.128 -        if (tag.equals("title")) {
   1.129 -            return "Title";
   1.130 -        }
   1.131 -        if (tag.equals("button")) {
   1.132 -            return "Button";
   1.133 -        }
   1.134 -        if (tag.equals("input")) {
   1.135 -            return "Input";
   1.136 -        }
   1.137 -        return "Element";
   1.138 -    }
   1.139 -
   1.140 -    private static String cnstnt(String id) {
   1.141 -        return id.toUpperCase(Locale.ENGLISH).replace('.', '_');
   1.142 -    }
   1.143 -
   1.144 -    private boolean initializeOnClick(PackageElement pe, Writer w, ProcessPage pp) throws IOException {
   1.145 -        TypeMirror stringType = processingEnv.getElementUtils().getTypeElement("java.lang.String").asType();
   1.146 -        for (Element clazz : pe.getEnclosedElements()) {
   1.147 -            if (clazz.getKind() != ElementKind.CLASS) {
   1.148 -                continue;
   1.149 -            }
   1.150 -            TypeElement type = (TypeElement)clazz;
   1.151 -            for (Element method : clazz.getEnclosedElements()) {
   1.152 -                OnClick oc = method.getAnnotation(OnClick.class);
   1.153 -                if (oc != null) {
   1.154 -                    for (String id : oc.id()) {
   1.155 -                        if (pp.tagNameForId(id) == null) {
   1.156 -                            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "id = " + oc.id() + " does not exist in the HTML page. Found only " + pp.ids(), method);
   1.157 -                            return false;
   1.158 -                        }
   1.159 -                        ExecutableElement ee = (ExecutableElement)method;
   1.160 -                        boolean hasParam;
   1.161 -                        if (ee.getParameters().isEmpty()) {
   1.162 -                            hasParam = false;
   1.163 -                        } else {
   1.164 -                            if (ee.getParameters().size() != 1 || ee.getParameters().get(0).asType() != stringType) {
   1.165 -                                processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@OnClick method should either have no arguments or one String argument", ee);
   1.166 -                                return false;
   1.167 -                            }
   1.168 -                            hasParam = true;
   1.169 -                        }
   1.170 -                        if (!ee.getModifiers().contains(Modifier.STATIC)) {
   1.171 -                            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@OnClick method has to be static", ee);
   1.172 -                            return false;
   1.173 -                        }
   1.174 -                        if (ee.getModifiers().contains(Modifier.PRIVATE)) {
   1.175 -                            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "@OnClick method can't be private", ee);
   1.176 -                            return false;
   1.177 -                        }
   1.178 -                        w.append("  ").append(cnstnt(id)).
   1.179 -                            append(".addOnClick(new Runnable() { public void run() {\n");
   1.180 -                        w.append("    ").append(type.getSimpleName().toString()).
   1.181 -                            append('.').append(ee.getSimpleName()).append("(");
   1.182 -                        if (hasParam) {
   1.183 -                            w.append("\"").append(id).append("\"");
   1.184 -                        }
   1.185 -                        w.append(");\n");
   1.186 -                        w.append("  }});\n");
   1.187 -                    }           
   1.188 -                }
   1.189 -            }
   1.190 -        }
   1.191 -        return true;
   1.192 -    }
   1.193 -
   1.194 -    @Override
   1.195 -    public Iterable<? extends Completion> getCompletions(
   1.196 -        Element element, AnnotationMirror annotation, 
   1.197 -        ExecutableElement member, String userText
   1.198 -    ) {
   1.199 -        if (!userText.startsWith("\"")) {
   1.200 -            return Collections.emptyList();
   1.201 -        }
   1.202 -        
   1.203 -        Element cls = findClass(element);
   1.204 -        Page p = cls.getAnnotation(Page.class);
   1.205 -        PackageElement pe = (PackageElement) cls.getEnclosingElement();
   1.206 -        String pkg = pe.getQualifiedName().toString();
   1.207 -        ProcessPage pp;
   1.208 -        try {
   1.209 -            InputStream is = openStream(pkg, p.xhtml());
   1.210 -            pp = ProcessPage.readPage(is);
   1.211 -            is.close();
   1.212 -        } catch (IOException iOException) {
   1.213 -            return Collections.emptyList();
   1.214 -        }
   1.215 -        
   1.216 -        List<Completion> cc = new ArrayList<Completion>();
   1.217 -        userText = userText.substring(1);
   1.218 -        for (String id : pp.ids()) {
   1.219 -            if (id.startsWith(userText)) {
   1.220 -                cc.add(Completions.of("\"" + id + "\"", id));
   1.221 -            }
   1.222 -        }
   1.223 -        return cc;
   1.224 -    }
   1.225 -    
   1.226 -    private static Element findClass(Element e) {
   1.227 -        if (e == null) {
   1.228 -            return null;
   1.229 -        }
   1.230 -        Page p = e.getAnnotation(Page.class);
   1.231 -        if (p != null) {
   1.232 -            return e;
   1.233 -        }
   1.234 -        return e.getEnclosingElement();
   1.235 -    }
   1.236 -}