Search in sources :

Example 1 with TemplateProcessingException

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);
    }
}
Also used : InputSource(org.xml.sax.InputSource) Attributes(org.xml.sax.Attributes) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) StringReader(java.io.StringReader) TemplateProcessingException(com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Map(java.util.Map) File(java.io.File)

Example 2 with TemplateProcessingException

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);
    }
}
Also used : RenderingContext(com.android.tools.idea.templates.recipe.RenderingContext) Recipe(com.android.tools.idea.templates.recipe.Recipe) JAXBException(javax.xml.bind.JAXBException) StringReader(java.io.StringReader) TemplateProcessingException(com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException) File(java.io.File) RecipeExecutor(com.android.tools.idea.templates.recipe.RecipeExecutor)

Example 3 with TemplateProcessingException

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);
    }
}
Also used : TemplateUserVisibleException(com.android.tools.idea.templates.FreemarkerUtils.TemplateUserVisibleException) TemplateProcessingException(com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException) File(java.io.File) TemplateMetadata(com.android.tools.idea.templates.TemplateMetadata) Constraint(com.android.tools.idea.templates.Parameter.Constraint)

Aggregations

TemplateProcessingException (com.android.tools.idea.templates.FreemarkerUtils.TemplateProcessingException)3 File (java.io.File)3 StringReader (java.io.StringReader)2 TemplateUserVisibleException (com.android.tools.idea.templates.FreemarkerUtils.TemplateUserVisibleException)1 Constraint (com.android.tools.idea.templates.Parameter.Constraint)1 TemplateMetadata (com.android.tools.idea.templates.TemplateMetadata)1 Recipe (com.android.tools.idea.templates.recipe.Recipe)1 RecipeExecutor (com.android.tools.idea.templates.recipe.RecipeExecutor)1 RenderingContext (com.android.tools.idea.templates.recipe.RenderingContext)1 IOException (java.io.IOException)1 Map (java.util.Map)1 JAXBException (javax.xml.bind.JAXBException)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 Attributes (org.xml.sax.Attributes)1 InputSource (org.xml.sax.InputSource)1 SAXException (org.xml.sax.SAXException)1 DefaultHandler (org.xml.sax.helpers.DefaultHandler)1