# HG changeset patch # User Jaroslav Tulach # Date 1348492003 -7200 # Node ID 03e4aaa4ef3d36c904cfc0d9024bd3bf8313e60e # Parent e214f04abb987ac68d713e04b5cc972f8afe216c Can extract 'id' elements from a page and generate appropriate HTML page diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/pom.xml --- a/htmlpage/pom.xml Mon Sep 24 12:39:21 2012 +0200 +++ b/htmlpage/pom.xml Mon Sep 24 15:06:43 2012 +0200 @@ -12,7 +12,7 @@ 1.0-SNAPSHOT htmlpage http://maven.apache.org - + UTF-8 @@ -27,5 +27,21 @@ + + org.netbeans.api + org-openide-util-lookup + + + + + com.mycila.maven-license-plugin + maven-license-plugin + 1.9.0 + +
../vm/src/header.txt
+
+
+
+
diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/PageProcessor.java Mon Sep 24 15:06:43 2012 +0200 @@ -0,0 +1,113 @@ +/** + * Java 4 Browser Bytecode Translator + * Copyright (C) 2012-2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.Locale; +import java.util.Set; +import javax.annotation.processing.AbstractProcessor; +import javax.annotation.processing.Processor; +import javax.annotation.processing.RoundEnvironment; +import javax.annotation.processing.SupportedAnnotationTypes; +import javax.lang.model.element.Element; +import javax.lang.model.element.PackageElement; +import javax.lang.model.element.TypeElement; +import javax.tools.Diagnostic; +import javax.tools.FileObject; +import javax.tools.StandardLocation; +import org.apidesign.bck2brwsr.htmlpage.api.Page; +import org.openide.util.lookup.ServiceProvider; + +/** Annotation processor to process an XHTML page and generate appropriate + * "id" file. + * + * @author Jaroslav Tulach + */ +@ServiceProvider(service=Processor.class) +@SupportedAnnotationTypes("org.apidesign.bck2brwsr.htmlpage.api.Page") +public final class PageProcessor extends AbstractProcessor { + @Override + public boolean process(Set annotations, RoundEnvironment roundEnv) { + for (Element e : roundEnv.getElementsAnnotatedWith(Page.class)) { + Page p = e.getAnnotation(Page.class); + PackageElement pe = (PackageElement)e; + String pkg = pe.getQualifiedName().toString(); + + ProcessPage pp; + 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; + } + Writer w; + try { + FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + p.name(), e); + w = new OutputStreamWriter(java.openOutputStream()); + w.append("package " + pkg + ";\n"); + w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n"); + w.append("class ").append(p.name()).append(" {\n"); + for (String id : pp.ids()) { + String tag = pp.tagNameForId(id); + String type = type(tag); + w.append(" ").append("public static final "). + append(type).append(' ').append(cnstnt(id)).append(" = new "). + append(type).append("(\"").append(id).append("\");\n"); + } + w.append("}"); + w.close(); + } catch (IOException ex) { + processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + p.name() + ".java", e); + return false; + } + } + return true; + } + + private InputStream openStream(String pkg, String name) throws IOException { + FileObject fo = processingEnv.getFiler().getResource( + StandardLocation.SOURCE_PATH, pkg, name); + try { + return fo.openInputStream(); + } catch (IOException ex) { + return processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, pkg, name).openInputStream(); + } + } + + private static String type(String tag) { + if (tag.equals("title")) { + return "Title"; + } + if (tag.equals("button")) { + return "Button"; + } + if (tag.equals("input")) { + return "Input"; + } + return "Element"; + } + + private static String cnstnt(String id) { + return id.toUpperCase(Locale.ENGLISH).replace('.', '_'); + } +} diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/ProcessPage.java --- a/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/ProcessPage.java Mon Sep 24 12:39:21 2012 +0200 +++ b/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/ProcessPage.java Mon Sep 24 15:06:43 2012 +0200 @@ -1,3 +1,20 @@ +/** + * Java 4 Browser Bytecode Translator + * Copyright (C) 2012-2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ package org.apidesign.bck2brwsr.htmlpage; import java.io.IOException; diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Button.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Button.java Mon Sep 24 15:06:43 2012 +0200 @@ -0,0 +1,32 @@ +/** + * Java 4 Browser Bytecode Translator + * Copyright (C) 2012-2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage.api; + +/** + * + * @author Jaroslav Tulach + */ +public class Button extends Element { + public Button(String id) { + super(id); + } + + @Override + void dontSubclass() { + } +} diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Element.java Mon Sep 24 15:06:43 2012 +0200 @@ -0,0 +1,32 @@ +/** + * Java 4 Browser Bytecode Translator + * Copyright (C) 2012-2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage.api; + +/** Represents a generic HTML element. + * + * @author Jaroslav Tulach + */ +public abstract class Element { + private final String id; + + public Element(String id) { + this.id = id; + } + + abstract void dontSubclass(); +} diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Input.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Input.java Mon Sep 24 15:06:43 2012 +0200 @@ -0,0 +1,32 @@ +/** + * Java 4 Browser Bytecode Translator + * Copyright (C) 2012-2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage.api; + +/** + * + * @author Jaroslav Tulach + */ +public class Input extends Element { + public Input(String id) { + super(id); + } + + @Override + void dontSubclass() { + } +} diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Page.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Page.java Mon Sep 24 15:06:43 2012 +0200 @@ -0,0 +1,43 @@ +/** + * Java 4 Browser Bytecode Translator + * Copyright (C) 2012-2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package org.apidesign.bck2brwsr.htmlpage.api; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** Converts an XHTML page into a Java class that contains + * references to all IDs. + * + * @author Jaroslav Tulach + */ +@Retention(RetentionPolicy.SOURCE) +@Target(ElementType.PACKAGE) +public @interface Page { + /** Path to the XHTML page to read in */ + String xhtml(); + /** Name of a Java class to generate. It will contain constants for all + * found elements with IDs. + */ + String name(); +} diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Title.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htmlpage/src/main/java/org/apidesign/bck2brwsr/htmlpage/api/Title.java Mon Sep 24 15:06:43 2012 +0200 @@ -0,0 +1,32 @@ +/** + * Java 4 Browser Bytecode Translator + * Copyright (C) 2012-2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +package org.apidesign.bck2brwsr.htmlpage.api; + +/** + * + * @author Jaroslav Tulach + */ +public class Title extends Element { + public Title(String id) { + super(id); + } + + @Override + void dontSubclass() { + } +} diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java --- a/htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java Mon Sep 24 12:39:21 2012 +0200 +++ b/htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/ProcessPageTest.java Mon Sep 24 15:06:43 2012 +0200 @@ -1,3 +1,20 @@ +/** + * Java 4 Browser Bytecode Translator + * Copyright (C) 2012-2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ package org.apidesign.bck2brwsr.htmlpage; import java.io.IOException; @@ -6,6 +23,8 @@ import org.testng.annotations.Test; import static org.testng.Assert.*; +import org.apidesign.bck2brwsr.htmlpage.api.*; + public class ProcessPageTest { @@ -20,4 +39,8 @@ assertEquals(res.tagNameForId("pg.button"), "button"); assertEquals(res.tagNameForId("pg.text"), "input"); } + + void testWhetherWeCanCallTheGeneratedIdFields() { + Title t = TestPage.PG_TITLE; + } } diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/package-info.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/htmlpage/src/test/java/org/apidesign/bck2brwsr/htmlpage/package-info.java Mon Sep 24 15:06:43 2012 +0200 @@ -0,0 +1,22 @@ +/** + * Java 4 Browser Bytecode Translator + * Copyright (C) 2012-2012 Jaroslav Tulach + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, version 2 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. Look for COPYING file in the top folder. + * If not, see http://opensource.org/licenses/GPL-2.0. + */ +@Page(xhtml="TestPage.xhtml", name="TestPage") +package org.apidesign.bck2brwsr.htmlpage; + +import org.apidesign.bck2brwsr.htmlpage.api.Page; + diff -r e214f04abb98 -r 03e4aaa4ef3d htmlpage/src/test/resources/org/apidesign/bck2brwsr/htmlpage/TestPage.xhtml --- a/htmlpage/src/test/resources/org/apidesign/bck2brwsr/htmlpage/TestPage.xhtml Mon Sep 24 12:39:21 2012 +0200 +++ b/htmlpage/src/test/resources/org/apidesign/bck2brwsr/htmlpage/TestPage.xhtml Mon Sep 24 15:06:43 2012 +0200 @@ -1,4 +1,23 @@ + diff -r e214f04abb98 -r 03e4aaa4ef3d pom.xml --- a/pom.xml Mon Sep 24 12:39:21 2012 +0200 +++ b/pom.xml Mon Sep 24 15:06:43 2012 +0200 @@ -61,6 +61,13 @@ RELEASE72 jar + + org.netbeans.api + org-openide-util-lookup + RELEASE72 + compile + jar +