    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;
    020
    021import org.apache.velocity.Template;
    022import org.apache.velocity.VelocityContext;
    023import org.apache.velocity.app.VelocityEngine;
    024import org.apache.velocity.runtime.RuntimeConstants;
    025import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
    026
    027/**
    028 * This class' purpose is to generate the <code>HEADER.html</code> that moves along with the
    029 * release for the sake of downloading the release from the distribution area.
    030 *
    031 * @since 1.3
    032 */
    033public class HeaderHtmlVelocityDelegate {
    034
    035    /** The location of the velocity template for this class. */
    036    private static final String TEMPLATE = "resources/org/apache/commons/release/plugin/velocity/HEADER.vm";
    037
    038    /** The private constructor to be used by the {@link HeaderHtmlVelocityDelegateBuilder}. */
    039    private HeaderHtmlVelocityDelegate() {
    040    }
    041
    042    /**
    043     * For instantiating our {@link HeaderHtmlVelocityDelegate} using the {@link HeaderHtmlVelocityDelegateBuilder}.
    044     *
    045     * @return a {@link HeaderHtmlVelocityDelegateBuilder}.
    046     */
    047    public static HeaderHtmlVelocityDelegateBuilder builder() {
    048        return new HeaderHtmlVelocityDelegateBuilder();
    049    }
    050
    051    /**
    052     * Builds the HEADER.vm velocity template to the writer passed in.
    053     *
    054     * @param writer any {@link Writer} that we wish to have the filled velocity template written to.
    055     * @return the {@link Writer} that we've filled out the template into.
    056     */
    057    public Writer render(final Writer writer) {
    058        final VelocityEngine ve = new VelocityEngine();
    059        ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    060        ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    061        ve.init();
    062        final Template template = ve.getTemplate(TEMPLATE);
    063        final VelocityContext context = new VelocityContext();
    064        template.merge(context, writer);
    065        return writer;
    066    }
    067
    068    /**
    069     * A builder class for instantiation of the {@link HeaderHtmlVelocityDelegate}.
    070     */
    071    public static class HeaderHtmlVelocityDelegateBuilder {
    072
    073        /**
    074         * Private constructor so that we can have a proper builder pattern.
    075         */
    076        private HeaderHtmlVelocityDelegateBuilder() {
    077        }
    078
    079        /**
    080         * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
    081         * @return a new {@link ReadmeHtmlVelocityDelegate}.
    082         */
    083        public HeaderHtmlVelocityDelegate build() {
    084            return new HeaderHtmlVelocityDelegate();
    085        }
    086    }
    087}