View Javadoc
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  
19  import java.io.Writer;
20  
21  import org.apache.velocity.Template;
22  import org.apache.velocity.VelocityContext;
23  import org.apache.velocity.app.VelocityEngine;
24  import org.apache.velocity.runtime.RuntimeConstants;
25  import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
26  
27  /**
28   * This class' purpose is to generate the <code>HEADER.html</code> that moves along with the
29   * release for the sake of downloading the release from the distribution area.
30   *
31   * @since 1.3
32   */
33  public class HeaderHtmlVelocityDelegate {
34  
35      /** The location of the velocity template for this class. */
36      private static final String TEMPLATE = "resources/org/apache/commons/release/plugin/velocity/HEADER.vm";
37  
38      /** The private constructor to be used by the {@link HeaderHtmlVelocityDelegateBuilder}. */
39      private HeaderHtmlVelocityDelegate() {
40      }
41  
42      /**
43       * For instantiating our {@link HeaderHtmlVelocityDelegate} using the {@link HeaderHtmlVelocityDelegateBuilder}.
44       *
45       * @return a {@link HeaderHtmlVelocityDelegateBuilder}.
46       */
47      public static HeaderHtmlVelocityDelegateBuilder builder() {
48          return new HeaderHtmlVelocityDelegateBuilder();
49      }
50  
51      /**
52       * Builds the HEADER.vm velocity template to the writer passed in.
53       *
54       * @param writer any {@link Writer} that we wish to have the filled velocity template written to.
55       * @return the {@link Writer} that we've filled out the template into.
56       */
57      public Writer render(final Writer writer) {
58          final VelocityEngine ve = new VelocityEngine();
59          ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
60          ve.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
61          ve.init();
62          final Template template = ve.getTemplate(TEMPLATE);
63          final VelocityContext context = new VelocityContext();
64          template.merge(context, writer);
65          return writer;
66      }
67  
68      /**
69       * A builder class for instantiation of the {@link HeaderHtmlVelocityDelegate}.
70       */
71      public static class HeaderHtmlVelocityDelegateBuilder {
72  
73          /**
74           * Private constructor so that we can have a proper builder pattern.
75           */
76          private HeaderHtmlVelocityDelegateBuilder() {
77          }
78  
79          /**
80           * Builds up the {@link ReadmeHtmlVelocityDelegate} from the previously set parameters.
81           * @return a new {@link ReadmeHtmlVelocityDelegate}.
82           */
83          public HeaderHtmlVelocityDelegate build() {
84              return new HeaderHtmlVelocityDelegate();
85          }
86      }
87  }