    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.release.plugin.velocity;
    018
    019import java.io.Writer;
    020import org.apache.velocity.Template;
    021import org.apache.velocity.VelocityContext;
    022import org.apache.velocity.app.VelocityEngine;
    023import org.apache.velocity.runtime.RuntimeConstants;
    024import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
    025
    026/**
    027 * This class' purpose is to generate the <code>HEADER.html</code> that moves along with the
    028 * release for the sake of downloading the release from the distribution area.
    029 *
    030 * @since 1.3
    031 */
    032public class HeaderHtmlVelocityDelegate {
    033
    034    /** The location of the velocity template for this class. */
    035    private static final String TEMPLATE = "resources/org/apache/commons/release/plugin/velocity/HEADER.vm";
    036
    037    /** The private constructor to be used by the {@link HeaderHtmlVelocityDelegateBuilder}. */
    038    private HeaderHtmlVelocityDelegate() {
    039    }
    040
    041    /**
    042     * For instantiating our {@link HeaderHtmlVelocityDelegate} using the {@link HeaderHtmlVelocityDelegateBuilder}.
    043     *
    044     * @return a {@link HeaderHtmlVelocityDelegateBuilder}.
    045     */
    046    public static HeaderHtmlVelocityDelegateBuilder builder() {
    047        return new HeaderHtmlVelocityDelegateBuilder();
    048    }
    049
    050    /**
    051     * Builds the HEADER.vm velocity template to the writer passed in.
    052     *
    053     * @param writer any {@link Writer} that we wish to have the filled velocity template written to.
    054     * @return the {@link Writer} that we've filled out the template into.
    055     */
    056    public Writer render(final Writer writer) {
    057        final VelocityEngine ve = new VelocityEngine();
    058        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    059        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    060        ve.init();
    061        final Template template = ve.getTemplate(TEMPLATE);
    062        final VelocityContext context = new VelocityContext();
    063        template.merge(context, writer);
    064        return writer;
    065    }
    066
    067    /**
    068     * A builder class for instantiation of the {@link HeaderHtmlVelocityDelegate}.
    069     */
    070    public static class HeaderHtmlVelocityDelegateBuilder {
    071
    072        /**
    073         * Private constructor so that we can have a proper builder pattern.
    074         */
    075        private HeaderHtmlVelocityDelegateBuilder() {
    076        }
    077
    078        /**
    079         * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
    080         * @return a new {@link ReadmeHtmlVelocityDelegate}.
    081         */
    082        public HeaderHtmlVelocityDelegate build() {
    083            return new HeaderHtmlVelocityDelegate();
    084        }
    085    }
    086}