    001    // Copyright 2007 by Basil Vandegriend.  All rights reserved.
    002    package com.basilv.envgen;
    003
    004    import java.io.IOException;
    005    import java.io.Writer;
    006    import java.util.Map;
    007
    008    import freemarker.template.TemplateModelException;
    009    import freemarker.template.TemplateTransformModel;
    010
    011    /**
    012     * Template to skip generation of the target file.
    013     * If the target file exists previously it is deleted.
    014     */
    015    public class SkipGenerationTransform implements TemplateTransformModel {
    016
    017
    018            private static final ThreadLocal skipGenerationFlag = new ThreadLocal();
    019
    020            public static boolean isSkipGenerationAndResetFlag() {
    021                    boolean isSkipGen = Boolean.TRUE.equals(skipGenerationFlag.get());
    022                    skipGenerationFlag.set(null);
    023                    return isSkipGen;
    024            }
    025
    026
    027            /**
    028             * Must have a default no-arg constructor.
    029             */
    030        public SkipGenerationTransform() {
    031        }
    032
    033
    034        public Writer getWriter(Writer out, Map args) throws TemplateModelException {
    035            SkipGenerationTransform.skipGenerationFlag.set(Boolean.TRUE);
    036            // Return a do-nothing writer.
    037                    return new Writer() {
    038                            public void write(char cbuf[], int off, int len) {
    039                            }
    040
    041                            public void flush() throws IOException {
    042                            }
    043
    044                            public void close() throws IOException {
    045                            }
    046                    };
    047
    048        }
    049
    050
    051    }