HeaderHtmlVelocityDelegate.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.release.plugin.velocity;

  18. import java.io.Writer;

  19. import org.apache.velocity.Template;
  20. import org.apache.velocity.VelocityContext;
  21. import org.apache.velocity.app.VelocityEngine;
  22. import org.apache.velocity.runtime.RuntimeConstants;
  23. import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

  24. /**
  25.  * This class' purpose is to generate the <code>HEADER.html</code> that moves along with the
  26.  * release for the sake of downloading the release from the distribution area.
  27.  *
  28.  * @since 1.3
  29.  */
  30. public class HeaderHtmlVelocityDelegate {

  31.     /** The location of the velocity template for this class. */
  32.     private static final String TEMPLATE = "resources/org/apache/commons/release/plugin/velocity/HEADER.vm";

  33.     /** The private constructor to be used by the {@link HeaderHtmlVelocityDelegateBuilder}. */
  34.     private HeaderHtmlVelocityDelegate() {
  35.     }

  36.     /**
  37.      * For instantiating our {@link HeaderHtmlVelocityDelegate} using the {@link HeaderHtmlVelocityDelegateBuilder}.
  38.      *
  39.      * @return a {@link HeaderHtmlVelocityDelegateBuilder}.
  40.      */
  41.     public static HeaderHtmlVelocityDelegateBuilder builder() {
  42.         return new HeaderHtmlVelocityDelegateBuilder();
  43.     }

  44.     /**
  45.      * Builds the HEADER.vm velocity template to the writer passed in.
  46.      *
  47.      * @param writer any {@link Writer} that we wish to have the filled velocity template written to.
  48.      * @return the {@link Writer} that we've filled out the template into.
  49.      */
  50.     public Writer render(final Writer writer) {
  51.         final VelocityEngine ve = new VelocityEngine();
  52.         ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
  53.         ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
  54.         ve.init();
  55.         final Template template = ve.getTemplate(TEMPLATE);
  56.         final VelocityContext context = new VelocityContext();
  57.         template.merge(context, writer);
  58.         return writer;
  59.     }

  60.     /**
  61.      * A builder class for instantiation of the {@link HeaderHtmlVelocityDelegate}.
  62.      */
  63.     public static class HeaderHtmlVelocityDelegateBuilder {

  64.         /**
  65.          * Private constructor so that we can have a proper builder pattern.
  66.          */
  67.         private HeaderHtmlVelocityDelegateBuilder() {
  68.         }

  69.         /**
  70.          * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
  71.          * @return a new {@link ReadmeHtmlVelocityDelegate}.
  72.          */
  73.         public HeaderHtmlVelocityDelegate build() {
  74.             return new HeaderHtmlVelocityDelegate();
  75.         }
  76.     }
  77. }