protected void writeTemplate()

in modello-plugins/modello-plugin-store/src/main/java/org/apache/archiva/redback/components/modello/plugin/store/AbstractVelocityModelloGenerator.java [114:163]


    protected void writeTemplate( String templateName, File file, Context context )
        throws ModelloException
    {
        Template template = getTemplate( templateName );

        if ( template == null )
        {
            ClassLoader old = Thread.currentThread().getContextClassLoader();

            try
            {
                Thread.currentThread().setContextClassLoader( this.getClass().getClassLoader() );

                template = getTemplate( templateName );
            }
            finally
            {
                Thread.currentThread().setContextClassLoader( old );
            }
        }

        if ( template == null )
        {
            throw new ModelloException( "Could not find the template '" + templateName + "'." );
        }

        if ( !file.getParentFile().exists() )
        {
            if ( !file.getParentFile().mkdirs() )
            {
                throw new ModelloException(
                    "Error while creating parent directories for '" + file.getAbsolutePath() + "'." );
            }
        }

        try
        {
            try (Writer writer = getEncoding() == null ? WriterFactory.newPlatformWriter( file )
                            : WriterFactory.newWriter( file, getEncoding() ))
            {

                template.merge( context, writer );
            }

        }
        catch ( Exception e )
        {
            throw new ModelloException( "Error while generating code.", e );
        }
    }