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