1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Mon Sep 24 15:06:43 2012 +0200
1.3 @@ -0,0 +1,113 @@
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.PackageElement;
1.35 +import javax.lang.model.element.TypeElement;
1.36 +import javax.tools.Diagnostic;
1.37 +import javax.tools.FileObject;
1.38 +import javax.tools.StandardLocation;
1.39 +import org.apidesign.bck2brwsr.htmlpage.api.Page;
1.40 +import org.openide.util.lookup.ServiceProvider;
1.41 +
1.42 +/** Annotation processor to process an XHTML page and generate appropriate
1.43 + * "id" file.
1.44 + *
1.45 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.46 + */
1.47 +@ServiceProvider(service=Processor.class)
1.48 +@SupportedAnnotationTypes("org.apidesign.bck2brwsr.htmlpage.api.Page")
1.49 +public final class PageProcessor extends AbstractProcessor {
1.50 + @Override
1.51 + public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
1.52 + for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) {
1.53 + Page p = e.getAnnotation(Page.class);
1.54 + PackageElement pe = (PackageElement)e;
1.55 + String pkg = pe.getQualifiedName().toString();
1.56 +
1.57 + ProcessPage pp;
1.58 + try {
1.59 + InputStream is = openStream(pkg, p.xhtml());
1.60 + pp = ProcessPage.readPage(is);
1.61 + is.close();
1.62 + } catch (IOException iOException) {
1.63 + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't read " + p.xhtml(), e);
1.64 + return false;
1.65 + }
1.66 + Writer w;
1.67 + try {
1.68 + FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + p.name(), e);
1.69 + w = new OutputStreamWriter(java.openOutputStream());
1.70 + w.append("package " + pkg + ";\n");
1.71 + w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
1.72 + w.append("class ").append(p.name()).append(" {\n");
1.73 + for (String id : pp.ids()) {
1.74 + String tag = pp.tagNameForId(id);
1.75 + String type = type(tag);
1.76 + w.append(" ").append("public static final ").
1.77 + append(type).append(' ').append(cnstnt(id)).append(" = new ").
1.78 + append(type).append("(\"").append(id).append("\");\n");
1.79 + }
1.80 + w.append("}");
1.81 + w.close();
1.82 + } catch (IOException ex) {
1.83 + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + p.name() + ".java", e);
1.84 + return false;
1.85 + }
1.86 + }
1.87 + return true;
1.88 + }
1.89 +
1.90 + private InputStream openStream(String pkg, String name) throws IOException {
1.91 + FileObject fo = processingEnv.getFiler().getResource(
1.92 + StandardLocation.SOURCE_PATH, pkg, name);
1.93 + try {
1.94 + return fo.openInputStream();
1.95 + } catch (IOException ex) {
1.96 + return processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, name).openInputStream();
1.97 + }
1.98 + }
1.99 +
1.100 + private static String type(String tag) {
1.101 + if (tag.equals("title")) {
1.102 + return "Title";
1.103 + }
1.104 + if (tag.equals("button")) {
1.105 + return "Button";
1.106 + }
1.107 + if (tag.equals("input")) {
1.108 + return "Input";
1.109 + }
1.110 + return "Element";
1.111 + }
1.112 +
1.113 + private static String cnstnt(String id) {
1.114 + return id.toUpperCase(Locale.ENGLISH).replace('.', '_');
1.115 + }
1.116 +}