Thank you Sir.
Here is the working code:
package com.google.appinventor.components.scripts;
import java.io.IOException;
import java.io.Writer;
import java.util.*;
import javax.tools.Diagnostic;
import javax.tools.Diagnostic.Kind;
import javax.tools.FileObject;
public final class ComponentTranslationGenerator extends ComponentProcessor {
// Where to write results.
private static final String OUTPUT_FILE_NAME = "ComponentsTranslation.java";
private static final String AUTOGEN_OUTPUT_FILE_NAME = "AutogeneratedOdeMessages.java";
private Map<String, String> tooltips = new TreeMap<>();
private Map<String, Set<String>> tooltipComponent = new TreeMap<>();
private Set<String> collisionKeys = new HashSet<>();
private Map<String, Parameter> parameters = new LinkedHashMap<String, Parameter>();
public String componentString(ComponentInfo component){
return "\n\n/* Component: " + component.getName() + " */\n\n" +
" map.put(\"COMPONENT-" + component.getName() + "\", MESSAGES."
+ Character.toLowerCase(component.getName().charAt(0)) + component.getName().substring(1)
+ "ComponentPallette());\n\n" +
" map.put(\"" + component.getName() + "-helpString\", MESSAGES."
+ component.getName() + "HelpStringComponentPallette());\n\n";
}
public String propertyString(ComponentInfo component,Set<String> outProperties){
StringBuilder sb = new StringBuilder();
sb.append("\n\n/* Properties */\n\n");
for (Property prop : component.properties.values()) {
String propertyName = prop.name;
if (prop.isUserVisible()
|| component.designerProperties.containsKey(propertyName)
|| prop.isDeprecated() // [lyn, 2015/12/30] For deprecated AI2 blocks (but not AI1 blocks)
// must translate property names so they can be displayed in bad blocks.
) {
sb.append(" propertyStringMap.put(\"PROPERTY-").append(propertyName).append("\", MESSAGES.").append(propertyName).append("Properties());\n");
outProperties.add(propertyName);
}
}
return sb.toString();
}
public String eventString(ComponentInfo component,Set<String> outEvents){
StringBuilder sb = new StringBuilder();
sb.append("\n\n/* Events */\n\n");
for (Event event : component.events.values()) {
String propertyName = event.name;
if (event.userVisible
|| event.deprecated // [lyn, 2015/12/30] For deprecated AI2 blocks (but not AI1 blocks)
// must translate property names so they can be displayed in bad blocks.
) {
sb.append(" eventStringMap.put(\"EVENT-").append(propertyName).append("\", MESSAGES.").append(propertyName).append("Events());\n");
for (Parameter parameter : event.parameters) {
parameters.put(parameter.name, parameter);
}
outEvents.add(propertyName);
}
}
return sb.toString();
}
public String methodString(ComponentInfo component,Set<String> outMethods){
StringBuilder sb = new StringBuilder();
sb.append("\n\n/* Methods */\n\n");
for (Method method : component.methods.values()) {
String propertyName = method.name;
if (method.userVisible
|| method.deprecated // [lyn, 2015/12/30] For deprecated AI2 blocks (but not AI1 blocks)
// must translate property names so they can be displayed in bad blocks.
) {
sb.append(" methodStringMap.put(\"METHOD-").append(propertyName).append("\", MESSAGES.").append(propertyName).append("Methods());\n");
for (Parameter parameter : method.parameters) {
parameters.put(parameter.name, parameter);
}
outMethods.add(propertyName);
}
}
return sb.toString();
}
private void outputEventString(ComponentInfo component,Set<String> outEvents, StringBuilder sb){
if (component.getExternal()) { // Avoid adding entries for external components
return;
}
sb.append(eventString(component,outEvents));
}
private void outputMethodString(ComponentInfo component,Set<String> outMethods, StringBuilder sb){
if (component.getExternal()) { // Avoid adding entries for external components
return;
}
sb.append(methodString(component,outMethods));
}
private void outputPropertyString(ComponentInfo component,Set<String> outProperties, StringBuilder sb){
if (component.getExternal()) { // Avoid adding entries for external components
return;
}
sb.append(propertyString(component,outProperties));
}
private void outputComponent(ComponentInfo component, Set<String> outProperties,
Set<String> outMethods, Set<String> outEvents, StringBuilder sb) {
if (component.getExternal()) { // Avoid adding entries for external components
return;
}
/*sb.append("public static HashMap<String,String> component(){\n" +
" HashMap<String, String> map = new HashMap<String, String>();");*/
sb.append(componentString(component));
/*sb.append(propertyString(component,outProperties));
sb.append(methodString(component,outMethods));*/
// This special case adds the notAlreadyHandled parameter, which is the second parameter for the generic event
// handlers. Since it's not explicitly declared in any event handler, we add it here for internationalization.
parameters.put("notAlreadyHandled", new Parameter("notAlreadyHandled", "boolean"));
sb.append("\n\n/* Parameters */\n\n");
for (Parameter parameter : parameters.values()) {
sb.append(" map.put(\"PARAM-").append(parameter.name).append("\", MESSAGES.").append(Character.toLowerCase(parameter.name.charAt(0))).append(parameter.name.substring(1)).append("Params());\n");
}
/*sb.append("return map;\n" +
" }");*/
}
private void outputCategory(String category, StringBuilder sb) {
// sanitize the category name
String[] parts = category.split(" ");
sb.append(" map.put(\"CATEGORY-").append(category).append("\", MESSAGES.").append(parts[0].replaceAll("[^A-Za-z0-9]", "").toLowerCase());
for (int i = 1; i < parts.length; i++) {
String lower = parts[i].replaceAll("[^A-Za-z0-9]", "").toLowerCase();
sb.append(Character.toUpperCase(lower.charAt(0)));
sb.append(lower.substring(1));
}
sb.append("ComponentPallette());\n");
}
private void outputPropertyCategory(String category, StringBuilder sb) {
sb.append(" map.put(\"CATEGORY-");
sb.append(category);
sb.append("\", MESSAGES.");
sb.append(category);
sb.append("PropertyCategory());\n");
}
private void outputComponentAutogen(ComponentInfo component,
Map<String, Property> outProperties, Map<String, Method> outMethods,
Map<String, Event> outEvents, StringBuilder sb) {
sb.append(" @DefaultMessage(\"");
sb.append(component.getName());
sb.append("\")\n");
sb.append(" @Description(\"\")\n");
sb.append(" String ");
sb.append(Character.toLowerCase(component.getName().charAt(0)));
sb.append(component.getName().substring(1));
sb.append("ComponentPallette();\n\n");
sb.append(" @DefaultMessage(\"");
sb.append(sanitize(component.description));
sb.append("\")\n");
sb.append(" @Description(\"\")\n");
sb.append(" String ");
sb.append(component.getName());
sb.append("HelpStringComponentPallette();\n\n");
for (Property property : component.properties.values()) {
if (property.isUserVisible() || component.designerProperties.containsKey(property.name) ||
property.isDeprecated()) {
outProperties.put(property.name, property);
}
}
for (Method method : component.methods.values()) {
if (method.userVisible || method.deprecated) {
outMethods.put(method.name, method);
}
}
for (Event event : component.events.values()) {
if (event.userVisible || event.deprecated) {
outEvents.put(event.name, event);
}
}
}
private void outputPropertyAutogen(Property property, StringBuilder sb) {
sb.append(" @DefaultMessage(\"");
sb.append(sanitize(property.name));
sb.append("\")\n");
sb.append(" @Description(\"\")\n");
sb.append(" String ");
sb.append(property.name);
sb.append("Properties();\n\n");
}
private void outputMethodAutogen(Method method, Map<String, Parameter> outParameters, StringBuilder sb) {
sb.append(" @DefaultMessage(\"");
sb.append(sanitize(method.name));
sb.append("\")\n");
sb.append(" @Description(\"\")\n");
sb.append(" String ");
sb.append(method.name);
sb.append("Methods();\n\n");
for (Parameter param : method.parameters) {
outParameters.put(param.name, param);
}
}
private void outputEventAutogen(Event event, Map<String, Parameter> outParameters, StringBuilder sb) {
sb.append(" @DefaultMessage(\"");
sb.append(sanitize(event.name));
sb.append("\")\n");
sb.append(" @Description(\"\")\n");
sb.append(" String ");
sb.append(event.name);
sb.append("Events();\n\n");
for (Parameter param : event.parameters) {
outParameters.put(param.name, param);
}
}
private void outputParameterAutogen(Parameter parameter, StringBuilder sb) {
sb.append(" @DefaultMessage(\"");
sb.append(sanitize(parameter.name));
sb.append("\")\n");
sb.append(" @Description(\"\")\n");
sb.append(" String ");
sb.append(parameter.name);
sb.append("Params();\n\n");
}
private void outputCategoryAutogen(String category, StringBuilder sb) {
String[] parts = category.split(" ");
sb.append(" @DefaultMessage(\"");
sb.append(category);
sb.append("\")\n");
sb.append(" @Description(\"\")\n");
sb.append(" String ");
sb.append(parts[0].replaceAll("[^A-Za-z0-9]", "").toLowerCase());
for (int i = 1; i < parts.length; i++) {
String lower = parts[i].replaceAll("[^A-Za-z0-9]", "").toLowerCase();
sb.append(Character.toUpperCase(lower.charAt(0)));
sb.append(lower.substring(1));
}
sb.append("ComponentPallette();\n\n");
}
private void outputPropertyCategoryAutogen(String category, StringBuilder sb) {
sb.append(" @DefaultMessage(\"");
sb.append(category);
sb.append("\")\n");
sb.append(" @Description(\"\")\n");
sb.append(" String ");
sb.append(category);
sb.append("PropertyCategory();\n\n");
}
private String sanitize(String input) {
return input.replaceAll("\r", "").replaceAll("\n", "").replaceAll("\\\\", "\\\\\\\\")
.replaceAll("\"", "\\\\\"").replaceAll("'", "''").replaceAll("[ \t]+", " ").trim();
}
private void storeTooltip(ComponentInfo component, String name, String suffix,
String description) {
String key = name + suffix;
String value = tooltips.get(key);
if (collisionKeys.contains(key)) {
// Already detected a collision
key = component.getName() + "__" + key;
tooltips.put(key, description);
} else if (value == null) {
// This is the first observation of this key
tooltips.put(key, description);
Set<String> components = new HashSet<>();
components.add(component.getName());
tooltipComponent.put(key, components);
} else if (!value.equals(description)) {
// Descriptions don't match == collision!
collisionKeys.add(key);
for (String componentName : tooltipComponent.get(key)) {
tooltips.put(componentName + "__" + key, value);
}
key = component.getName() + "__" + key;
tooltips.put(key, description);
} else {
// Two (or more) components have the exact same description. Technically not a collision, but
// we need to do some bookkeeping in case a collision is detected with another component.
tooltipComponent.get(key).add(component.getName());
}
}
private void computeTooltipMap(ComponentInfo component) {
for (Property property : component.properties.values()) {
storeTooltip(component, property.name, "PropertyDescriptions", property.getDescription());
}
for (Method method : component.methods.values()) {
storeTooltip(component, method.name, "MethodDescriptions", method.description);
}
for (Event event : component.events.values()) {
storeTooltip(component, event.name, "EventDescriptions", event.description);
}
}
protected void outputAutogenOdeMessages() throws IOException {
Set<String> categories = new TreeSet<>();
Map<String, Property> properties = new TreeMap<>();
Map<String, Method> methods = new TreeMap<>();
Map<String, Event> events = new TreeMap<>();
Map<String, Parameter> parameters = new TreeMap<>();
StringBuilder sb = new StringBuilder();
sb.append("// THIS FILE IS AUTOMATICALLY GENERATED DURING COMPILATION.\n");
sb.append("// DO NOT EDIT THIS FILE. ANY CHANGES WILL BE OVERWRITTEN.\n\n");
sb.append("package com.google.appinventor.client;\n\n");
sb.append("import com.google.gwt.i18n.client.Messages;\n\n");
sb.append("public interface AutogeneratedOdeMessages extends Messages {\n");
sb.append("\n /* Components */\n");
for (Map.Entry<String, ComponentInfo> entry : components.entrySet()) {
ComponentInfo component = entry.getValue();
outputComponentAutogen(component, properties, methods, events, sb);
computeTooltipMap(component);
categories.add(component.getCategory());
}
for (String key : collisionKeys) {
tooltips.remove(key);
}
sb.append("\n /* Properties */\n");
for (Map.Entry<String, Property> entry : properties.entrySet()) {
outputPropertyAutogen(entry.getValue(), sb);
}
sb.append("\n /* Methods */\n");
for (Map.Entry<String, Method> entry : methods.entrySet()) {
outputMethodAutogen(entry.getValue(), parameters, sb);
}
sb.append("\n /* Events */\n");
for (Map.Entry<String, Event> entry : events.entrySet()) {
outputEventAutogen(entry.getValue(), parameters, sb);
}
for (Map.Entry<String, String> entry : tooltips.entrySet()) {
sb.append(" @DefaultMessage(\"");
sb.append(sanitize(entry.getValue()));
sb.append("\")\n");
sb.append(" @Description(\"\")\n");
sb.append(" String ");
sb.append(entry.getKey());
sb.append("();\n\n");
}
sb.append("\n /* Parameters */\n");
for (Map.Entry<String, Parameter> entry : parameters.entrySet()) {
outputParameterAutogen(entry.getValue(), sb);
}
sb.append("\n /* Component Categories */\n");
for (String category : categories) {
outputCategoryAutogen(category, sb);
}
sb.append("\n /* Property Categories */\n");
outputPropertyCategoryAutogen("Appearance", sb);
outputPropertyCategoryAutogen("Behavior", sb);
outputPropertyCategoryAutogen("Unspecified", sb);
sb.append("}\n");
FileObject src = createOutputFileObject(AUTOGEN_OUTPUT_FILE_NAME);
Writer writer = src.openWriter();
writer.write(sb.toString());
writer.flush();
writer.close();
messager.printMessage(Kind.NOTE, "Wrote file " + src.toUri());
}
public String oR(){
return "package com.google.appinventor.client;\n" +
"\n" +
"import java.util.HashMap;\n" +
"import java.util.Map;\n" +
"\n" +
"import static com.google.appinventor.client.Ode.MESSAGES;\n" +
"\n" +
"public class ComponentsTranslation {\n" +
" public static Map<String, String> myMap = map();\n" +
" public static Map<String,String> propertyMap = property();\n" +
" public static Map<String,String> eventMap = event();\n" +
" public static Map<String,String> methodMap = method();\n" +
" public static Map<String,String> componentMap = component();\n" +
"public static Map<String,String> propertyStringMap = propertyString();\n" +
" public static Map<String,String> methodStringMap = methodString();\n" +
" public static Map<String,String> eventStringMap = eventString();\n" +
" private static String getName(String key,int type) {\n" +
" String value = \"\";\n" +
" if (type == 1){\n" +
" value = propertyMap.get(key);\n" +
" }else if(type == 2){\n" +
" value = methodMap.get(key);\n" +
" }else if(type == 3){\n" +
" value = eventMap.get(key);\n" +
" }else if(type == 4){\n" +
" value = componentMap.get(key);\n" +
" }else if(type == 5){\n" +
" value = eventStringMap.get(key);\n" +
" }else if(type == 6){\n" +
" value = methodStringMap.get(key);\n" +
" }else if(type == 7){\n" +
" value = propertyStringMap.get(key);\n" +
" }else {\n" +
" value = myMap.get(key);\n" +
" }\n" +
" if (key == null) {\n" +
" return \"\";\n" +
" } else {\n" +
" return value;\n" +
" }\n" +
" }" +
" public static String getPropertyName(String key) {\n" +
" String value = getName(\"PROPERTY-\" + key,1);\n" +
" if(value == null) return key;\n" +
" return value;\n" +
" }\n" +
" public static String getPropertyDescription(String key) {\n" +
" String value = getName(\"PROPDESC-\" + key,1);\n" +
" if(value == null) return key;\n" +
" return value;\n" +
" }\n" +
" public static String getMethodName(String key) {\n" +
" String value = getName(\"METHOD-\" + key,2);\n" +
" if(value == null) return key;\n" +
" return value;\n" +
" }\n" +
" public static String getEventName(String key) {\n" +
" String value = getName(\"EVENT-\" + key,3);\n" +
" if(value == null) return key;\n" +
" return value;\n" +
" }\n" +
" public static String getComponentName(String key) {\n" +
" String value = getName(\"COMPONENT-\" + key,4);\n" +
" if(value == null) return key;\n" +
" return value;\n" +
" }\n" +
" public static String getCategoryName(String key) {\n" +
" String value = getName(\"CATEGORY-\" + key,0);\n" +
" if(value == null) return key;\n" +
" return value;\n" +
" }\n" +
" public static String getComponentHelpString(String key) {\n" +
" String value = \"\";\n" +
" if (key.contains(\"EVENT-\")) {\n" +
" value = getName(key + \"-helpString\", 5);\n" +
" }else if(key.contains(\"METHOD-\")){\n" +
" value = getName(key + \"-helpString\", 6);\n" +
" }else if (key.contains(\"PROPERTY-\")){\n" +
" value = getName(key + \"-helpString\", 7);\n" +
" }\n" +
" if(value == null) return key;\n" +
" return value;\n" +
" }\n" +
" public static HashMap<String, String> map() {\n" +
" HashMap<String, String> map = new HashMap<String, String>();\n" +
" /*map.putAll(component());\n" +
" map.putAll(event());\n" +
" map.putAll(method());\n" +
" map.putAll(property());*/\n" +
" map.putAll(category());\n";
}
@Override
protected void outputResults() throws IOException {
outputAutogenOdeMessages();
StringBuilder sb = new StringBuilder();
sb.append(oR());
Set<String> categories = new TreeSet<>();
for (String category : categories) {
outputCategory(category, sb);
}
sb.append("return map;\n" +
" }\n");
sb.append("public static HashMap<String,String> component(){\n" +
" HashMap<String, String> map = new HashMap<String, String>();");
// Components are already sorted.
Set<String> properties = new TreeSet<>();
Set<String> methods = new TreeSet<>();
Set<String> events = new TreeSet<>();
for (Map.Entry<String, ComponentInfo> entry : components.entrySet()) {
ComponentInfo component = entry.getValue();
outputComponent(component, properties, methods, events, sb);
categories.add(component.getCategory());
}
sb.append("return map;\n" +
" }");
sb.append("public static HashMap<String,String> eventString(){\n" +
" HashMap<String, String> map = new HashMap<String, String>();");
for (Map.Entry<String, ComponentInfo> entry : components.entrySet()) {
ComponentInfo component = entry.getValue();
outputEventString(component,events,sb);
categories.add(component.getCategory());
}
sb.append("return map;\n" +
" }");
sb.append("public static HashMap<String,String> methodString(){\n" +
" HashMap<String, String> map = new HashMap<String, String>();");
for (Map.Entry<String, ComponentInfo> entry : components.entrySet()) {
ComponentInfo component = entry.getValue();
outputMethodString(component,methods,sb);
categories.add(component.getCategory());
}
sb.append("return map;\n" +
" }");
sb.append("public static HashMap<String,String> propertyString(){\n" +
" HashMap<String, String> map = new HashMap<String, String>();");
for (Map.Entry<String, ComponentInfo> entry : components.entrySet()) {
ComponentInfo component = entry.getValue();
outputPropertyString(component,properties,sb);
categories.add(component.getCategory());
}
sb.append("return map;\n" +
" }");
sb.append("\n\n /* Categories */\n\n");
sb.append("public static HashMap<String,String> category(){\n" +
" HashMap<String, String> map = new HashMap<String, String>();");
sb.append("return map;\n" +
" }");
sb.append("\n\n /* Descriptions */\n\n");
sb.append("public static HashMap<String,String> property(){\n" +
" HashMap<String, String> map = new HashMap<String, String>();");
for (String key : tooltips.keySet()) {
if (key.endsWith("PropertyDescriptions")) {
sb.append(" map.put(\"PROPDESC-");
sb.append(key.replaceAll("__", "."));
sb.append("\", MESSAGES.");
sb.append(key);
sb.append("());\n");
}
}
sb.append("return map;\n" +
" }");
sb.append("public static HashMap<String,String> method(){\n" +
" HashMap<String, String> map = new HashMap<String, String>();");
for (String key : tooltips.keySet()) {
if (key.endsWith("MethodDescriptions")) {
sb.append(" map.put(\"METHODDESC-");
sb.append(key.replaceAll("__", "."));
sb.append("\", MESSAGES.");
sb.append(key);
sb.append("());\n");
}
}
sb.append("return map;\n" +
" }");
sb.append("public static HashMap<String,String> event(){\n" +
" HashMap<String, String> map = new HashMap<String, String>();");
for (String key : tooltips.keySet()) {
if (key.endsWith("EventDescriptions")) {
sb.append(" map.put(\"EVENTDESC-");
sb.append(key.replaceAll("__", "."));
sb.append("\", MESSAGES.");
sb.append(key);
sb.append("());\n");
}
}
sb.append("return map;\n" +
" }");
sb.append("}\n");
FileObject src = createOutputFileObject(OUTPUT_FILE_NAME);
Writer writer = src.openWriter();
writer.write(sb.toString());
writer.flush();
writer.close();
messager.printMessage(Diagnostic.Kind.NOTE, "Wrote file " + src.toUri());
}
}
Kindly check and let me know if there is anything wrong.
Thanks once again.