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