AI2 Component  (Version nb184)
ComponentListGenerator.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2019 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.scripts;
8 
9 import com.google.appinventor.common.utils.StringUtils;
11 
12 import java.io.IOException;
13 import java.io.Writer;
14 import java.util.Collections;
15 import java.util.Map;
16 import java.util.Set;
17 import javax.tools.Diagnostic;
18 import javax.tools.FileObject;
19 
26 public final class ComponentListGenerator extends ComponentProcessor {
27  // Where to write results. Build Info is the collection of permissions, asset and library info.
28  private static final String COMPONENT_LIST_OUTPUT_FILE_NAME = "simple_components.txt";
29  private static final String COMPONENT_BUILD_INFO_OUTPUT_FILE_NAME =
30  "simple_components_build_info.json";
31 
32  @Override
33  protected void outputResults() throws IOException {
34  // Build the component list and build info simultaneously.
35  StringBuilder componentList = new StringBuilder();
36  StringBuilder componentBuildInfo = new StringBuilder();
37  componentBuildInfo.append("[\n");
38 
39  // Components are already sorted.
40  String listSeparator = "";
41  String jsonSeparator = "";
42  for (Map.Entry<String, ComponentInfo> entry : components.entrySet()) {
43  ComponentInfo component = entry.getValue();
44 
45  componentList.append(listSeparator).append(component.type);
46  listSeparator = "\n";
47 
48  componentBuildInfo.append(jsonSeparator);
49  outputComponentBuildInfo(component, componentBuildInfo);
50  jsonSeparator = ",\n";
51  }
52 
53  componentBuildInfo.append("\n]");
54 
55  FileObject src = createOutputFileObject(COMPONENT_LIST_OUTPUT_FILE_NAME);
56  Writer writer = src.openWriter();
57  try {
58  writer.write(componentList.toString());
59  writer.flush();
60  } finally {
61  writer.close();
62  }
63  messager.printMessage(Diagnostic.Kind.NOTE, "Wrote file " + src.toUri());
64 
65  src = createOutputFileObject(COMPONENT_BUILD_INFO_OUTPUT_FILE_NAME);
66  writer = src.openWriter();
67  try {
68  writer.write(componentBuildInfo.toString());
69  writer.flush();
70  } finally {
71  writer.close();
72  }
73  messager.printMessage(Diagnostic.Kind.NOTE, "Wrote file " + src.toUri());
74  }
75 
76  private static void outputComponentBuildInfo(ComponentInfo component, StringBuilder sb) {
77  sb.append("{\"type\": \"");
78  sb.append(component.type).append("\"");
79  appendComponentInfo(sb, ComponentDescriptorConstants.PERMISSIONS_TARGET, component.permissions);
80  appendComponentInfo(sb, ComponentDescriptorConstants.LIBRARIES_TARGET, component.libraries);
81  appendComponentInfo(sb, ComponentDescriptorConstants.NATIVE_TARGET, component.nativeLibraries);
82  appendComponentInfo(sb, ComponentDescriptorConstants.ASSETS_TARGET, component.assets);
83  appendComponentInfo(sb, ComponentDescriptorConstants.ACTIVITIES_TARGET, component.activities);
84  appendComponentInfo(sb, ComponentDescriptorConstants.METADATA_TARGET, component.metadata);
85  appendComponentInfo(sb, ComponentDescriptorConstants.ACTIVITY_METADATA_TARGET, component.activityMetadata);
86  appendComponentInfo(sb, ComponentDescriptorConstants.ANDROIDMINSDK_TARGET, Collections.singleton(Integer.toString(component.getAndroidMinSdk())));
87  appendComponentInfo(sb, ComponentDescriptorConstants.BROADCAST_RECEIVERS_TARGET, component.broadcastReceivers);
88  appendComponentInfo(sb, ComponentDescriptorConstants.SERVICES_TARGET, component.services);
89  appendComponentInfo(sb, ComponentDescriptorConstants.CONTENT_PROVIDERS_TARGET, component.contentProviders);
90  appendConditionalComponentInfo(component, sb);
91  // TODO(Will): Remove the following call once the deprecated
92  // @SimpleBroadcastReceiver annotation is removed. It should
93  // should remain for the time being because otherwise we'll break
94  // extensions currently using @SimpleBroadcastReceiver.
95  appendComponentInfo(sb, ComponentDescriptorConstants.BROADCAST_RECEIVER_TARGET, component.classNameAndActionsBR);
96  sb.append("}");
97  }
98 
106  private static void appendConditionalComponentInfo(ComponentInfo component, StringBuilder sb) {
107  if (component.conditionalPermissions.size() +
108  component.conditionalBroadcastReceivers.size() +
109  component.conditionalServices.size() +
110  component.conditionalContentProviders.size() == 0) {
111  return;
112  }
113  sb.append(", \"" + ComponentDescriptorConstants.CONDITIONALS_TARGET + "\": { ");
114  sb.append("\"" + ComponentDescriptorConstants.PERMISSIONS_TARGET + "\": ");
115  appendMap(sb, component.conditionalPermissions);
116  sb.append(", \"" + ComponentDescriptorConstants.BROADCAST_RECEIVERS_TARGET + "\": ");
117  appendMap(sb, component.conditionalBroadcastReceivers);
118  sb.append(", \"" + ComponentDescriptorConstants.SERVICES_TARGET + "\": ");
119  appendMap(sb, component.conditionalServices);
120  sb.append(", \"" + ComponentDescriptorConstants.CONTENT_PROVIDERS_TARGET + "\": ");
121  appendMap(sb, component.conditionalContentProviders);
122  sb.append("}");
123  }
124 
132  private static void appendMap(StringBuilder sb, Map<String, String[]> map) {
133  sb.append("{");
134  boolean first = true;
135  for (Map.Entry<String, String[]> entry : map.entrySet()) {
136  if (!first) sb.append(", ");
137  sb.append("\"");
138  sb.append(entry.getKey());
139  sb.append("\": [\"");
140  StringUtils.join(sb, "\", \"", entry.getValue());
141  sb.append("\"]");
142  first = false;
143  }
144  sb.append("}");
145  }
146 
147  private static void appendComponentInfo(StringBuilder sb,
148  String infoName, Set<String> infoEntries) {
149  sb.append(", \"").append(infoName).append("\": [");
150  String separator = "";
151  for (String infoEntry : infoEntries) {
152  sb.append(separator).append("\"").append(infoEntry).append("\"");
153  separator = ", ";
154  }
155  sb.append("]");
156  }
157 }
com.google.appinventor.components.common.ComponentDescriptorConstants.NATIVE_TARGET
static final String NATIVE_TARGET
Definition: ComponentDescriptorConstants.java:26
com.google.appinventor.components.common.ComponentDescriptorConstants.ACTIVITIES_TARGET
static final String ACTIVITIES_TARGET
Definition: ComponentDescriptorConstants.java:22
com.google.appinventor.components.common.ComponentDescriptorConstants.ANDROIDMINSDK_TARGET
static final String ANDROIDMINSDK_TARGET
Definition: ComponentDescriptorConstants.java:31
com.google.appinventor.components
com.google.appinventor.components.common.ComponentDescriptorConstants.LIBRARIES_TARGET
static final String LIBRARIES_TARGET
Definition: ComponentDescriptorConstants.java:25
com.google.appinventor.components.common.ComponentDescriptorConstants.BROADCAST_RECEIVER_TARGET
static final String BROADCAST_RECEIVER_TARGET
Definition: ComponentDescriptorConstants.java:38
com.google.appinventor.components.common.ComponentDescriptorConstants.SERVICES_TARGET
static final String SERVICES_TARGET
Definition: ComponentDescriptorConstants.java:29
com.google.appinventor.components.common.ComponentDescriptorConstants
Definition: ComponentDescriptorConstants.java:12
com.google.appinventor.components.scripts.ComponentProcessor.ComponentInfo.type
final String type
Definition: ComponentProcessor.java:767
com.google.appinventor.components.scripts.ComponentProcessor.messager
Messager messager
Definition: ComponentProcessor.java:194
com.google.appinventor.components.scripts.ComponentListGenerator
Definition: ComponentListGenerator.java:26
com.google.appinventor.components.common.ComponentDescriptorConstants.PERMISSIONS_TARGET
static final String PERMISSIONS_TARGET
Definition: ComponentDescriptorConstants.java:27
com.google.appinventor.components.scripts.ComponentProcessor.ComponentInfo
Definition: ComponentProcessor.java:644
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.scripts.ComponentProcessor.components
final SortedMap< String, ComponentInfo > components
Definition: ComponentProcessor.java:207
com.google.appinventor.components.common.ComponentDescriptorConstants.BROADCAST_RECEIVERS_TARGET
static final String BROADCAST_RECEIVERS_TARGET
Definition: ComponentDescriptorConstants.java:28
com.google
com.google.appinventor.components.common.ComponentDescriptorConstants.ASSETS_TARGET
static final String ASSETS_TARGET
Definition: ComponentDescriptorConstants.java:21
com
com.google.appinventor.components.scripts.ComponentListGenerator.outputResults
void outputResults()
Definition: ComponentListGenerator.java:33
com.google.appinventor.components.common.ComponentDescriptorConstants.ACTIVITY_METADATA_TARGET
static final String ACTIVITY_METADATA_TARGET
Definition: ComponentDescriptorConstants.java:24
com.google.appinventor.components.scripts.ComponentProcessor
Definition: ComponentProcessor.java:124
com.google.appinventor.components.common.ComponentDescriptorConstants.CONTENT_PROVIDERS_TARGET
static final String CONTENT_PROVIDERS_TARGET
Definition: ComponentDescriptorConstants.java:30
com.google.appinventor
com.google.appinventor.components.common.ComponentDescriptorConstants.METADATA_TARGET
static final String METADATA_TARGET
Definition: ComponentDescriptorConstants.java:23