javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ElementGenerator.java
changeset 866 9b4751828ceb
child 1074 3ab2f70b300d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/javaquery/api/src/main/java/org/apidesign/bck2brwsr/htmlpage/ElementGenerator.java	Thu Mar 21 15:45:42 2013 +0100
     1.3 @@ -0,0 +1,175 @@
     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.OutputStreamWriter;
    1.25 +import java.io.Writer;
    1.26 +import java.util.Arrays;
    1.27 +import java.util.Collections;
    1.28 +import java.util.HashMap;
    1.29 +import java.util.Map;
    1.30 +import java.util.Map.Entry;
    1.31 +import java.util.ServiceLoader;
    1.32 +import javax.annotation.processing.ProcessingEnvironment;
    1.33 +import javax.lang.model.element.Element;
    1.34 +import javax.tools.Diagnostic;
    1.35 +import javax.tools.FileObject;
    1.36 +import org.netbeans.modules.html.editor.lib.api.HtmlVersion;
    1.37 +import org.netbeans.modules.html.editor.lib.api.model.HtmlModel;
    1.38 +import org.netbeans.modules.html.editor.lib.api.model.HtmlModelProvider;
    1.39 +import org.netbeans.modules.html.editor.lib.api.model.HtmlTag;
    1.40 +import org.netbeans.modules.html.editor.lib.api.model.HtmlTagAttribute;
    1.41 +
    1.42 +/**
    1.43 + *
    1.44 + * @author Jan Horvath <jhorvath@netbeans.org>
    1.45 + */
    1.46 +public class ElementGenerator {
    1.47 +
    1.48 +    static final Map<String, String> NAMING_EXCEPTIONS = new HashMap<String, String>() {
    1.49 +        {
    1.50 +            put("img", "Image");
    1.51 +            put("class", "Clazz");
    1.52 +        }
    1.53 +    };
    1.54 +    
    1.55 +    static final String javaKeywords[] = {
    1.56 +        "abstract", "assert", "boolean", "break", "byte", "case",
    1.57 +        "catch", "char", "class", "const", "continue", "default", 
    1.58 +        "do", "double", "else", "extends", "false", "final", "finally", 
    1.59 +        "float", "for", "goto", "if", "implements", "import", 
    1.60 +        "instanceof", "int", "interface", "long", "native", "new",
    1.61 +        "null", "package", "private", "protected", "public",
    1.62 +        "return", "short", "static", "strictfp", "super",
    1.63 +        "switch", "synchronized", "this", "throw", "throws",
    1.64 +        "transient", "true", "try", "void", "volatile", "while"
    1.65 +    };
    1.66 +    
    1.67 +    private static Map<String, String> elements = new HashMap<String, String>();
    1.68 +    private final ProcessingEnvironment processingEnv;
    1.69 +    private HtmlModel model = null;
    1.70 +
    1.71 +    ElementGenerator(ProcessingEnvironment processingEnv) {
    1.72 +        this.processingEnv = processingEnv;
    1.73 +    }
    1.74 +
    1.75 +    String getType(String pkg, String tag, Element e) {
    1.76 +        String className = elements.get(tag);
    1.77 +        if (className == null) {
    1.78 +            className = createClass(pkg, tag, e);
    1.79 +            elements.put(tag, className);
    1.80 +        }
    1.81 +        return className;
    1.82 +    }
    1.83 +
    1.84 +    private String createClass(String pkg, String tag, Element e) {
    1.85 +        String className = className(tag);
    1.86 +        Writer w;
    1.87 +        try {
    1.88 +            FileObject java = processingEnv.getFiler().createSourceFile(pkg + '.' + className, e);
    1.89 +            w = new OutputStreamWriter(java.openOutputStream());
    1.90 +            try {
    1.91 +                w.append("package " + pkg + ";\n\n");
    1.92 +                w.append("import org.apidesign.bck2brwsr.htmlpage.api.*;\n");
    1.93 +                PredefinedFields.appendImports(w, tag);
    1.94 +                w.append("\n");
    1.95 +                
    1.96 +                w.append("class ").append(className).append(" extends Element {\n\n");
    1.97 +                w.append("    public ").append(className).append("(String id) {\n");
    1.98 +                w.append("        super(id);\n");
    1.99 +                w.append("    }\n\n");
   1.100 +                for (Entry<String, String> entry : getAttributes(tag).entrySet()) {
   1.101 +                    String attrName = entry.getKey();
   1.102 +                    String attrType = entry.getValue();
   1.103 +                    // getter
   1.104 +                    w.append("    public ").append(attrType).append(" ")
   1.105 +                            .append("get").append(className(attrName)).append("() {\n");
   1.106 +                    w.append("        return (").append(attrType).append(")getAttribute(\"")
   1.107 +                            .append(attrName).append("\");\n");
   1.108 +                    w.append("    }\n\n");
   1.109 +                    // setter
   1.110 +                    w.append("    public void ")
   1.111 +                            .append("set").append(className(attrName)).append("(")
   1.112 +                            .append(attrType).append(" ").append(attributeName(attrName)).append(") {\n");
   1.113 +                    w.append("        setAttribute(\"").append(attrName).append("\", ").append(attributeName(attrName)).append(");\n");
   1.114 +                    w.append("    }\n\n");
   1.115 +                }
   1.116 +                PredefinedFields.appendFields(w, tag);
   1.117 +                w.append("}\n");
   1.118 +            } finally {
   1.119 +                w.close();
   1.120 +            }
   1.121 +        } catch (IOException ex) {
   1.122 +            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, "Can't create " + className + ".java", e);
   1.123 +            return null;
   1.124 +        }
   1.125 +        return className;
   1.126 +    }
   1.127 +
   1.128 +    Map<String, String> getAttributes(String tagName) {
   1.129 +        Map<String, String> result = new HashMap<String, String>();
   1.130 +
   1.131 +        if (model == null) {
   1.132 +            // HtmlModelProvider modelProvider = Lookup.getDefault().lookup(HtmlModelProvider.class);
   1.133 +            ServiceLoader<HtmlModelProvider> hmpLoader = 
   1.134 +                    ServiceLoader.load(HtmlModelProvider.class, this.getClass().getClassLoader());
   1.135 +            for (HtmlModelProvider htmlModelProvider : hmpLoader) {
   1.136 +                model = htmlModelProvider.getModel(HtmlVersion.HTML5);
   1.137 +                if (model != null) {
   1.138 +                    break;
   1.139 +                }
   1.140 +            }
   1.141 +        }
   1.142 +        
   1.143 +        if (model == null) {
   1.144 +            processingEnv.getMessager().printMessage(Diagnostic.Kind.ERROR, 
   1.145 +                    "HTML 5 model provider was not found on classpath");
   1.146 +            return Collections.emptyMap();
   1.147 +        }
   1.148 +        HtmlTag tag = model.getTag(tagName);
   1.149 +        for (HtmlTagAttribute attr : tag.getAttributes()) {
   1.150 +            String name = attr.getName();
   1.151 +            String type = Attributes.TYPES.get(name);
   1.152 +            if (type != null) {
   1.153 +                result.put(name, type);
   1.154 +            }
   1.155 +        }
   1.156 +        
   1.157 +        return result;
   1.158 +    }
   1.159 +
   1.160 +    private String className(String s) {
   1.161 +        if (s.length() == 0) {
   1.162 +            return s;
   1.163 +        }
   1.164 +        String name = NAMING_EXCEPTIONS.get(s.toLowerCase());
   1.165 +        if (name == null) {
   1.166 +            name = s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
   1.167 +        }
   1.168 +        return name;
   1.169 +    }
   1.170 +
   1.171 +    private String attributeName(String s) {
   1.172 +        if (Arrays.binarySearch(javaKeywords, s) >= 0) {
   1.173 +            return String.format("%sAttr", s);
   1.174 +        }
   1.175 +        return s;
   1.176 +    }
   1.177 +    
   1.178 +}