use of com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException in project android by JetBrains.
the class Template method processXml.
private void processXml(@NotNull final RenderingContext context, @NotNull String xml) throws TemplateProcessingException {
try {
xml = XmlUtils.stripBom(xml);
InputSource inputSource = new InputSource(new StringReader(xml));
SAXParserFactory.newInstance().newSAXParser().parse(inputSource, new DefaultHandler() {
@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
try {
Map<String, Object> paramMap = context.getParamMap();
if (TAG_PARAMETER.equals(name)) {
String id = attributes.getValue(ATTR_ID);
if (!paramMap.containsKey(id)) {
String value = attributes.getValue(ATTR_DEFAULT);
Object mapValue = value;
if (value != null && !value.isEmpty()) {
String type = attributes.getValue(ATTR_TYPE);
if ("boolean".equals(type)) {
mapValue = Boolean.valueOf(value);
}
}
paramMap.put(id, mapValue);
}
} else if (TAG_GLOBAL.equals(name)) {
String id = attributes.getValue(ATTR_ID);
if (!paramMap.containsKey(id)) {
paramMap.put(id, TypedVariable.parseGlobal(attributes));
}
} else if (TAG_GLOBALS.equals(name)) {
// Handle evaluation of variables
File globalsFile = getPath(attributes, ATTR_FILE);
if (globalsFile != null) {
processFile(context, globalsFile);
}
// else: <globals> root element
} else if (TAG_EXECUTE.equals(name)) {
File recipeFile = getPath(attributes, ATTR_FILE);
if (recipeFile != null) {
executeRecipeFile(context, recipeFile);
}
} else if (!name.equals("template") && !name.equals("category") && !name.equals("option") && !name.equals(TAG_THUMBS) && !name.equals(TAG_THUMB) && !name.equals(TAG_ICONS) && !name.equals(TAG_DEPENDENCY) && !name.equals(TAG_FORMFACTOR)) {
LOG.error("WARNING: Unknown template directive " + name);
}
} catch (TemplateProcessingException e) {
throw new SAXException(e);
}
}
});
} catch (SAXException ex) {
if (ex.getCause() instanceof TemplateProcessingException) {
throw (TemplateProcessingException) ex.getCause();
}
throw new TemplateProcessingException(ex);
} catch (ParserConfigurationException ex) {
throw new TemplateProcessingException(ex);
} catch (IOException ex) {
throw new TemplateProcessingException(ex);
}
}
use of com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException in project android by JetBrains.
the class ServiceXmlParser method analyzeRecipe.
private void analyzeRecipe(boolean findOnlyReferences, @Nullable Collection<File> openFiles, @Nullable SetMultimap<String, String> dependencies, @Nullable Collection<String> classpathEntries, @Nullable Collection<String> plugins, @Nullable Collection<File> sourceFiles, @Nullable Collection<File> targetFiles) {
RenderingContext context = null;
try {
File moduleRoot = new File(myModule.getModuleFilePath()).getParentFile();
// @formatter:off
context = RenderingContext.Builder.newContext(myRootPath, myModule.getProject()).withParams(myContext.toValueMap()).withOutputRoot(moduleRoot).withModuleRoot(moduleRoot).withFindOnlyReferences(findOnlyReferences).withGradleSync(false).intoOpenFiles(openFiles).intoDependencies(dependencies).intoClasspathEntries(classpathEntries).intoPlugins(plugins).intoSourceFiles(sourceFiles).intoTargetFiles(targetFiles).build();
// @formatter:on
String xml = FreemarkerUtils.processFreemarkerTemplate(context, myRecipeFile, null);
Recipe recipe = Recipe.parse(new StringReader(xml));
RecipeExecutor recipeExecutor = context.getRecipeExecutor();
recipe.execute(recipeExecutor);
} catch (TemplateProcessingException e) {
// TODO(b/31039466): Extra logging to track down a rare issue.
LOG.warn("Template processing exception with context in the following state: " + context);
throw new RuntimeException(e);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
use of com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException in project android by JetBrains.
the class Template method doRender.
/**
* Render the template.
* Warnings are only generated during a dry run i.e. no files are changed yet.
* The user may select to proceed anyway in which case we expect another call
* to render with dry run set to false.
* Errors may be shown regardless of the dry run flag.
*/
private boolean doRender(@NotNull RenderingContext context) {
TemplateMetadata metadata = getMetadata();
assert metadata != null;
enforceParameterTypes(metadata, context.getParamMap());
try {
processFile(context, new File(TEMPLATE_XML_NAME));
if (!context.showWarnings() || context.getWarnings().isEmpty()) {
return true;
}
if (!context.getProject().isInitialized() && myTemplateRoot.getPath().contains(GOOGLE_GLASS_PATH_19)) {
// there are files that are overwritten during project creation by the Glass activity templates.
return true;
}
// @formatter:off
int result = Messages.showOkCancelDialog(context.getProject(), formatWarningMessage(context), String.format("%1$s %2$s", context.getCommandName(), StringUtil.pluralize("Warning")), "Proceed Anyway", "Cancel", Messages.getWarningIcon());
// @formatter:on
return result == Messages.OK;
} catch (TemplateUserVisibleException e) {
if (context.showErrors()) {
// @formatter:off
Messages.showErrorDialog(context.getProject(), formatErrorMessage(context, e), String.format("%1$s Failed", context.getCommandName()));
// @formatter:on
} else {
throw new RuntimeException(e);
}
return false;
} catch (TemplateProcessingException e) {
throw new RuntimeException(e);
}
}
Aggregations